home *** CD-ROM | disk | FTP | other *** search
/ Our Birds: Texas / eViewer 3.9.iso / MDAC_TYP.EXE / sqlnet.cab / instcat.sql < prev    next >
Encoding:
Text File  |  2004-02-06  |  934.6 KB  |  24,587 lines

  1. /*
  2. **  INSTCAT.SQL
  3. **  Installs catalog stored procedures on the Microsoft SQL Server.
  4. ** Copyright Microsoft, Inc. 1994 - 2000
  5. ** All Rights Reserved.
  6. **
  7. ** Owners:
  8. **
  9. ** <owner current="true" primary="true">iliak</owner>
  10. */
  11.  
  12.  
  13.  
  14. /****************************************************************************/
  15. /* This portion sets up the ability to perform all the functions in this    */
  16. /* script                                                                   */
  17. /****************************************************************************/
  18. go
  19. use master
  20. go
  21. dump tran master with no_log
  22. go
  23.  
  24. set quoted_identifier on
  25. go
  26.  
  27. if (exists (select * from sysobjects
  28.         where name = 'sp_configure' and type = 'P '))
  29. begin
  30.     execute sp_configure 'update',1
  31. end
  32. reconfigure with override
  33. go
  34.  
  35. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  36.  
  37. go
  38.  
  39. /*
  40. ** If old versions of tables exist, drop them.
  41. */
  42. if (exists (select * from sysobjects
  43.         where name = 'MSdatatype_info' and type = 'U '))
  44.     drop table MSdatatype_info
  45. go
  46. if (exists (select * from sysobjects
  47.         where name = 'MSdatatype_info_ext' and type = 'U '))
  48.     drop table MSdatatype_info_ext
  49. go
  50. if (exists (select * from sysobjects
  51.         where name = 'MStable_types' and type = 'U '))
  52.     drop table MStable_types
  53. go
  54. if (exists (select * from sysobjects
  55.         where name = 'MSserver_info' and type = 'U '))
  56.     drop table MSserver_info
  57. go
  58. if (exists (select * from sysobjects
  59.         where name = 'spt_table_types' and type = 'U '))
  60.     drop table spt_table_types
  61. go
  62. /*
  63. ** If tables or procs already exist, drop them.
  64. */
  65.  
  66. if (exists (select * from sysobjects
  67.         where name = 'spt_datatype_info' and type = 'U '))
  68.     drop table spt_datatype_info
  69. go
  70. if (exists (select * from sysobjects
  71.         where name = 'spt_datatype_info_ext' and type = 'U '))
  72.     drop table spt_datatype_info_ext
  73. go
  74. if (exists (select * from sysobjects
  75.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  76.     drop proc sp_add_server_sortinfo
  77. go
  78. if (exists (select * from sysobjects
  79.         where name = 'sp_add_server_sortinfo75' and type = 'P '))
  80.     drop proc sp_add_server_sortinfo75
  81. go
  82. if (exists (select * from sysobjects
  83.         where name = 'spt_server_info' and type = 'U '))
  84.     drop table spt_server_info
  85. go
  86. if (exists (select * from sysobjects
  87.         where name = 'sp_tables' and type = 'P '))
  88.     drop proc sp_tables
  89. go
  90. if (exists (select * from sysobjects
  91.         where name = 'sp_statistics' and type = 'P '))
  92.     drop proc sp_statistics
  93. go
  94. if (exists (select * from sysobjects
  95.         where name = 'sp_columns' and type = 'P '))
  96.     drop proc sp_columns
  97. go
  98. if (exists (select * from sysobjects
  99.         where name = 'sp_fkeys' and type = 'P '))
  100.     drop proc sp_fkeys
  101. go
  102. if (exists (select * from sysobjects
  103.         where name = 'sp_pkeys' and type = 'P '))
  104.     drop proc sp_pkeys
  105. dump tran master with no_log
  106. go
  107.  
  108. go
  109. if (exists (select * from sysobjects
  110.         where name = 'sp_stored_procedures' and type = 'P '))
  111.     drop proc sp_stored_procedures
  112. go
  113. if (exists (select * from sysobjects
  114.         where name = 'sp_sproc_columns' and type = 'P '))
  115.     drop proc sp_sproc_columns
  116. go
  117. if (exists (select * from sysobjects
  118.         where name = 'sp_table_privileges' and type = 'P '))
  119.     drop proc sp_table_privileges
  120. go
  121. if (exists (select * from sysobjects
  122.         where name = 'sp_column_privileges' and type = 'P '))
  123.     drop proc sp_column_privileges
  124. go
  125. dump tran master with no_log
  126. go
  127. if (exists (select * from sysobjects
  128.         where name = 'sp_server_info' and type = 'P '))
  129.     drop proc sp_server_info
  130. go
  131. if (exists (select * from sysobjects
  132.         where name = 'sp_datatype_info' and type = 'P '))
  133.     drop proc sp_datatype_info
  134. go
  135. if (exists (select * from sysobjects
  136.         where name = 'sp_special_columns' and type = 'P '))
  137.     drop proc sp_special_columns
  138. go
  139. if (exists (select * from sysobjects
  140.         where name = 'sp_databases' and type = 'P '))
  141.     drop proc sp_databases
  142. go
  143. dump tran master with no_log
  144. go
  145. if (exists (select * from sysobjects
  146.         where name = 'sp_ddopen' and type = 'P '))
  147.     drop proc sp_ddopen
  148. go
  149. if (exists (select * from sysobjects
  150.         where name = 'sp_tableswc' and type = 'P '))
  151.     drop proc sp_tableswc
  152. go
  153.  
  154. if (exists (select * from sysobjects
  155.         where name = 'sp_tablecollations' and type = 'P'))
  156.     drop proc sp_tablecollations
  157. go
  158.  
  159. if (exists (select * from sysobjects
  160.         where name = 'sp_bcp_dbcmptlevel' and type = 'P'))
  161.     drop proc sp_bcp_dbcmptlevel
  162. go
  163.  
  164. dump tran master with no_log
  165. go
  166.  
  167.  
  168. if (exists (select * from sysobjects
  169.         where name = 'spt_provider_types' and type = 'U '))
  170.     begin
  171.     drop table spt_provider_types
  172.     dump tran master with no_log
  173.     end
  174. go
  175. if (exists (select * from sysobjects
  176.         where name = 'sp_catalogs_rowset' and type = 'P '))
  177.     begin
  178.     drop procedure sp_catalogs_rowset
  179.     dump tran master with no_log
  180.     end
  181. go
  182. if (exists (select * from sysobjects
  183.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  184.     begin
  185.     drop procedure sp_column_privileges_rowset
  186.     dump tran master with no_log
  187.     end
  188. go
  189. if (exists (select * from sysobjects
  190.         where name = 'sp_columns_rowset' and type = 'P '))
  191.     begin
  192.     drop procedure sp_columns_rowset
  193.     dump tran master with no_log
  194.     end
  195. go
  196. if (exists (select * from sysobjects
  197.         where name = 'sp_check_constraints_rowset' and type = 'P '))
  198.     begin
  199.     drop procedure sp_check_constraints_rowset
  200.     dump tran master with no_log
  201.     end
  202. go
  203. if (exists (select * from sysobjects
  204.         where name = 'sp_check_constbytable_rowset' and type = 'P '))
  205.     begin
  206.     drop procedure sp_check_constbytable_rowset
  207.     dump tran master with no_log
  208.     end
  209. go
  210. if (exists (select * from sysobjects
  211.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  212.     begin
  213.     drop procedure sp_foreign_keys_rowset
  214.     dump tran master with no_log
  215.     end
  216. go
  217. if (exists (select * from sysobjects
  218.         where name = 'sp_indexes_rowset' and type = 'P '))
  219.     begin
  220.     drop procedure sp_indexes_rowset
  221.     dump tran master with no_log
  222.     end
  223. go
  224. if (exists (select * from sysobjects
  225.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  226.     begin
  227.     drop procedure sp_primary_keys_rowset
  228.     dump tran master with no_log
  229.     end
  230. go
  231. if (exists (select * from sysobjects
  232.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  233.     begin
  234.     drop procedure sp_procedure_params_rowset
  235.     dump tran master with no_log
  236.     end
  237. go
  238. if (exists (select * from sysobjects
  239.         where name = 'sp_procedures_rowset' and type = 'P '))
  240.     begin
  241.     drop procedure sp_procedures_rowset
  242.     dump tran master with no_log
  243.     end
  244. go
  245. if (exists (select * from sysobjects
  246.         where name = 'sp_provider_types_rowset' and type = 'P '))
  247.     begin
  248.     drop procedure sp_provider_types_rowset
  249.     dump tran master with no_log
  250.     end
  251. go
  252. if (exists (select * from sysobjects
  253.         where name = 'sp_schemata_rowset' and type = 'P '))
  254.     begin
  255.     drop procedure sp_schemata_rowset
  256.     dump tran master with no_log
  257.     end
  258. go
  259. if (exists (select * from sysobjects
  260.         where name = 'sp_statistics_rowset' and type = 'P '))
  261.     begin
  262.     drop procedure sp_statistics_rowset
  263.     dump tran master with no_log
  264.     end
  265. go
  266. if (exists (select * from sysobjects
  267.         where name = 'sp_tables_rowset' and type = 'P '))
  268.     begin
  269.     drop procedure sp_tables_rowset
  270.     dump tran master with no_log
  271.     end
  272. go
  273. if (exists (select * from sysobjects
  274.         where name = 'sp_tables_info_rowset' and type = 'P '))
  275.     begin
  276.     drop procedure sp_tables_info_rowset
  277.     dump tran master with no_log
  278.     end
  279. go
  280. if (exists (select * from sysobjects
  281.         where name = 'sp_tables_info_rowset_64' and type = 'P '))
  282.     begin
  283.     drop procedure sp_tables_info_rowset_64
  284.     dump tran master with no_log
  285.     end
  286. go
  287. if (exists (select * from sysobjects
  288.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  289.     begin
  290.     drop proc sp_table_constraints_rowset
  291.     dump tran master with no_log
  292.     end
  293. go
  294. if (exists (select * from sysobjects
  295.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  296.     begin
  297.     drop proc sp_table_privileges_rowset
  298.     dump tran master with no_log
  299.     end
  300. go
  301. if (exists (select * from sysobjects
  302.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  303.     begin
  304.     drop proc sp_linkedservers_rowset
  305.     dump tran master with no_log
  306.     end
  307. go
  308.  
  309. if (exists (select * from sysobjects
  310.         where name = 'sp_table_statistics_rowset' and type = 'P '))
  311.     begin
  312.     drop proc sp_table_statistics_rowset
  313.     dump tran master with no_log
  314.     end
  315. go
  316.  
  317. if (exists (select * from sysobjects
  318.         where name = 'sp_oledb_column_constraints' and type = 'P '))
  319.     begin
  320.     drop proc sp_oledb_column_constraints
  321.     dump tran master with no_log
  322.     end
  323. go
  324.  
  325. if (exists (select * from sysobjects
  326.         where name = 'sp_oledb_indexinfo' and type = 'P '))
  327.     begin
  328.     drop proc sp_oledb_indexinfo
  329.     dump tran master with no_log
  330.     end
  331. go
  332.  
  333. if (exists (select * from sysobjects
  334.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  335.     begin
  336.     drop proc sp_oledb_ro_usrname
  337.     dump tran master with no_log
  338.     end
  339. go
  340.  
  341. if (exists (select * from sysobjects
  342.         where name = 'sp_oledb_deflang' and type = 'P '))
  343.     begin
  344.     drop proc sp_oledb_deflang
  345.     dump tran master with no_log
  346.     end
  347. go
  348.  
  349. if (exists (select * from sysobjects
  350.         where name = 'sp_oledb_defdb' and type = 'P '))
  351.     begin
  352.     drop proc sp_oledb_defdb
  353.     dump tran master with no_log
  354.     end
  355. go
  356.  
  357. if (exists (select * from sysobjects
  358.         where name = 'sp_oledb_database' and type = 'P '))
  359.     begin
  360.     drop proc sp_oledb_database
  361.     dump tran master with no_log
  362.     end
  363. go
  364.  
  365. if (exists (select * from sysobjects
  366.         where name = 'sp_oledb_language' and type = 'P '))
  367.     begin
  368.     drop proc sp_oledb_language
  369.     dump tran master with no_log
  370.     end
  371. go
  372.  
  373. print 'creating table spt_datatype_info_ext'
  374. go
  375.  
  376. if (charindex('6.00', @@version) = 0 and
  377.     charindex('6.50', @@version) = 0 and
  378.     charindex('7.00', @@version) = 0 and
  379.     charindex('8.00', @@version) = 0)
  380. begin   /*  Pre 6.0 server */
  381.     print ''
  382.     print ''
  383.     print 'Warning:'
  384.     print 'you are installing the stored procedures '
  385.     print 'on a pre 6.0 SQL Server.'
  386.     print 'Ignore the following error.'
  387.     create table spt_datatype_info_ext (
  388.                 user_type       smallint    not null,
  389.                 CREATE_PARAMS   varchar(32) null,
  390.                 AUTO_INCREMENT  smallint null,
  391.                 typename        varchar(32))
  392. end
  393. go
  394. if (charindex('6.00', @@version) > 0 or
  395.     charindex('6.50', @@version) > 0 or
  396.     charindex('7.00', @@version) > 0 or
  397.     charindex('8.00', @@version) > 0)
  398. begin   /* 6.0 or later server */
  399.     create table spt_datatype_info_ext (
  400.                 user_type       smallint    not null,
  401.                 CREATE_PARAMS   varchar(32) null,
  402.                 AUTO_INCREMENT  smallint null,
  403.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  404. end
  405. go
  406.  
  407. grant select on spt_datatype_info_ext to public
  408. go
  409.  
  410.  
  411. insert into spt_datatype_info_ext
  412.     /* CHAR      user_type, create_params, auto_increment */
  413.     values           (1,    'length' ,0, 'char')
  414.  
  415. insert into spt_datatype_info_ext
  416.     /* VARCHAR   user_type, create_params, auto_increment */
  417.     values           (2,    'max length' ,0, 'varchar')
  418.  
  419. insert into spt_datatype_info_ext
  420.     /* BINARY    user_type, create_params, auto_increment */
  421.     values           (3,    'length' ,0, 'binary')
  422.  
  423. insert into spt_datatype_info_ext
  424.     /* VARBINARY user_type, create_params, auto_increment */
  425.     values           (4,    'max length' ,0, 'varbinary')
  426.  
  427. if  (charindex('6.00', @@version) > 0 or
  428.      charindex('6.50', @@version) > 0 or
  429.      charindex('7.00', @@version) > 0 or
  430.      charindex('8.00', @@version) > 0)
  431. begin   /*  Add 6.0 data types */
  432.     insert into spt_datatype_info_ext
  433.         /* DECIMAL user_type, create_params, auto_increment */
  434.         values           (26,   'precision,scale' ,0, 'decimal')
  435.  
  436.     insert into spt_datatype_info_ext
  437.         /* NUMERIC user_type, create_params, auto_increment */
  438.         values           (25,   'precision,scale' ,0, 'numeric')
  439.  
  440.     insert into spt_datatype_info_ext
  441.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  442.         values           (26,   'precision' ,1, 'decimal')
  443.  
  444.     insert into spt_datatype_info_ext
  445.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  446.         values           (25,   'precision' ,1, 'numeric')
  447.  
  448. end
  449. else    /*  Pre 6.0 server, add SYSNAME create param */
  450.     begin
  451.         insert into spt_datatype_info_ext
  452.             /* SYSNAME   user_type, create_param, auto_increments */
  453.             values           (18,   'max length' ,0, 'sysname')
  454.  
  455.     end
  456. go
  457.  
  458. if (charindex('7.00', @@version) = 0 and
  459.     charindex('8.00', @@version) = 0)
  460. begin
  461.     print ''
  462.     print ''
  463.     print 'Warning:'
  464.     print 'you are installing the stored procedures '
  465.     print 'on a pre 8.0 SQL Server.'
  466.     print 'Ignore the following errors.'
  467. end
  468. go
  469. if (charindex('7.00', @@version) > 0 or
  470.     charindex('8.00', @@version) > 0)
  471. begin   /*  Update usertypes for 8.0 server */
  472.     begin tran
  473.     insert into spt_datatype_info_ext
  474.         /* NCHAR     user_type, create_params, auto_increment */
  475.         values           (0,    'length' ,0, 'nchar')
  476.  
  477.     insert into spt_datatype_info_ext
  478.         /* NVARCHAR  user_type, create_params, auto_increment */
  479.         values           (0,    'max length' ,0, 'nvarchar')
  480.  
  481.     /* SET user_type TO SPHINX VALUES */
  482.     update spt_datatype_info_ext set user_type = xusertype
  483.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  484.  
  485.     commit tran
  486. end
  487. go
  488.  
  489. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  490. go
  491.  
  492. print 'creating table spt_datatype_info'
  493. go
  494. if (charindex('6.00', @@version) = 0 and
  495.     charindex('6.50', @@version) = 0 and
  496.     charindex('7.00', @@version) = 0 and
  497.     charindex('8.00', @@version) = 0)
  498. begin   /*  Pre 6.0 server */
  499.     print ''
  500.     print ''
  501.     print 'Warning:'
  502.     print 'you are installing the stored procedures '
  503.     print 'on a pre 6.0 SQL Server.'
  504.     print 'Ignore the following error.'
  505.     create table spt_datatype_info (
  506.         ss_dtype            tinyint     not null,
  507.         fixlen              int         null,       /* datatype len for variable, else null */
  508.         ODBCVer             tinyint     null,       /* version if needed, else null */
  509.         TYPE_NAME           varchar(32) not null,
  510.         DATA_TYPE           smallint    not null,
  511.         data_precision      int         null,
  512.         numeric_scale       smallint    null,   /* min scale if 6.0 */
  513.         RADIX               smallint    null,
  514.         length              int         null,
  515.         LITERAL_PREFIX      varchar(32) null,
  516.         LITERAL_SUFFIX      varchar(32) null,
  517.         CREATE_PARAMS       varchar(32) null,
  518.         NULLABLE            smallint    not null,
  519.         CASE_SENSITIVE      smallint    not null,
  520.         SEARCHABLE          smallint    not null,
  521.         UNSIGNED_ATTRIBUTE  smallint    null,
  522.         MONEY               smallint    not null,
  523.         AUTO_INCREMENT      smallint    null,
  524.         LOCAL_TYPE_NAME     varchar(32) null,
  525.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  526.         SQL_DATA_TYPE       smallint    not null,
  527.         SQL_DATETIME_SUB    smallint    null)
  528. end
  529. go
  530. if (charindex('6.00', @@version) > 0 or
  531.     charindex('6.50', @@version) > 0 or
  532.     charindex('7.00', @@version) > 0 or
  533.     charindex('8.00', @@version) > 0)
  534. begin   /* 6.0 or later server */
  535.     create table spt_datatype_info (
  536.         ss_dtype            tinyint     not null,
  537.         fixlen              int         null,       /* datatype len for variable, else null */
  538.         ODBCVer             tinyint     null,       /* version if needed, else null */
  539.         TYPE_NAME           sysname     not null,
  540.         DATA_TYPE           smallint    not null,
  541.         data_precision      int         null,
  542.         numeric_scale       smallint    null,   /* min scale if 6.0 */
  543.         RADIX               smallint    null,
  544.         length              int         null,
  545.         LITERAL_PREFIX      varchar(32) null,
  546.         LITERAL_SUFFIX      varchar(32) null,
  547.         CREATE_PARAMS       varchar(32) null,
  548.         NULLABLE            smallint    not null,
  549.         CASE_SENSITIVE      smallint    not null,
  550.         SEARCHABLE          smallint    not null,
  551.         UNSIGNED_ATTRIBUTE  smallint    null,
  552.         MONEY               smallint    not null,
  553.         AUTO_INCREMENT      smallint    null,
  554.         LOCAL_TYPE_NAME     sysname     null,
  555.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  556.         SQL_DATA_TYPE       smallint    not null,
  557.         SQL_DATETIME_SUB    smallint    null)
  558. end
  559. go
  560.  
  561. grant select on spt_datatype_info to public
  562. go
  563.  
  564. /* Get case sensitivity */
  565. if 'A' = 'A' /* create dummy begin block */
  566. begin
  567.     declare @case smallint
  568.  
  569.     begin tran
  570.     select @case = 0
  571.     select @case = 1 where 'a' <> 'A'
  572.  
  573.     /* Local Binary */
  574.     insert into spt_datatype_info values
  575.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  576.  
  577.     /* Local Bit */
  578.     insert into spt_datatype_info values
  579.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  580.  
  581.     /* Local Char */
  582.     insert into spt_datatype_info values
  583.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  584.  
  585.     /* Local Datetime */
  586.     insert into spt_datatype_info values
  587.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  588.     insert into spt_datatype_info values
  589.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  590.  
  591.     /* Local Smalldatetime */
  592.     insert into spt_datatype_info values
  593.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  594.     insert into spt_datatype_info values
  595.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  596.  
  597.     /* Local Float */
  598.     insert into spt_datatype_info values
  599.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  600.     insert into spt_datatype_info values
  601.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  602.  
  603.     /* Local Real */
  604.     insert into spt_datatype_info values
  605.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  606.     insert into spt_datatype_info values
  607.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  608.  
  609.     /* Local Smallmoney */
  610.     insert into spt_datatype_info values
  611.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  612.  
  613.     /* Local Money */
  614.     insert into spt_datatype_info values
  615.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  616.  
  617.     /* Local Int */
  618.     insert into spt_datatype_info values
  619.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  620.  
  621.     commit tran
  622. end
  623. go
  624.  
  625. if 'A' = 'A' /* create dummy begin block */
  626. begin
  627.     declare @case smallint
  628.  
  629.     begin tran
  630.     select @case = 0
  631.     select @case = 1 where 'a' <> 'A'
  632.  
  633.  
  634.     /* Local Smallint */
  635.     insert into spt_datatype_info values
  636.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  637.     insert into spt_datatype_info values
  638.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  639.  
  640.     /* Local Tinyint */
  641.     insert into spt_datatype_info values
  642.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  643.  
  644.     /* Local Text */
  645.     insert into spt_datatype_info values
  646.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  647.  
  648.     /* Local Varbinary */
  649.     insert into spt_datatype_info values
  650.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  651.  
  652.     /* Local Varchar */
  653.     insert into spt_datatype_info values
  654.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  655.  
  656.     /* Local Image */
  657.     insert into spt_datatype_info values
  658.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  659.  
  660.     if (charindex('6.00', @@version) > 0 or
  661.         charindex('6.50', @@version) > 0 or
  662.         charindex('7.00', @@version) > 0 or
  663.         charindex('8.00', @@version) > 0)
  664.     begin   /*  Add 6.0 data types */
  665.         /* Local Decimal */
  666.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  667.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  668.  
  669.         /* Local Numeric */
  670.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  671.         (63,null,null,'numeric',2   ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  672.  
  673.         /* Identity attribute data types */
  674.  
  675.         /* Identity Int */
  676.         insert into spt_datatype_info values
  677.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  678.  
  679.         /* Identity Smallint */
  680.         insert into spt_datatype_info values
  681.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  682.  
  683.         /* Identity Tinyint */
  684.         insert into spt_datatype_info values
  685.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  686.  
  687.         /* Identity Numeric */
  688.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  689.         (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)
  690.  
  691.         /* Identity Numeric */
  692.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  693.         (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)
  694.  
  695.     end
  696.  
  697.     if (charindex('7.00', @@version) = 0 and
  698.         charindex('8.00', @@version) = 0)
  699.     begin   /*  Add nullable type for non-8.0 server */
  700.         /* Local Datetimn */
  701.         insert into spt_datatype_info values
  702.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  703.         insert into spt_datatype_info values
  704.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  705.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  706.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  707.         insert into spt_datatype_info values
  708.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  709.  
  710.         /* Local Floatn */
  711.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  712.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  713.         insert into spt_datatype_info values
  714.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  715.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  716.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  717.         insert into spt_datatype_info values
  718.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  719.  
  720.         /* Local Moneyn */
  721.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  722.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  723.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  724.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  725.  
  726.         /* Local Intn */
  727.         insert into spt_datatype_info values /* sql server type is 'intn' */
  728.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  729.         insert into spt_datatype_info values /* sql server type is 'intn' */
  730.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  731.         insert into spt_datatype_info values
  732.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  733.  
  734.         if (charindex('6.00', @@version) > 0 or
  735.             charindex('6.50', @@version) > 0 or
  736.             charindex('7.00', @@version) > 0 or
  737.             charindex('8.00', @@version) > 0)
  738.         begin   /*  Add 6.0 data types */
  739.             /* Local Decimaln */
  740.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  741.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  742.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  743.             (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)
  744.  
  745.             /* Local Numericn */
  746.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  747.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  748.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  749.             (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)
  750.         end
  751.     end
  752.  
  753.     commit tran
  754. end
  755. go
  756.  
  757. if (charindex('7.00', @@version) = 0 and
  758.     charindex('8.00', @@version) = 0)
  759. begin
  760.     print ''
  761.     print ''
  762.     print 'Warning:'
  763.     print 'you are installing the stored procedures '
  764.     print 'on a pre 8.0 SQL Server.'
  765.     print 'Ignore the following errors.'
  766. end
  767. go
  768. if (charindex('7.00', @@version) > 0 or
  769.     charindex('8.00', @@version) > 0)
  770.     begin
  771.         declare @ncase smallint
  772.  
  773.         select @ncase = 0
  774.         select @ncase = 1 where N'a' <> N'A'
  775.  
  776.         /* Local Timestamp */
  777.         insert into spt_datatype_info values
  778.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  779.  
  780.         /* Local GUID */
  781.         insert into spt_datatype_info values
  782.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  783.  
  784.         /* Local NChar */
  785.         insert into spt_datatype_info values
  786.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  787.  
  788.         /* Local NVarchar */
  789.         insert into spt_datatype_info values
  790.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  791.  
  792.         /* Local NText */
  793.         insert into spt_datatype_info values
  794.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  795.  
  796.         if (charindex('8.00', @@version) > 0)
  797.         begin
  798. --          /* Local BIGINT */
  799.             insert into spt_datatype_info values
  800.             (127,8,null,'bigint',-5,19,0,10,null,null,null,null,1,0,2,0,0,0,'bigint',null,-5,null)
  801. --          /* Identity BIGINT */
  802.             insert into spt_datatype_info values
  803.             (127,8,null,'bigint identity',-5,19,0,10,null,null,null,null,0,0,2,0,0,1,'bigint identity',null,-5,null)
  804.  
  805.  
  806. --          /* sql_variant */
  807.             insert into spt_datatype_info values
  808.             (
  809.             98,                 --ss_dtype      
  810.             null,               --fixlen
  811.             null,               --ODBCVer
  812.             'sql_variant',      --TYPE_NAME
  813.             -150,               --SQL DATA TYPE
  814.             8000,               --data_precision
  815.             0,                  --numeric_scale
  816.             10,                 --RADIX
  817.             8000,               --length
  818.             null,               --PREFIX
  819.             null,               --SUFFIX
  820.             null,               --Create Params
  821.             1,                  --Nullable
  822.             0,              --Case sensitive
  823.             2,                  --Searchable
  824.             null,               --UNSIGNED_ATTRIBUTE
  825.             0,              --MONEY
  826.             null,               --AUTO_INCREMENT
  827.             'sql_variant',      --LOCAL TYPE NAME
  828.             0,                  --CHARBIN
  829.             -150,               --SQL_DATA_TYPE
  830.             null                --SQL_DATETIME_SUB
  831.             )
  832.         
  833.         end
  834.  
  835.  
  836.  
  837.         update spt_datatype_info set NULLABLE = 1
  838.             where TYPE_NAME = 'bit'
  839.  
  840.         update spt_datatype_info set ss_dtype = isnull((select distinct xtype from systypes
  841.             where TYPE_NAME like name+'%'),0)
  842.     end
  843. go
  844.  
  845. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  846. go
  847.  
  848. dump tran master with no_log
  849. go
  850.  
  851. print 'creating table spt_server_info'
  852. go
  853. create table spt_server_info (
  854.               attribute_id      int NOT NULL,
  855.               attribute_name    varchar(60) NOT NULL,
  856.               attribute_value   varchar(255) NOT NULL)
  857. go
  858.  
  859. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  860. go
  861.  
  862. if (charindex('7.00', @@version) = 0 and charindex('8.00', @@version) = 0)
  863.     begin
  864.         drop procedure sp_add_server_sortinfo   /* not used by other servers */
  865.         drop procedure sp_add_server_sortinfo75 /* not used by older servers */
  866.         dump tran master with no_log
  867.     end
  868. go
  869.  
  870. insert into spt_server_info
  871.     values (1,'DBMS_NAME','Microsoft SQL Server')
  872. insert into spt_server_info
  873.     values (2,'DBMS_VER',@@version)
  874. insert into spt_server_info
  875.     values (10,'OWNER_TERM','owner')
  876. insert into spt_server_info
  877.     values (11,'TABLE_TERM','table')
  878. insert into spt_server_info
  879.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  880. insert into spt_server_info
  881.     values (13,'TABLE_LENGTH','30')
  882. insert into spt_server_info
  883.     values (14,'MAX_QUAL_LENGTH','30')
  884. insert into spt_server_info
  885.     values (15,'COLUMN_LENGTH','30')
  886.  
  887. if 'A' = 'a' /* If not case sensitive server */
  888. begin
  889.     insert into spt_server_info
  890.         values (16,'IDENTIFIER_CASE','MIXED')
  891. end
  892. else
  893. begin
  894.     insert into spt_server_info
  895.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  896. end
  897.  
  898. insert into spt_server_info
  899.     values (17,'TX_ISOLATION','2')
  900.  
  901. if (charindex('6.00', @@version) > 0 or
  902.     charindex('6.50', @@version) > 0 or
  903.     charindex('7.00', @@version) > 0) 
  904. begin   /*  Add 6.0 collation sequence */
  905.     insert into spt_server_info
  906.         select 18,'COLLATION_SEQ',
  907.             'charset='+t2.name+' sort_order='+t1.name
  908.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  909.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  910.         from syscharsets t1, syscharsets t2, sysconfigures t3
  911.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  912. end
  913. else 
  914. begin   /*  Add 4.2x collation sequence */
  915.     insert into spt_server_info
  916.         select 18,'COLLATION_SEQ',
  917.             'charset='+t2.name+' sort_order='+t1.name
  918.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  919.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  920.         from syscharsets t1, syscharsets t2, sysconfigures t3
  921.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  922. end
  923.  
  924. insert into spt_server_info
  925.     values (19,'SAVEPOINT_SUPPORT','Y')
  926. insert into spt_server_info
  927.     values (20,'MULTI_RESULT_SETS','Y')
  928. insert into spt_server_info
  929.     values (22,'ACCESSIBLE_TABLES','Y')
  930. insert into spt_server_info
  931.     values (100,'USERID_LENGTH','30')
  932. insert into spt_server_info
  933.     values (101,'QUALIFIER_TERM','database')
  934. insert into spt_server_info
  935.     values (102,'NAMED_TRANSACTIONS','Y')
  936. insert into spt_server_info
  937.     values (103,'SPROC_AS_LANGUAGE','Y')
  938. insert into spt_server_info
  939.     values (104,'ACCESSIBLE_SPROC','Y')
  940. insert into spt_server_info
  941.     values (105,'MAX_INDEX_COLS','16')
  942. insert into spt_server_info
  943.     values (106,'RENAME_TABLE','Y')
  944. insert into spt_server_info
  945.     values (107,'RENAME_COLUMN','Y')
  946.  
  947. if (charindex('8.00', @@version) > 0)
  948. begin
  949.     /* Columns may be dropped on 8.0 or later */
  950.     insert into spt_server_info
  951.         values (108,'DROP_COLUMN','Y')
  952. end
  953. else
  954. begin
  955.     insert into spt_server_info
  956.         values (108,'DROP_COLUMN','N')
  957. end
  958.  
  959. if (charindex('8.00', @@version) > 0)
  960. begin
  961.     /* Columns size may be changed on 8.0 or later */
  962.     insert into spt_server_info
  963.         values (109,'INCREASE_COLUMN_LENGTH','Y')
  964. end
  965. else
  966. begin
  967.     insert into spt_server_info
  968.         values (109,'INCREASE_COLUMN_LENGTH','N')
  969. end
  970.  
  971. if (charindex('6.50', @@version) = 0 and
  972.     charindex('7.00', @@version) = 0 and
  973.     charindex('8.00', @@version) = 0)
  974. begin
  975.     insert into spt_server_info
  976.         values (110,'DDL_IN_TRANSACTION','N')
  977. end
  978. else
  979. begin
  980.     insert into spt_server_info
  981.         values (110,'DDL_IN_TRANSACTION','Y')
  982. end
  983.  
  984.  
  985. if (charindex('8.00', @@version) > 0)
  986. begin
  987.     /* Descending indexes allowed on 8.0 or later */
  988.     insert into spt_server_info
  989.         values (111,'DESCENDING_INDEXES','Y')
  990. end
  991. else
  992. begin
  993.     insert into spt_server_info
  994.         values (111,'DESCENDING_INDEXES','N')
  995. end
  996.  
  997. insert into spt_server_info
  998.     values (112,'SP_RENAME','Y')
  999. insert into spt_server_info
  1000.     values (113,'REMOTE_SPROC','Y')
  1001. insert into spt_server_info
  1002.     values (500,'SYS_SPROC_VERSION',convert(varchar(9), serverproperty ('ProductVersion')))
  1003. go
  1004.  
  1005. if (charindex('7.00', @@version) > 0 or
  1006.     charindex('8.00', @@version) > 0)
  1007. begin   /*  Update values for 8.0 server */
  1008.     update spt_server_info set attribute_value = '128'
  1009.         where attribute_id in (12,13,14,15,100)
  1010. end
  1011. go
  1012.  
  1013. grant select on spt_server_info to public
  1014. go
  1015.  
  1016. print 'creating sp_column_privileges'
  1017. go
  1018.  
  1019. /*  Procedure for pre 6.50 server */
  1020. CREATE PROCEDURE sp_column_privileges (
  1021.             @table_name         varchar(32),
  1022.             @table_owner        varchar(32) = null,
  1023.             @table_qualifier    varchar(32) = null,
  1024.             @column_name        varchar(96) = null) /* 3*32 */
  1025. as
  1026.  
  1027.     set nocount on
  1028.     declare @table_id    int
  1029.     DECLARE @full_table_name    varchar(65) /* 2*32 + 1 */
  1030.     declare @low smallint                    /* range of userids to check */
  1031.     declare @high smallint
  1032.     declare @owner_uid smallint
  1033.  
  1034.     select @low = 0, @high = 32767
  1035.  
  1036.     if @column_name is null /*  If column name not supplied, match all */
  1037.         select @column_name = '%'
  1038.  
  1039.     if @table_qualifier is not null
  1040.     begin
  1041.         if db_name() <> @table_qualifier
  1042.         begin   /* If qualifier doesn't match current database */
  1043.             raiserror 20001 '~~Rush_5~~'
  1044.             return
  1045.         end
  1046.     end
  1047.     if @table_owner is null
  1048.     begin   /* If unqualified table name */
  1049.         SELECT @full_table_name = @table_name
  1050.     end
  1051.     else
  1052.     begin   /* Qualified table name */
  1053.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1054.     end
  1055.     /*  Get Object ID */
  1056.     select @table_id = object_id(@full_table_name)
  1057.  
  1058.     if (@@trancount <> 0)
  1059.     begin   /* If inside a transaction */
  1060.         raiserror 20003 '~~Rush_6~~'
  1061.         return
  1062.     end
  1063.  
  1064.     /*
  1065.     ** We need to create a table which will contain a row for every row to
  1066.     ** be returned to the client.
  1067.     */
  1068.  
  1069.     create table #column_priv1(
  1070.         COLUMN_NAME             varchar(32) NOT NULL,
  1071.         grantor                 smallint NOT NULL,
  1072.         grantee                 smallint NOT NULL,
  1073.         select_privilege        bit,
  1074.         select_grantable        bit,
  1075.         insert_privilege        bit,
  1076.         insert_grantable        bit,
  1077.         update_privilege        bit,
  1078.         update_grantable        bit,
  1079.         references_privilege    bit,
  1080.         references_grantable    bit,
  1081.         uid                     smallint NOT NULL,
  1082.         gid                     smallint NOT NULL)
  1083.  
  1084. /*
  1085. ** insert a row for the table owner (who has all permissions)
  1086. */
  1087.     select @owner_uid = (
  1088.         select uid
  1089.         from sysobjects
  1090.         where id = @table_id)
  1091.  
  1092.     if (charindex('6.00', @@version) > 0)
  1093.     begin
  1094.         insert into #column_priv1
  1095.             select
  1096.                 c.name,
  1097.                 u.uid,
  1098.                 @owner_uid,
  1099.                 0,
  1100.                 1,
  1101.                 0,
  1102.                 1,
  1103.                 0,
  1104.                 1,
  1105.                 0,
  1106.                 1,
  1107.                 @owner_uid,
  1108.                 0
  1109.             from syscolumns c, sysusers u
  1110.             where id = @table_id
  1111.                 and c.number = 0
  1112.                 and u.uid = 1       /* grantor is dbo of database */
  1113.     end
  1114.     else
  1115.     begin
  1116.         insert into #column_priv1
  1117.             select
  1118.                 c.name,
  1119.                 u.uid,
  1120.                 @owner_uid,
  1121.                 0,
  1122.                 1,
  1123.                 0,
  1124.                 1,
  1125.                 0,
  1126.                 1,
  1127.                 0,
  1128.                 0,
  1129.                 @owner_uid,
  1130.                 0
  1131.             from syscolumns c, sysusers u
  1132.             where id = @table_id
  1133.                 and c.number = 0
  1134.                 and u.uid = 1       /* grantor is dbo of database */
  1135.     end
  1136. /*
  1137. ** now stick in a row for every column for every user in the database
  1138. ** we will need to weed out those who have no permissions later
  1139. ** (and yes this is a cartesion product: the uid field in sysprotects
  1140. ** can also have a group id, in which case we need to extend those
  1141. ** privileges to all group members).
  1142. */
  1143.  
  1144.     insert into #column_priv1
  1145.         select distinct
  1146.             c.name,
  1147.             o.uid,
  1148.             u.uid,
  1149.             0,
  1150.             0,
  1151.             0,
  1152.             0,
  1153.             0,
  1154.             0,
  1155.             0,
  1156.             0,
  1157.             u.uid,
  1158.             u.gid
  1159.         from sysusers u, syscolumns c, sysobjects o
  1160.         where o.id = @table_id
  1161.             and c.id = o.id
  1162.             and c.number = 0
  1163.             and u.gid <> u.uid
  1164.             and u.uid <> @owner_uid
  1165.  
  1166.     /*
  1167.     ** we need to create another temporary table to contain all the various
  1168.     ** protection information for the table in question
  1169.     */
  1170.     create table #protects (
  1171.                 uid         smallint NOT NULL,
  1172.                 grantor     smallint NOT NULL,
  1173.                 action      tinyint NOT NULL,
  1174.                 protecttype tinyint NOT NULL,
  1175.                 name        varchar(32) NOT NULL)
  1176.  
  1177.     insert into #protects
  1178.         select
  1179.             p.uid,
  1180.             p.uid,
  1181.             p.action,
  1182.             p.protecttype,
  1183.             isnull(col_name(id, c.number), '~All')
  1184.             from
  1185.                 sysprotects p,
  1186.                 master.dbo.spt_values c,
  1187.                 master.dbo.spt_values a,
  1188.                 master.dbo.spt_values b
  1189.             where
  1190.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1191.                     & c.high <> 0
  1192.                     and c.number <= (
  1193.                         select count(*)
  1194.                         from syscolumns
  1195.                         where id = @table_id)
  1196.                 and c.type = 'P'
  1197.                 and a.type = 'T'
  1198.                 and a.number = p.action
  1199.                 and p.action in (193,195,197,26)
  1200.                 and b.type = 'T'
  1201.                 and b.number = p.protecttype
  1202.                 and p.id = @table_id
  1203.                 and p.uid between @low and @high
  1204.  
  1205.  
  1206.     update #column_priv1
  1207.     set select_privilege = 1
  1208.         from #protects p
  1209.     where
  1210.         p.protecttype = 205
  1211.         and p.action = 193
  1212.         and (p.name = #column_priv1.COLUMN_NAME
  1213.             or name = '~All')
  1214.         and (p.uid = 0
  1215.             or p.uid = #column_priv1.gid
  1216.             or p.uid = #column_priv1.uid)
  1217.         and not exists (
  1218.             select * from #protects
  1219.             where
  1220.                 protecttype = 206
  1221.                 and action = 193
  1222.                 and (name = #column_priv1.COLUMN_NAME
  1223.                     or name = '~All')
  1224.                 and ( uid = 0
  1225.                     or uid = #column_priv1.gid
  1226.                     or uid = #column_priv1.uid))
  1227.  
  1228.     update #column_priv1
  1229.     set insert_privilege = 1
  1230.         from #protects p
  1231.     where
  1232.         p.protecttype = 205
  1233.         and p.action = 195
  1234.         and (p.name = #column_priv1.COLUMN_NAME
  1235.             or name = '~All')
  1236.         and (p.uid = 0
  1237.             or p.uid = #column_priv1.gid
  1238.             or p.uid = #column_priv1.uid)
  1239.         and not exists (
  1240.             select * from #protects
  1241.             where
  1242.                 protecttype = 206
  1243.                 and action = 195
  1244.                 and (name = #column_priv1.COLUMN_NAME
  1245.                     or name = '~All')
  1246.                 and (uid = 0
  1247.                     or uid = #column_priv1.gid
  1248.                     or uid = #column_priv1.uid))
  1249.  
  1250.     update #column_priv1
  1251.     set update_privilege = 1
  1252.         from #protects p
  1253.     where
  1254.         p.protecttype = 205
  1255.         and p.action = 197
  1256.         and (p.name = #column_priv1.COLUMN_NAME
  1257.             or name = '~All')
  1258.         and (p.uid = 0
  1259.             or p.uid = #column_priv1.gid
  1260.             or p.uid = #column_priv1.uid)
  1261.         and not exists (
  1262.             select * from #protects
  1263.                 where protecttype = 206
  1264.                 and action = 197
  1265.                 and (name = #column_priv1.COLUMN_NAME
  1266.                     or name = '~All')
  1267.                 and (uid = 0
  1268.                     or uid = #column_priv1.gid
  1269.                     or uid = #column_priv1.uid))
  1270.  
  1271.     update #column_priv1
  1272.     set references_privilege = 1
  1273.         from #protects p
  1274.     where
  1275.         p.protecttype = 205
  1276.         and p.action = 26
  1277.         and (p.name = #column_priv1.COLUMN_NAME
  1278.             or name = '~All')
  1279.         and (p.uid = 0
  1280.             or p.uid = #column_priv1.gid
  1281.             or p.uid = #column_priv1.uid)
  1282.         and not exists (
  1283.             select * from #protects
  1284.                 where protecttype = 206
  1285.                 and action = 26
  1286.                 and (name = #column_priv1.COLUMN_NAME
  1287.                     or name = '~All')
  1288.                 and (uid = 0
  1289.                     or uid = #column_priv1.gid
  1290.                     or uid = #column_priv1.uid))
  1291.  
  1292.     update #column_priv1
  1293.     set select_grantable = 1
  1294.         from #protects p
  1295.     where
  1296.         p.protecttype = 204
  1297.         and p.action = 193
  1298.         and (p.name = #column_priv1.COLUMN_NAME
  1299.             or name = '~All')
  1300.         and (p.uid = 0
  1301.             or p.uid = #column_priv1.gid
  1302.             or p.uid = #column_priv1.uid)
  1303.         and not exists (
  1304.             select * from #protects
  1305.             where
  1306.                 protecttype = 206
  1307.                 and action = 193
  1308.                 and (name = #column_priv1.COLUMN_NAME
  1309.                     or name = '~All')
  1310.                 and ( uid = 0
  1311.                     or uid = #column_priv1.gid
  1312.                     or uid = #column_priv1.uid))
  1313.  
  1314.     update #column_priv1
  1315.     set insert_grantable = 1
  1316.         from #protects p
  1317.     where
  1318.         p.protecttype = 204
  1319.         and p.action = 195
  1320.         and (p.name = #column_priv1.COLUMN_NAME
  1321.             or name = '~All')
  1322.         and (p.uid = 0
  1323.             or p.uid = #column_priv1.gid
  1324.             or p.uid = #column_priv1.uid)
  1325.         and not exists (
  1326.             select * from #protects
  1327.             where
  1328.                 protecttype = 206
  1329.                 and action = 195
  1330.                 and (name = #column_priv1.COLUMN_NAME
  1331.                     or name = '~All')
  1332.                 and ( uid = 0
  1333.                     or uid = #column_priv1.gid
  1334.                     or uid = #column_priv1.uid))
  1335.  
  1336.     update #column_priv1
  1337.     set update_grantable = 1
  1338.         from #protects p
  1339.     where
  1340.         p.protecttype = 204
  1341.         and p.action = 197
  1342.         and (p.name = #column_priv1.COLUMN_NAME
  1343.             or name = '~All')
  1344.         and (p.uid = 0
  1345.             or p.uid = #column_priv1.gid
  1346.             or p.uid = #column_priv1.uid)
  1347.         and not exists (
  1348.             select * from #protects
  1349.             where
  1350.                 protecttype = 206
  1351.                 and action = 197
  1352.                 and (name = #column_priv1.COLUMN_NAME
  1353.                     or name = '~All')
  1354.                 and ( uid = 0
  1355.                     or uid = #column_priv1.gid
  1356.                     or uid = #column_priv1.uid))
  1357.  
  1358.     update #column_priv1
  1359.     set references_grantable = 1
  1360.         from #protects p
  1361.     where
  1362.         p.protecttype = 204
  1363.         and p.action = 26
  1364.         and (p.name = #column_priv1.COLUMN_NAME
  1365.             or name = '~All')
  1366.         and (p.uid = 0
  1367.             or p.uid = #column_priv1.gid
  1368.             or p.uid = #column_priv1.uid)
  1369.         and not exists (
  1370.             select * from #protects
  1371.             where
  1372.                 protecttype = 206
  1373.                 and action = 26
  1374.                 and (name = #column_priv1.COLUMN_NAME
  1375.                     or name = '~All')
  1376.                 and ( uid = 0
  1377.                     or uid = #column_priv1.gid
  1378.                     or uid = #column_priv1.uid))
  1379.  
  1380.     create table #column_priv2(
  1381.         COLUMN_NAME     varchar(32) NOT NULL,
  1382.         grantor         smallint NULL,
  1383.         grantee         smallint NOT NULL,
  1384.         PRIVILEGE       varchar(32) NOT NULL,
  1385.         IS_GRANTABLE    varchar(3) NULL)
  1386.  
  1387.     insert into #column_priv2
  1388.         select
  1389.             COLUMN_NAME,
  1390.             grantor,
  1391.             grantee,
  1392.             'SELECT',
  1393.             'NO'
  1394.         from #column_priv1
  1395.         where select_privilege = 1 and select_grantable = 0
  1396.  
  1397.     insert into #column_priv2
  1398.         select
  1399.             COLUMN_NAME,
  1400.             grantor,
  1401.             grantee,
  1402.             'INSERT',
  1403.             'NO'
  1404.         from #column_priv1
  1405.         where insert_privilege = 1 and insert_grantable = 0
  1406.  
  1407.     insert into #column_priv2
  1408.         select
  1409.             COLUMN_NAME,
  1410.             grantor,
  1411.             grantee,
  1412.             'UPDATE',
  1413.             'NO'
  1414.         from #column_priv1
  1415.         where update_privilege = 1 and update_grantable = 0
  1416.  
  1417.     insert into #column_priv2
  1418.         select
  1419.             COLUMN_NAME,
  1420.             grantor,
  1421.             grantee,
  1422.             'REFERENCES',
  1423.             'NO'
  1424.         from #column_priv1
  1425.         where references_privilege = 1 and references_grantable = 0
  1426.  
  1427.     insert into #column_priv2
  1428.         select
  1429.             COLUMN_NAME,
  1430.             grantor,
  1431.             grantee,
  1432.             'SELECT',
  1433.             'YES'
  1434.         from #column_priv1
  1435.         where select_grantable = 1
  1436.  
  1437.     insert into #column_priv2
  1438.         select
  1439.             COLUMN_NAME,
  1440.             grantor,
  1441.             grantee,
  1442.             'INSERT',
  1443.             'YES'
  1444.         from #column_priv1
  1445.         where insert_grantable = 1
  1446.  
  1447.     insert into #column_priv2
  1448.         select
  1449.             COLUMN_NAME,
  1450.             grantor,
  1451.             grantee,
  1452.             'UPDATE',
  1453.             'YES'
  1454.         from #column_priv1
  1455.         where update_grantable = 1
  1456.  
  1457.     insert into #column_priv2
  1458.         select
  1459.             COLUMN_NAME,
  1460.             grantor,
  1461.             grantee,
  1462.             'REFERENCES',
  1463.             'YES'
  1464.         from #column_priv1
  1465.         where references_grantable = 1
  1466.  
  1467.     select
  1468.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1469.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1470.         @table_name TABLE_NAME,
  1471.         COLUMN_NAME,
  1472.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1473.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1474.         PRIVILEGE,
  1475.         IS_GRANTABLE
  1476.     from #column_priv2
  1477.     where COLUMN_NAME like @column_name
  1478.     order by 4, 7
  1479. go
  1480.  
  1481. if (charindex('6.50', @@version) = 0 and
  1482.     charindex('7.00', @@version) = 0 and
  1483.     charindex('8.00', @@version) = 0)
  1484. begin
  1485.     print ''
  1486.     print ''
  1487.     print 'Warning:'
  1488.     print 'you are installing the stored procedures '
  1489.     print 'on a pre 6.50 SQL Server.'
  1490.     print 'Ignore the following errors.'
  1491. end
  1492. else
  1493.     drop proc sp_column_privileges
  1494. go
  1495.  
  1496. /*  Procedure for 6.50 server */
  1497. CREATE PROCEDURE sp_column_privileges (
  1498.             @table_name         varchar(32),
  1499.             @table_owner        varchar(32) = null,
  1500.             @table_qualifier    varchar(32) = null,
  1501.             @column_name        varchar(96) = null) /* 3*32 */
  1502. as
  1503.  
  1504.     declare @table_id    int
  1505.  
  1506.     if @column_name is null /*  If column name not supplied, match all */
  1507.         select @column_name = '%'
  1508.  
  1509.     if @table_qualifier is not null
  1510.     begin
  1511.         if db_name() <> @table_qualifier
  1512.         begin   /* If qualifier doesn't match current database */
  1513.             raiserror (15250, -1,-1)
  1514.             return
  1515.         end
  1516.     end
  1517.     if @table_owner is null
  1518.     begin   /* If unqualified table name */
  1519.         select @table_id = object_id(@table_name)
  1520.     end
  1521.     else
  1522.     begin   /* Qualified table name */
  1523.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1524.     end
  1525.  
  1526.     select
  1527.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1528.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1529.         @table_name TABLE_NAME,
  1530.         convert(varchar(32),c.name) COLUMN_NAME,
  1531.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1532.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1533.         convert (varchar(32),case p.action
  1534.              when 193 then 'SELECT'
  1535.              when 195 then 'INSERT'
  1536.              when 197 then 'UPDATE'
  1537.              else 'REFERENCES'
  1538.         end) PRIVILEGE,
  1539.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1540.             else 'YES'
  1541.         end) IS_GRANTABLE
  1542.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1543.     where
  1544.         c.id = @table_id
  1545.         and c.name like @column_name
  1546.         and c.id = p.id
  1547.         and c.id = o.id
  1548.         and case substring(p.columns, 1, 1) & 1
  1549.                 when NULL then 255  /* all columns have permission */
  1550.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1551.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1552.             end
  1553.             & v.high <> 0           /* permission applies to this column */
  1554.         and v.number <= (select count(*) from syscolumns
  1555.             where id = @table_id)   /* ranges from 1 to # of columns in table */
  1556.         and v.type = 'P'
  1557.         and v.number = c.colid
  1558.             /* expand groups */
  1559.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1560.              (p.uid = u.gid and u.uid <> u.gid))
  1561.         and p.protecttype <> 206    /* only grant rows */
  1562.         and p.action in (26,193,195,197)
  1563.         and o.uid <> u.uid          /* no rows for owner */
  1564.         and not exists (            /* exclude revoke'd privileges */
  1565.             select *
  1566.             from sysprotects p1
  1567.             where
  1568.                 p1.protecttype = 206
  1569.                 and p1.action = p.action
  1570.                 and p1.id = p.id
  1571.                 and p1.uid = u.uid
  1572.                 and case substring(p1.columns, 1, 1) & 1
  1573.                         when NULL then 255  /* all columns have permission */
  1574.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1575.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1576.                     end
  1577.                     & v.high <> 0)          /* permission applies to this column */
  1578.     union
  1579.     select  /*  Add rows for table owner */
  1580.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1581.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1582.         @table_name TABLE_NAME,
  1583.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1584.         convert(varchar(32),user_name(u.uid)) grantor,
  1585.         convert(varchar(32),user_name(o.uid)) grantee,
  1586.         convert (varchar(32),case v.number
  1587.             when 193 then 'SELECT'
  1588.             when 195 then 'INSERT'
  1589.             when 197 then 'UPDATE'
  1590.             else 'REFERENCES'
  1591.         end) PRIVILEGE,
  1592.         convert(varchar(3),'YES') IS_GRANTABLE
  1593.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1594.     where
  1595.         c.id = @table_id
  1596.         and c.name like @column_name
  1597.         and c.id = o.id
  1598.         and u.uid = 1       /* grantor is dbo of database */
  1599.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1600.         and v.number in (26,193,195,197)
  1601.         and not exists (    /* exclude revoke'd privileges */
  1602.             select *
  1603.             from sysprotects p1
  1604.             where
  1605.                 p1.protecttype = 206
  1606.                 and p1.action = v.number
  1607.                 and p1.id = o.id
  1608.                 and p1.uid = o.uid)
  1609.     order by 4, 7
  1610. go
  1611.  
  1612.  
  1613.  
  1614. if (charindex('7.00', @@version) = 0 and
  1615.     charindex('8.00', @@version) = 0)
  1616. begin
  1617.     print ''
  1618.     print ''
  1619.     print 'Warning:'
  1620.     print 'you are installing the stored procedures '
  1621.     print 'on a pre 8.0 SQL Server.'
  1622.     print 'Ignore the following errors.'
  1623. end
  1624. else
  1625.     drop proc sp_column_privileges
  1626. go
  1627.  
  1628. /*  Procedure for 8.0 server */
  1629. CREATE PROCEDURE sp_column_privileges (
  1630.             @table_name         sysname,
  1631.             @table_owner        sysname = null,
  1632.             @table_qualifier    sysname = null,
  1633.             @column_name        nvarchar(384) = null)   /* 3*128 */
  1634. as
  1635.  
  1636.     declare @table_id    int
  1637.  
  1638.     if @column_name is null /*  If column name not supplied, match all */
  1639.         select @column_name = '%'
  1640.  
  1641.     if @table_qualifier is not null
  1642.     begin
  1643.         if db_name() <> @table_qualifier
  1644.         begin   /* If qualifier doesn't match current database */
  1645.             raiserror (15250, -1,-1)
  1646.             return
  1647.         end
  1648.     end
  1649.     if @table_owner is null
  1650.     begin   /* If unqualified table name */
  1651.         select @table_id = object_id(quotename(@table_name))
  1652.     end
  1653.     else
  1654.     begin   /* Qualified table name */
  1655.         if @table_owner = N''
  1656.         begin   /* If empty owner name */
  1657.             select @table_id = 0
  1658.         end
  1659.         else
  1660.         begin
  1661.             select @table_id = object_id(quotename(@table_owner) +
  1662.                 '.' + quotename(@table_name))
  1663.         end
  1664.     end
  1665.  
  1666.     select
  1667.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1668.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1669.         @table_name TABLE_NAME,
  1670.         convert(sysname,c.name) COLUMN_NAME,
  1671.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1672.         convert(sysname,user_name(u.uid)) GRANTEE,
  1673.         convert (varchar(32),case p.action
  1674.              when 193 then 'SELECT'
  1675.              when 195 then 'INSERT'
  1676.              when 197 then 'UPDATE'
  1677.              else 'REFERENCES'
  1678.         end) PRIVILEGE,
  1679.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1680.             else 'YES'
  1681.         end) IS_GRANTABLE
  1682.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1683.     where
  1684.         c.id = @table_id
  1685.         and c.name like @column_name
  1686.         and c.id = p.id
  1687.         and c.id = o.id
  1688.         and case substring(p.columns, 1, 1) & 1
  1689.                 when NULL then 255  /* all columns have permission */
  1690.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1691.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1692.             end
  1693.             & v.high <> 0           /* permission applies to this column */
  1694.         and v.number <= (select count(*) from syscolumns
  1695.             where id = @table_id)   /* ranges from 1 to # of columns in table */
  1696.         and v.type = N'P'
  1697.         and v.number = c.colid
  1698.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1699.         and (u.uid > 0 and u.uid < 16384)
  1700.         and ((p.uid = u.uid) or 
  1701.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1702.         and p.protecttype <> 206    /* only grant rows */
  1703.         and p.action in (26,193,195,197)
  1704.         and o.uid <> u.uid          /* no rows for owner */
  1705.         and not exists (            /* exclude revoke'd privileges */
  1706.             select *
  1707.             from sysprotects p1
  1708.             where
  1709.                 p1.protecttype = 206
  1710.                 and p1.action = p.action
  1711.                 and p1.id = p.id
  1712.                 and p1.uid = u.uid
  1713.                 and case substring(p1.columns, 1, 1) & 1
  1714.                         when NULL then 255  /* all columns have permission */
  1715.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1716.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1717.                     end
  1718.                     & v.high <> 0)          /* permission applies to this column */
  1719.     union
  1720.     select  /*  Add rows for table owner */
  1721.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1722.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1723.         @table_name TABLE_NAME,
  1724.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1725.         convert(sysname,user_name(u.uid)) grantor,
  1726.         convert(sysname,user_name(o.uid)) grantee,
  1727.         convert (varchar(32),case v.number
  1728.             when 193 then 'SELECT'
  1729.             when 195 then 'INSERT'
  1730.             when 197 then 'UPDATE'
  1731.             else 'REFERENCES'
  1732.         end) PRIVILEGE,
  1733.         convert(varchar(3),'YES') IS_GRANTABLE
  1734.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1735.     where
  1736.         c.id = @table_id
  1737.         and c.name like @column_name
  1738.         and c.id = o.id
  1739.         and u.uid = 1       /* grantor is 'dbo' of database */
  1740.         and v.type = N'P'   /* cross product to get all exposed privileges */
  1741.         and v.number in (26,193,195,197)
  1742.         and not exists (    /* exclude revoke'd privileges */
  1743.             select *
  1744.             from sysprotects p1
  1745.             where
  1746.                 p1.protecttype = 206
  1747.                 and p1.action = v.number
  1748.                 and p1.id = o.id
  1749.                 and p1.uid = o.uid)
  1750.     order by 4, 7
  1751. go
  1752.  
  1753.  
  1754. grant execute on sp_column_privileges to public
  1755. go
  1756.  
  1757. dump tran master with no_log
  1758. go
  1759.  
  1760. print 'creating sp_columns'
  1761. go
  1762.  
  1763. /*  Procedure for pre-6.0 server */
  1764. CREATE PROCEDURE sp_columns (
  1765.                  @table_name        varchar(96),
  1766.                  @table_owner       varchar(96) = null,
  1767.                  @table_qualifier   varchar(32) = null,
  1768.                  @column_name       varchar(96) = null,
  1769.                  @ODBCVer           int = 2)
  1770. AS
  1771.     DECLARE @full_table_name varchar(193)
  1772.     DECLARE @table_id        int
  1773.  
  1774.     if @ODBCVer <> 3
  1775.         select @ODBCVer = 2
  1776.     if @column_name is null /*  If column name not supplied, match all */
  1777.         select @column_name = '%'
  1778.     if @table_qualifier is not null
  1779.     begin
  1780.         if db_name() <> @table_qualifier
  1781.         begin   /* If qualifier doesn't match current database */
  1782.             raiserror 20001 '~~Rush_5~~'
  1783.             return
  1784.         end
  1785.     end
  1786.     if @table_name is null
  1787.     begin   /*  If table name not supplied, match all */
  1788.         select @table_name = '%'
  1789.     end
  1790.     if @table_owner is null
  1791.     begin   /* If unqualified table name */
  1792.         SELECT @full_table_name = @table_name
  1793.     end
  1794.     else
  1795.     begin   /* Qualified table name */
  1796.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1797.     end
  1798.  
  1799.     /*  Get Object ID */
  1800.     SELECT @table_id = object_id(@full_table_name)
  1801.     if ((charindex('%',@full_table_name) = 0) and
  1802.         (charindex('_',@full_table_name) = 0) and
  1803.         @table_id <> 0)
  1804.     begin
  1805.         /* this block is for the case where there is no pattern
  1806.              matching required for the table name */
  1807.  
  1808.         SELECT
  1809.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1810.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1811.             TABLE_NAME = convert(varchar(32),o.name),
  1812.             COLUMN_NAME = convert(varchar(32),c.name),
  1813.             d.DATA_TYPE,
  1814.             TYPE_NAME = t.name,
  1815.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1816.             LENGTH = isnull(d.length, convert(int,c.length)),
  1817.             SCALE = d.numeric_scale,
  1818.             d.RADIX,
  1819.             NULLABLE =  /* set nullability from status flag */
  1820.                 convert(smallint, convert(bit, c.status&8)),
  1821.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  1822.             COLUMN_DEF = text,
  1823.             d.SQL_DATA_TYPE,
  1824.             d.SQL_DATETIME_SUB,
  1825.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1826.             ORDINAL_POSITION = convert(int,
  1827.                        (
  1828.                         select count(*)
  1829.                         from syscolumns sc
  1830.                         where sc.id     =  c.id
  1831.                           AND sc.number =  c.number
  1832.                           AND sc.colid  <= c.colid
  1833.                         )),
  1834.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1835.             SS_DATA_TYPE = c.type
  1836.         FROM
  1837.             syscolumns c,
  1838.             sysobjects o,
  1839.             syscomments m,
  1840.             master.dbo.spt_datatype_info d,
  1841.             systypes t
  1842.         WHERE
  1843.             o.id = @table_id
  1844.             AND c.id = o.id
  1845.             AND t.type = d.ss_dtype
  1846.             AND c.length = isnull(d.fixlen, c.length)
  1847.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1848.             AND o.type <> 'P'
  1849.             AND c.usertype = t.usertype
  1850.             AND c.name like @column_name
  1851.             AND c.cdefault *= m.id
  1852.             AND m.colid = 1
  1853.         ORDER BY 17
  1854.     end
  1855.     else
  1856.     begin
  1857.         /* this block is for the case where there IS pattern
  1858.              matching done on the table name */
  1859.         if @table_owner is null /*  If owner not supplied, match all */
  1860.             select @table_owner = '%'
  1861.  
  1862.         SELECT
  1863.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1864.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1865.             TABLE_NAME = convert(varchar(32),o.name),
  1866.             COLUMN_NAME = convert(varchar(32),c.name),
  1867.             d.DATA_TYPE,
  1868.             TYPE_NAME = t.name,
  1869.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1870.             LENGTH = isnull(d.length, convert(int,c.length)),
  1871.             SCALE = d.numeric_scale,
  1872.             d.RADIX,
  1873.             NULLABLE =  /* set nullability from status flag */
  1874.                 convert(smallint, convert(bit, c.status&8)),
  1875.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  1876.             COLUMN_DEF = text,
  1877.             d.SQL_DATA_TYPE,
  1878.             d.SQL_DATETIME_SUB,
  1879.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1880.             ORDINAL_POSITION = convert(int,
  1881.                        (
  1882.                         select count(*)
  1883.                         from syscolumns sc
  1884.                         where sc.id     =  c.id
  1885.                           AND sc.number =  c.number
  1886.                           AND sc.colid  <= c.colid
  1887.                         )),
  1888.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1889.             SS_DATA_TYPE = c.type
  1890.         FROM
  1891.             syscolumns c,
  1892.             sysobjects o,
  1893.             syscomments m,
  1894.             master.dbo.spt_datatype_info d,
  1895.             systypes t
  1896.         WHERE
  1897.             o.name like @table_name
  1898.             AND user_name(o.uid) like @table_owner
  1899.             AND o.id = c.id
  1900.             AND t.type = d.ss_dtype
  1901.             AND c.length = isnull(d.fixlen, c.length)
  1902.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1903.             AND o.type <> 'P'
  1904.             AND c.usertype = t.usertype
  1905.             AND c.name like @column_name
  1906.             AND c.cdefault *= m.id
  1907.             AND m.colid = 1
  1908.         ORDER BY 2, 3, 17
  1909.     end
  1910. go
  1911.  
  1912. if (charindex('6.00', @@version) = 0 and
  1913.     charindex('6.50', @@version) = 0 and
  1914.     charindex('7.00', @@version) = 0 and
  1915.     charindex('8.00', @@version) = 0)
  1916. begin
  1917.     print ''
  1918.     print ''
  1919.     print 'Warning:'
  1920.     print 'you are installing the stored procedures '
  1921.     print 'on a pre 6.0 SQL Server.'
  1922.     print 'Ignore the following error.'
  1923. end
  1924. else
  1925.     drop proc sp_columns
  1926. go
  1927.  
  1928. /*  Procedure for 6.0 and 6.50 server */
  1929. CREATE PROCEDURE sp_columns (
  1930.                  @table_name        varchar(96),
  1931.                  @table_owner       varchar(96) = null,
  1932.                  @table_qualifier   varchar(32) = null,
  1933.                  @column_name       varchar(96) = null,
  1934.                  @ODBCVer           int = 2)
  1935. AS
  1936.     DECLARE @full_table_name    varchar(193)
  1937.     DECLARE @table_id int
  1938.  
  1939.     if @ODBCVer <> 3
  1940.         select @ODBCVer = 2
  1941.     if @column_name is null /*  If column name not supplied, match all */
  1942.         select @column_name = '%'
  1943.     if @table_qualifier is not null
  1944.     begin
  1945.         if db_name() <> @table_qualifier
  1946.         begin   /* If qualifier doesn't match current database */
  1947.             raiserror (15250, -1,-1)
  1948.             return
  1949.         end
  1950.     end
  1951.     if @table_name is null
  1952.     begin
  1953.         /* If table name not supplied, match all */
  1954.         select @table_name = '%'
  1955.     end
  1956.     if @table_owner is null
  1957.     begin
  1958.         /* If unqualified table name */
  1959.         SELECT @full_table_name = @table_name
  1960.     end
  1961.     else
  1962.     begin
  1963.         /* Qualified table name */
  1964.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1965.     end
  1966.  
  1967.     /* Get Object ID */
  1968.     SELECT @table_id = object_id(@full_table_name)
  1969.     if ((charindex('%',@full_table_name) = 0) and
  1970.         (charindex('[',@full_table_name) = 0) and
  1971.         (charindex('_',@full_table_name) = 0) and
  1972.         @table_id <> 0)
  1973.     begin
  1974.         /* this block is for the case where there is no pattern
  1975.             matching required for the table name */
  1976.  
  1977.         SELECT
  1978.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1979.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1980.             TABLE_NAME = convert(varchar(32),o.name),
  1981.             COLUMN_NAME = convert(varchar(32),c.name),
  1982.             DATA_TYPE = d.DATA_TYPE,
  1983.             TYPE_NAME = convert(varchar(32),
  1984.                     case
  1985.                         when t.usertype > 100 or t.usertype in (18,80) then t.name
  1986.                         else d.TYPE_NAME
  1987.                     end),
  1988.             "PRECISION" = convert(int,
  1989.                     case
  1990.                         when d.DATA_TYPE in (6,7) then d.data_precision /* FLOAT/REAL */
  1991.                         else isnull(convert(int,c.prec), 2147483647)
  1992.                     end),
  1993.             LENGTH = convert(int,
  1994.                     case
  1995.                         when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  1996.                         c.prec+2
  1997.                         else
  1998.                             isnull(d.length, c.length)
  1999.                     end),
  2000.             SCALE = convert(smallint, c.scale),
  2001.             RADIX = d.RADIX,
  2002.             NULLABLE =  /* set nullability from status flag */
  2003.                 convert(smallint, convert(bit, c.status&8)),
  2004.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2005.             COLUMN_DEF = text,
  2006.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2007.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2008.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  2009.             ORDINAL_POSITION = convert(int,
  2010.                        (
  2011.                         select count(*)
  2012.                         from syscolumns sc
  2013.                         where sc.id     =  c.id
  2014.                           AND sc.number =  c.number
  2015.                           AND sc.colid  <= c.colid
  2016.                         )),
  2017.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2018.             SS_DATA_TYPE = c.type
  2019.         FROM
  2020.             syscolumns c,
  2021.             sysobjects o,
  2022.             syscomments m,
  2023.             master.dbo.spt_datatype_info d,
  2024.             systypes t
  2025.         WHERE
  2026.             o.id = @table_id
  2027.             AND c.id = o.id
  2028.             AND t.type = d.ss_dtype
  2029.             AND c.length = isnull(d.fixlen, c.length)
  2030.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2031.             AND o.type <> 'P'
  2032.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2033.             AND c.usertype = t.usertype
  2034.             AND c.name like @column_name
  2035.             AND c.cdefault *= m.id
  2036.             AND m.colid = 1
  2037.         ORDER BY 17
  2038.     end
  2039.     else
  2040.     begin
  2041.         /* this block is for the case where there IS pattern
  2042.             matching done on the table name */
  2043.  
  2044.         if @table_owner is null /*  If owner not supplied, match all */
  2045.             select @table_owner = '%'
  2046.  
  2047.         SELECT
  2048.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  2049.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  2050.             TABLE_NAME = convert(varchar(32),o.name),
  2051.             COLUMN_NAME = convert(varchar(32),c.name),
  2052.             DATA_TYPE = d.DATA_TYPE,
  2053.             TYPE_NAME = convert(varchar(32), 
  2054.                 case 
  2055.                     when t.usertype > 100 or t.usertype in (18,80) then t.name
  2056.                     else d.TYPE_NAME
  2057.                 end),
  2058.             "PRECISION" = convert(int,
  2059.                     case
  2060.                         when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2061.                         else isnull(convert(int,c.prec), 2147483647)
  2062.                     end),
  2063.             LENGTH = convert(int,
  2064.                     case
  2065.                         when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  2066.                             c.prec+2
  2067.                         else
  2068.                             isnull(d.length, c.length)
  2069.                     end),
  2070.             SCALE = convert(smallint, c.scale),
  2071.             RADIX = d.RADIX,
  2072.             NULLABLE =  /* set nullability from status flag */
  2073.                 convert(smallint, convert(bit, c.status&8)),
  2074.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2075.             COLUMN_DEF = text,
  2076.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2077.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2078.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647) + d.charbin,
  2079.             ORDINAL_POSITION = convert(int,
  2080.                        (
  2081.                         select count(*)
  2082.                         from syscolumns sc
  2083.                         where sc.id     =  c.id
  2084.                           AND sc.number =  c.number
  2085.                           AND sc.colid  <= c.colid
  2086.                         )),
  2087.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2088.             SS_DATA_TYPE = c.type
  2089.         FROM
  2090.             syscolumns c,
  2091.             sysobjects o,
  2092.             syscomments m,
  2093.             master.dbo.spt_datatype_info d,
  2094.             systypes t
  2095.         WHERE
  2096.             o.name like @table_name
  2097.             AND user_name(o.uid) like @table_owner
  2098.             AND o.id = c.id
  2099.             AND t.type = d.ss_dtype
  2100.             AND c.length = isnull(d.fixlen, c.length)
  2101.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2102.             AND o.type <> 'P'
  2103.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2104.             AND c.usertype = t.usertype
  2105.             AND c.name like @column_name
  2106.             AND c.cdefault *= m.id
  2107.             AND m.colid = 1
  2108.         ORDER BY 2, 3, 17
  2109.     end
  2110. go
  2111.  
  2112. if (charindex('7.00', @@version) = 0 and
  2113.     charindex('8.00', @@version) = 0)
  2114. begin
  2115.     print ''
  2116.     print ''
  2117.     print 'Warning:'
  2118.     print 'you are installing the stored procedures '
  2119.     print 'on a pre 7.0 SQL Server.'
  2120.     print 'Ignore the following errors.'
  2121. end
  2122. else
  2123.     drop proc sp_columns
  2124. go
  2125.  
  2126. /*  Procedure for 7.0 server */
  2127. CREATE PROCEDURE sp_columns (
  2128.                  @table_name        nvarchar(384),
  2129.                  @table_owner       nvarchar(384) = null,
  2130.                  @table_qualifier   sysname = null,
  2131.                  @column_name       nvarchar(384) = null,
  2132.                  @ODBCVer           int = 2)
  2133. AS
  2134.     DECLARE @full_table_name    nvarchar(769)
  2135.     DECLARE @table_id int
  2136.  
  2137.     if @ODBCVer <> 3
  2138.         select @ODBCVer = 2
  2139.     if @column_name is null /*  If column name not supplied, match all */
  2140.         select @column_name = '%'
  2141.     if @table_qualifier is not null
  2142.     begin
  2143.         if db_name() <> @table_qualifier
  2144.         begin   /* If qualifier doesn't match current database */
  2145.             raiserror (15250, -1,-1)
  2146.             return
  2147.         end
  2148.     end
  2149.     if @table_name is null
  2150.     begin   /*  If table name not supplied, match all */
  2151.         select @table_name = '%'
  2152.     end
  2153.     if @table_owner is null
  2154.     begin   /* If unqualified table name */
  2155.         SELECT @full_table_name = quotename(@table_name)
  2156.     end
  2157.     else
  2158.     begin   /* Qualified table name */
  2159.         if @table_owner = ''
  2160.         begin   /* If empty owner name */
  2161.             SELECT @full_table_name = quotename(@table_owner)
  2162.         end
  2163.         else
  2164.         begin
  2165.             SELECT @full_table_name = quotename(@table_owner) +
  2166.                 '.' + quotename(@table_name)
  2167.         end
  2168.     end
  2169.  
  2170.     /*  Get Object ID */
  2171.     SELECT @table_id = object_id(@full_table_name)
  2172.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2173.         (isnull(charindex('[', @table_name),0) = 0) and
  2174.         (isnull(charindex('[', @table_owner),0) = 0) and
  2175.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2176.         @table_id <> 0)
  2177.     begin
  2178.         /* this block is for the case where there is no pattern
  2179.             matching required for the table name */
  2180.         
  2181.         SELECT
  2182.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2183.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2184.             TABLE_NAME = convert(sysname,o.name),
  2185.             COLUMN_NAME = convert(sysname,c.name),
  2186.             d.DATA_TYPE,
  2187.             convert (sysname,case
  2188.                 when t.xusertype > 255 then t.name
  2189.                 else d.TYPE_NAME
  2190.             end) TYPE_NAME,
  2191.             convert(int,case
  2192.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2193.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2194.             end) "PRECISION",
  2195.             convert(int,case
  2196.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2197.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2198.                 else
  2199.                     isnull(d.length, c.length)
  2200.             end) LENGTH,
  2201.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2202.             d.RADIX,
  2203.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2204.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2205.             COLUMN_DEF = text,
  2206.             d.SQL_DATA_TYPE,
  2207.             d.SQL_DATETIME_SUB,
  2208.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2209.             ORDINAL_POSITION = convert(int,
  2210.                        (
  2211.                         select count(*)
  2212.                         from syscolumns sc
  2213.                         where sc.id     =  c.id
  2214.                           AND sc.number =  c.number
  2215.                           AND sc.colid  <= c.colid
  2216.                         )),
  2217.             IS_NULLABLE = convert(varchar(254),
  2218.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2219.             SS_DATA_TYPE = c.type
  2220.         FROM
  2221.             sysobjects o,
  2222.             master.dbo.spt_datatype_info d,
  2223.             systypes t,
  2224.             syscolumns c
  2225.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2226.                 AND m.colid = 1
  2227.         WHERE
  2228.             o.id = @table_id
  2229.             AND c.id = o.id
  2230.             AND t.xtype = d.ss_dtype
  2231.             AND c.length = isnull(d.fixlen, c.length)
  2232.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2233.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2234.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2235.             AND c.xusertype = t.xusertype
  2236.             AND c.name like @column_name
  2237.         ORDER BY 17
  2238.     end
  2239.     else
  2240.     begin
  2241.         /* this block is for the case where there IS pattern
  2242.             matching done on the table name */
  2243.  
  2244.         if @table_owner is null /*  If owner not supplied, match all */
  2245.             select @table_owner = '%'
  2246.  
  2247.         SELECT
  2248.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2249.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2250.             TABLE_NAME = convert(sysname,o.name),
  2251.             COLUMN_NAME = convert(sysname,c.name),
  2252.             d.DATA_TYPE,
  2253.             convert (sysname,case
  2254.                 when t.xusertype > 255 then t.name
  2255.                 else d.TYPE_NAME
  2256.             end) TYPE_NAME,
  2257.             convert(int,case
  2258.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2259.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2260.             end) "PRECISION",
  2261.             convert(int,case
  2262.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2263.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2264.                 else
  2265.                     isnull(d.length, c.length)
  2266.             end) LENGTH,
  2267.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2268.             d.RADIX,
  2269.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2270.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2271.             COLUMN_DEF = text,
  2272.             d.SQL_DATA_TYPE,
  2273.             d.SQL_DATETIME_SUB,
  2274.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2275.             ORDINAL_POSITION = convert(int,
  2276.                        (
  2277.                         select count(*)
  2278.                         from syscolumns sc
  2279.                         where sc.id     =  c.id
  2280.                           AND sc.number =  c.number
  2281.                           AND sc.colid  <= c.colid
  2282.                         )),
  2283.             IS_NULLABLE = convert(varchar(254),
  2284.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2285.             SS_DATA_TYPE = c.type
  2286.         FROM
  2287.             sysobjects o,
  2288.             master.dbo.spt_datatype_info d,
  2289.             systypes t,
  2290.             syscolumns c
  2291.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2292.                 AND m.colid = 1
  2293.         WHERE
  2294.             o.name like @table_name
  2295.             AND user_name(o.uid) like @table_owner
  2296.             AND o.id = c.id
  2297.             AND t.xtype = d.ss_dtype
  2298.             AND c.length = isnull(d.fixlen, c.length)
  2299.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2300.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2301.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2302.             AND c.xusertype = t.xusertype
  2303.             AND c.name like @column_name
  2304.         ORDER BY 2, 3, 17
  2305.     end
  2306. go
  2307.  
  2308. if (charindex('8.00', @@version) = 0)
  2309. begin
  2310.     print ''
  2311.     print ''
  2312.     print 'Warning:'
  2313.     print 'you are installing the stored procedures '
  2314.     print 'on a pre 8.0 SQL Server.'
  2315.     print 'Ignore the following errors.'
  2316. end
  2317. else
  2318.     drop proc sp_columns
  2319. go
  2320.  
  2321. /*  Procedure for 8.0 server */
  2322. CREATE PROCEDURE sp_columns (
  2323.                  @table_name        nvarchar(384),
  2324.                  @table_owner       nvarchar(384) = null,
  2325.                  @table_qualifier   sysname = null,
  2326.                  @column_name       nvarchar(384) = null,
  2327.                  @ODBCVer           int = 2)
  2328. AS
  2329.     DECLARE @full_table_name    nvarchar(769)
  2330.     DECLARE @table_id int
  2331.  
  2332.     if @ODBCVer <> 3
  2333.         select @ODBCVer = 2
  2334.     if @column_name is null /*  If column name not supplied, match all */
  2335.         select @column_name = '%'
  2336.     if @table_qualifier is not null
  2337.     begin
  2338.         if db_name() <> @table_qualifier
  2339.         begin   /* If qualifier doesn't match current database */
  2340.             raiserror (15250, -1,-1)
  2341.             return
  2342.         end
  2343.     end
  2344.     if @table_name is null
  2345.     begin   /*  If table name not supplied, match all */
  2346.         select @table_name = '%'
  2347.     end
  2348.     if @table_owner is null
  2349.     begin   /* If unqualified table name */
  2350.         SELECT @full_table_name = quotename(@table_name)
  2351.     end
  2352.     else
  2353.     begin   /* Qualified table name */
  2354.         if @table_owner = ''
  2355.         begin   /* If empty owner name */
  2356.             SELECT @full_table_name = quotename(@table_owner)
  2357.         end
  2358.         else
  2359.         begin
  2360.             SELECT @full_table_name = quotename(@table_owner) +
  2361.                 '.' + quotename(@table_name)
  2362.         end
  2363.     end
  2364.  
  2365.     /*  Get Object ID */
  2366.     SELECT @table_id = object_id(@full_table_name)
  2367.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2368.         (isnull(charindex('[', @table_name),0) = 0) and
  2369.         (isnull(charindex('[', @table_owner),0) = 0) and
  2370.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2371.         @table_id <> 0)
  2372.     begin
  2373.         /* this block is for the case where there is no pattern
  2374.             matching required for the table name */
  2375.         
  2376.         SELECT
  2377.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2378.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2379.             TABLE_NAME = convert(sysname,o.name),
  2380.             COLUMN_NAME = convert(sysname,c.name),
  2381.             d.DATA_TYPE,
  2382.             convert (sysname,case
  2383.                 when t.xusertype > 255 then t.name
  2384.                 else d.TYPE_NAME collate database_default
  2385.             end) TYPE_NAME,
  2386.             convert(int,case
  2387.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2388.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2389.             end) "PRECISION",
  2390.             convert(int,case
  2391.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2392.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2393.                 else
  2394.                     isnull(d.length, c.length)
  2395.             end) LENGTH,
  2396.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2397.             d.RADIX,
  2398.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2399.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2400.             COLUMN_DEF = text,
  2401.             d.SQL_DATA_TYPE,
  2402.             d.SQL_DATETIME_SUB,
  2403.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2404.             ORDINAL_POSITION = convert(int,
  2405.                        (
  2406.                         select count(*)
  2407.                         from syscolumns sc
  2408.                         where sc.id     =  c.id
  2409.                           AND sc.number =  c.number
  2410.                           AND sc.colid  <= c.colid
  2411.                         )),
  2412.             IS_NULLABLE = convert(varchar(254),
  2413.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2414.             SS_DATA_TYPE = c.type
  2415.         FROM
  2416.             sysobjects o,
  2417.             master.dbo.spt_datatype_info d,
  2418.             systypes t,
  2419.             syscolumns c
  2420.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2421.                 AND m.colid = 1
  2422.         WHERE
  2423.             o.id = @table_id
  2424.             AND c.id = o.id
  2425.             AND t.xtype = d.ss_dtype
  2426.             AND c.length = isnull(d.fixlen, c.length)
  2427.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2428.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2429.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2430.             AND c.xusertype = t.xusertype
  2431.             AND c.name like @column_name
  2432.         ORDER BY 17
  2433.     end
  2434.     else
  2435.     begin
  2436.         /* this block is for the case where there IS pattern
  2437.             matching done on the table name */
  2438.  
  2439.         if @table_owner is null /*  If owner not supplied, match all */
  2440.             select @table_owner = '%'
  2441.  
  2442.         SELECT
  2443.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2444.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2445.             TABLE_NAME = convert(sysname,o.name),
  2446.             COLUMN_NAME = convert(sysname,c.name),
  2447.             d.DATA_TYPE,
  2448.             convert (sysname,case
  2449.                 when t.xusertype > 255 then t.name
  2450.                 else d.TYPE_NAME collate database_default
  2451.             end) TYPE_NAME,
  2452.             convert(int,case
  2453.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2454.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2455.             end) "PRECISION",
  2456.             convert(int,case
  2457.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2458.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2459.                 else
  2460.                     isnull(d.length, c.length)
  2461.             end) LENGTH,
  2462.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2463.             d.RADIX,
  2464.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2465.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  2466.             COLUMN_DEF = text,
  2467.             d.SQL_DATA_TYPE,
  2468.             d.SQL_DATETIME_SUB,
  2469.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2470.             ORDINAL_POSITION = convert(int,
  2471.                        (
  2472.                         select count(*)
  2473.                         from syscolumns sc
  2474.                         where sc.id     =  c.id
  2475.                           AND sc.number =  c.number
  2476.                           AND sc.colid  <= c.colid
  2477.                         )),
  2478.             IS_NULLABLE = convert(varchar(254),
  2479.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2480.             SS_DATA_TYPE = c.type
  2481.         FROM
  2482.             sysobjects o,
  2483.             master.dbo.spt_datatype_info d,
  2484.             systypes t,
  2485.             syscolumns c
  2486.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2487.                 AND m.colid = 1
  2488.         WHERE
  2489.             o.name like @table_name
  2490.             AND user_name(o.uid) like @table_owner
  2491.             AND o.id = c.id
  2492.             AND t.xtype = d.ss_dtype
  2493.             AND c.length = isnull(d.fixlen, c.length)
  2494.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2495.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2496.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2497.             AND c.xusertype = t.xusertype
  2498.             AND c.name like @column_name
  2499.         ORDER BY 2, 3, 17
  2500.     end
  2501. go
  2502.  
  2503. grant execute on sp_columns to public
  2504. go
  2505.  
  2506. dump tran master with no_log
  2507. go
  2508.  
  2509. print 'creating sp_databases'
  2510. go
  2511.  
  2512. /*  Procedure for pre-7.0 server */
  2513. create proc sp_databases
  2514. as
  2515.     set nocount on
  2516.     /* Use temporary table to sum up database size w/o using group by */
  2517.     create table #databases (
  2518.                   DATABASE_NAME varchar(32) NOT NULL,
  2519.                   size int NOT NULL)
  2520.  
  2521.     /* Insert row for each database */
  2522.     insert into #databases
  2523.         select
  2524.             name,
  2525.             (select sum(size) from master.dbo.sysusages
  2526.                 where dbid = d.dbid)
  2527.         from master.dbo.sysdatabases d
  2528.  
  2529.     select
  2530.          DATABASE_NAME,
  2531.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2532.          REMARKS = convert(varchar(254),null)   /* Remarks are NULL */
  2533.     from #databases
  2534.     order by 1
  2535. go
  2536.  
  2537. if (charindex('7.00', @@version) = 0 and
  2538.     charindex('8.00', @@version) = 0)
  2539. begin
  2540.     print ''
  2541.     print ''
  2542.     print 'Warning:'
  2543.     print 'you are installing the stored procedures '
  2544.     print 'on a pre 8.0 SQL Server.'
  2545.     print 'Ignore the following errors.'
  2546. end
  2547. else
  2548.     drop proc sp_databases
  2549. go
  2550.  
  2551. /*  Procedure for 8.0 server */
  2552. create proc sp_databases
  2553. as
  2554.     set nocount on
  2555.     declare @name sysname
  2556.     declare @SQL  nvarchar(600)
  2557.  
  2558.     /* Use temporary table to sum up database size w/o using group by */
  2559.     create table #databases (
  2560.                   DATABASE_NAME sysname NOT NULL,
  2561.                   size int NOT NULL)
  2562.  
  2563.     declare c1 cursor for 
  2564.         select name from master.dbo.sysdatabases
  2565.             where has_dbaccess(name) = 1 -- Only look at databases to which we have access
  2566.  
  2567.     open c1
  2568.     fetch c1 into @name
  2569.  
  2570.     while @@fetch_status >= 0
  2571.     begin
  2572.         select @SQL = 'insert into #databases
  2573.                 select N'''+ @name + ''', sum(size) from '
  2574.                 + QuoteName(@name) + '.dbo.sysfiles'
  2575.         /* Insert row for each database */
  2576.         execute (@SQL)
  2577.         fetch c1 into @name
  2578.     end
  2579.     deallocate c1
  2580.  
  2581.     select  
  2582.         DATABASE_NAME,
  2583.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2584.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2585.     from #databases
  2586.     order by 1
  2587. go
  2588.  
  2589. grant execute on sp_databases to public
  2590. go
  2591.  
  2592. dump tran master with no_log
  2593. go
  2594.  
  2595. print 'creating sp_datatype_info'
  2596. go
  2597.  
  2598. /*  Procedure for pre-6.0 server */
  2599. create proc sp_datatype_info
  2600.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2601. as
  2602.     if @ODBCVer <> 3
  2603.         select @ODBCVer = 2
  2604.     if @data_type = 0
  2605.         select
  2606.             TYPE_NAME = t.name,
  2607.             d.DATA_TYPE,
  2608.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2609.             d.LITERAL_PREFIX,
  2610.             d.LITERAL_SUFFIX,
  2611.             e.CREATE_PARAMS,
  2612.             d.NULLABLE,
  2613.             d.CASE_SENSITIVE,
  2614.             d.SEARCHABLE,
  2615.             d.UNSIGNED_ATTRIBUTE,
  2616.             d.MONEY,
  2617.             d.AUTO_INCREMENT,
  2618.             LOCAL_TYPE_NAME = t.name,
  2619.             MINIMUM_SCALE = d.numeric_scale,
  2620.             MAXIMUM_SCALE = d.numeric_scale,
  2621.             d.SQL_DATA_TYPE,
  2622.             d.SQL_DATETIME_SUB,
  2623.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2624.             INTERVAL_PRECISION = convert(smallint,NULL),
  2625.             USERTYPE = t.usertype
  2626.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2627.         where
  2628.             d.ss_dtype = t.type
  2629.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2630.             and t.usertype *= e.user_type
  2631.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2632.                 t.usertype > 100)
  2633.         order by 2, 12, 11, t.usertype
  2634.  
  2635.     else
  2636.         select
  2637.             TYPE_NAME = t.name,
  2638.             d.DATA_TYPE,
  2639.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2640.             d.LITERAL_PREFIX,
  2641.             d.LITERAL_SUFFIX,
  2642.             e.CREATE_PARAMS,
  2643.             d.NULLABLE,
  2644.             d.CASE_SENSITIVE,
  2645.             d.SEARCHABLE,
  2646.             d.UNSIGNED_ATTRIBUTE,
  2647.             d.MONEY,
  2648.             d.AUTO_INCREMENT,
  2649.             LOCAL_TYPE_NAME = t.name,
  2650.             MINIMUM_SCALE = d.numeric_scale,
  2651.             MAXIMUM_SCALE = d.numeric_scale,
  2652.             d.SQL_DATA_TYPE,
  2653.             d.SQL_DATETIME_SUB,
  2654.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2655.             INTERVAL_PRECISION = convert(smallint,NULL),
  2656.             USERTYPE = t.usertype
  2657.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2658.         where
  2659.             DATA_TYPE = @data_type
  2660.             and d.ss_dtype = t.type
  2661.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2662.             and t.usertype *= e.user_type
  2663.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2664.                 t.usertype > 100)
  2665.         order by 12, 11, t.usertype
  2666.  
  2667. go
  2668.  
  2669. if (charindex('6.00', @@version) = 0 and
  2670.     charindex('6.50', @@version) = 0 and
  2671.     charindex('7.00', @@version) = 0 and
  2672.     charindex('8.00', @@version) = 0)
  2673. begin
  2674.     print ''
  2675.     print ''
  2676.     print 'Warning:'
  2677.     print 'you are installing the stored procedures '
  2678.     print 'on a pre 6.0 SQL Server.'
  2679.     print 'Ignore the following errors.'
  2680. end
  2681. else
  2682.     drop proc sp_datatype_info
  2683. go
  2684.  
  2685. /*  Procedure for 6.0 and 6.50 servers */
  2686. create proc sp_datatype_info
  2687.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2688. as
  2689.     if @ODBCVer <> 3
  2690.         select @ODBCVer = 2
  2691.     if @data_type = 0
  2692.         select
  2693.             convert(varchar(32),case
  2694.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2695.                 else d.TYPE_NAME
  2696.             end) TYPE_NAME,
  2697.             d.DATA_TYPE,
  2698.             convert(int,case
  2699.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2700.                 when d.ss_dtype in (35,34) then 2147483647              /* TEXT/IMAGE */
  2701.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2702.                 else t.prec
  2703.             end) "PRECISION",
  2704.             d.LITERAL_PREFIX,
  2705.             d.LITERAL_SUFFIX,
  2706.             e.CREATE_PARAMS,
  2707.             convert(smallint,case
  2708.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2709.                 else t.allownulls
  2710.             end) NULLABLE,
  2711.             d.CASE_SENSITIVE,
  2712.             d.SEARCHABLE,
  2713.             d.UNSIGNED_ATTRIBUTE,
  2714.             d.MONEY,
  2715.             d.AUTO_INCREMENT,
  2716.             convert(varchar(32),case
  2717.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2718.                 else d.TYPE_NAME
  2719.             end) LOCAL_TYPE_NAME,
  2720.             convert(smallint,case
  2721.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2722.                 else d.numeric_scale
  2723.             end) MINIMUM_SCALE,
  2724.             convert(smallint,case
  2725.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2726.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2727.                 else t.scale
  2728.             end) MAXIMUM_SCALE,
  2729.             d.SQL_DATA_TYPE,
  2730.             d.SQL_DATETIME_SUB,
  2731.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2732.             INTERVAL_PRECISION = convert(smallint,NULL),
  2733.             USERTYPE = t.usertype
  2734.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2735.         where
  2736.             d.ss_dtype = t.type
  2737.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2738.             and t.usertype *= e.user_type
  2739.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2740.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2741.                 t.usertype > 100)
  2742.             and (t.usertype <= 100 or
  2743.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2744.         order by 2, 12, 11, t.usertype
  2745.  
  2746.     else
  2747.         select
  2748.             convert(varchar(32),case
  2749.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2750.                 else d.TYPE_NAME
  2751.             end) TYPE_NAME,
  2752.             d.DATA_TYPE,
  2753.             convert(int,case
  2754.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2755.                 when d.ss_dtype in (35,34) then 2147483647              /* TEXT/IMAGE */
  2756.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2757.                 else t.prec
  2758.             end) "PRECISION",
  2759.             d.LITERAL_PREFIX,
  2760.             d.LITERAL_SUFFIX,
  2761.             e.CREATE_PARAMS,
  2762.             convert(smallint,case
  2763.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2764.                 else t.allownulls
  2765.             end) NULLABLE,
  2766.             d.CASE_SENSITIVE,
  2767.             d.SEARCHABLE,
  2768.             d.UNSIGNED_ATTRIBUTE,
  2769.             d.MONEY,
  2770.             d.AUTO_INCREMENT,
  2771.             convert(varchar(32),case
  2772.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2773.                 else d.TYPE_NAME
  2774.             end) LOCAL_TYPE_NAME,
  2775.             convert(smallint,case
  2776.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2777.                 else d.numeric_scale
  2778.             end) MINIMUM_SCALE,
  2779.             convert(smallint,case
  2780.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2781.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2782.                 else t.scale
  2783.             end) MAXIMUM_SCALE,
  2784.             d.SQL_DATA_TYPE,
  2785.             d.SQL_DATETIME_SUB,
  2786.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2787.             INTERVAL_PRECISION = convert(smallint,NULL),
  2788.             USERTYPE = t.usertype
  2789.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2790.         where
  2791.             d.DATA_TYPE = @data_type
  2792.             and d.ss_dtype = t.type
  2793.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2794.             and t.usertype *= e.user_type
  2795.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2796.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2797.                 t.usertype > 100)
  2798.             and (t.usertype <= 100 or
  2799.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2800.         order by 12, 11, t.usertype
  2801. go
  2802.  
  2803. if (charindex('7.00', @@version) = 0 and
  2804.     charindex('8.00', @@version) = 0)
  2805. begin
  2806.     print ''
  2807.     print ''
  2808.     print 'Warning:'
  2809.     print 'you are installing the stored procedures '
  2810.     print 'on a pre 7.0 SQL Server.'
  2811.     print 'Ignore the following errors.'
  2812. end
  2813. else
  2814.     drop proc sp_datatype_info
  2815. go
  2816.  
  2817. /*  Procedure for 7.0 server */
  2818. create proc sp_datatype_info
  2819.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2820. as
  2821.     declare @mintype int
  2822.     declare @maxtype int
  2823.  
  2824.     if @ODBCVer <> 3
  2825.         select @ODBCVer = 2
  2826.     if @data_type = 0
  2827.     begin
  2828.         select @mintype = -32768
  2829.         select @maxtype = 32767
  2830.     end
  2831.     else
  2832.     begin
  2833.         select @mintype = @data_type
  2834.         select @maxtype = @data_type
  2835.     end
  2836.  
  2837.     select
  2838.         convert(sysname,case
  2839.             when t.xusertype > 255 then t.name
  2840.             else d.TYPE_NAME
  2841.         end) TYPE_NAME,
  2842.         d.DATA_TYPE,
  2843.         convert(int,case
  2844.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2845.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2846.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2847.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2848.         end) "PRECISION",
  2849.         d.LITERAL_PREFIX,
  2850.         d.LITERAL_SUFFIX,
  2851.         e.CREATE_PARAMS,
  2852.         convert(smallint,case
  2853.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2854.             else TypeProperty (t.name, 'AllowsNull')
  2855.         end) NULLABLE,
  2856.         d.CASE_SENSITIVE,
  2857.         d.SEARCHABLE,
  2858.         d.UNSIGNED_ATTRIBUTE,
  2859.         d.MONEY,
  2860.         d.AUTO_INCREMENT,
  2861.         convert(sysname,case
  2862.             when t.xusertype > 255 then t.name
  2863.             else d.LOCAL_TYPE_NAME
  2864.         end) LOCAL_TYPE_NAME,
  2865.         MINIMUM_SCALE = d.numeric_scale,
  2866.         convert(smallint,case
  2867.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2868.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2869.             else TypeProperty (t.name, 'Scale')
  2870.         end) MAXIMUM_SCALE,
  2871.         d.SQL_DATA_TYPE,
  2872.         d.SQL_DATETIME_SUB,
  2873.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2874.         INTERVAL_PRECISION = convert(smallint,NULL),
  2875.         USERTYPE = t.usertype
  2876.     from master.dbo.spt_datatype_info d
  2877.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2878.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2879.             t.xusertype = e.user_type
  2880.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2881.     where
  2882.         d.DATA_TYPE between @mintype and @maxtype
  2883.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2884.         and (t.xusertype <= 255 or
  2885.             isnull(d.AUTO_INCREMENT,0) = 0)
  2886.     order by 2, 12, 11,
  2887.     case
  2888.         when t.usertype=18 then 255
  2889.         else t.usertype
  2890.     end
  2891.  
  2892. go
  2893.  
  2894. if (charindex('8.00', @@version) = 0)
  2895. begin
  2896.     print ''
  2897.     print ''
  2898.     print 'Warning:'
  2899.     print 'you are installing the stored procedures '
  2900.     print 'on a pre 8.0 SQL Server.'
  2901.     print 'Ignore the following errors.'
  2902. end
  2903. else
  2904.     drop proc sp_datatype_info
  2905. go
  2906.  
  2907. /*  Procedure for 8.0 server */
  2908. create proc sp_datatype_info
  2909.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2910. as
  2911.     declare @mintype int
  2912.     declare @maxtype int
  2913.  
  2914.     if @ODBCVer <> 3
  2915.         select @ODBCVer = 2
  2916.     if @data_type = 0
  2917.     begin
  2918.         select @mintype = -32768
  2919.         select @maxtype = 32767
  2920.     end
  2921.     else
  2922.     begin
  2923.         select @mintype = @data_type
  2924.         select @maxtype = @data_type
  2925.     end
  2926.  
  2927.     select
  2928.         convert(sysname,case
  2929.             when t.xusertype > 255 then t.name
  2930.             else d.TYPE_NAME collate database_default
  2931.         end) TYPE_NAME,
  2932.         d.DATA_TYPE,
  2933.         convert(int,case
  2934.             when d.DATA_TYPE in (6,7,-150) then d.data_precision        /* FLOAT/REAL/sql_variant*/
  2935.             when d.ss_dtype in (55,63,106,108) and
  2936.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2937.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2938.         end) "PRECISION",
  2939.         d.LITERAL_PREFIX,
  2940.         d.LITERAL_SUFFIX,
  2941.         e.CREATE_PARAMS,
  2942.         convert(smallint,case
  2943.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2944.             else TypeProperty (t.name, 'AllowsNull')
  2945.         end) NULLABLE,
  2946.         d.CASE_SENSITIVE,
  2947.         d.SEARCHABLE,
  2948.         d.UNSIGNED_ATTRIBUTE,
  2949.         d.MONEY,
  2950.         d.AUTO_INCREMENT,
  2951.         convert(sysname,case
  2952.             when t.xusertype > 255 then t.name
  2953.             else d.LOCAL_TYPE_NAME collate database_default
  2954.         end) LOCAL_TYPE_NAME,
  2955.         convert(smallint,case
  2956.             when d.ss_dtype in (55,63,106,108) and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2957.             else d.numeric_scale
  2958.         end) MINIMUM_SCALE,
  2959.         convert(smallint,case
  2960.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2961.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2962.             else TypeProperty (t.name, 'Scale')
  2963.         end) MAXIMUM_SCALE,
  2964.         d.SQL_DATA_TYPE,
  2965.         d.SQL_DATETIME_SUB,
  2966.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2967.         INTERVAL_PRECISION = convert(smallint,NULL),
  2968.         USERTYPE = t.usertype
  2969.     from master.dbo.spt_datatype_info d
  2970.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2971.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2972.             t.xusertype = e.user_type
  2973.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2974.     where
  2975.         d.DATA_TYPE between @mintype and @maxtype
  2976.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2977.         and (t.xusertype <= 255 or
  2978.             isnull(d.AUTO_INCREMENT,0) = 0)
  2979.     order by 2, 12, 11,
  2980.     case
  2981.         when t.usertype=18 then 255
  2982.         else t.usertype
  2983.     end
  2984.  
  2985. go
  2986.  
  2987. grant execute on sp_datatype_info to public
  2988. go
  2989.  
  2990. dump tran master with no_log
  2991. go
  2992.  
  2993. print 'creating sp_fkeys'
  2994. go
  2995.  
  2996. /*  Procedure for pre-6.0 server */
  2997. CREATE PROCEDURE sp_fkeys(
  2998.                @pktable_name        varchar(32) = null,
  2999.                @pktable_owner       varchar(32) = null,
  3000.                @pktable_qualifier   varchar(32) = null,
  3001.                @fktable_name        varchar(32) = null,
  3002.                @fktable_owner       varchar(32) = null,
  3003.                @fktable_qualifier   varchar(32) = null )
  3004. as
  3005.     set nocount on
  3006.     declare @order_by_pk int
  3007.  
  3008.     select  @order_by_pk = 0
  3009.  
  3010.     if (@pktable_name is null) and (@fktable_name is null)
  3011.     begin   /* If neither primary key nor foreign key table names given */
  3012.         raiserror 20004 '~~Rush_25~~'
  3013.         return
  3014.     end
  3015.     if @fktable_qualifier is not null
  3016.     begin
  3017.         if db_name() <> @fktable_qualifier
  3018.         begin   /* If qualifier doesn't match current database */
  3019.             raiserror 20001 '~~Rush_26~~'
  3020.             return
  3021.         end
  3022.     end
  3023.     if @pktable_qualifier is not null
  3024.     begin
  3025.         if db_name() <> @pktable_qualifier
  3026.         begin   /* If qualifier doesn't match current database */
  3027.             raiserror 20001 '~~Rush_27~~'
  3028.             return
  3029.         end
  3030.     end
  3031.  
  3032.     if @pktable_name is null
  3033.     begin /*  If table name not supplied, match all */
  3034.         select @pktable_name = '%'
  3035.         select @order_by_pk = 1
  3036.     end
  3037.     if @pktable_owner is null   /*  If PK owner not supplied, match all */
  3038.         select @pktable_owner = '%'
  3039.     if @fktable_name is null    /*  If table name not supplied, match all */
  3040.         select @fktable_name = '%'
  3041.     if @fktable_owner is null   /*  If FK owner not supplied, match all */
  3042.         select @fktable_owner = '%'
  3043.  
  3044.     if @@trancount <> 0
  3045.     begin   /* If inside a transaction */
  3046.         raiserror 20003 '~~Rush_28~~'
  3047.         return
  3048.     end
  3049.     create table #fkeys(
  3050.              PKTABLE_QUALIFIER  varchar(32) NULL,
  3051.              PKTABLE_OWNER      varchar(32) NULL,
  3052.              PKTABLE_NAME       varchar(32) NOT NULL,
  3053.              PKCOLUMN_NAME      varchar(32) NOT NULL,
  3054.              FKTABLE_QUALIFIER  varchar(32) NULL,
  3055.              FKTABLE_OWNER      varchar(32) NULL,
  3056.              FKTABLE_NAME       varchar(32) NOT NULL,
  3057.              FKCOLUMN_NAME      varchar(32) NOT NULL,
  3058.              KEY_SEQ            smallint NOT NULL)
  3059.  
  3060.     /*  SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3061.     /*  Process syskeys for each relationship */
  3062.     /*  The inserts below adds a row to the temp table for each of the
  3063.         8 possible relationships */
  3064.     insert into #fkeys
  3065.         select
  3066.             db_name(),
  3067.             user_name(o1.uid),
  3068.             object_name(k.depid),
  3069.             c2.name,
  3070.             db_name(),
  3071.             user_name(o2.uid),
  3072.             object_name(k.id),
  3073.             c1.name,
  3074.             1
  3075.         from
  3076.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3077.         where
  3078.             c1.id = k.id
  3079.             and k.type = 2  /* Foreign type key */
  3080.             and c1.colid = k.key1
  3081.             and c2.id = k.depid
  3082.             and c2.colid = k.depkey1
  3083.             and o1.id = k.depid
  3084.             and o2.id = k.id
  3085.     union all
  3086.         select
  3087.             db_name(),
  3088.             user_name(o1.uid),
  3089.             object_name(k.depid),
  3090.             c2.name,
  3091.             db_name(),
  3092.             user_name(o2.uid),
  3093.             object_name(k.id),
  3094.             c1.name,
  3095.             2
  3096.         from
  3097.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3098.         where
  3099.             c1.id = k.id
  3100.             and k.type = 2  /* Foreign type key */
  3101.             and c1.colid = k.key2
  3102.             and c2.id = k.depid
  3103.             and c2.colid = k.depkey2
  3104.             and o1.id = k.depid
  3105.             and o2.id = k.id
  3106.     union all
  3107.         select
  3108.             db_name(),
  3109.             user_name(o1.uid),
  3110.             object_name(k.depid),
  3111.             c2.name,
  3112.             db_name(),
  3113.             user_name(o2.uid),
  3114.             object_name(k.id),
  3115.             c1.name,
  3116.             3
  3117.         from
  3118.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3119.         where
  3120.             c1.id = k.id
  3121.             and k.type = 2  /* Foreign type key */
  3122.             and c1.colid = k.key3
  3123.             and c2.id = k.depid
  3124.             and c2.colid = k.depkey3
  3125.             and o1.id = k.depid
  3126.             and o2.id = k.id
  3127.     union all
  3128.         select
  3129.             db_name(),
  3130.             user_name(o1.uid),
  3131.             object_name(k.depid),
  3132.             c2.name,
  3133.             db_name(),
  3134.             user_name(o2.uid),
  3135.             object_name(k.id),
  3136.             c1.name,
  3137.             4
  3138.         from
  3139.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3140.         where
  3141.             c1.id = k.id
  3142.             and k.type = 2  /* Foreign type key */
  3143.             and c1.colid = k.key4
  3144.             and c2.id = k.depid
  3145.             and c2.colid = k.depkey4
  3146.             and o1.id = k.depid
  3147.             and o2.id = k.id
  3148.     union all
  3149.         select
  3150.             db_name(),
  3151.             user_name(o1.uid),
  3152.             object_name(k.depid),
  3153.             c2.name,
  3154.             db_name(),
  3155.             user_name(o2.uid),
  3156.             object_name(k.id),
  3157.             c1.name,
  3158.             5
  3159.         from
  3160.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3161.         where
  3162.             c1.id = k.id
  3163.             and k.type = 2  /* Foreign type key */
  3164.             and c1.colid = k.key5
  3165.             and c2.id = k.depid
  3166.             and c2.colid = k.depkey5
  3167.             and o1.id = k.depid
  3168.             and o2.id = k.id
  3169.     union all
  3170.         select
  3171.             db_name(),
  3172.             user_name(o1.uid),
  3173.             object_name(k.depid),
  3174.             c2.name,
  3175.             db_name(),
  3176.             user_name(o2.uid),
  3177.             object_name(k.id),
  3178.             c1.name,
  3179.             6
  3180.         from
  3181.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3182.         where
  3183.             c1.id = k.id
  3184.             and k.type = 2  /* Foreign type key */
  3185.             and c1.colid = k.key6
  3186.             and c2.id = k.depid
  3187.             and c2.colid = k.depkey6
  3188.             and o1.id = k.depid
  3189.             and o2.id = k.id
  3190.     union all
  3191.         select
  3192.             db_name(),
  3193.             user_name(o1.uid),
  3194.             object_name(k.depid),
  3195.             c2.name,
  3196.             db_name(),
  3197.             user_name(o2.uid),
  3198.             object_name(k.id),
  3199.             c1.name,
  3200.             7
  3201.         from
  3202.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3203.         where
  3204.             c1.id = k.id
  3205.             and k.type = 2  /* Foreign type key */
  3206.             and c1.colid = k.key7
  3207.             and c2.id = k.depid
  3208.             and c2.colid = k.depkey7
  3209.             and o1.id = k.depid
  3210.             and o2.id = k.id
  3211.     union all
  3212.         select
  3213.             db_name(),
  3214.             user_name(o1.uid),
  3215.             object_name(k.depid),
  3216.             c2.name,
  3217.             db_name(),
  3218.             user_name(o2.uid),
  3219.             object_name(k.id),
  3220.             c1.name,
  3221.             8
  3222.         from
  3223.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3224.         where
  3225.             c1.id = k.id
  3226.             and k.type = 2  /* Foreign type key */
  3227.             and c1.colid = k.key8
  3228.             and c2.id = k.depid
  3229.             and c2.colid = k.depkey8
  3230.             and o1.id = k.depid
  3231.             and o2.id = k.id
  3232.  
  3233.     if @order_by_pk = 1 /*  If order by PK fields */
  3234.         select
  3235.             PKTABLE_QUALIFIER,
  3236.             PKTABLE_OWNER,
  3237.             PKTABLE_NAME,
  3238.             PKCOLUMN_NAME,
  3239.             FKTABLE_QUALIFIER,
  3240.             FKTABLE_OWNER,
  3241.             FKTABLE_NAME,
  3242.             FKCOLUMN_NAME,
  3243.             KEY_SEQ,
  3244.             UPDATE_RULE = convert(smallint, null),
  3245.             DELETE_RULE = convert(smallint,null),
  3246.             FK_NAME = convert(varchar(32),null),
  3247.             PK_NAME = convert(varchar(32),null),
  3248.             DEFERRABILITY = convert(smallint,7)
  3249.         from #fkeys
  3250.         where FKTABLE_NAME like @fktable_name
  3251.             and FKTABLE_OWNER like @fktable_owner
  3252.             and PKTABLE_NAME  like @pktable_name
  3253.             and PKTABLE_OWNER like @pktable_owner
  3254.         order by 1, 2, 3, 9, 4
  3255.     else        /*  Order by FK fields */
  3256.         select
  3257.             PKTABLE_QUALIFIER,
  3258.             PKTABLE_OWNER,
  3259.             PKTABLE_NAME,
  3260.             PKCOLUMN_NAME,
  3261.             FKTABLE_QUALIFIER,
  3262.             FKTABLE_OWNER,
  3263.             FKTABLE_NAME,
  3264.             FKCOLUMN_NAME,
  3265.             KEY_SEQ,
  3266.             UPDATE_RULE = convert(smallint,null),
  3267.             DELETE_RULE = convert(smallint,null),
  3268.             FK_NAME = convert(varchar(32),null),
  3269.             PK_NAME = convert(varchar(32),null),
  3270.             DEFERRABILITY = convert(smallint,7)
  3271.         from #fkeys
  3272.         where FKTABLE_NAME like @fktable_name
  3273.             and FKTABLE_OWNER like @fktable_owner
  3274.             and PKTABLE_NAME  like @pktable_name
  3275.             and PKTABLE_OWNER like @pktable_owner
  3276.         order by 5, 6, 7, 9, 8
  3277. go
  3278.  
  3279. if (charindex('6.00', @@version) = 0 and
  3280.     charindex('6.50', @@version) = 0 and
  3281.     charindex('7.00', @@version) = 0 and
  3282.     charindex('8.00', @@version) = 0)
  3283. begin
  3284.     print ''
  3285.     print ''
  3286.     print 'Warning:'
  3287.     print 'you are installing the stored procedures '
  3288.     print 'on a pre 6.0 SQL Server.'
  3289.     print 'Ignore the following errors.'
  3290. end
  3291. else
  3292.     drop proc sp_fkeys
  3293. go
  3294.  
  3295. /*  Procedure for 6.0 and 6.50 servers */
  3296. CREATE PROCEDURE sp_fkeys(
  3297.                @pktable_name        varchar(32) = null,
  3298.                @pktable_owner       varchar(32) = null,
  3299.                @pktable_qualifier   varchar(32) = null,
  3300.                @fktable_name        varchar(32) = null,
  3301.                @fktable_owner       varchar(32) = null,
  3302.                @fktable_qualifier   varchar(32) = null )
  3303. as
  3304.     set nocount on
  3305.     DECLARE @pktable_id         int
  3306.     DECLARE @pkfull_table_name  varchar(65) /* 2*32 +1 */
  3307.     DECLARE @fktable_id         int
  3308.     DECLARE @fkfull_table_name  varchar(65) /* 2*32 +1 */
  3309.     declare @order_by_pk        int
  3310.  
  3311.     select  @order_by_pk = 0
  3312.  
  3313.     if (@pktable_name is null) and (@fktable_name is null)
  3314.     begin   /* If neither primary key nor foreign key table names given */
  3315.         raiserror (15252,-1,-1)
  3316.         return
  3317.     end
  3318.     if @fktable_qualifier is not null
  3319.     begin
  3320.         if db_name() <> @fktable_qualifier
  3321.         begin   /* If qualifier doesn't match current database */
  3322.             raiserror (15250, -1,-1)
  3323.             return
  3324.         end
  3325.     end
  3326.     if @pktable_qualifier is not null
  3327.     begin
  3328.         if db_name() <> @pktable_qualifier
  3329.         begin   /* If qualifier doesn't match current database */
  3330.             raiserror (15250, -1,-1)
  3331.             return
  3332.         end
  3333.     end
  3334.  
  3335.     if @pktable_owner is null
  3336.     begin   /* If unqualified primary key table name */
  3337.         SELECT @pkfull_table_name = @pktable_name
  3338.     end
  3339.     else
  3340.     begin   /* Qualified primary key table name */
  3341.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3342.     end
  3343.     /*  Get Object ID */
  3344.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3345.  
  3346.     if @fktable_owner is null
  3347.     begin   /* If unqualified foreign key table name */
  3348.         SELECT @fkfull_table_name = @fktable_name
  3349.     end
  3350.     else
  3351.     begin   /* Qualified foreign key table name */
  3352.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3353.     end
  3354.     /*  Get Object ID */
  3355.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3356.  
  3357.     if @fktable_name is not null
  3358.     begin
  3359.         if @fktable_id is null
  3360.             SELECT @fktable_id = 0  /* fk table not found, empty result */
  3361.     end
  3362.  
  3363.     if @pktable_name is null
  3364.     begin /*  If table name not supplied, match all */
  3365.         select @order_by_pk = 1
  3366.     end
  3367.     else
  3368.     begin
  3369.         if @pktable_id is null
  3370.         begin
  3371.             SELECT @pktable_id = 0  /* pk table not found, empty result */
  3372.         end
  3373.     end
  3374.  
  3375.     if (@@trancount <> 0 and
  3376.         charindex('6.50', @@version) = 0)
  3377.     begin   /* If inside a transaction */
  3378.         raiserror (15002,-1,-1,'sp_fkeys')
  3379.         return
  3380.     end
  3381.  
  3382.     create table #fkeys(
  3383.              pkdb_id        int NOT NULL,
  3384.              pktable_id     int NOT NULL,
  3385.              pkcolid        int NOT NULL,
  3386.              fkdb_id        int NOT NULL,
  3387.              fktable_id     int NOT NULL,
  3388.              fkcolid        int NOT NULL,
  3389.              KEY_SEQ        smallint NOT NULL,
  3390.              fk_id          int NOT NULL,
  3391.              pk_id          int NOT NULL)
  3392.  
  3393.     /*  SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3394.     /*  Process syskeys for each relationship */
  3395.     /*  The inserts below adds a row to the temp table for each of the
  3396.         16 possible relationships */
  3397.     insert into #fkeys
  3398.         select
  3399.             r.rkeydbid,
  3400.             r.rkeyid,
  3401.             r.rkey1,
  3402.             r.fkeydbid,
  3403.             r.fkeyid,
  3404.             r.fkey1,
  3405.             1,
  3406.             r.constid,
  3407.             s.constid
  3408.         from
  3409.             sysreferences r, sysconstraints s
  3410.         where   r.rkeyid = s.id
  3411.             AND (s.status & 0xf) = 1
  3412.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3413.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3414.       union all
  3415.         select
  3416.             r.rkeydbid,
  3417.             r.rkeyid,
  3418.             r.rkey2,
  3419.             r.fkeydbid,
  3420.             r.fkeyid,
  3421.             r.fkey2,
  3422.             2,
  3423.             r.constid,
  3424.             s.constid
  3425.         from
  3426.             sysreferences r, sysconstraints s
  3427.         where   r.rkeyid = s.id
  3428.             AND (s.status & 0xf) = 1
  3429.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3430.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3431.       union all
  3432.         select
  3433.             r.rkeydbid,
  3434.             r.rkeyid,
  3435.             r.rkey3,
  3436.             r.fkeydbid,
  3437.             r.fkeyid,
  3438.             r.fkey3,
  3439.             3,
  3440.             r.constid,
  3441.             s.constid
  3442.         from
  3443.             sysreferences r, sysconstraints s
  3444.         where   r.rkeyid = s.id
  3445.             AND (s.status & 0xf) = 1
  3446.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3447.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3448.       union all
  3449.         select
  3450.             r.rkeydbid,
  3451.             r.rkeyid,
  3452.             r.rkey4,
  3453.             r.fkeydbid,
  3454.             r.fkeyid,
  3455.             r.fkey4,
  3456.             4,
  3457.             r.constid,
  3458.             s.constid
  3459.         from
  3460.             sysreferences r, sysconstraints s
  3461.         where   r.rkeyid = s.id
  3462.             AND (s.status & 0xf) = 1
  3463.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3464.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3465.       union all
  3466.         select
  3467.             r.rkeydbid,
  3468.             r.rkeyid,
  3469.             r.rkey5,
  3470.             r.fkeydbid,
  3471.             r.fkeyid,
  3472.             r.fkey5,
  3473.             5,
  3474.             r.constid,
  3475.             s.constid
  3476.         from
  3477.             sysreferences r, sysconstraints s
  3478.         where   r.rkeyid = s.id
  3479.             AND (s.status & 0xf) = 1
  3480.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3481.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3482.       union all
  3483.         select
  3484.             r.rkeydbid,
  3485.             r.rkeyid,
  3486.             r.rkey6,
  3487.             r.fkeydbid,
  3488.             r.fkeyid,
  3489.             r.fkey6,
  3490.             6,
  3491.             r.constid,
  3492.             s.constid
  3493.         from
  3494.             sysreferences r, sysconstraints s
  3495.         where   r.rkeyid = s.id
  3496.             AND (s.status & 0xf) = 1
  3497.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3498.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3499.       union all
  3500.         select
  3501.             r.rkeydbid,
  3502.             r.rkeyid,
  3503.             r.rkey7,
  3504.             r.fkeydbid,
  3505.             r.fkeyid,
  3506.             r.fkey7,
  3507.             7,
  3508.             r.constid,
  3509.             s.constid
  3510.         from
  3511.             sysreferences r, sysconstraints s
  3512.         where   r.rkeyid = s.id
  3513.             AND (s.status & 0xf) = 1
  3514.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3515.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3516.       union all
  3517.         select
  3518.             r.rkeydbid,
  3519.             r.rkeyid,
  3520.             r.rkey8,
  3521.             r.fkeydbid,
  3522.             r.fkeyid,
  3523.             r.fkey8,
  3524.             8,
  3525.             r.constid,
  3526.             s.constid
  3527.         from
  3528.             sysreferences r, sysconstraints s
  3529.         where   r.rkeyid = s.id
  3530.             AND (s.status & 0xf) = 1
  3531.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3532.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3533.       union all
  3534.         select
  3535.             r.rkeydbid,
  3536.             r.rkeyid,
  3537.             r.rkey9,
  3538.             r.fkeydbid,
  3539.             r.fkeyid,
  3540.             r.fkey9,
  3541.             9,
  3542.             r.constid,
  3543.             s.constid
  3544.         from
  3545.             sysreferences r, sysconstraints s
  3546.         where   r.rkeyid = s.id
  3547.             AND (s.status & 0xf) = 1
  3548.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3549.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3550.       union all
  3551.         select
  3552.             r.rkeydbid,
  3553.             r.rkeyid,
  3554.             r.rkey10,
  3555.             r.fkeydbid,
  3556.             r.fkeyid,
  3557.             r.fkey10,
  3558.             10,
  3559.             r.constid,
  3560.             s.constid
  3561.         from
  3562.             sysreferences r, sysconstraints s
  3563.         where   r.rkeyid = s.id
  3564.             AND (s.status & 0xf) = 1
  3565.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3566.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3567.       union all
  3568.         select
  3569.             r.rkeydbid,
  3570.             r.rkeyid,
  3571.             r.rkey11,
  3572.             r.fkeydbid,
  3573.             r.fkeyid,
  3574.             r.fkey11,
  3575.             11,
  3576.             r.constid,
  3577.             s.constid
  3578.         from
  3579.             sysreferences r, sysconstraints s
  3580.         where   r.rkeyid = s.id
  3581.             AND (s.status & 0xf) = 1
  3582.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3583.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3584.       union all
  3585.         select
  3586.             r.rkeydbid,
  3587.             r.rkeyid,
  3588.             r.rkey12,
  3589.             r.fkeydbid,
  3590.             r.fkeyid,
  3591.             r.fkey12,
  3592.             12,
  3593.             r.constid,
  3594.             s.constid
  3595.         from
  3596.             sysreferences r, sysconstraints s
  3597.         where   r.rkeyid = s.id
  3598.             AND (s.status & 0xf) = 1
  3599.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3600.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3601.       union all
  3602.         select
  3603.             r.rkeydbid,
  3604.             r.rkeyid,
  3605.             r.rkey13,
  3606.             r.fkeydbid,
  3607.             r.fkeyid,
  3608.             r.fkey13,
  3609.             13,
  3610.             r.constid,
  3611.             s.constid
  3612.         from
  3613.             sysreferences r, sysconstraints s
  3614.         where   r.rkeyid = s.id
  3615.             AND (s.status & 0xf) = 1
  3616.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3617.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3618.       union all
  3619.         select
  3620.             r.rkeydbid,
  3621.             r.rkeyid,
  3622.             r.rkey14,
  3623.             r.fkeydbid,
  3624.             r.fkeyid,
  3625.             r.fkey14,
  3626.             14,
  3627.             r.constid,
  3628.             s.constid
  3629.         from
  3630.             sysreferences r, sysconstraints s
  3631.         where   r.rkeyid = s.id
  3632.             AND (s.status & 0xf) = 1
  3633.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3634.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3635.       union all
  3636.         select
  3637.             r.rkeydbid,
  3638.             r.rkeyid,
  3639.             r.rkey15,
  3640.             r.fkeydbid,
  3641.             r.fkeyid,
  3642.             r.fkey15,
  3643.             15,
  3644.             r.constid,
  3645.             s.constid
  3646.         from
  3647.             sysreferences r, sysconstraints s
  3648.         where   r.rkeyid = s.id
  3649.             AND (s.status & 0xf) = 1
  3650.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3651.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3652.       union all
  3653.         select
  3654.             r.rkeydbid,
  3655.             r.rkeyid,
  3656.             r.rkey16,
  3657.             r.fkeydbid,
  3658.             r.fkeyid,
  3659.             r.fkey16,
  3660.             16,
  3661.             r.constid,
  3662.             s.constid
  3663.         from
  3664.             sysreferences r, sysconstraints s
  3665.         where   r.rkeyid = s.id
  3666.             AND (s.status & 0xf) = 1
  3667.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3668.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3669.  
  3670.     if @order_by_pk = 1 /*  If order by PK fields */
  3671.         select
  3672.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3673.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3674.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3675.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3676.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3677.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3678.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3679.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3680.             KEY_SEQ,
  3681.             UPDATE_RULE = convert(smallint,1),
  3682.             DELETE_RULE = convert(smallint,1),
  3683.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3684.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3685.             DEFERRABILITY = convert(smallint,7)
  3686.         from #fkeys f,
  3687.             sysobjects o1, sysobjects o2,
  3688.             syscolumns c1, syscolumns c2
  3689.         where   o1.id = f.pktable_id
  3690.             AND o2.id = f.fktable_id
  3691.             AND c1.id = f.pktable_id
  3692.             AND c2.id = f.fktable_id
  3693.             AND c1.colid = f.pkcolid
  3694.             AND c2.colid = f.fkcolid
  3695.         order by 1,2,3,9,4
  3696.     else        /*  Order by FK fields */
  3697.         select
  3698.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3699.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3700.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3701.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3702.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3703.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3704.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3705.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3706.             KEY_SEQ,
  3707.             UPDATE_RULE = convert(smallint,1),
  3708.             DELETE_RULE = convert(smallint,1),
  3709.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3710.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3711.             DEFERRABILITY = convert(smallint,7)
  3712.         from #fkeys f,
  3713.             sysobjects o1, sysobjects o2,
  3714.             syscolumns c1, syscolumns c2
  3715.         where   o1.id = f.pktable_id
  3716.             AND o2.id = f.fktable_id
  3717.             AND c1.id = f.pktable_id
  3718.             AND c2.id = f.fktable_id
  3719.             AND c1.colid = f.pkcolid
  3720.             AND c2.colid = f.fkcolid
  3721.         order by 5,6,7,9,8
  3722. go
  3723.  
  3724. if (charindex('7.00', @@version) = 0 and
  3725.     charindex('8.00', @@version) = 0)
  3726. begin
  3727.     print ''
  3728.     print ''
  3729.     print 'Warning:'
  3730.     print 'you are installing the stored procedures '
  3731.     print 'on a pre 7.0 SQL Server.'
  3732.     print 'Ignore the following errors.'
  3733. end
  3734. else
  3735.     drop proc sp_fkeys
  3736. go
  3737.  
  3738. /*  Procedure for 7.0 server */
  3739. CREATE PROCEDURE sp_fkeys(
  3740.                @pktable_name        sysname = null,
  3741.                @pktable_owner       sysname = null,
  3742.                @pktable_qualifier   sysname = null,
  3743.                @fktable_name        sysname = null,
  3744.                @fktable_owner       sysname = null,
  3745.                @fktable_qualifier   sysname = null )
  3746. as
  3747.     set nocount on
  3748.     DECLARE @pktable_id         int
  3749.     DECLARE @pkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  3750.     DECLARE @fktable_id         int
  3751.     DECLARE @fkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  3752.     declare @order_by_pk        int
  3753.  
  3754.     /* select 'XXX starting table creation' */
  3755.  
  3756.     create table #fkeysall(
  3757.             rkeyid int NOT NULL,
  3758.             rkey1 int NOT NULL,
  3759.                 rkey2 int NOT NULL,
  3760.                 rkey3 int NOT NULL,
  3761.                 rkey4 int NOT NULL,
  3762.                 rkey5 int NOT NULL,
  3763.                 rkey6 int NOT NULL,
  3764.                 rkey7 int NOT NULL,
  3765.                 rkey8 int NOT NULL,
  3766.                 rkey9 int NOT NULL,
  3767.                 rkey10 int NOT NULL,
  3768.                 rkey11 int NOT NULL,
  3769.                 rkey12 int NOT NULL,
  3770.                 rkey13 int NOT NULL,
  3771.                 rkey14 int NOT NULL,
  3772.                 rkey15 int NOT NULL,
  3773.                 rkey16 int NOT NULL,
  3774.             fkeyid int NOT NULL,
  3775.             fkey1 int NOT NULL,
  3776.                 fkey2 int NOT NULL,
  3777.                 fkey3 int NOT NULL,
  3778.                 fkey4 int NOT NULL,
  3779.                 fkey5 int NOT NULL,
  3780.                 fkey6 int NOT NULL,
  3781.                 fkey7 int NOT NULL,
  3782.                 fkey8 int NOT NULL,
  3783.                 fkey9 int NOT NULL,
  3784.                 fkey10 int NOT NULL,
  3785.                 fkey11 int NOT NULL,
  3786.                 fkey12 int NOT NULL,
  3787.                 fkey13 int NOT NULL,
  3788.                 fkey14 int NOT NULL,
  3789.                 fkey15 int NOT NULL,
  3790.                 fkey16 int NOT NULL,
  3791.             constid int NOT NULL,
  3792.             name sysname NOT NULL)
  3793.  
  3794.     create table #fkeys(
  3795.             pktable_id      int NOT NULL,
  3796.             pkcolid         int NOT NULL,
  3797.             fktable_id      int NOT NULL,
  3798.             fkcolid         int NOT NULL,
  3799.             KEY_SEQ         smallint NOT NULL,
  3800.             fk_id           int NOT NULL,
  3801.             PK_NAME         sysname NOT NULL)
  3802.  
  3803.     create table #fkeysout(
  3804.             PKTABLE_QUALIFIER sysname NULL,
  3805.             PKTABLE_OWNER sysname NULL,
  3806.             PKTABLE_NAME sysname NOT NULL,
  3807.             PKCOLUMN_NAME sysname NOT NULL,
  3808.             FKTABLE_QUALIFIER sysname NULL,
  3809.             FKTABLE_OWNER sysname NULL,
  3810.             FKTABLE_NAME sysname NOT NULL,
  3811.             FKCOLUMN_NAME sysname NOT NULL,
  3812.             KEY_SEQ smallint NOT NULL,
  3813.             UPDATE_RULE smallint NULL,
  3814.             DELETE_RULE smallint NULL,
  3815.             FK_NAME sysname NULL,
  3816.             PK_NAME sysname NULL,
  3817.             DEFERRABILITY smallint null)
  3818.  
  3819.     /* select 'XXX starting parameter analysis' */
  3820.  
  3821.     select  @order_by_pk = 0
  3822.  
  3823.     if (@pktable_name is null) and (@fktable_name is null)
  3824.     begin   /* If neither primary key nor foreign key table names given */
  3825.         raiserror (15252,-1,-1)
  3826.         return
  3827.     end
  3828.     if @fktable_qualifier is not null
  3829.     begin
  3830.         if db_name() <> @fktable_qualifier
  3831.         begin   /* If qualifier doesn't match current database */
  3832.             raiserror (15250, -1,-1)
  3833.             return
  3834.         end
  3835.     end
  3836.     if @pktable_qualifier is not null
  3837.     begin
  3838.         if db_name() <> @pktable_qualifier
  3839.         begin   /* If qualifier doesn't match current database */
  3840.             raiserror (15250, -1,-1)
  3841.             return
  3842.         end
  3843.     end
  3844.  
  3845.     if @pktable_owner is null
  3846.     begin   /* If unqualified primary key table name */
  3847.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3848.     end
  3849.     else
  3850.     begin   /* Qualified primary key table name */
  3851.         if @pktable_owner = ''
  3852.         begin   /* If empty owner name */
  3853.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3854.         end
  3855.         else
  3856.         begin
  3857.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3858.                 '.' + quotename(@pktable_name)
  3859.         end
  3860.     end
  3861.     /*  Get Object ID */
  3862.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3863.  
  3864.     if @fktable_owner is null
  3865.     begin   /* If unqualified foreign key table name */
  3866.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3867.     end
  3868.     else
  3869.     begin   /* Qualified foreign key table name */
  3870.         if @fktable_owner = ''
  3871.         begin   /* If empty owner name */
  3872.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3873.         end
  3874.         else
  3875.         begin
  3876.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3877.                 '.' + quotename(@fktable_name)
  3878.         end
  3879.     end
  3880.     /*  Get Object ID */
  3881.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3882.  
  3883.     if @fktable_name is not null
  3884.     begin
  3885.         if @fktable_id is null
  3886.             SELECT @fktable_id = 0  /* fk table not found, empty result */
  3887.     end
  3888.  
  3889.     if @pktable_name is null
  3890.     begin /*  If table name not supplied, match all */
  3891.         select @order_by_pk = 1
  3892.     end
  3893.     else
  3894.     begin
  3895.         if @pktable_id is null
  3896.         begin
  3897.             SELECT @pktable_id = 0  /* pk table not found, empty result */
  3898.         end
  3899.     end
  3900.  
  3901.     /*  SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3902.     /*  Process syskeys for each relationship */
  3903.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3904.         them out with a 16-way "insert select ... union select ..." */
  3905.  
  3906.     /* select 'XXX starting data analysis' */
  3907.  
  3908.     insert into #fkeysall
  3909.         select
  3910.             r.rkeyid,
  3911.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3912.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3913.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3914.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3915.             r.fkeyid,
  3916.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3917.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3918.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3919.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3920.             r.constid,
  3921.             i.name
  3922.         from
  3923.             sysreferences r, sysobjects o, sysindexes i
  3924.         where   r.constid = o.id
  3925.             AND o.xtype = 'F'
  3926.             AND r.rkeyindid = i.indid
  3927.             AND r.rkeyid = i.id
  3928.             AND r.rkeyid between isnull(@pktable_id, 0)
  3929.                             and isnull(@pktable_id, 0x7fffffff)
  3930.             AND r.fkeyid between isnull(@fktable_id, 0)
  3931.                             and isnull(@fktable_id, 0x7fffffff)
  3932.  
  3933.     /* select count (*) as 'XXX countall' from #fkeysall */
  3934.  
  3935.     insert into #fkeys
  3936.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3937.             from #fkeysall
  3938.         union all
  3939.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3940.             from #fkeysall
  3941.         union all
  3942.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3943.             from #fkeysall
  3944.         union all
  3945.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3946.             from #fkeysall
  3947.         union all
  3948.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3949.             from #fkeysall
  3950.         union all
  3951.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3952.             from #fkeysall
  3953.         union all
  3954.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3955.             from #fkeysall
  3956.         union all
  3957.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3958.             from #fkeysall
  3959.         union all
  3960.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3961.             from #fkeysall
  3962.         union all
  3963.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3964.             from #fkeysall
  3965.         union all
  3966.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3967.             from #fkeysall
  3968.         union all
  3969.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3970.             from #fkeysall
  3971.         union all
  3972.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3973.             from #fkeysall
  3974.         union all
  3975.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3976.             from #fkeysall
  3977.         union all
  3978.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3979.             from #fkeysall
  3980.         union all
  3981.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3982.             from #fkeysall
  3983.  
  3984.     /* select count (*) as 'XXX count' from #fkeys */
  3985.  
  3986.     insert into #fkeysout
  3987.         select
  3988.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3989.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3990.             PKTABLE_NAME = convert(sysname,o1.name),
  3991.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3992.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3993.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3994.             FKTABLE_NAME = convert(sysname,o2.name),
  3995.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3996.             KEY_SEQ,
  3997.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  3998.                 convert(smallint,0) ELSE convert(smallint,1) END,
  3999.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4000.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4001.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4002.             PK_NAME,
  4003.             DEFERRABILITY = 7   /* SQL_NOT_DEFERRABLE */
  4004.         from #fkeys f,
  4005.             sysobjects o1, sysobjects o2,
  4006.             syscolumns c1, syscolumns c2
  4007.         where   o1.id = f.pktable_id
  4008.             AND o2.id = f.fktable_id
  4009.             AND c1.id = f.pktable_id
  4010.             AND c2.id = f.fktable_id
  4011.             AND c1.colid = f.pkcolid
  4012.             AND c2.colid = f.fkcolid
  4013.     /* select count (*) as 'XXX countout' from #fkeysout */
  4014.  
  4015.     if @order_by_pk = 1 /*  If order by PK fields */
  4016.         select
  4017.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4018.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4019.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4020.         from #fkeysout
  4021.         order by 1,2,3,9,4
  4022.     else        /*  Order by FK fields */
  4023.         select
  4024.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4025.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4026.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4027.         from #fkeysout
  4028.         order by 5,6,7,9,8
  4029. go
  4030.  
  4031. if (charindex('8.00', @@version) = 0)
  4032. begin
  4033.     print ''
  4034.     print ''
  4035.     print 'Warning:'
  4036.     print 'you are installing the stored procedures '
  4037.     print 'on a pre 8.0 SQL Server.'
  4038.     print 'Ignore the following errors.'
  4039. end
  4040. else
  4041.     drop proc sp_fkeys
  4042. go
  4043.  
  4044. /*  Procedure for 8.0 server */
  4045. CREATE PROCEDURE sp_fkeys(
  4046.                @pktable_name        sysname = null,
  4047.                @pktable_owner       sysname = null,
  4048.                @pktable_qualifier   sysname = null,
  4049.                @fktable_name        sysname = null,
  4050.                @fktable_owner       sysname = null,
  4051.                @fktable_qualifier   sysname = null )
  4052. as
  4053.     set nocount on
  4054.     DECLARE @pktable_id         int
  4055.     DECLARE @pkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  4056.     DECLARE @fktable_id         int
  4057.     DECLARE @fkfull_table_name  nvarchar(257) /* 2*128 + 1 */
  4058.     declare @order_by_pk        int
  4059.  
  4060.     /* select 'XXX starting table creation' */
  4061.  
  4062.     create table #fkeysall(
  4063.             rkeyid int NOT NULL,
  4064.             rkey1 int NOT NULL,
  4065.                 rkey2 int NOT NULL,
  4066.                 rkey3 int NOT NULL,
  4067.                 rkey4 int NOT NULL,
  4068.                 rkey5 int NOT NULL,
  4069.                 rkey6 int NOT NULL,
  4070.                 rkey7 int NOT NULL,
  4071.                 rkey8 int NOT NULL,
  4072.                 rkey9 int NOT NULL,
  4073.                 rkey10 int NOT NULL,
  4074.                 rkey11 int NOT NULL,
  4075.                 rkey12 int NOT NULL,
  4076.                 rkey13 int NOT NULL,
  4077.                 rkey14 int NOT NULL,
  4078.                 rkey15 int NOT NULL,
  4079.                 rkey16 int NOT NULL,
  4080.             fkeyid int NOT NULL,
  4081.             fkey1 int NOT NULL,
  4082.                 fkey2 int NOT NULL,
  4083.                 fkey3 int NOT NULL,
  4084.                 fkey4 int NOT NULL,
  4085.                 fkey5 int NOT NULL,
  4086.                 fkey6 int NOT NULL,
  4087.                 fkey7 int NOT NULL,
  4088.                 fkey8 int NOT NULL,
  4089.                 fkey9 int NOT NULL,
  4090.                 fkey10 int NOT NULL,
  4091.                 fkey11 int NOT NULL,
  4092.                 fkey12 int NOT NULL,
  4093.                 fkey13 int NOT NULL,
  4094.                 fkey14 int NOT NULL,
  4095.                 fkey15 int NOT NULL,
  4096.                 fkey16 int NOT NULL,
  4097.             constid int NOT NULL,
  4098.             name sysname collate database_default NOT NULL)
  4099.  
  4100.     create table #fkeys(
  4101.             pktable_id      int NOT NULL,
  4102.             pkcolid         int NOT NULL,
  4103.             fktable_id      int NOT NULL,
  4104.             fkcolid         int NOT NULL,
  4105.             KEY_SEQ         smallint NOT NULL,
  4106.             fk_id           int NOT NULL,
  4107.             PK_NAME         sysname collate database_default NOT NULL)
  4108.  
  4109.     create table #fkeysout(
  4110.             PKTABLE_QUALIFIER sysname collate database_default NULL,
  4111.             PKTABLE_OWNER sysname collate database_default NULL,
  4112.             PKTABLE_NAME sysname collate database_default NOT NULL,
  4113.             PKCOLUMN_NAME sysname collate database_default NOT NULL,
  4114.             FKTABLE_QUALIFIER sysname collate database_default NULL,
  4115.             FKTABLE_OWNER sysname collate database_default NULL,
  4116.             FKTABLE_NAME sysname collate database_default NOT NULL,
  4117.             FKCOLUMN_NAME sysname collate database_default NOT NULL,
  4118.             KEY_SEQ smallint NOT NULL,
  4119.             UPDATE_RULE smallint NULL,
  4120.             DELETE_RULE smallint NULL,
  4121.             FK_NAME sysname collate database_default NULL,
  4122.             PK_NAME sysname collate database_default NULL,
  4123.             DEFERRABILITY smallint null)
  4124.  
  4125.     /* select 'XXX starting parameter analysis' */
  4126.  
  4127.     select  @order_by_pk = 0
  4128.  
  4129.     if (@pktable_name is null) and (@fktable_name is null)
  4130.     begin   /* If neither primary key nor foreign key table names given */
  4131.         raiserror (15252,-1,-1)
  4132.         return
  4133.     end
  4134.     if @fktable_qualifier is not null
  4135.     begin
  4136.         if db_name() <> @fktable_qualifier
  4137.         begin   /* If qualifier doesn't match current database */
  4138.             raiserror (15250, -1,-1)
  4139.             return
  4140.         end
  4141.     end
  4142.     if @pktable_qualifier is not null
  4143.     begin
  4144.         if db_name() <> @pktable_qualifier
  4145.         begin   /* If qualifier doesn't match current database */
  4146.             raiserror (15250, -1,-1)
  4147.             return
  4148.         end
  4149.     end
  4150.  
  4151.     if @pktable_owner is null
  4152.     begin   /* If unqualified primary key table name */
  4153.         SELECT @pkfull_table_name = quotename(@pktable_name)
  4154.     end
  4155.     else
  4156.     begin   /* Qualified primary key table name */
  4157.         if @pktable_owner = ''
  4158.         begin   /* If empty owner name */
  4159.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  4160.         end
  4161.         else
  4162.         begin
  4163.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  4164.                 '.' + quotename(@pktable_name)
  4165.         end
  4166.     end
  4167.     /*  Get Object ID */
  4168.     SELECT @pktable_id = object_id(@pkfull_table_name)
  4169.  
  4170.     if @fktable_owner is null
  4171.     begin   /* If unqualified foreign key table name */
  4172.         SELECT @fkfull_table_name = quotename(@fktable_name)
  4173.     end
  4174.     else
  4175.     begin   /* Qualified foreign key table name */
  4176.         if @fktable_owner = ''
  4177.         begin   /* If empty owner name */
  4178.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  4179.         end
  4180.         else
  4181.         begin
  4182.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  4183.                 '.' + quotename(@fktable_name)
  4184.         end
  4185.     end
  4186.     /*  Get Object ID */
  4187.     SELECT @fktable_id = object_id(@fkfull_table_name)
  4188.  
  4189.     if @fktable_name is not null
  4190.     begin
  4191.         if @fktable_id is null
  4192.             SELECT @fktable_id = 0  /* fk table not found, empty result */
  4193.     end
  4194.  
  4195.     if @pktable_name is null
  4196.     begin /*  If table name not supplied, match all */
  4197.         select @order_by_pk = 1
  4198.     end
  4199.     else
  4200.     begin
  4201.         if @pktable_id is null
  4202.         begin
  4203.             SELECT @pktable_id = 0  /* pk table not found, empty result */
  4204.         end
  4205.     end
  4206.  
  4207.     /*  SQL Server supports upto 16 PK/FK relationships between 2 tables */
  4208.     /*  Process syskeys for each relationship */
  4209.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  4210.         them out with a 16-way "insert select ... union select ..." */
  4211.  
  4212.     /* select 'XXX starting data analysis' */
  4213.  
  4214.     insert into #fkeysall
  4215.         select
  4216.             r.rkeyid,
  4217.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  4218.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  4219.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  4220.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  4221.             r.fkeyid,
  4222.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  4223.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  4224.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  4225.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  4226.             r.constid,
  4227.             i.name
  4228.         from
  4229.             sysreferences r, sysobjects o, sysindexes i
  4230.         where   r.constid = o.id
  4231.             AND o.xtype = 'F'
  4232.             AND r.rkeyindid = i.indid
  4233.             AND r.rkeyid = i.id
  4234.             AND r.rkeyid between isnull(@pktable_id, 0)
  4235.                             and isnull(@pktable_id, 0x7fffffff)
  4236.             AND r.fkeyid between isnull(@fktable_id, 0)
  4237.                             and isnull(@fktable_id, 0x7fffffff)
  4238.  
  4239.     /* select count (*) as 'XXX countall' from #fkeysall */
  4240.  
  4241.     insert into #fkeys
  4242.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  4243.             from #fkeysall
  4244.         union all
  4245.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  4246.             from #fkeysall
  4247.         union all
  4248.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  4249.             from #fkeysall
  4250.         union all
  4251.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  4252.             from #fkeysall
  4253.         union all
  4254.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  4255.             from #fkeysall
  4256.         union all
  4257.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  4258.             from #fkeysall
  4259.         union all
  4260.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  4261.             from #fkeysall
  4262.         union all
  4263.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  4264.             from #fkeysall
  4265.         union all
  4266.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  4267.             from #fkeysall
  4268.         union all
  4269.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  4270.             from #fkeysall
  4271.         union all
  4272.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  4273.             from #fkeysall
  4274.         union all
  4275.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  4276.             from #fkeysall
  4277.         union all
  4278.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  4279.             from #fkeysall
  4280.         union all
  4281.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  4282.             from #fkeysall
  4283.         union all
  4284.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  4285.             from #fkeysall
  4286.         union all
  4287.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  4288.             from #fkeysall
  4289.  
  4290.     /* select count (*) as 'XXX count' from #fkeys */
  4291.  
  4292.     insert into #fkeysout
  4293.         select
  4294.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  4295.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  4296.             PKTABLE_NAME = convert(sysname,o1.name),
  4297.             PKCOLUMN_NAME = convert(sysname,c1.name),
  4298.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  4299.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  4300.             FKTABLE_NAME = convert(sysname,o2.name),
  4301.             FKCOLUMN_NAME = convert(sysname,c2.name),
  4302.             KEY_SEQ,
  4303.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  4304.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4305.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4306.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4307.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4308.             PK_NAME,
  4309.             DEFERRABILITY = 7   /* SQL_NOT_DEFERRABLE */
  4310.         from #fkeys f,
  4311.             sysobjects o1, sysobjects o2,
  4312.             syscolumns c1, syscolumns c2
  4313.         where   o1.id = f.pktable_id
  4314.             AND o2.id = f.fktable_id
  4315.             AND c1.id = f.pktable_id
  4316.             AND c2.id = f.fktable_id
  4317.             AND c1.colid = f.pkcolid
  4318.             AND c2.colid = f.fkcolid
  4319.     /* select count (*) as 'XXX countout' from #fkeysout */
  4320.  
  4321.     if @order_by_pk = 1 /*  If order by PK fields */
  4322.         select
  4323.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4324.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4325.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4326.         from #fkeysout
  4327.         order by 1,2,3,9,4
  4328.     else        /*  Order by FK fields */
  4329.         select
  4330.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4331.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4332.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4333.         from #fkeysout
  4334.         order by 5,6,7,9,8
  4335. go
  4336. grant execute on sp_fkeys to public
  4337. go
  4338.  
  4339. dump tran master with no_log
  4340. go
  4341.  
  4342. print 'creating sp_pkeys'
  4343. go
  4344.  
  4345. /*  Procedure for pre-6.0 server */
  4346. CREATE PROCEDURE sp_pkeys(
  4347.                @table_name      varchar(32),
  4348.                @table_owner     varchar(32) = null,
  4349.                @table_qualifier varchar(32) = null )
  4350. as
  4351.     set nocount on
  4352.     if @table_qualifier is not null
  4353.     begin
  4354.         if db_name() <> @table_qualifier
  4355.         begin   /* If qualifier doesn't match current database */
  4356.             raiserror 20001 '~~Rush_5~~'
  4357.             return
  4358.         end
  4359.     end
  4360.     if @table_owner is null /*  If owner not supplied, match all */
  4361.         select @table_owner = '%'
  4362.     if @@trancount <> 0
  4363.     begin   /* If inside a transaction */
  4364.         raiserror 20003 '~~Rush_35~~'
  4365.         return
  4366.     end
  4367.  
  4368.     create table #pkeys(
  4369.              TABLE_QUALIFIER varchar(32) NULL,
  4370.              TABLE_OWNER     varchar(32) NULL,
  4371.              TABLE_NAME      varchar(32) NOT NULL,
  4372.              COLUMN_NAME     varchar(32) NOT NULL,
  4373.              KEY_SEQ         smallint NOT NULL)
  4374.  
  4375.     /*  SQL Server supports upto 8 PK/FK relationships between 2 tables */
  4376.     /*  Process syskeys for each relationship */
  4377.     /*  The inserts below adds a row to the temp table for each of the
  4378.         8 possible relationships */
  4379.     insert into #pkeys
  4380.         select
  4381.             db_name(),
  4382.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4383.             object_name(k.id),
  4384.             c.name,
  4385.             1
  4386.         from
  4387.             syskeys k, syscolumns c
  4388.         where
  4389.             c.id = k.id
  4390.             and k.type = 1  /* Primary type key */
  4391.             and c.colid = k.key1
  4392.     if (@@rowcount = 0)
  4393.         goto done
  4394.  
  4395.     insert into #pkeys
  4396.         select
  4397.             db_name(),
  4398.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4399.             object_name(k.id),
  4400.             c.name,
  4401.             2
  4402.         from
  4403.             syskeys k, syscolumns c
  4404.         where
  4405.             c.id = k.id
  4406.             and k.type = 1  /* Primary type key */
  4407.             and c.colid = key2
  4408.     if (@@rowcount = 0)
  4409.         goto done
  4410.  
  4411.     insert into #pkeys
  4412.         select
  4413.             db_name(),
  4414.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4415.             object_name(k.id),
  4416.             c.name,
  4417.             3
  4418.         from
  4419.             syskeys k, syscolumns c
  4420.         where
  4421.             c.id = k.id
  4422.             and k.type = 1  /* Primary type key */
  4423.             and c.colid = key3
  4424.     if (@@rowcount = 0)
  4425.         goto done
  4426.  
  4427.     insert into #pkeys
  4428.         select
  4429.             db_name(),
  4430.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4431.             object_name(k.id),
  4432.             c.name,
  4433.             4
  4434.         from
  4435.             syskeys k, syscolumns c
  4436.         where
  4437.             c.id = k.id
  4438.             and k.type = 1  /* Primary type key */
  4439.             and c.colid = key4
  4440.     if (@@rowcount = 0)
  4441.         goto done
  4442.  
  4443.     insert into #pkeys
  4444.         select
  4445.             db_name(),
  4446.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4447.             object_name(k.id),
  4448.             c.name,
  4449.             5
  4450.         from
  4451.             syskeys k, syscolumns c
  4452.         where
  4453.             c.id = k.id
  4454.             and k.type = 1  /* Primary type key */
  4455.             and c.colid = key5
  4456.     if (@@rowcount = 0)
  4457.         goto done
  4458.  
  4459.     insert into #pkeys
  4460.         select
  4461.             db_name(),
  4462.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4463.             object_name(k.id),
  4464.             c.name,
  4465.             6
  4466.         from
  4467.             syskeys k, syscolumns c
  4468.         where
  4469.             c.id = k.id
  4470.             and k.type = 1  /* Primary type key */
  4471.             and c.colid = key6
  4472.     if (@@rowcount = 0)
  4473.         goto done
  4474.  
  4475.     insert into #pkeys
  4476.         select
  4477.             db_name(),
  4478.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4479.             object_name(k.id),
  4480.             c.name,
  4481.             7
  4482.         from
  4483.             syskeys k, syscolumns c
  4484.         where
  4485.             c.id = k.id
  4486.             and k.type = 1  /* Primary type key */
  4487.             and c.colid = key7
  4488.     if (@@rowcount = 0)
  4489.         goto done
  4490.  
  4491.     insert into #pkeys
  4492.          select
  4493.              db_name(),
  4494.              (select user_name(uid) from sysobjects o where o.id = k.id),
  4495.              object_name(k.id),
  4496.              c.name,
  4497.              8
  4498.          from
  4499.              syskeys k, syscolumns c
  4500.          where
  4501.              c.id = k.id
  4502.              and k.type = 1 /* Primary type key */
  4503.              and c.colid = key8
  4504.  
  4505.     done:
  4506.     select
  4507.         TABLE_QUALIFIER,
  4508.         TABLE_OWNER,
  4509.         TABLE_NAME,
  4510.         COLUMN_NAME,
  4511.         KEY_SEQ,
  4512.         PK_NAME = convert(varchar(32),null)
  4513.     from #pkeys
  4514.     where TABLE_NAME = @table_name
  4515.         and TABLE_OWNER like @table_owner
  4516.     order by 1, 2, 3, 5
  4517. go
  4518.  
  4519. if (charindex('6.00', @@version) = 0 and
  4520.     charindex('6.50', @@version) = 0 and
  4521.     charindex('7.00', @@version) = 0 and
  4522.     charindex('8.00', @@version) = 0)
  4523. begin
  4524.     print ''
  4525.     print ''
  4526.     print 'Warning:'
  4527.     print 'you are installing the stored procedures '
  4528.     print 'on a pre 6.0 SQL Server.'
  4529.     print 'Ignore the following error.'
  4530. end
  4531. else
  4532.     drop proc sp_pkeys
  4533. go
  4534.  
  4535. /*  Procedure for 6.0 and 6.50 servers */
  4536. CREATE PROCEDURE sp_pkeys(
  4537.                @table_name      sysname,
  4538.                @table_owner     sysname = null,
  4539.                @table_qualifier sysname = null )
  4540. as
  4541.     DECLARE @table_id           int
  4542.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4543.  
  4544.     if @table_qualifier is not null
  4545.     begin
  4546.         if db_name() <> @table_qualifier
  4547.         begin   /* If qualifier doesn't match current database */
  4548.             raiserror (15250, -1,-1)
  4549.             return
  4550.         end
  4551.     end
  4552.     if @table_owner is null
  4553.     begin   /* If unqualified table name */
  4554.         SELECT @full_table_name = @table_name
  4555.     end
  4556.     else
  4557.     begin   /* Qualified table name */
  4558.         if @table_owner = ''
  4559.         begin   /* If empty owner name */
  4560.             SELECT @full_table_name = @table_owner
  4561.         end
  4562.         else
  4563.         begin
  4564.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4565.         end
  4566.     end
  4567.     /*  Get Object ID */
  4568.     SELECT @table_id = object_id(@full_table_name)
  4569.  
  4570.     select
  4571.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4572.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4573.         TABLE_NAME = convert(sysname,o.name),
  4574.         COLUMN_NAME = convert(sysname,c.name),
  4575.         KEY_SEQ =
  4576.             case
  4577.                 when c.name = index_col(@full_table_name, i.indid,  1) then convert (smallint,1)
  4578.                 when c.name = index_col(@full_table_name, i.indid,  2) then convert (smallint,2)
  4579.                 when c.name = index_col(@full_table_name, i.indid,  3) then convert (smallint,3)
  4580.                 when c.name = index_col(@full_table_name, i.indid,  4) then convert (smallint,4)
  4581.                 when c.name = index_col(@full_table_name, i.indid,  5) then convert (smallint,5)
  4582.                 when c.name = index_col(@full_table_name, i.indid,  6) then convert (smallint,6)
  4583.                 when c.name = index_col(@full_table_name, i.indid,  7) then convert (smallint,7)
  4584.                 when c.name = index_col(@full_table_name, i.indid,  8) then convert (smallint,8)
  4585.                 when c.name = index_col(@full_table_name, i.indid,  9) then convert (smallint,9)
  4586.                 when c.name = index_col(@full_table_name, i.indid, 10) then convert (smallint,10)
  4587.                 when c.name = index_col(@full_table_name, i.indid, 11) then convert (smallint,11)
  4588.                 when c.name = index_col(@full_table_name, i.indid, 12) then convert (smallint,12)
  4589.                 when c.name = index_col(@full_table_name, i.indid, 13) then convert (smallint,13)
  4590.                 when c.name = index_col(@full_table_name, i.indid, 14) then convert (smallint,14)
  4591.                 when c.name = index_col(@full_table_name, i.indid, 15) then convert (smallint,15)
  4592.                 when c.name = index_col(@full_table_name, i.indid, 16) then convert (smallint,16)
  4593.             end,
  4594.         PK_NAME = convert(sysname,i.name)
  4595.     from
  4596.         sysindexes i, syscolumns c, sysobjects o
  4597.     where
  4598.         o.id = @table_id
  4599.         and o.id = c.id
  4600.         and o.id = i.id
  4601.         and (i.status & 0x800) = 0x800
  4602.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4603.              c.name = index_col (@full_table_name, i.indid,  2) or
  4604.              c.name = index_col (@full_table_name, i.indid,  3) or
  4605.              c.name = index_col (@full_table_name, i.indid,  4) or
  4606.              c.name = index_col (@full_table_name, i.indid,  5) or
  4607.              c.name = index_col (@full_table_name, i.indid,  6) or
  4608.              c.name = index_col (@full_table_name, i.indid,  7) or
  4609.              c.name = index_col (@full_table_name, i.indid,  8) or
  4610.              c.name = index_col (@full_table_name, i.indid,  9) or
  4611.              c.name = index_col (@full_table_name, i.indid, 10) or
  4612.              c.name = index_col (@full_table_name, i.indid, 11) or
  4613.              c.name = index_col (@full_table_name, i.indid, 12) or
  4614.              c.name = index_col (@full_table_name, i.indid, 13) or
  4615.              c.name = index_col (@full_table_name, i.indid, 14) or
  4616.              c.name = index_col (@full_table_name, i.indid, 15) or
  4617.              c.name = index_col (@full_table_name, i.indid, 16)
  4618.             )
  4619.     order by 1, 2, 3, 5
  4620. go
  4621.  
  4622. if (charindex('7.00', @@version) = 0 and
  4623.     charindex('8.00', @@version) = 0)
  4624. begin
  4625.     print ''
  4626.     print ''
  4627.     print 'Warning:'
  4628.     print 'you are installing the stored procedures '
  4629.     print 'on a pre 7.0 SQL Server.'
  4630.     print 'Ignore the following errors.'
  4631. end
  4632. else
  4633.     drop proc sp_pkeys
  4634. go
  4635.  
  4636. /*  Procedure for 7.0 & 8.0 servers */
  4637. CREATE PROCEDURE sp_pkeys(
  4638.                @table_name      sysname,
  4639.                @table_owner     sysname = null,
  4640.                @table_qualifier sysname = null )
  4641. as
  4642.     DECLARE @table_id       int
  4643.     DECLARE @full_table_name    nvarchar(255)
  4644.  
  4645.     if @table_qualifier is not null
  4646.     begin
  4647.         if db_name() <> @table_qualifier
  4648.         begin   /* If qualifier doesn't match current database */
  4649.             raiserror (15250, -1,-1)
  4650.             return
  4651.         end
  4652.     end
  4653.     if @table_owner is null
  4654.     begin   /* If unqualified table name */
  4655.         SELECT @full_table_name = quotename(@table_name)
  4656.     end
  4657.     else
  4658.     begin   /* Qualified table name */
  4659.         if @table_owner = ''
  4660.         begin   /* If empty owner name */
  4661.             SELECT @full_table_name = quotename(@table_owner)
  4662.         end
  4663.         else
  4664.         begin
  4665.             SELECT @full_table_name = quotename(@table_owner) +
  4666.                 '.' + quotename(@table_name)
  4667.         end
  4668.     end
  4669.     /*  Get Object ID */
  4670.     SELECT @table_id = object_id(@full_table_name)
  4671.  
  4672.     select
  4673.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4674.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4675.         TABLE_NAME = convert(sysname,o.name),
  4676.         COLUMN_NAME = convert(sysname,c.name),
  4677.         KEY_SEQ =
  4678.             case
  4679.                 when c.name = index_col(@full_table_name, i.indid,  1) then convert (smallint,1)
  4680.                 when c.name = index_col(@full_table_name, i.indid,  2) then convert (smallint,2)
  4681.                 when c.name = index_col(@full_table_name, i.indid,  3) then convert (smallint,3)
  4682.                 when c.name = index_col(@full_table_name, i.indid,  4) then convert (smallint,4)
  4683.                 when c.name = index_col(@full_table_name, i.indid,  5) then convert (smallint,5)
  4684.                 when c.name = index_col(@full_table_name, i.indid,  6) then convert (smallint,6)
  4685.                 when c.name = index_col(@full_table_name, i.indid,  7) then convert (smallint,7)
  4686.                 when c.name = index_col(@full_table_name, i.indid,  8) then convert (smallint,8)
  4687.                 when c.name = index_col(@full_table_name, i.indid,  9) then convert (smallint,9)
  4688.                 when c.name = index_col(@full_table_name, i.indid, 10) then convert (smallint,10)
  4689.                 when c.name = index_col(@full_table_name, i.indid, 11) then convert (smallint,11)
  4690.                 when c.name = index_col(@full_table_name, i.indid, 12) then convert (smallint,12)
  4691.                 when c.name = index_col(@full_table_name, i.indid, 13) then convert (smallint,13)
  4692.                 when c.name = index_col(@full_table_name, i.indid, 14) then convert (smallint,14)
  4693.                 when c.name = index_col(@full_table_name, i.indid, 15) then convert (smallint,15)
  4694.                 when c.name = index_col(@full_table_name, i.indid, 16) then convert (smallint,16)
  4695.             end,
  4696.         PK_NAME = convert(sysname,i.name)
  4697.     from
  4698.         sysindexes i, syscolumns c, sysobjects o --, syscolumns c1
  4699.     where
  4700.         o.id = @table_id
  4701.         and o.id = c.id
  4702.         and o.id = i.id
  4703.         and (i.status & 0x800) = 0x800
  4704.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4705.              c.name = index_col (@full_table_name, i.indid,  2) or
  4706.              c.name = index_col (@full_table_name, i.indid,  3) or
  4707.              c.name = index_col (@full_table_name, i.indid,  4) or
  4708.              c.name = index_col (@full_table_name, i.indid,  5) or
  4709.              c.name = index_col (@full_table_name, i.indid,  6) or
  4710.              c.name = index_col (@full_table_name, i.indid,  7) or
  4711.              c.name = index_col (@full_table_name, i.indid,  8) or
  4712.              c.name = index_col (@full_table_name, i.indid,  9) or
  4713.              c.name = index_col (@full_table_name, i.indid, 10) or
  4714.              c.name = index_col (@full_table_name, i.indid, 11) or
  4715.              c.name = index_col (@full_table_name, i.indid, 12) or
  4716.              c.name = index_col (@full_table_name, i.indid, 13) or
  4717.              c.name = index_col (@full_table_name, i.indid, 14) or
  4718.              c.name = index_col (@full_table_name, i.indid, 15) or
  4719.              c.name = index_col (@full_table_name, i.indid, 16)
  4720.             )
  4721.     order by 1, 2, 3, 5
  4722. go
  4723.  
  4724.  
  4725. grant execute on sp_pkeys to public
  4726. go
  4727.  
  4728. dump tran master with no_log
  4729. go
  4730.  
  4731. print 'creating sp_server_info'
  4732. go
  4733.  
  4734. create proc sp_server_info (
  4735.             @attribute_id  int = null)
  4736. as
  4737.     if @attribute_id is not null
  4738.         select *
  4739.         from master.dbo.spt_server_info
  4740.         where attribute_id = @attribute_id
  4741.     else
  4742.         select *
  4743.         from master.dbo.spt_server_info
  4744.         order by attribute_id
  4745. go
  4746.  
  4747. grant execute on sp_server_info to public
  4748. go
  4749.  
  4750. dump tran master with no_log
  4751. go
  4752.  
  4753. print 'creating sp_special_columns'
  4754. go
  4755.  
  4756. /*  Procedure for pre-6.0 server */
  4757. CREATE PROCEDURE sp_special_columns (
  4758.                  @table_name        varchar(32),
  4759.                  @table_owner       varchar(32) = null,
  4760.                  @table_qualifier   varchar(32) = null,
  4761.                  @col_type          char(1) = 'R',
  4762.                  @scope             char(1) = 'T',
  4763.                  @nullable          char(1) = 'U',
  4764.                  @ODBCVer           int = 2)
  4765. AS
  4766.     DECLARE @indid              int
  4767.     DECLARE @table_id           int
  4768.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4769.     DECLARE @scopeout           smallint
  4770.  
  4771.     if @col_type not in ('R','V') or @col_type is null
  4772.     begin
  4773.         raiserror 20002 '~~Rush_42~~'
  4774.         return
  4775.     end
  4776.  
  4777.     if @scope = 'C'
  4778.         select @scopeout = 0
  4779.     else if @scope = 'T'
  4780.         select @scopeout = 1
  4781.     else
  4782.     begin
  4783.         raiserror 20002 '~~Rush_43~~'
  4784.         return
  4785.     end
  4786.  
  4787.     if @nullable not in ('U','O') or @nullable is null
  4788.     begin
  4789.         raiserror 20002 '~~Rush_44~~'
  4790.         return
  4791.     end
  4792.  
  4793.     if @table_qualifier is not null
  4794.     begin
  4795.         if db_name() <> @table_qualifier
  4796.         begin /* If qualifier doesn't match current database */
  4797.             raiserror 20001 '~~Rush_5~~'
  4798.             return
  4799.         end
  4800.     end
  4801.     if @table_owner is null
  4802.     begin    /* If unqualified table name */
  4803.         SELECT @full_table_name = @table_name
  4804.     end
  4805.     else
  4806.     begin   /* Qualified table name */
  4807.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4808.     end
  4809.     /*  Get Object ID */
  4810.     SELECT @table_id = object_id(@full_table_name)
  4811.  
  4812.     if @col_type = 'V'
  4813.     BEGIN /* if ROWVER, just run that query */
  4814.         SELECT
  4815.             SCOPE = convert(smallint,NULL),
  4816.             COLUMN_NAME = convert(varchar(32),c.name),
  4817.             DATA_TYPE = convert(smallint, -3),
  4818.             TYPE_NAME = t.name,
  4819.             "PRECISION" = convert(int,8),
  4820.             LENGTH = convert(int,8),
  4821.             SCALE = convert(smallint, NULL),
  4822.             PSEUDO_COLUMN = convert(smallint,1)
  4823.         FROM
  4824.             systypes t, syscolumns c
  4825.         WHERE
  4826.             c.id = @table_id
  4827.             AND c.usertype = 80 /*  TIMESTAMP */
  4828.             AND t.usertype = 80 /*  TIMESTAMP */
  4829.         RETURN
  4830.     END
  4831.  
  4832.     /* ROWID, now find the id of the 'best' index for this table */
  4833.  
  4834.     IF @nullable = 'O'  /* Don't include any indexes that contain
  4835.                            nullable columns. */
  4836.  
  4837.             SELECT @indid = MIN(indid)
  4838.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4839.                 WHERE
  4840.                     i.status&2 = 2      /*  If Unique Index */
  4841.                     AND c.id = i.id
  4842.                     AND c2.id = c.id
  4843.                     AND c2.colid < i.keycnt + (i.status&16)/16
  4844.                     AND i.id = @table_id
  4845.                     AND indid > 0       /*  Eliminate Table Row */
  4846.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4847.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4848.  
  4849.     ELSE    /* Include indexes that are partially nullable. */
  4850.  
  4851.         SELECT @indid = MIN(indid)
  4852.             FROM sysindexes i
  4853.             WHERE
  4854.                 status&2 = 2        /*  If Unique Index */
  4855.                 AND id = @table_id
  4856.                 AND indid > 0       /*  Eliminate Table Row */
  4857.  
  4858.     SELECT
  4859.         SCOPE = @scopeout,
  4860.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4861.         d.DATA_TYPE,
  4862.         TYPE_NAME = t.name,
  4863.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4864.         LENGTH = isnull(d.length, convert(int,c.length)),
  4865.         SCALE = d.numeric_scale,
  4866.         PSEUDO_COLUMN = convert(smallint,1)
  4867.     FROM
  4868.         sysindexes x,
  4869.         syscolumns c,
  4870.         master.dbo.spt_datatype_info d,
  4871.         systypes t,
  4872.         syscolumns c2   /* Self-join to generate list of index columns and */
  4873.                         /* to extract datatype names */
  4874.     WHERE
  4875.         x.id = @table_id
  4876.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4877.         AND c.id = x.id
  4878.         AND c2.id = x.id
  4879.         AND c2.colid < keycnt+(x.status&16)/16
  4880.         AND x.indid = @indid
  4881.         AND t.type = d.ss_dtype
  4882.         AND c.length = d.fixlen
  4883.         AND c.usertype = t.usertype
  4884.  
  4885. go
  4886.  
  4887. if (charindex('6.00', @@version) = 0 and
  4888.     charindex('6.50', @@version) = 0 and
  4889.     charindex('7.00', @@version) = 0 and
  4890.     charindex('8.00', @@version) = 0)
  4891. begin
  4892.     print ''
  4893.     print ''
  4894.     print 'Warning:'
  4895.     print 'you are installing the stored procedures '
  4896.     print 'on a pre 6.0 SQL Server.'
  4897.     print 'Ignore the following errors.'
  4898. end
  4899. else
  4900.     drop proc sp_special_columns
  4901. go
  4902.  
  4903. /*  Procedure for 6.0 and 6.50 servers */
  4904. CREATE PROCEDURE sp_special_columns (
  4905.                  @table_name        varchar(32),
  4906.                  @table_owner       varchar(32) = null,
  4907.                  @table_qualifier   varchar(32) = null,
  4908.                  @col_type          char(1) = 'R',
  4909.                  @scope             char(1) = 'T',
  4910.                  @nullable          char(1) = 'U',
  4911.                  @ODBCVer           int = 2)
  4912. AS
  4913.     DECLARE @indid              int
  4914.     DECLARE @table_id           int
  4915.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4916.     DECLARE @scopeout           smallint
  4917.  
  4918.     if @col_type not in ('R','V') or @col_type is null
  4919.     begin
  4920.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4921.         return
  4922.     end
  4923.  
  4924.     if @scope = 'C'
  4925.         select @scopeout = 0
  4926.     else if @scope = 'T'
  4927.         select @scopeout = 1
  4928.     else
  4929.     begin
  4930.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4931.         return
  4932.     end
  4933.  
  4934.     if @nullable not in ('U','O') or @nullable is null
  4935.     begin
  4936.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4937.         return
  4938.     end
  4939.  
  4940.     if @table_qualifier is not null
  4941.     begin
  4942.         if db_name() <> @table_qualifier
  4943.         begin /* If qualifier doesn't match current database */
  4944.             raiserror (15250, -1,-1)
  4945.             return
  4946.         end
  4947.     end
  4948.     if @table_owner is null
  4949.     begin    /* If unqualified table name */
  4950.         SELECT @full_table_name = @table_name
  4951.     end
  4952.     else
  4953.     begin    /* Qualified table name */
  4954.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4955.     end
  4956.     /*  Get Object ID */
  4957.     SELECT @table_id = object_id(@full_table_name)
  4958.  
  4959.     if @col_type = 'V'
  4960.     BEGIN /* if ROWVER, just run that query */
  4961.         SELECT
  4962.             SCOPE = convert(smallint,NULL),
  4963.             COLUMN_NAME = convert(varchar(32),c.name),
  4964.             DATA_TYPE = convert(smallint, -2),
  4965.             TYPE_NAME = t.name,
  4966.             "PRECISION" = convert(int,8),
  4967.             LENGTH = convert(int,8),
  4968.             SCALE = convert(smallint, NULL),
  4969.             PSEUDO_COLUMN = convert(smallint,1)
  4970.         FROM
  4971.             systypes t, syscolumns c
  4972.         WHERE
  4973.             c.id = @table_id
  4974.             AND c.usertype = 80 /*  TIMESTAMP */
  4975.             AND t.usertype = 80 /*  TIMESTAMP */
  4976.         RETURN
  4977.     END
  4978.  
  4979.     /* ROWID, now find the id of the 'best' index for this table */
  4980.  
  4981.     IF @nullable = 'O'  /* Don't include any indexes that contain
  4982.                            nullable columns. */
  4983.  
  4984.         SELECT @indid = MIN(indid)
  4985.             FROM sysindexes i,syscolumns c,syscolumns c2
  4986.             WHERE
  4987.                 i.status&2 = 2      /*  If Unique Index */
  4988.                 AND c.id = i.id
  4989.                 AND c2.id = c.id
  4990.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4991.                 AND i.id = @table_id
  4992.                 AND indid > 0       /*  Eliminate Table Row */
  4993.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4994.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4995.  
  4996.     ELSE    /* Include indexes that are partially nullable. */
  4997.  
  4998.         SELECT @indid = MIN(indid)
  4999.             FROM sysindexes i
  5000.             WHERE
  5001.                 status&2 = 2        /*  If Unique Index */
  5002.                 AND id = @table_id
  5003.                 AND indid > 0       /*  Eliminate Table Row */
  5004.  
  5005.     SELECT
  5006.         SCOPE = @scopeout,
  5007.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  5008.         d.DATA_TYPE,
  5009.         convert(varchar(32),case
  5010.             when (t.usertype > 100 or t.usertype in (18,80))
  5011.                 then t.name
  5012.             else d.TYPE_NAME
  5013.         end) TYPE_NAME,
  5014.         convert(int,case
  5015.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5016.             else isnull(convert(int,c.prec), 2147483647)
  5017.         end) "PRECISION",
  5018.         convert(int,case
  5019.             when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  5020.                 convert(int,c.prec+2)
  5021.             else
  5022.                 isnull(d.length, c.length)
  5023.         end) LENGTH,
  5024.         SCALE = convert(smallint, c.scale),
  5025.         PSEUDO_COLUMN = convert(smallint,1)
  5026.     FROM
  5027.         sysindexes x,
  5028.         syscolumns c,
  5029.         master.dbo.spt_datatype_info d,
  5030.         systypes t,
  5031.         syscolumns c2   /* Self-join to generate list of index columns and */
  5032.                         /* to extract datatype names */
  5033.     WHERE
  5034.         x.id = @table_id
  5035.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5036.         AND c.id = x.id
  5037.         AND c2.id = x.id
  5038.         AND c2.colid < x.keycnt+(x.status&16)/16
  5039.         AND x.indid = @indid
  5040.         AND t.type = d.ss_dtype
  5041.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5042.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  5043.         AND c.usertype = t.usertype
  5044. go
  5045.  
  5046. if (charindex('7.00', @@version) = 0 and
  5047.     charindex('8.00', @@version) = 0)
  5048. begin
  5049.     print ''
  5050.     print ''
  5051.     print 'Warning:'
  5052.     print 'you are installing the stored procedures '
  5053.     print 'on a pre 7.0 SQL Server.'
  5054.     print 'Ignore the following errors.'
  5055. end
  5056. else
  5057.     drop proc sp_special_columns
  5058. go
  5059.  
  5060. /*  Procedure for 7.0 servers */
  5061. CREATE PROCEDURE sp_special_columns (
  5062.                  @table_name        sysname,
  5063.                  @table_owner       sysname = null,
  5064.                  @table_qualifier   sysname = null,
  5065.                  @col_type          char(1) = 'R',
  5066.                  @scope             char(1) = 'T',
  5067.                  @nullable          char(1) = 'U',
  5068.                  @ODBCVer           int = 2)
  5069. AS
  5070.     DECLARE @indid              int
  5071.     DECLARE @table_id           int
  5072.     DECLARE @full_table_name    nvarchar(257)
  5073.     DECLARE @scopeout           smallint
  5074.  
  5075.     if @col_type not in ('R','V') or @col_type is null
  5076.     begin
  5077.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5078.         return
  5079.     end
  5080.  
  5081.     if @scope = 'C'
  5082.         select @scopeout = 0
  5083.     else if @scope = 'T'
  5084.         select @scopeout = 1
  5085.     else
  5086.     begin
  5087.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5088.         return
  5089.     end
  5090.  
  5091.     if @nullable not in ('U','O') or @nullable is null
  5092.     begin
  5093.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5094.         return
  5095.     end
  5096.  
  5097.     if @table_qualifier is not null
  5098.     begin
  5099.         if db_name() <> @table_qualifier
  5100.         begin /* If qualifier doesn't match current database */
  5101.             raiserror (15250, -1,-1)
  5102.             return
  5103.         end
  5104.     end
  5105.     if @table_owner is null
  5106.     begin    /* If unqualified table name */
  5107.         SELECT @full_table_name = quotename(@table_name)
  5108.     end
  5109.     else
  5110.     begin   /* Qualified table name */
  5111.         if @table_owner = ''
  5112.         begin   /* If empty owner name */
  5113.             SELECT @full_table_name = quotename(@table_owner)
  5114.         end
  5115.         else
  5116.         begin
  5117.             SELECT @full_table_name = quotename(@table_owner) +
  5118.                 '.' + quotename(@table_name)
  5119.         end
  5120.     end
  5121.     /*  Get Object ID */
  5122.     SELECT @table_id = object_id(@full_table_name)
  5123.  
  5124.     if @col_type = 'V'
  5125.     BEGIN /* if ROWVER, just run that query */
  5126.         SELECT
  5127.             SCOPE = convert(smallint,NULL),
  5128.             COLUMN_NAME = convert(sysname,c.name),
  5129.             DATA_TYPE = convert(smallint, -2),
  5130.             TYPE_NAME = t.name,
  5131.             "PRECISION" = convert(int,8),
  5132.             LENGTH = convert(int,8),
  5133.             SCALE = convert(smallint, NULL),
  5134.             PSEUDO_COLUMN = convert(smallint,1)
  5135.         FROM
  5136.             systypes t, syscolumns c
  5137.         WHERE
  5138.             not (@table_id is null)
  5139.             AND c.id = @table_id
  5140.             AND t.name = 'timestamp'    /*  TIMESTAMP  */
  5141.             AND t.xtype = c.xtype       
  5142.             AND t.xusertype = c.xusertype   
  5143.         RETURN
  5144.     END
  5145.  
  5146.     /* ROWID, now find the id of the 'best' index for this table */
  5147.  
  5148.     IF @nullable = 'O'  /* Don't include any indexes that contain
  5149.                            nullable columns. */
  5150.  
  5151.         SELECT @indid = MIN(indid)
  5152.             FROM sysindexes x, syscolumns c, syscolumns c2
  5153.             WHERE
  5154.                 not (@table_id is null)
  5155.                 AND x.status&2 = 2      /*  If Unique Index */
  5156.                 AND c.id = x.id
  5157.                 AND c2.id = c.id
  5158.                 AND c2.colid < x.keycnt + (x.status&16)/16
  5159.                 AND x.id = @table_id
  5160.                 AND indid > 0       /*  Eliminate Table Row */
  5161.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5162.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5163.  
  5164.     ELSE    /* Include indexes that are partially nullable. */
  5165.  
  5166.         SELECT @indid = MIN(indid)
  5167.             FROM sysindexes x
  5168.             WHERE
  5169.                 not (@table_id is null)
  5170.                 AND status&2 = 2        /*  If Unique Index */
  5171.                 AND id = @table_id
  5172.                 AND indid > 0       /*  Eliminate Table Row */
  5173.  
  5174.     SELECT
  5175.         SCOPE = @scopeout,
  5176.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5177.         d.DATA_TYPE,
  5178.         convert(sysname,case
  5179.             when t.xusertype > 255 then t.name
  5180.             else d.TYPE_NAME
  5181.         end) TYPE_NAME,
  5182.         convert(int,case
  5183.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5184.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5185.         end) "PRECISION",
  5186.         convert(int,case
  5187.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5188.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5189.             else isnull(d.length, c.length)
  5190.         end) LENGTH,
  5191.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5192.         PSEUDO_COLUMN = convert(smallint,1)
  5193.     FROM
  5194.         sysindexes x,
  5195.         syscolumns c,
  5196.         master.dbo.spt_datatype_info d,
  5197.         systypes t,
  5198.         syscolumns c2   /* Self-join to generate list of index columns and */
  5199.                         /* to extract datatype names */
  5200.     WHERE
  5201.         not (@table_id is null)
  5202.         AND x.id = @table_id
  5203.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5204.         AND c.id = x.id
  5205.         AND c2.id = x.id
  5206.         AND c2.colid < x.keycnt + (x.status&16)/16
  5207.         AND x.indid = @indid
  5208.         AND t.xtype = d.ss_dtype
  5209.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5210.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5211.         AND c.xusertype = t.xusertype
  5212. go
  5213.  
  5214. if (charindex('8.00', @@version) = 0)
  5215. begin
  5216.     print ''
  5217.     print ''
  5218.     print 'Warning:'
  5219.     print 'you are installing the stored procedures '
  5220.     print 'on a pre 8.0 SQL Server.'
  5221.     print 'Ignore the following errors.'
  5222. end
  5223. else
  5224.     drop proc sp_special_columns
  5225. go
  5226.  
  5227. /*  Procedure for 8.0 and later servers */
  5228. CREATE PROCEDURE sp_special_columns (
  5229.                  @table_name        sysname,
  5230.                  @table_owner       sysname = null,
  5231.                  @table_qualifier   sysname = null,
  5232.                  @col_type          char(1) = 'R',
  5233.                  @scope             char(1) = 'T',
  5234.                  @nullable          char(1) = 'U',
  5235.                  @ODBCVer           int = 2)
  5236. AS
  5237.     DECLARE @indid              int
  5238.     DECLARE @table_id           int
  5239.     DECLARE @full_table_name    nvarchar(257)
  5240.     DECLARE @scopeout           smallint
  5241.  
  5242.     if @col_type not in ('R','V') or @col_type is null
  5243.     begin
  5244.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5245.         return
  5246.     end
  5247.  
  5248.     if @scope = 'C'
  5249.         select @scopeout = 0
  5250.     else if @scope = 'T'
  5251.         select @scopeout = 1
  5252.     else
  5253.     begin
  5254.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5255.         return
  5256.     end
  5257.  
  5258.     if @nullable not in ('U','O') or @nullable is null
  5259.     begin
  5260.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5261.         return
  5262.     end
  5263.  
  5264.     if @table_qualifier is not null
  5265.     begin
  5266.         if db_name() <> @table_qualifier
  5267.         begin /* If qualifier doesn't match current database */
  5268.             raiserror (15250, -1,-1)
  5269.             return
  5270.         end
  5271.     end
  5272.     if @table_owner is null
  5273.     begin    /* If unqualified table name */
  5274.         SELECT @full_table_name = quotename(@table_name)
  5275.     end
  5276.     else
  5277.     begin   /* Qualified table name */
  5278.         if @table_owner = ''
  5279.         begin   /* If empty owner name */
  5280.             SELECT @full_table_name = quotename(@table_owner)
  5281.         end
  5282.         else
  5283.         begin
  5284.             SELECT @full_table_name = quotename(@table_owner) +
  5285.                 '.' + quotename(@table_name)
  5286.         end
  5287.     end
  5288.     /*  Get Object ID */
  5289.     SELECT @table_id = object_id(@full_table_name)
  5290.  
  5291.     if @col_type = 'V'
  5292.     BEGIN /* if ROWVER, just run that query */
  5293.         SELECT
  5294.             SCOPE = convert(smallint,NULL),
  5295.             COLUMN_NAME = convert(sysname,c.name),
  5296.             DATA_TYPE = convert(smallint, -2),
  5297.             TYPE_NAME = t.name,
  5298.             "PRECISION" = convert(int,8),
  5299.             LENGTH = convert(int,8),
  5300.             SCALE = convert(smallint, NULL),
  5301.             PSEUDO_COLUMN = convert(smallint,1)
  5302.         FROM
  5303.             systypes t, syscolumns c
  5304.         WHERE
  5305.             not (@table_id is null)
  5306.             AND c.id = @table_id
  5307.             AND t.name = 'timestamp'    /*  TIMESTAMP  */
  5308.             AND t.xtype = c.xtype       
  5309.             AND t.xusertype = c.xusertype   
  5310.         RETURN
  5311.     END
  5312.  
  5313.     /* ROWID, now find the id of the 'best' index for this table */
  5314.  
  5315.     IF @nullable = 'O'  /* Don't include any indexes that contain
  5316.                            nullable columns. */
  5317.  
  5318.         SELECT @indid = MIN(indid)
  5319.             FROM sysindexes x, syscolumns c, syscolumns c2
  5320.             WHERE
  5321.                 not (@table_id is null)
  5322.                 AND x.status&2 = 2      /*  If Unique Index */
  5323.                 AND c.id = x.id
  5324.                 AND c2.id = c.id
  5325.                 AND c2.colid < x.keycnt + (x.status&18)/18
  5326.                 AND x.id = @table_id
  5327.                 AND indid > 0       /*  Eliminate Table Row */
  5328.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5329.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5330.  
  5331.     ELSE    /* Include indexes that are partially nullable. */
  5332.  
  5333.         SELECT @indid = MIN(indid)
  5334.             FROM sysindexes x
  5335.             WHERE
  5336.                 not (@table_id is null)
  5337.                 AND status&2 = 2        /*  If Unique Index */
  5338.                 AND id = @table_id
  5339.                 AND indid > 0       /*  Eliminate Table Row */
  5340.  
  5341.     SELECT
  5342.         SCOPE = @scopeout,
  5343.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5344.         d.DATA_TYPE,
  5345.         convert(sysname,case
  5346.             when t.xusertype > 255 then t.name
  5347.             else d.TYPE_NAME collate database_default
  5348.         end) TYPE_NAME,
  5349.         convert(int,case
  5350.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5351.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5352.         end) "PRECISION",
  5353.         convert(int,case
  5354.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5355.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5356.             else isnull(d.length, c.length)
  5357.         end) LENGTH,
  5358.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5359.         PSEUDO_COLUMN = convert(smallint,1)
  5360.     FROM
  5361.         sysindexes x,
  5362.         syscolumns c,
  5363.         master.dbo.spt_datatype_info d,
  5364.         systypes t,
  5365.         syscolumns c2   /* Self-join to generate list of index columns and */
  5366.                         /* to extract datatype names */
  5367.     WHERE
  5368.         not (@table_id is null)
  5369.         AND x.id = @table_id
  5370.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5371.         AND c.id = x.id
  5372.         AND c2.id = x.id
  5373.         AND c2.colid < x.keycnt + (x.status&16)/16
  5374.         AND x.indid = @indid
  5375.         AND t.xtype = d.ss_dtype
  5376.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5377.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5378.         AND c.xusertype = t.xusertype
  5379. go
  5380.  
  5381. grant execute on sp_special_columns to public
  5382. go
  5383.  
  5384. dump tran master with no_log
  5385. go
  5386.  
  5387. print 'creating sp_sproc_columns'
  5388. go
  5389.  
  5390. /*  Procedure for pre-6.0 server */
  5391. CREATE PROCEDURE sp_sproc_columns (
  5392.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  5393.                  @procedure_owner       varchar(96) = null,
  5394.                  @procedure_qualifier   varchar(32) = null,
  5395.                  @column_name           varchar(96) = null,
  5396.                  @ODBCVer               int = 2)
  5397. AS
  5398.     DECLARE @group_num_lower smallint
  5399.     DECLARE @group_num_upper smallint
  5400.     DECLARE @semi_position int
  5401.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  5402.     DECLARE @procedure_id int
  5403.  
  5404.     if @column_name is null /*  If column name not supplied, match all */
  5405.         select @column_name = '%'
  5406.     if @procedure_qualifier is not null
  5407.     begin
  5408.         if db_name() <> @procedure_qualifier
  5409.         begin
  5410.             if @procedure_qualifier = ''
  5411.             begin
  5412.                 /* in this case, we need to return an empty result set */
  5413.                 /* because the user has requested a database with an empty name */
  5414.                 select @procedure_name = ''
  5415.                 select @procedure_owner = ''
  5416.             end
  5417.             else
  5418.             begin   /* If qualifier doesn't match current database */
  5419.                 raiserror 20001 '~~Rush_51~~'
  5420.                 return
  5421.             end
  5422.         end
  5423.     end
  5424.  
  5425.     if @procedure_name is null
  5426.     begin   /*  If procedure name not supplied, match all */
  5427.         select @procedure_name = '%'
  5428.     end
  5429.  
  5430.     /* first we need to extract the procedure group number, if one exists */
  5431.     select @semi_position = charindex(';',@procedure_name)
  5432.     if (@semi_position > 0)
  5433.     begin   /* If group number separator (;) found */
  5434.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5435.         select @group_num_upper = @group_num_lower
  5436.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5437.     end
  5438.     else
  5439.     begin   /* No group separator, so default to group number of 1 */
  5440.         select @group_num_lower = 1
  5441.         select @group_num_upper = 32767         
  5442.     end
  5443.  
  5444.     if @procedure_owner is null
  5445.     begin   /* If unqualified procedure name */
  5446.         SELECT @full_procedure_name = @procedure_name
  5447.     end
  5448.     else
  5449.     begin   /* Qualified procedure name */
  5450.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5451.     end
  5452.  
  5453.     /*  Get Object ID */
  5454.     SELECT @procedure_id = object_id(@full_procedure_name)
  5455.     if ((charindex('%',@full_procedure_name) = 0) and
  5456.         (charindex('_',@full_procedure_name) = 0) and
  5457.         @procedure_id <> 0)
  5458.     begin
  5459.         /* this block is for the case where there is no pattern
  5460.             matching required for the procedure name */
  5461.         SELECT
  5462.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5463.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5464.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5465.             COLUMN_NAME = convert(varchar(32),c.name),
  5466.             COLUMN_TYPE = convert(smallint, 0),
  5467.             d.DATA_TYPE,
  5468.             TYPE_NAME = t.name,
  5469.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5470.             LENGTH = isnull(d.length, convert(int,c.length)),
  5471.             SCALE = d.numeric_scale,
  5472.             d.RADIX,
  5473.             d.NULLABLE,
  5474.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5475.             COLUMN_DEF = convert(varchar(255),null),
  5476.             d.SQL_DATA_TYPE,
  5477.             d.SQL_DATETIME_SUB,
  5478.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5479.             ORDINAL_POSITION = convert(int, c.colid),
  5480.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5481.             SS_DATA_TYPE = c.type
  5482.         FROM
  5483.             syscolumns c,
  5484.             sysobjects o,
  5485.             master.dbo.spt_datatype_info d,
  5486.             systypes t
  5487.         WHERE
  5488.             o.id = @procedure_id
  5489.             AND c.id = o.id
  5490.             AND t.type = d.ss_dtype
  5491.             AND c.length = isnull(d.fixlen, c.length)
  5492.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5493.             AND c.usertype = t.usertype
  5494.             AND c.name like @column_name
  5495.             AND c.number between @group_num_lower and @group_num_upper
  5496.         UNION ALL
  5497.         SELECT         /* return value row*/
  5498.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5499.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5500.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5501.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5502.             COLUMN_TYPE = convert(smallint, 5),
  5503.             DATA_TYPE = convert(smallint, 4),
  5504.             TYPE_NAME = convert(varchar(32),'int'),
  5505.             "PRECISION" = convert(int,10),
  5506.             LENGTH = convert(int,4),
  5507.             SCALE = convert(smallint,0),
  5508.             RADIX = convert(smallint,10),
  5509.             NULLABLE = convert(smallint,0),
  5510.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5511.             COLUMN_DEF = convert(varchar(255),NULL),
  5512.             SQL_DATA_TYPE = convert(smallint, 4),
  5513.             SQL_DATETIME_SUB = convert(smallint,null),
  5514.             CHAR_OCTET_LENGTH = convert(int,null),
  5515.             ORDINAL_POSITION = convert(int,0),
  5516.             IS_NULLABLE = convert(varchar(254),'NO'),
  5517.             SS_DATA_TYPE = convert(tinyint,56)
  5518.         FROM
  5519.             syscomments c, sysobjects o
  5520.         WHERE
  5521.             o.id = @procedure_id
  5522.             AND c.id = o.id
  5523.             AND c.colid = 1
  5524.             AND o.type = 'P'                        /* Just Procedures */
  5525.             AND '@RETURN_VALUE' like @column_name
  5526.             AND c.number between @group_num_lower and @group_num_upper      
  5527.         ORDER BY 1, 2, 3, 18
  5528.     end
  5529.     else
  5530.     begin
  5531.         /* this block is for the case where there IS pattern
  5532.             matching done on the procedure name */
  5533.         if @procedure_owner is null
  5534.             select @procedure_owner = '%'
  5535.         SELECT
  5536.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5537.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5538.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5539.             COLUMN_NAME = convert(varchar(32),c.name),
  5540.             COLUMN_TYPE = convert(smallint, 0),
  5541.             d.DATA_TYPE,
  5542.             TYPE_NAME = t.name,
  5543.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5544.             LENGTH = isnull(d.length, convert(int,c.length)),
  5545.             SCALE = d.numeric_scale,
  5546.             d.RADIX,
  5547.             d.NULLABLE,
  5548.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5549.             COLUMN_DEF = convert(varchar(255),null),
  5550.             d.SQL_DATA_TYPE,
  5551.             d.SQL_DATETIME_SUB,
  5552.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5553.             ORDINAL_POSITION = convert(int, c.colid),
  5554.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5555.             SS_DATA_TYPE = c.type
  5556.         FROM
  5557.             syscolumns c,
  5558.             sysobjects o,
  5559.             master.dbo.spt_datatype_info d,
  5560.             systypes t
  5561.         WHERE
  5562.             o.name like @procedure_name
  5563.             AND user_name(o.uid) like @procedure_owner
  5564.             AND o.id = c.id
  5565.             AND t.type = d.ss_dtype
  5566.             AND c.length = isnull(d.fixlen, c.length)
  5567.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5568.             AND c.usertype = t.usertype
  5569.             AND o.type = 'P'                            /* Just Procedures */
  5570.             AND c.name like @column_name
  5571.             AND c.number between @group_num_lower and @group_num_upper
  5572.         UNION ALL
  5573.         SELECT         /* return value row*/
  5574.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5575.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5576.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5577.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5578.             COLUMN_TYPE = convert(smallint, 5),
  5579.             DATA_TYPE = convert(smallint, 4),
  5580.             TYPE_NAME = convert(varchar(32),'int'),
  5581.             "PRECISION" = convert(int,10),
  5582.             LENGTH = convert(int,4),
  5583.             SCALE = convert(smallint,0),
  5584.             RADIX = convert(smallint,10),
  5585.             NULLABLE = convert(smallint,0),
  5586.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5587.             COLUMN_DEF = convert(varchar(255),NULL),
  5588.             SQL_DATA_TYPE = convert(smallint, 4),
  5589.             SQL_DATETIME_SUB = convert(smallint,null),
  5590.             CHAR_OCTET_LENGTH = convert(int,null),
  5591.             ORDINAL_POSITION = convert(int,0),
  5592.             IS_NULLABLE = convert(varchar(254),'NO'),
  5593.             SS_DATA_TYPE = convert(tinyint,56)
  5594.         FROM
  5595.             syscomments c, sysobjects o
  5596.         WHERE
  5597.             o.name like @procedure_name
  5598.             AND user_name(o.uid) like @procedure_owner
  5599.             AND c.id = o.id
  5600.             AND c.colid = 1
  5601.             AND o.type = 'P'                        /* Just Procedures */
  5602.             AND '@RETURN_VALUE' like @column_name
  5603.             AND c.number between @group_num_lower and @group_num_upper
  5604.         ORDER BY 1, 2, 3, 18
  5605.     end
  5606. go
  5607.  
  5608. if (charindex('6.00', @@version) = 0 and
  5609.     charindex('6.50', @@version) = 0 and
  5610.     charindex('7.00', @@version) = 0 and
  5611.     charindex('8.00', @@version) = 0)
  5612. begin
  5613.     print ''
  5614.     print ''
  5615.     print 'Warning:'
  5616.     print 'you are installing the stored procedures '
  5617.     print 'on a pre 6.0 SQL Server.'
  5618.     print 'Ignore the following error.'
  5619. end
  5620. else
  5621.     drop proc sp_sproc_columns
  5622. go
  5623.  
  5624. /*  Procedure for 6.0 and 6.50 servers */
  5625. CREATE PROCEDURE sp_sproc_columns (
  5626.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  5627.                  @procedure_owner       varchar(96) = null,
  5628.                  @procedure_qualifier   varchar(32) = null,
  5629.                  @column_name           varchar(96) = null,
  5630.                  @ODBCVer               int = 2)
  5631. AS
  5632.     DECLARE @group_num_lower smallint
  5633.     DECLARE @group_num_upper smallint
  5634.     DECLARE @semi_position int
  5635.     DECLARE @full_procedure_name    varchar(205)
  5636.     DECLARE @procedure_id int
  5637.  
  5638.     if @column_name is null /*  If column name not supplied, match all */
  5639.         select @column_name = '%'
  5640.     if @procedure_qualifier is not null
  5641.     begin
  5642.         if db_name() <> @procedure_qualifier
  5643.         begin
  5644.             if @procedure_qualifier = ''
  5645.             begin
  5646.                 /* in this case, we need to return an empty result set */
  5647.                 /* because the user has requested a database with an empty name */
  5648.                 select @procedure_name = ''
  5649.                 select @procedure_owner = ''
  5650.             end
  5651.             else
  5652.             begin   /* If qualifier doesn't match current database */
  5653.                 raiserror (15250, -1,-1)
  5654.                 return
  5655.             end
  5656.         end
  5657.     end
  5658.  
  5659.     if @procedure_name is null
  5660.     begin   /*  If procedure name not supplied, match all */
  5661.         select @procedure_name = '%'
  5662.     end
  5663.  
  5664.     /* first we need to extract the procedure group number, if one exists */
  5665.     select @semi_position = charindex(';',@procedure_name)
  5666.     if (@semi_position > 0)
  5667.     begin   /* If group number separator (;) found */
  5668.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5669.         select @group_num_upper = @group_num_lower
  5670.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5671.     end
  5672.     else
  5673.     begin   /* No group separator, so default to group number of 1 */
  5674.         select @group_num_lower = 1
  5675.         select @group_num_upper = 32767         
  5676.     end
  5677.  
  5678.     if @procedure_owner is null
  5679.     begin   /* If unqualified procedure name */
  5680.         SELECT @full_procedure_name = @procedure_name
  5681.     end
  5682.     else
  5683.     begin   /* Qualified procedure name */
  5684.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5685.     end
  5686.  
  5687.     /*  Get Object ID */
  5688.     SELECT @procedure_id = object_id(@full_procedure_name)
  5689.     if ((charindex('%',@full_procedure_name) = 0) and
  5690.         (charindex('[',@full_procedure_name) = 0) and
  5691.         (charindex('_',@full_procedure_name) = 0) and
  5692.         @procedure_id <> 0)
  5693.     begin
  5694.         /* this block is for the case where there is no pattern
  5695.             matching required for the procedure name */
  5696.         SELECT
  5697.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5698.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5699.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5700.             COLUMN_NAME = convert(varchar(32),c.name),
  5701.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5702.             d.DATA_TYPE,
  5703.             TYPE_NAME = t.name,
  5704.             convert(int,case
  5705.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5706.                 else isnull(convert(int,c.prec), 2147483647)
  5707.             end) "PRECISION",
  5708.             convert(int,case
  5709.                 when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  5710.                     c.prec+2
  5711.                 else
  5712.                     isnull(d.length, c.length)
  5713.             end) LENGTH,
  5714.             SCALE = convert(smallint, c.scale),
  5715.             d.RADIX,
  5716.             d.NULLABLE,
  5717.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5718.             COLUMN_DEF = convert(varchar(255),NULL),
  5719.             d.SQL_DATA_TYPE,
  5720.             d.SQL_DATETIME_SUB,
  5721.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5722.             ORDINAL_POSITION = convert(int, c.colid),
  5723.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5724.             SS_DATA_TYPE = c.type
  5725.         FROM
  5726.             syscolumns c,
  5727.             sysobjects o,
  5728.             master.dbo.spt_datatype_info d,
  5729.             systypes t
  5730.         WHERE
  5731.             o.id = @procedure_id
  5732.             AND c.id = o.id
  5733.             AND c.type = d.ss_dtype
  5734.             AND c.length = isnull(d.fixlen, c.length)
  5735.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5736.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5737.             AND c.usertype = t.usertype
  5738.             AND c.name like @column_name
  5739.             AND c.number between @group_num_lower and @group_num_upper
  5740.         UNION ALL
  5741.         SELECT         /* return value row*/
  5742.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5743.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5744.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5745.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5746.             COLUMN_TYPE = convert(smallint, 5),
  5747.             DATA_TYPE = convert(smallint, 4),
  5748.             TYPE_NAME = convert(varchar(32),'int'),
  5749.             "PRECISION" = convert(int,10),
  5750.             LENGTH = convert(int,4),
  5751.             SCALE = convert(smallint,0),
  5752.             RADIX = convert(smallint,10),
  5753.             NULLABLE = convert(smallint,0),
  5754.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5755.             COLUMN_DEF = convert(varchar(255),NULL),
  5756.             SQL_DATA_TYPE = convert(smallint, 4),
  5757.             SQL_DATETIME_SUB = convert(smallint,null),
  5758.             CHAR_OCTET_LENGTH = convert(int,null),
  5759.             ORDINAL_POSITION = convert(int,0),
  5760.             IS_NULLABLE = convert(varchar(254),'NO'),
  5761.             SS_DATA_TYPE = convert(tinyint,56)
  5762.         FROM
  5763.             syscomments c, sysobjects o
  5764.         WHERE
  5765.             o.id = @procedure_id
  5766.             AND c.id = o.id
  5767.             AND c.colid = 1
  5768.             AND o.type = 'P'                        /* Just Procedures */
  5769.             AND '@RETURN_VALUE' like @column_name
  5770.             AND c.number between @group_num_lower and @group_num_upper
  5771.         ORDER BY 1, 2, 3, 18
  5772.     end
  5773.     else
  5774.     begin
  5775.         /* this block is for the case where there IS pattern
  5776.             matching done on the procedure name */
  5777.         if @procedure_owner is null
  5778.             select @procedure_owner = '%'
  5779.         SELECT
  5780.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5781.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5782.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5783.             COLUMN_NAME = convert(varchar(32),c.name),
  5784.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5785.             d.DATA_TYPE,
  5786.             TYPE_NAME = t.name,
  5787.             convert(int,case
  5788.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5789.                 else isnull(convert(int,c.prec), 2147483647)
  5790.             end) "PRECISION",
  5791.             convert(int,case
  5792.                 when d.ss_dtype IN (106, 108, 55, 63) then  /* decimal/numeric types */
  5793.                     c.prec+2
  5794.                 else
  5795.                     isnull(d.length, c.length)
  5796.             end) LENGTH,
  5797.             SCALE = convert(smallint, c.scale),
  5798.             d.RADIX,
  5799.             d.NULLABLE,
  5800.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5801.             COLUMN_DEF = convert(varchar(255),NULL),
  5802.             d.SQL_DATA_TYPE,
  5803.             d.SQL_DATETIME_SUB,
  5804.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5805.             ORDINAL_POSITION = convert(int, c.colid),
  5806.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5807.             SS_DATA_TYPE = c.type
  5808.         FROM
  5809.             syscolumns c,
  5810.             sysobjects o,
  5811.             master.dbo.spt_datatype_info d,
  5812.             systypes t
  5813.         WHERE
  5814.             o.name like @procedure_name
  5815.             AND user_name(o.uid) like @procedure_owner
  5816.             AND o.id = c.id
  5817.             AND c.type = d.ss_dtype
  5818.             AND c.length = isnull(d.fixlen, c.length)
  5819.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5820.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5821.             AND c.usertype = t.usertype
  5822.             AND o.type = 'P'                            /* Just Procedures */
  5823.             AND c.name like @column_name
  5824.             AND c.number between @group_num_lower and @group_num_upper
  5825.         UNION ALL
  5826.         SELECT         /* return value row*/
  5827.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5828.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5829.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5830.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5831.             COLUMN_TYPE = convert(smallint, 5),
  5832.             DATA_TYPE = convert(smallint, 4),
  5833.             TYPE_NAME = convert(varchar(32),'int'),
  5834.             "PRECISION" = convert(int,10),
  5835.             LENGTH = convert(int,4),
  5836.             SCALE = convert(smallint,0),
  5837.             RADIX = convert(smallint,10),
  5838.             NULLABLE = convert(smallint,0),
  5839.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5840.             COLUMN_DEF = convert(varchar(255),NULL),
  5841.             SQL_DATA_TYPE = convert(smallint, 4),
  5842.             SQL_DATETIME_SUB = convert(smallint,null),
  5843.             CHAR_OCTET_LENGTH = convert(int,null),
  5844.             ORDINAL_POSITION = convert(int,0),
  5845.             IS_NULLABLE = convert(varchar(254),'NO'),
  5846.             SS_DATA_TYPE = convert(tinyint,56)
  5847.         FROM
  5848.             syscomments c, sysobjects o
  5849.         WHERE
  5850.             o.name like @procedure_name
  5851.             AND user_name(o.uid) like @procedure_owner
  5852.             AND c.id = o.id
  5853.             AND c.colid = 1
  5854.             AND o.type = 'P'                        /* Just Procedures */
  5855.             AND '@RETURN_VALUE' like @column_name
  5856.             AND c.number between @group_num_lower and @group_num_upper
  5857.         ORDER BY 1, 2, 3, 18
  5858.     end
  5859. go
  5860.  
  5861. if (charindex('7.00', @@version) = 0 and
  5862.     charindex('8.00', @@version) = 0)
  5863. begin
  5864.     print ''
  5865.     print ''
  5866.     print 'Warning:'
  5867.     print 'you are installing the stored procedures '
  5868.     print 'on a pre 7.0 SQL Server.'
  5869.     print 'Ignore the following errors.'
  5870. end
  5871. else
  5872.     drop proc sp_sproc_columns
  5873. go
  5874.  
  5875. /*  Procedure for 7.0 server */
  5876. CREATE PROCEDURE sp_sproc_columns (
  5877.                  @procedure_name        nvarchar(390) = '%',
  5878.                  @procedure_owner       nvarchar(384) = null,
  5879.                  @procedure_qualifier   sysname = null,
  5880.                  @column_name           nvarchar(384) = null,
  5881.                  @ODBCVer               int = 2)
  5882. AS
  5883.     DECLARE @group_num_lower smallint
  5884.     DECLARE @group_num_upper smallint
  5885.     DECLARE @semi_position int
  5886.     DECLARE @full_procedure_name    nvarchar(774)
  5887.     DECLARE @procedure_id int
  5888.  
  5889.     if @column_name is null /*  If column name not supplied, match all */
  5890.         select @column_name = '%'
  5891.     if @procedure_qualifier is not null
  5892.     begin
  5893.         if db_name() <> @procedure_qualifier
  5894.         begin
  5895.             if @procedure_qualifier = ''
  5896.             begin
  5897.                 /* in this case, we need to return an empty result set */
  5898.                 /* because the user has requested a database with an empty name */
  5899.                 select @procedure_name = ''
  5900.                 select @procedure_owner = ''
  5901.             end
  5902.             else
  5903.             begin   /* If qualifier doesn't match current database */
  5904.                 raiserror (15250, -1,-1)
  5905.                 return
  5906.             end
  5907.         end
  5908.     end
  5909.  
  5910.     if @procedure_name is null
  5911.     begin   /*  If procedure name not supplied, match all */
  5912.         select @procedure_name = '%'
  5913.     end
  5914.  
  5915.     /* first we need to extract the procedure group number, if one exists */
  5916.     select @semi_position = charindex(';',@procedure_name)
  5917.     if (@semi_position > 0)
  5918.     begin   /* If group number separator (;) found */
  5919.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5920.         select @group_num_upper = @group_num_lower
  5921.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5922.     end
  5923.     else
  5924.     begin   /* No group separator, so default to all groups */
  5925.         select @group_num_lower = 1
  5926.         select @group_num_upper = 32767         
  5927.     end
  5928.  
  5929.     if @procedure_owner is null
  5930.     begin   /* If unqualified procedure name */
  5931.         SELECT @full_procedure_name = quotename(@procedure_name)
  5932.     end
  5933.     else
  5934.     begin   /* Qualified procedure name */
  5935.         if @procedure_owner = ''
  5936.         begin   /* If empty owner name */
  5937.             SELECT @full_procedure_name = quotename(@procedure_owner)
  5938.         end
  5939.         else
  5940.         begin
  5941.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  5942.                 '.' + quotename(@procedure_name)
  5943.         end
  5944.     end
  5945.  
  5946.     /*  Get Object ID */
  5947.     SELECT @procedure_id = object_id(@full_procedure_name)
  5948.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  5949.         (isnull(charindex('[', @procedure_name),0) = 0) and
  5950.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  5951.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  5952.         not (@procedure_id is null))
  5953.     begin
  5954.         /* this block is for the case where there is no pattern
  5955.             matching required for the procedure name */
  5956.         SELECT
  5957.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5958.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5959.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5960.             COLUMN_NAME = convert(sysname,c.name),
  5961.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  5962.             d.DATA_TYPE,
  5963.             TYPE_NAME = t.name,
  5964.             convert(int,case
  5965.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5966.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5967.             end) "PRECISION",
  5968.             convert(int,case
  5969.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5970.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5971.                 else
  5972.                     isnull(d.length, c.length)
  5973.             end) LENGTH,
  5974.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5975.             d.RADIX,
  5976.             d.NULLABLE,
  5977.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  5978.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5979.             d.SQL_DATA_TYPE,
  5980.             d.SQL_DATETIME_SUB,
  5981.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5982.             ORDINAL_POSITION = convert(int, c.colid),
  5983.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5984.             SS_DATA_TYPE = c.type
  5985.         FROM
  5986.             syscolumns c,
  5987.             sysobjects o,
  5988.             master.dbo.spt_datatype_info d,
  5989.             systypes t
  5990.         WHERE
  5991.             o.id = @procedure_id
  5992.             AND c.id = o.id
  5993.             AND c.xtype = d.ss_dtype
  5994.             AND c.length = isnull(d.fixlen, c.length)
  5995.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5996.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5997.             AND c.xusertype = t.xusertype
  5998.             AND c.name like @column_name
  5999.             AND c.number between @group_num_lower and @group_num_upper
  6000.         UNION ALL
  6001.         SELECT         /* return value row*/
  6002.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6003.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6004.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6005.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6006.             COLUMN_TYPE = convert(smallint, 5),
  6007.             DATA_TYPE = convert(smallint, 4),
  6008.             TYPE_NAME = convert(sysname,'int'),
  6009.             "PRECISION" = convert(int,10),
  6010.             LENGTH = convert(int,4),
  6011.             SCALE = convert(smallint,0),
  6012.             RADIX = convert(smallint,10),
  6013.             NULLABLE = convert(smallint,0),
  6014.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6015.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6016.             SQL_DATA_TYPE = convert(smallint, 4),
  6017.             SQL_DATETIME_SUB = convert(smallint,null),
  6018.             CHAR_OCTET_LENGTH = convert(int,null),
  6019.             ORDINAL_POSITION = convert(int,0),
  6020.             IS_NULLABLE = convert(varchar(254),'NO'),
  6021.             SS_DATA_TYPE = convert(tinyint,56)
  6022.         FROM
  6023.             syscomments c, sysobjects o
  6024.         WHERE
  6025.             o.id = @procedure_id
  6026.             AND c.id = o.id
  6027.             AND c.colid = 1
  6028.             AND o.type = 'P'                        /* Just Procedures */
  6029.             AND '@RETURN_VALUE' like @column_name
  6030.             AND c.number between @group_num_lower and @group_num_upper      
  6031.         ORDER BY 1, 2, 3, 18
  6032.     end
  6033.     else
  6034.     begin
  6035.         /* this block is for the case where there IS pattern
  6036.             matching done on the procedure name */
  6037.         if @procedure_owner is null
  6038.             select @procedure_owner = '%'
  6039.         SELECT
  6040.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6041.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6042.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6043.             COLUMN_NAME = convert(sysname,c.name),
  6044.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6045.             d.DATA_TYPE,
  6046.             TYPE_NAME = t.name,
  6047.             convert(int,case
  6048.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6049.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6050.             end) "PRECISION",
  6051.             convert(int,case
  6052.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6053.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6054.                 else
  6055.                     isnull(d.length, c.length)
  6056.             end) LENGTH,
  6057.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6058.             d.RADIX,
  6059.             d.NULLABLE,
  6060.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6061.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6062.             d.SQL_DATA_TYPE,
  6063.             d.SQL_DATETIME_SUB,
  6064.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6065.             ORDINAL_POSITION = convert(int, c.colid),
  6066.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6067.             SS_DATA_TYPE = c.type
  6068.         FROM
  6069.             syscolumns c,
  6070.             sysobjects o,
  6071.             master.dbo.spt_datatype_info d,
  6072.             systypes t
  6073.         WHERE
  6074.             o.name like @procedure_name
  6075.             AND user_name(o.uid) like @procedure_owner
  6076.             AND o.id = c.id
  6077.             AND c.xtype = d.ss_dtype
  6078.             AND c.length = isnull(d.fixlen, c.length)
  6079.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6080.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6081.             AND c.xusertype = t.xusertype
  6082.             AND o.type = 'P'                            /* Just Procedures */
  6083.             AND c.name like @column_name
  6084.             AND c.number between @group_num_lower and @group_num_upper
  6085.         UNION ALL
  6086.         SELECT         /* return value row*/
  6087.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6088.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6089.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6090.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6091.             COLUMN_TYPE = convert(smallint, 5),
  6092.             DATA_TYPE = convert(smallint, 4),
  6093.             TYPE_NAME = convert(sysname,'int'),
  6094.             "PRECISION" = convert(int,10),
  6095.             LENGTH = convert(int,4),
  6096.             SCALE = convert(smallint,0),
  6097.             RADIX = convert(smallint,10),
  6098.             NULLABLE = convert(smallint,0),
  6099.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6100.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6101.             SQL_DATA_TYPE = convert(smallint, 4),
  6102.             SQL_DATETIME_SUB = convert(smallint,null),
  6103.             CHAR_OCTET_LENGTH = convert(int,null),
  6104.             ORDINAL_POSITION = convert(int,0),
  6105.             IS_NULLABLE = convert(varchar(254),'NO'),
  6106.             SS_DATA_TYPE = convert(tinyint,56)
  6107.         FROM
  6108.             syscomments c, sysobjects o
  6109.         WHERE
  6110.             o.name like @procedure_name
  6111.             AND c.id = o.id
  6112.             AND user_name(o.uid) like @procedure_owner
  6113.             AND c.colid = 1
  6114.             AND o.type = 'P'                        /* Just Procedures */
  6115.             AND '@RETURN_VALUE' like @column_name
  6116.             AND c.number between @group_num_lower and @group_num_upper
  6117.         ORDER BY 1, 2, 3, 18
  6118.     end
  6119. go
  6120.  
  6121. if (charindex('8.00', @@version) = 0)
  6122. begin
  6123.     print ''
  6124.     print ''
  6125.     print 'Warning:'
  6126.     print 'you are installing the stored procedures '
  6127.     print 'on a pre 8.0 SQL Server.'
  6128.     print 'Ignore the following errors.'
  6129. end
  6130. else
  6131.     drop proc sp_sproc_columns
  6132. go
  6133.  
  6134. /*  Procedure for 8.0 server */
  6135. CREATE PROCEDURE sp_sproc_columns (
  6136.                  @procedure_name        nvarchar(390) = '%',
  6137.                  @procedure_owner       nvarchar(384) = null,
  6138.                  @procedure_qualifier       sysname = null,
  6139.                  @column_name           nvarchar(384) = null,
  6140.                  @ODBCVer               int = 2)
  6141. AS
  6142.     DECLARE @group_num_lower smallint
  6143.     DECLARE @group_num_upper smallint
  6144.     DECLARE @semi_position int
  6145.     DECLARE @full_procedure_name nvarchar(774)
  6146.     DECLARE @procedure_id int
  6147.  
  6148.     if @column_name is null /*  If column name not supplied, match all */
  6149.         select @column_name = '%'
  6150.     if @procedure_qualifier is not null
  6151.     begin
  6152.         if db_name() <> @procedure_qualifier
  6153.         begin
  6154.             if @procedure_qualifier = ''
  6155.             begin
  6156.                 /* in this case, we need to return an empty result set */
  6157.                 /* because the user has requested a database with an empty name */
  6158.                 select @procedure_name = ''
  6159.                 select @procedure_owner = ''
  6160.             end
  6161.             else
  6162.             begin   /* If qualifier doesn't match current database */
  6163.                 raiserror (15250, -1,-1)
  6164.                 return
  6165.             end
  6166.         end
  6167.     end
  6168.  
  6169.     if @procedure_name is null
  6170.     begin   /*  If procedure name not supplied, match all */
  6171.         select @procedure_name = '%'
  6172.     end
  6173.  
  6174.     /* first we need to extract the procedure group number, if one exists */
  6175.     select @semi_position = charindex(';',@procedure_name)
  6176.     if (@semi_position > 0)
  6177.     begin   /* If group number separator (;) found */
  6178.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  6179.         select @group_num_upper = @group_num_lower
  6180.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  6181.     end
  6182.     else
  6183.     begin   /* No group separator, so default to all groups */
  6184.         select @group_num_lower = 1
  6185.         select @group_num_upper = 32767         
  6186.     end
  6187.  
  6188.     if @procedure_owner is null
  6189.     begin   /* If unqualified procedure name */
  6190.         SELECT @full_procedure_name = quotename(@procedure_name)
  6191.     end
  6192.     else
  6193.     begin   /* Qualified procedure name */
  6194.         if @procedure_owner = ''
  6195.         begin   /* If empty owner name */
  6196.             SELECT @full_procedure_name = quotename(@procedure_owner)
  6197.         end
  6198.         else
  6199.         begin
  6200.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  6201.                 '.' + quotename(@procedure_name)
  6202.         end
  6203.     end
  6204.  
  6205.     /*  Get Object ID */
  6206.     SELECT @procedure_id = object_id(@full_procedure_name)
  6207.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  6208.         (isnull(charindex('[', @procedure_name),0) = 0) and
  6209.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  6210.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  6211.         not (@procedure_id is null))
  6212.     begin
  6213.         /* this block is for the case where there is no pattern
  6214.             matching required for the procedure name */
  6215.         SELECT
  6216.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6217.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6218.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6219.             COLUMN_NAME = convert(sysname,c.name),
  6220.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6221.             d.DATA_TYPE,
  6222.             TYPE_NAME = t.name,
  6223.             convert(int,case
  6224.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6225.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6226.             end) "PRECISION",
  6227.             convert(int,case
  6228.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6229.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6230.                 else
  6231.                     isnull(d.length, c.length)
  6232.             end) LENGTH,
  6233.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6234.             d.RADIX,
  6235.             d.NULLABLE,
  6236.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6237.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6238.             d.SQL_DATA_TYPE,
  6239.             d.SQL_DATETIME_SUB,
  6240.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6241.             ORDINAL_POSITION = convert(int, c.colid),
  6242.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6243.             SS_DATA_TYPE = c.type
  6244.         FROM
  6245.             syscolumns c,
  6246.             sysobjects o,
  6247.             master.dbo.spt_datatype_info d,
  6248.             systypes t
  6249.         WHERE
  6250.             o.id = @procedure_id
  6251.             AND c.id = o.id
  6252.             AND c.xtype = d.ss_dtype
  6253.             AND c.length = isnull(d.fixlen, c.length)
  6254.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6255.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6256.             AND c.xusertype = t.xusertype
  6257.             AND c.name like @column_name
  6258.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6259.             AND ((c.number between @group_num_lower and @group_num_upper)
  6260.                  OR (c.number = 0 and o.type = 'FN'))
  6261.         UNION ALL
  6262.         SELECT         /* return value row*/
  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),
  6268.             DATA_TYPE = convert(smallint, 4),
  6269.             TYPE_NAME = convert(sysname,'int'),
  6270.             "PRECISION" = convert(int,10),
  6271.             LENGTH = convert(int,4),
  6272.             SCALE = convert(smallint,0),
  6273.             RADIX = convert(smallint,10),
  6274.             NULLABLE = convert(smallint,0),
  6275.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6276.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6277.             SQL_DATA_TYPE = convert(smallint, 4),
  6278.             SQL_DATETIME_SUB = convert(smallint,null),
  6279.             CHAR_OCTET_LENGTH = convert(int,null),
  6280.             ORDINAL_POSITION = convert(int,0),
  6281.             IS_NULLABLE = convert(varchar(254),'NO'),
  6282.             SS_DATA_TYPE = convert(tinyint,56)
  6283.         FROM
  6284.             syscomments c, sysobjects o
  6285.         WHERE
  6286.             o.id = @procedure_id
  6287.             AND c.id = o.id
  6288.             AND c.colid = 1
  6289.             AND o.type = 'P'            /* Procedures */
  6290.             AND '@RETURN_VALUE' like @column_name
  6291.             AND c.number between @group_num_lower and @group_num_upper
  6292.         UNION ALL
  6293.         SELECT      /* UDF return value */
  6294.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6295.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6296.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6297.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6298.             COLUMN_TYPE = convert(smallint, 5), /* SQL_RETURN_VALUE */
  6299.             d.DATA_TYPE,
  6300.             TYPE_NAME = t.name,
  6301.             convert(int,case
  6302.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6303.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6304.             end) "PRECISION",
  6305.             convert(int,case
  6306.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6307.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6308.                 else
  6309.                     isnull(d.length, c.length)
  6310.             end) LENGTH,
  6311.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6312.             d.RADIX,
  6313.             NULLABLE = convert(smallint, c.isnullable),
  6314.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6315.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6316.             d.SQL_DATA_TYPE,
  6317.             d.SQL_DATETIME_SUB,
  6318.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6319.             ORDINAL_POSITION = convert(int,0),
  6320.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6321.             SS_DATA_TYPE = c.type
  6322.         FROM
  6323.             syscolumns c,
  6324.             sysobjects o,
  6325.             master.dbo.spt_datatype_info d,
  6326.             systypes t
  6327.         WHERE
  6328.             o.name like @procedure_name
  6329.             AND o.id = c.id
  6330.             AND c.xtype = d.ss_dtype
  6331.             AND c.length = isnull(d.fixlen, c.length)
  6332.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6333.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6334.             AND c.xusertype = t.xusertype
  6335.             AND o.type = 'FN'           /* Scalar UDF */
  6336.             AND c.name like @column_name
  6337.             AND c.colid = 0
  6338.             AND c.number = 0
  6339.         UNION ALL
  6340.         SELECT      /* Table valued functions */
  6341.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6342.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6343.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6344.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6345.             COLUMN_TYPE = convert(smallint, 3),
  6346.             DATA_TYPE = convert(smallint, NULL),
  6347.             TYPE_NAME = convert(sysname, 'table'),
  6348.             "PRECISION" = convert(int,0),
  6349.             LENGTH = convert(int,0),
  6350.             SCALE = convert(smallint,0),
  6351.             RADIX = convert(smallint,0),
  6352.             NULLABLE = convert(smallint,0),
  6353.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6354.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6355.             SQL_DATA_TYPE = convert(smallint, NULL),
  6356.             SQL_DATETIME_SUB = convert(smallint,null),
  6357.             CHAR_OCTET_LENGTH = convert(int,null),
  6358.             ORDINAL_POSITION = convert(int,0),
  6359.             IS_NULLABLE = convert(varchar(254),'NO'),
  6360.             SS_DATA_TYPE = convert(tinyint,0)
  6361.         FROM
  6362.             syscomments c, sysobjects o
  6363.         WHERE
  6364.             o.id = @procedure_id
  6365.             AND c.id = o.id
  6366.             AND c.colid = 1
  6367.             AND o.type IN ('TF', 'IF')
  6368.             AND '@TABLE_RETURN_VALUE' like @column_name
  6369.             AND c.number = 0
  6370.         ORDER BY 1, 2, 3, 18
  6371.     end
  6372.     else
  6373.     begin
  6374.         /* this block is for the case where there IS pattern
  6375.             matching done on the procedure name */
  6376.         if @procedure_owner is null
  6377.             select @procedure_owner = '%'
  6378.  
  6379.         SELECT
  6380.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6381.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6382.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6383.             COLUMN_NAME = convert(sysname,c.name),
  6384.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6385.             d.DATA_TYPE,
  6386.             TYPE_NAME = t.name,
  6387.             convert(int,case
  6388.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6389.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6390.             end) "PRECISION",
  6391.             convert(int,case
  6392.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6393.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6394.                 else
  6395.                     isnull(d.length, c.length)
  6396.             end) LENGTH,
  6397.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6398.             d.RADIX,
  6399.             d.NULLABLE,
  6400.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6401.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6402.             d.SQL_DATA_TYPE,
  6403.             d.SQL_DATETIME_SUB,
  6404.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6405.             ORDINAL_POSITION = convert(int, c.colid),
  6406.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6407.             SS_DATA_TYPE = c.type
  6408.         FROM
  6409.             syscolumns c,
  6410.             sysobjects o,
  6411.             master.dbo.spt_datatype_info d,
  6412.             systypes t
  6413.         WHERE
  6414.             o.name like @procedure_name
  6415.             AND user_name(o.uid) like @procedure_owner
  6416.             AND o.id = c.id
  6417.             AND c.xtype = d.ss_dtype
  6418.             AND c.length = isnull(d.fixlen, c.length)
  6419.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6420.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6421.             AND c.xusertype = t.xusertype
  6422.             AND c.name like @column_name
  6423.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6424.             AND ((c.number between @group_num_lower and @group_num_upper)
  6425.                  OR (c.number = 0 and o.type = 'FN'))
  6426.         UNION ALL
  6427.         SELECT         /* return value row*/
  6428.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6429.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6430.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6431.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6432.             COLUMN_TYPE = convert(smallint, 5), /* SQL_RETURN_VALUE */
  6433.             DATA_TYPE = convert(smallint, 4),
  6434.             TYPE_NAME = convert(sysname,'int'),
  6435.             "PRECISION" = convert(int,10),
  6436.             LENGTH = convert(int,4),
  6437.             SCALE = convert(smallint,0),
  6438.             RADIX = convert(smallint,10),
  6439.             NULLABLE = convert(smallint,0),
  6440.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6441.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6442.             SQL_DATA_TYPE = convert(smallint, 4),
  6443.             SQL_DATETIME_SUB = convert(smallint,null),
  6444.             CHAR_OCTET_LENGTH = convert(int,null),
  6445.             ORDINAL_POSITION = convert(int,0),
  6446.             IS_NULLABLE = convert(varchar(254),'NO'),
  6447.             SS_DATA_TYPE = convert(tinyint,56)
  6448.         FROM
  6449.             syscomments c, sysobjects o
  6450.         WHERE
  6451.             o.name like @procedure_name
  6452.             AND c.id = o.id
  6453.             AND user_name(o.uid) like @procedure_owner
  6454.             AND c.colid = 1
  6455.             AND o.type = 'P'                    /* Procedures */
  6456.             AND '@RETURN_VALUE' like @column_name
  6457.             AND c.number between @group_num_lower and @group_num_upper
  6458.         UNION ALL
  6459.         SELECT      /* UDF return value */
  6460.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6461.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6462.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6463.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6464.             COLUMN_TYPE = convert(smallint, 5), /* SQL_RETURN_VALUE */
  6465.             d.DATA_TYPE,
  6466.             TYPE_NAME = t.name,
  6467.             convert(int,case
  6468.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6469.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6470.             end) "PRECISION",
  6471.             convert(int,case
  6472.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6473.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6474.                 else
  6475.                     isnull(d.length, c.length)
  6476.             end) LENGTH,
  6477.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6478.             d.RADIX,
  6479.             NULLABLE = convert(smallint, c.isnullable),
  6480.             REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  6481.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6482.             d.SQL_DATA_TYPE,
  6483.             d.SQL_DATETIME_SUB,
  6484.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6485.             ORDINAL_POSITION = convert(int,0),
  6486.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6487.             SS_DATA_TYPE = c.type
  6488.         FROM
  6489.             syscolumns c,
  6490.             sysobjects o,
  6491.             master.dbo.spt_datatype_info d,
  6492.             systypes t
  6493.         WHERE
  6494.             o.name like @procedure_name
  6495.             AND user_name(o.uid) like @procedure_owner
  6496.             AND o.id = c.id
  6497.             AND c.xtype = d.ss_dtype
  6498.             AND c.length = isnull(d.fixlen, c.length)
  6499.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6500.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6501.             AND c.xusertype = t.xusertype
  6502.             AND o.type = 'FN'           /* Scalar UDF */
  6503.             AND c.name like @column_name
  6504.             AND c.colid = 0
  6505.             AND c.number = 0
  6506.         UNION ALL
  6507.         SELECT      /* Table valued functions */
  6508.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6509.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6510.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6511.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6512.             COLUMN_TYPE = convert(smallint, 3),
  6513.             DATA_TYPE = convert(smallint, NULL),
  6514.             TYPE_NAME = convert(sysname, 'table'),
  6515.             "PRECISION" = convert(int,0),
  6516.             LENGTH = convert(int,0),
  6517.             SCALE = convert(smallint,0),
  6518.             RADIX = convert(smallint,0),
  6519.             NULLABLE = convert(smallint,0),
  6520.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6521.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6522.             SQL_DATA_TYPE = convert(smallint, NULL),
  6523.             SQL_DATETIME_SUB = convert(smallint,null),
  6524.             CHAR_OCTET_LENGTH = convert(int,null),
  6525.             ORDINAL_POSITION = convert(int,0),
  6526.             IS_NULLABLE = convert(varchar(254),'NO'),
  6527.             SS_DATA_TYPE = convert(tinyint,0)
  6528.         FROM
  6529.             syscomments c, sysobjects o
  6530.         WHERE
  6531.             o.name like @procedure_name
  6532.             AND user_name(o.uid) like @procedure_owner
  6533.             AND c.id = o.id
  6534.             AND c.colid = 1
  6535.             AND o.type IN ('TF', 'IF')
  6536.             AND '@TABLE_RETURN_VALUE' like @column_name
  6537.             AND c.number = 0
  6538.         ORDER BY 1, 2, 3, 18
  6539.     end
  6540. go
  6541.  
  6542. grant execute on sp_sproc_columns to public
  6543. go
  6544.  
  6545. dump tran master with no_log
  6546. go
  6547.  
  6548. print 'creating sp_statistics'
  6549. go
  6550.  
  6551. /*  Procedure for pre-7.0 server */
  6552. CREATE PROCEDURE sp_statistics (
  6553.                  @table_name        varchar(32),
  6554.                  @table_owner       varchar(32) = null,
  6555.                  @table_qualifier   varchar(32) = null,
  6556.                  @index_name        varchar(32) = '%',
  6557.                  @is_unique         char(1) = 'N',
  6558.                  @accuracy          char(1) = 'Q')
  6559. AS
  6560.     set nocount on
  6561.     DECLARE @indid              int
  6562.     DECLARE @lastindid          int
  6563.     DECLARE @table_id           int
  6564.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  6565.  
  6566.     create table #TmpIndex(
  6567.         TABLE_QUALIFIER varchar(32) NULL,
  6568.         TABLE_OWNER     varchar(32) NULL,
  6569.         TABLE_NAME      varchar(32) NOT NULL,
  6570.         INDEX_QUALIFIER varchar(32) null,
  6571.         INDEX_NAME      varchar(32) null,
  6572.         NON_UNIQUE      smallint null,
  6573.         TYPE            smallint NOT NULL,
  6574.         SEQ_IN_INDEX    smallint null,
  6575.         COLUMN_NAME     varchar(32) null,
  6576.         COLLATION       char(1) null,
  6577.         index_id        int null,
  6578.         CARDINALITY     int null,
  6579.         PAGES           int null,
  6580.         status          smallint NOT NULL)
  6581.  
  6582.     if @table_qualifier is not null
  6583.     begin
  6584.         if db_name() <> @table_qualifier
  6585.         begin   /* If qualifier doesn't match current database */
  6586.             raiserror 20001 '~~Rush_5~~'
  6587.             return
  6588.         end
  6589.     end
  6590.  
  6591.     if @accuracy not in ('Q','E')
  6592.         begin
  6593.             raiserror 20002 '~~Rush_58~~'
  6594.             return
  6595.         end
  6596.  
  6597.     if (@@trancount <> 0 and
  6598.         charindex('6.50', @@version) = 0 and
  6599.         charindex('7.00', @@version) = 0 and
  6600.         charindex('8.00', @@version) = 0)
  6601.     begin   /* If inside a transaction */
  6602.         raiserror 20003 '~~Rush_59~~'
  6603.         return
  6604.     end
  6605.  
  6606.     if @table_owner is null
  6607.     begin   /* If unqualified table name */
  6608.         SELECT @full_table_name = @table_name
  6609.     end
  6610.     else
  6611.     begin   /* Qualified table name */
  6612.         if @table_owner = ''
  6613.         begin   /* If empty owner name */
  6614.             SELECT @full_table_name = @table_owner
  6615.         end
  6616.         else
  6617.         begin
  6618.             SELECT @full_table_name = @table_owner + '.' + @table_name
  6619.         end
  6620.     end
  6621.     /*  Get Object ID */
  6622.     SELECT @table_id = object_id(@full_table_name)
  6623.  
  6624.     /*  Start at lowest index id */
  6625.     SELECT @indid = min(indid)
  6626.     FROM sysindexes
  6627.     WHERE id = @table_id
  6628.         AND indid > 0
  6629.         AND indid < 255
  6630.  
  6631.     WHILE @indid is not NULL
  6632.     BEGIN
  6633.         INSERT #TmpIndex    /* Add all columns that are in index */
  6634.             SELECT
  6635.                 DB_NAME(),                              /* TABLE_QUALIFIER */
  6636.                 USER_NAME(o.uid),                       /* TABLE_OWNER     */
  6637.                 o.name,                                 /* TABLE_NAME      */
  6638.                 o.name,                                 /* INDEX_QUALIFIER */
  6639.                 x.name,                                 /* INDEX_NAME      */
  6640.                 0,                                      /* NON_UNIQUE      */
  6641.                 1,                                      /* SQL_INDEX_CLUSTERED */
  6642.                 colid,                                  /* SEQ_IN_INDEX    */
  6643.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME     */
  6644.                 'A',                                    /* COLLATION       */
  6645.                 @indid,                                 /* index_id        */
  6646.                 x.rows,                                 /* CARDINALITY     */
  6647.                 x.dpages,                               /* PAGES           */
  6648.                 x.status                                /* status           */
  6649.             FROM sysindexes x, syscolumns c, sysobjects o
  6650.             WHERE
  6651.                 x.id = @table_id
  6652.                 AND x.id = o.id
  6653.                 AND x.id = c.id
  6654.                 AND c.colid < keycnt+(x.status&16)/16   /* all but Unique Clust indices have an extra key */
  6655.                 AND x.indid = @indid
  6656.         /*
  6657.         **    Now move @indid to the next index.
  6658.         */
  6659.         SELECT @lastindid = @indid
  6660.         SELECT @indid = NULL
  6661.  
  6662.         SELECT @indid = min(indid)
  6663.         FROM sysindexes
  6664.         WHERE id = @table_id
  6665.             AND indid > @lastindid
  6666.             AND indid < 255
  6667.     END
  6668.  
  6669.     UPDATE #TmpIndex
  6670.         SET NON_UNIQUE = 1
  6671.         WHERE status&2 <> 2 /* If non-unique index */
  6672.     UPDATE #TmpIndex
  6673.         SET
  6674.             TYPE = 3,           /* SQL_INDEX_OTHER */
  6675.             CARDINALITY = NULL,
  6676.             PAGES = NULL
  6677.         WHERE index_id > 1  /* If non-clustered index */
  6678.  
  6679.     /* now add row for table statistics */
  6680.     INSERT #TmpIndex
  6681.         SELECT
  6682.             DB_NAME(),              /* TABLE_QUALIFIER */
  6683.             USER_NAME(o.uid),       /* TABLE_OWNER     */
  6684.             o.name,                 /* TABLE_NAME      */
  6685.             null,                   /* INDEX_QUALIFIER */
  6686.             null,                   /* INDEX_NAME      */
  6687.             null,                   /* NON_UNIQUE      */
  6688.             0,                      /* SQL_TABLE_STAT  */
  6689.             null,                   /* SEQ_IN_INDEX    */
  6690.             null,                   /* COLUMN_NAME     */
  6691.             null,                   /* COLLATION       */
  6692.             0,                      /* index_id        */
  6693.             x.rows,                 /* CARDINALITY     */
  6694.             x.dpages,               /* PAGES           */
  6695.             0                       /* status          */
  6696.         FROM sysindexes x, sysobjects o
  6697.         WHERE o.id = @table_id
  6698.             AND x.id = o.id
  6699.             AND (x.indid = 0 or x.indid = 1)    /*  If there are no indexes */
  6700.                                                 /*  then table stats are in */
  6701.                                                 /*  a row with indid =0     */
  6702.  
  6703.     if @is_unique <> 'Y'    /* If all indexes desired */
  6704.         SELECT
  6705.             TABLE_QUALIFIER,
  6706.             TABLE_OWNER,
  6707.             TABLE_NAME,
  6708.             NON_UNIQUE,
  6709.             INDEX_QUALIFIER,
  6710.             INDEX_NAME,
  6711.             TYPE,
  6712.             SEQ_IN_INDEX,
  6713.             COLUMN_NAME,
  6714.             COLLATION,
  6715.             CARDINALITY,
  6716.             PAGES,
  6717.             FILTER_CONDITION = convert(varchar(128),null)
  6718.         FROM #TmpIndex
  6719.         WHERE
  6720.             INDEX_NAME like @index_name /* If matching name */
  6721.             or INDEX_NAME is null       /* If SQL_TABLE_STAT row */
  6722.         ORDER BY 4, 7, 6, 8
  6723.     else                    /* If only unique indexes desired */
  6724.         SELECT
  6725.             TABLE_QUALIFIER,
  6726.             TABLE_OWNER,
  6727.             TABLE_NAME,
  6728.             NON_UNIQUE,
  6729.             INDEX_QUALIFIER,
  6730.             INDEX_NAME,
  6731.             TYPE,
  6732.             SEQ_IN_INDEX,
  6733.             COLUMN_NAME,
  6734.             COLLATION,
  6735.             CARDINALITY,
  6736.             PAGES,
  6737.             FILTER_CONDITION = convert(varchar(128),null)
  6738.         FROM #TmpIndex
  6739.         WHERE
  6740.             (NON_UNIQUE = 0             /* If unique */
  6741.                 or NON_UNIQUE is NULL)  /* If SQL_TABLE_STAT row */
  6742.             and (INDEX_NAME like @index_name    /* If matching name */
  6743.                 or INDEX_NAME is null)  /* If SQL_TABLE_STAT row */
  6744.         ORDER BY 4, 7, 6, 8
  6745.  
  6746.     DROP TABLE #TmpIndex
  6747. go
  6748.  
  6749. if (charindex('7.00', @@version) = 0 and
  6750.     charindex('8.00', @@version) = 0)
  6751. begin
  6752.     print ''
  6753.     print ''
  6754.     print 'Warning:'
  6755.     print 'you are installing the stored procedures '
  6756.     print 'on a pre 7.0 SQL Server.'
  6757.     print 'Ignore the following errors.'
  6758. end
  6759. else
  6760.     drop proc sp_statistics
  6761. go
  6762.  
  6763. /*  Procedure for 7.0 server */
  6764. CREATE PROCEDURE sp_statistics (
  6765.                  @table_name        sysname,
  6766.                  @table_owner       sysname = null,
  6767.                  @table_qualifier   sysname = null,
  6768.                  @index_name        sysname = '%',
  6769.                  @is_unique         char(1) = 'N',
  6770.                  @accuracy          char(1) = 'Q')
  6771. AS
  6772.     set nocount on
  6773.     DECLARE @indid              int
  6774.     DECLARE @lastindid          int
  6775.     DECLARE @table_id           int
  6776.     DECLARE @full_table_name    nvarchar(257)
  6777.  
  6778.     create table #TmpIndex(
  6779.         TABLE_QUALIFIER sysname NULL,
  6780.         TABLE_OWNER     sysname NULL,
  6781.         TABLE_NAME      sysname NOT NULL,
  6782.         INDEX_QUALIFIER sysname null,
  6783.         INDEX_NAME      sysname null,
  6784.         NON_UNIQUE      smallint null,
  6785.         TYPE            smallint NOT NULL,
  6786.         SEQ_IN_INDEX    smallint null,
  6787.         COLUMN_NAME     sysname null,
  6788.         COLLATION       char(1) null,
  6789.         index_id        int null,
  6790.         CARDINALITY     int null,
  6791.         PAGES           int null,
  6792.         status          int NOT NULL)
  6793.  
  6794.     if @table_qualifier is not null
  6795.     begin
  6796.         if db_name() <> @table_qualifier
  6797.         begin   /* If qualifier doesn't match current database */
  6798.             raiserror (15250, -1,-1)
  6799.             return
  6800.         end
  6801.     end
  6802.  
  6803.     if @accuracy not in ('Q','E')
  6804.         begin
  6805.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6806.             return
  6807.         end
  6808.  
  6809.     if @table_owner is null
  6810.     begin   /* If unqualified table name */
  6811.         SELECT @full_table_name = quotename(@table_name)
  6812.     end
  6813.     else
  6814.     begin   /* Qualified table name */
  6815.         if @table_owner = ''
  6816.         begin   /* If empty owner name */
  6817.             SELECT @full_table_name = quotename(@table_owner)
  6818.         end
  6819.         else
  6820.         begin
  6821.             SELECT @full_table_name = quotename(@table_owner) +
  6822.                 '.' + quotename(@table_name)
  6823.         end
  6824.     end
  6825.     /*  Get Object ID */
  6826.     SELECT @table_id = object_id(@full_table_name)
  6827.  
  6828.     /*  Start at lowest index id */
  6829.     SELECT @indid = min(indid)
  6830.     FROM sysindexes
  6831.     WHERE not (@table_id is null)
  6832.         AND id = @table_id
  6833.         AND indid > 0
  6834.         AND indid < 255
  6835.  
  6836.     /* Create a temp table to correct the ordinal position of the columns */
  6837.     create table #TmpColumns
  6838.     (ordinal int identity(1,1),
  6839.      colid   smallint not null)
  6840.  
  6841.     /* Load columns into the temp table */
  6842.     insert into #TmpColumns (colid)
  6843.     select c.colid
  6844.     from syscolumns c
  6845.     where c.id = @table_id
  6846.     order by c.colid
  6847.     
  6848.     WHILE @indid is not NULL
  6849.     BEGIN
  6850.         INSERT #TmpIndex    /* Add all columns that are in index */
  6851.             SELECT
  6852.                 DB_NAME(),                              /* TABLE_QUALIFIER */
  6853.                 USER_NAME(o.uid),                       /* TABLE_OWNER     */
  6854.                 o.name,                                 /* TABLE_NAME      */
  6855.                 o.name,                                 /* INDEX_QUALIFIER */
  6856.                 x.name,                                 /* INDEX_NAME      */
  6857.                 case                                    /* NON_UNIQUE      */
  6858.                     WHEN x.status&2 <> 2 then 1         /* Nonunique index */
  6859.                     else 0                              /* Unique index    */
  6860.                 end,
  6861.                 case                                    /* TYPE            */
  6862.                     when @indid > 1 then 3              /* Non-Clustered   */
  6863.                     else 1                              /* Clustered index */
  6864.                 end,
  6865.                 tc.ordinal,                                 /* SEQ_IN_INDEX    */
  6866.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME      */
  6867.                 'A',                                    /* COLLATION       */
  6868.                 @indid,                                 /* index_id        */
  6869.                 case                                    /* CARDINALITY     */
  6870.                     when @indid > 1 then NULL           /* Non-Clustered   */
  6871.                     else x.rows                         /* Clustered index */
  6872.                 end,
  6873.                 case                                    /* PAGES           */
  6874.                     when @indid > 1 then NULL           /* Non-Clustered   */
  6875.                     else x.dpages                       /* Clustered index */
  6876.                 end,
  6877.                 x.status                                /* status           */
  6878.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  6879.             WHERE
  6880.                 not (@table_id is null)
  6881.                 AND x.id = @table_id
  6882.                 AND x.id = o.id
  6883.                 AND x.id = c.id
  6884.                 AND tc.colid = c.colid
  6885.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  6886.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  6887.                 AND indid = @indid
  6888.                 AND (x.status&2 = 2
  6889.                     OR @is_unique <> 'Y')
  6890.                 AND (x.status&32) = 0
  6891.         /*
  6892.         **    Now move @indid to the next index.
  6893.         */
  6894.         SELECT @lastindid = @indid
  6895.         SELECT @indid = NULL
  6896.  
  6897.         SELECT @indid = min(indid)
  6898.         FROM sysindexes
  6899.         WHERE not (@table_id is null)
  6900.             AND id = @table_id
  6901.             AND indid > @lastindid
  6902.             AND indid < 255
  6903.     END
  6904.  
  6905.     /* now add row for table statistics */
  6906.     INSERT #TmpIndex
  6907.         SELECT
  6908.             DB_NAME(),              /* TABLE_QUALIFIER */
  6909.             USER_NAME(o.uid),       /* TABLE_OWNER     */
  6910.             o.name,                 /* TABLE_NAME      */
  6911.             null,                   /* INDEX_QUALIFIER */
  6912.             null,                   /* INDEX_NAME      */
  6913.             null,                   /* NON_UNIQUE      */
  6914.             0,                      /* SQL_TABLE_STAT  */
  6915.             null,                   /* SEQ_IN_INDEX    */
  6916.             null,                   /* COLUMN_NAME     */
  6917.             null,                   /* COLLATION       */
  6918.             0,                      /* index_id        */
  6919.             x.rows,                 /* CARDINALITY     */
  6920.             x.dpages,               /* PAGES           */
  6921.             0                       /* status          */
  6922.         FROM sysindexes x, sysobjects o
  6923.         WHERE not (@table_id is null)
  6924.             AND o.id = @table_id
  6925.             AND x.id = o.id
  6926.             AND (x.indid = 0 or x.indid = 1)    /*  If there are no indexes */
  6927.                                                 /*  then table stats are in */
  6928.                                                 /*  a row with indid =0     */
  6929.  
  6930.     SELECT
  6931.         TABLE_QUALIFIER,
  6932.         TABLE_OWNER,
  6933.         TABLE_NAME,
  6934.         NON_UNIQUE,
  6935.         INDEX_QUALIFIER,
  6936.         INDEX_NAME,
  6937.         TYPE,
  6938.         SEQ_IN_INDEX,
  6939.         COLUMN_NAME,
  6940.         COLLATION,
  6941.         CARDINALITY,
  6942.         PAGES,
  6943.         FILTER_CONDITION = convert(varchar(128),null)
  6944.     FROM #TmpIndex
  6945.     WHERE
  6946.         INDEX_NAME like @index_name /* If matching name */
  6947.         or INDEX_NAME is null       /* If SQL_TABLE_STAT row */
  6948.     ORDER BY 4, 7, 6, 8
  6949.  
  6950.     DROP TABLE #TmpIndex, #TmpColumns
  6951. go
  6952.  
  6953. if (charindex('8.00', @@version) = 0)
  6954. begin
  6955.     print ''
  6956.     print ''
  6957.     print 'Warning:'
  6958.     print 'you are installing the stored procedures '
  6959.     print 'on a pre 8.0 SQL Server.'
  6960.     print 'Ignore the following errors.'
  6961. end
  6962. else
  6963.     drop proc sp_statistics
  6964. go
  6965.  
  6966. /*  Procedure for 8.0 server */
  6967. CREATE PROCEDURE sp_statistics (
  6968.                  @table_name        sysname,
  6969.                  @table_owner       sysname = null,
  6970.                  @table_qualifier   sysname = null,
  6971.                  @index_name        sysname = '%',
  6972.                  @is_unique         char(1) = 'N',
  6973.                  @accuracy          char(1) = 'Q')
  6974. AS
  6975.     set nocount on
  6976.     DECLARE @indid              int
  6977.     DECLARE @lastindid          int
  6978.     DECLARE @table_id           int
  6979.     DECLARE @full_table_name    nvarchar(257)
  6980.  
  6981.     create table #TmpIndex(
  6982.         TABLE_QUALIFIER sysname collate database_default NULL,
  6983.         TABLE_OWNER     sysname collate database_default NULL,
  6984.         TABLE_NAME      sysname collate database_default NOT NULL,
  6985.         INDEX_QUALIFIER sysname collate database_default null,
  6986.         INDEX_NAME      sysname collate database_default null,
  6987.         NON_UNIQUE      smallint null,
  6988.         TYPE            smallint NOT NULL,
  6989.         SEQ_IN_INDEX    smallint null,
  6990.         COLUMN_NAME     sysname collate database_default null,
  6991.         COLLATION       char(1) collate database_default null,
  6992.         index_id        int null,
  6993.         CARDINALITY     int null,
  6994.         PAGES           int null,
  6995.         status          int NOT NULL)
  6996.  
  6997.     if @table_qualifier is not null
  6998.     begin
  6999.         if db_name() <> @table_qualifier
  7000.         begin   /* If qualifier doesn't match current database */
  7001.             raiserror (15250, -1,-1)
  7002.             return
  7003.         end
  7004.     end
  7005.  
  7006.     if @accuracy not in ('Q','E')
  7007.         begin
  7008.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  7009.             return
  7010.         end
  7011.  
  7012.     if @table_owner is null
  7013.     begin   /* If unqualified table name */
  7014.         SELECT @full_table_name = quotename(@table_name)
  7015.     end
  7016.     else
  7017.     begin   /* Qualified table name */
  7018.         if @table_owner = ''
  7019.         begin   /* If empty owner name */
  7020.             SELECT @full_table_name = quotename(@table_owner)
  7021.         end
  7022.         else
  7023.         begin
  7024.             SELECT @full_table_name = quotename(@table_owner) +
  7025.                 '.' + quotename(@table_name)
  7026.         end
  7027.     end
  7028.     /*  Get Object ID */
  7029.     SELECT @table_id = object_id(@full_table_name)
  7030.  
  7031.     /*  Start at lowest index id */
  7032.     SELECT @indid = min(indid)
  7033.     FROM sysindexes
  7034.     WHERE not (@table_id is null)
  7035.         AND id = @table_id
  7036.         AND indid > 0
  7037.         AND indid < 255
  7038.  
  7039.     /* Create a temp table to correct the ordinal position of the columns */
  7040.     create table #TmpColumns
  7041.     (ordinal int identity(1,1),
  7042.      colid   smallint not null)
  7043.  
  7044.     /* Load columns into the temp table */
  7045.     insert into #TmpColumns (colid)
  7046.     select c.colid
  7047.     from syscolumns c
  7048.     where c.id = @table_id
  7049.     order by c.colid
  7050.     
  7051.     WHILE @indid is not NULL
  7052.     BEGIN
  7053.         INSERT #TmpIndex    /* Add all columns that are in index */
  7054.             SELECT
  7055.                 DB_NAME(),                              /* TABLE_QUALIFIER */
  7056.                 USER_NAME(o.uid),                       /* TABLE_OWNER     */
  7057.                 o.name,                                 /* TABLE_NAME      */
  7058.                 o.name,                                 /* INDEX_QUALIFIER */
  7059.                 x.name,                                 /* INDEX_NAME      */
  7060.                 case                                    /* NON_UNIQUE      */
  7061.                     WHEN x.status&2 <> 2 then 1         /* Nonunique index */
  7062.                     else 0                              /* Unique index    */
  7063.                 end,
  7064.                 case                                    /* TYPE            */
  7065.                     when @indid > 1 then 3              /* Non-Clustered   */
  7066.                     else 1                              /* Clustered index */
  7067.                 end,
  7068.                 tc.ordinal,                                 /* SEQ_IN_INDEX    */
  7069.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME      */
  7070.                 'A',                                    /* COLLATION       */
  7071.                 @indid,                                 /* index_id        */
  7072.                 case                                    /* CARDINALITY     */
  7073.                     when @indid > 1 then NULL           /* Non-Clustered   */
  7074.                     else x.rows                         /* Clustered index */
  7075.                 end,
  7076.                 case                                    /* PAGES           */
  7077.                     when @indid > 1 then NULL           /* Non-Clustered   */
  7078.                     else x.dpages                       /* Clustered index */
  7079.                 end,
  7080.                 x.status                                /* status           */
  7081.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  7082.             WHERE
  7083.                 not (@table_id is null)
  7084.                 AND x.id = @table_id
  7085.                 AND x.id = o.id
  7086.                 AND x.id = c.id
  7087.                 AND tc.colid = c.colid
  7088.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  7089.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  7090.                 AND indid = @indid
  7091.                 AND (x.status&2 = 2
  7092.                     OR @is_unique <> 'Y')
  7093.                 AND (x.status&32) = 0
  7094.         /*
  7095.         **    Now move @indid to the next index.
  7096.         */
  7097.         SELECT @lastindid = @indid
  7098.         SELECT @indid = NULL
  7099.  
  7100.         SELECT @indid = min(indid)
  7101.         FROM sysindexes
  7102.         WHERE not (@table_id is null)
  7103.             AND id = @table_id
  7104.             AND indid > @lastindid
  7105.             AND indid < 255
  7106.     END
  7107.  
  7108.     /* now add row for table statistics */
  7109.     INSERT #TmpIndex
  7110.         SELECT
  7111.             DB_NAME(),              /* TABLE_QUALIFIER */
  7112.             USER_NAME(o.uid),       /* TABLE_OWNER     */
  7113.             o.name,                 /* TABLE_NAME      */
  7114.             null,                   /* INDEX_QUALIFIER */
  7115.             null,                   /* INDEX_NAME      */
  7116.             null,                   /* NON_UNIQUE      */
  7117.             0,                      /* SQL_TABLE_STAT  */
  7118.             null,                   /* SEQ_IN_INDEX    */
  7119.             null,                   /* COLUMN_NAME     */
  7120.             null,                   /* COLLATION       */
  7121.             0,                      /* index_id        */
  7122.             x.rows,                 /* CARDINALITY     */
  7123.             x.dpages,               /* PAGES           */
  7124.             0                       /* status          */
  7125.         FROM sysindexes x, sysobjects o
  7126.         WHERE not (@table_id is null)
  7127.             AND o.id = @table_id
  7128.             AND x.id = o.id
  7129.             AND (x.indid = 0 or x.indid = 1)    /*  If there are no indexes */
  7130.                                                 /*  then table stats are in */
  7131.                                                 /*  a row with indid =0     */
  7132.  
  7133.     SELECT
  7134.         TABLE_QUALIFIER,
  7135.         TABLE_OWNER,
  7136.         TABLE_NAME,
  7137.         NON_UNIQUE,
  7138.         INDEX_QUALIFIER,
  7139.         INDEX_NAME,
  7140.         TYPE,
  7141.         SEQ_IN_INDEX,
  7142.         COLUMN_NAME,
  7143.         COLLATION,
  7144.         CARDINALITY,
  7145.         PAGES,
  7146.         FILTER_CONDITION = convert(varchar(128),null)
  7147.     FROM #TmpIndex
  7148.     WHERE
  7149.         INDEX_NAME like @index_name /* If matching name */
  7150.         or INDEX_NAME is null       /* If SQL_TABLE_STAT row */
  7151.     ORDER BY 4, 7, 6, 8
  7152.  
  7153.     DROP TABLE #TmpIndex, #TmpColumns
  7154. go
  7155.  
  7156. grant execute on sp_statistics to public
  7157. go
  7158.  
  7159. dump tran master with no_log
  7160. go
  7161.  
  7162. print 'creating sp_stored_procedures'
  7163. go
  7164.  
  7165.  
  7166. /* pre 8.0 version */
  7167. create procedure sp_stored_procedures(
  7168.                         @sp_name        varchar(102) = null,
  7169.                         @sp_owner       varchar(96) = null,
  7170.                         @sp_qualifier   varchar(32) = null)
  7171. as
  7172.     declare @proc_type smallint
  7173.  
  7174.     if @sp_qualifier is not null
  7175.     begin
  7176.         if db_name() <> @sp_qualifier
  7177.         begin
  7178.             if @sp_qualifier = ''
  7179.             begin
  7180.                 /* in this case, we need to return an empty result set */
  7181.                 /* because the user has requested a database with an empty name */
  7182.                 select @sp_name = ''
  7183.                 select @sp_owner = ''
  7184.             end else
  7185.             begin   /* If qualifier doesn't match current database */
  7186.                 raiserror 20001 '~~Rush_51~~'
  7187.                 return
  7188.             end
  7189.         end
  7190.     end
  7191.  
  7192.     if @sp_name is null
  7193.     begin  /*  If procedure name not supplied, match all */
  7194.         select @sp_name = '%'
  7195.     end
  7196.     else begin
  7197.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7198.         begin
  7199.             if exists (select * from sysobjects
  7200.                 where uid = user_id()
  7201.                     and name = @sp_name
  7202.                     and type = 'P') /* Object type of Procedure */
  7203.             begin
  7204.                 select @sp_owner = user_name()
  7205.             end
  7206.         end
  7207.     end
  7208.     if @sp_owner is null    /*  If procedure owner not supplied, match all */
  7209.         select @sp_owner = '%'
  7210.  
  7211.     select @proc_type=2     /* Return 2 for 4.2 and later servers. */
  7212.  
  7213.     select
  7214.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  7215.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  7216.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  7217.         NUM_INPUT_PARAMS = -1,  /* Constant since value unknown */
  7218.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7219.         NUM_RESULT_SETS = -1,   /* Constant since value unknown */
  7220.         REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  7221.         PROCEDURE_TYPE = @proc_type
  7222.     from
  7223.         sysobjects o,syscomments c,sysusers u
  7224.     where
  7225.         o.name like @sp_name
  7226.         and c.colid = 1
  7227.         and user_name(o.uid) like @sp_owner
  7228.         and o.type = 'P'        /* Object type of Procedure */
  7229.         and c.id = o.id
  7230.         and u.uid = user_id()   /* constrain sysusers uid for use in subquery */
  7231.         and (suser_id() = 1     /* User is the System Administrator */
  7232.             or o.uid = user_id()    /* User created the object */
  7233.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  7234.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  7235.              from sysprotects p
  7236.              /* outer join to correlate with all rows in sysobjects */
  7237.              where p.id =* o.id
  7238.                  /*  get rows for public,current user,user's group */
  7239.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  7240.                  /* check for SELECT,EXECUTE privilege */
  7241.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  7242.             ) = 1    /* final magic...compare Grants    */
  7243.         )
  7244.     order by 1, 2, 3
  7245. go
  7246.  
  7247. grant execute on sp_stored_procedures to public
  7248. go
  7249.  
  7250.  
  7251. if (charindex('7.00', @@version) > 0 or
  7252.     charindex('8.00', @@version) > 0)
  7253.     drop procedure sp_stored_procedures
  7254. else
  7255. begin
  7256.     print ''
  7257.     print ''
  7258.     print 'Warning:'
  7259.     print 'you are installing the stored procedures '
  7260.     print 'on a pre 7.0 SQL Server.'
  7261.     print 'Ignore the following errors.'
  7262. end
  7263. go
  7264.  
  7265.  
  7266. /* 7.0 version */
  7267. create procedure sp_stored_procedures(
  7268.                         @sp_name        nvarchar(390) = null,
  7269.                         @sp_owner       nvarchar(384) = null,
  7270.                         @sp_qualifier   sysname = null)
  7271. as
  7272.     declare @proc_type smallint
  7273.  
  7274.     if @sp_qualifier is not null
  7275.     begin
  7276.         if db_name() <> @sp_qualifier
  7277.         begin
  7278.             if @sp_qualifier = ''
  7279.             begin
  7280.                 /* in this case, we need to return an empty result set */
  7281.                 /* because the user has requested a database with an empty name */
  7282.                 select @sp_name = ''
  7283.                 select @sp_owner = ''
  7284.             end else
  7285.             begin   /* If qualifier doesn't match current database */
  7286.                 raiserror (15250, -1,-1)
  7287.                 return
  7288.             end
  7289.         end
  7290.     end
  7291.  
  7292.     if @sp_name is null
  7293.     begin  /*  If procedure name not supplied, match all */
  7294.         select @sp_name = '%'
  7295.     end
  7296.     else begin
  7297.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7298.         begin
  7299.             if exists (select * from sysobjects
  7300.                 where uid = user_id()
  7301.                     and name = @sp_name
  7302.                     and type = 'P') /* Object type of Procedure */
  7303.             begin
  7304.                 select @sp_owner = user_name()
  7305.             end
  7306.         end
  7307.     end
  7308.     if @sp_owner is null    /*  If procedure owner not supplied, match all */
  7309.         select @sp_owner = '%'
  7310.  
  7311.     select @proc_type=2     /* Return 2 for 4.2 and later servers. */
  7312.  
  7313.     select
  7314.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7315.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7316.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7317.         NUM_INPUT_PARAMS = -1,  /* Constant since value unknown */
  7318.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7319.         NUM_RESULT_SETS = -1,   /* Constant since value unknown */
  7320.         REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  7321.         PROCEDURE_TYPE = @proc_type
  7322.     from
  7323.         sysobjects o,syscomments c
  7324.     where
  7325.         o.name like @sp_name
  7326.         and c.colid = 1
  7327.         and user_name(o.uid) like @sp_owner
  7328.         and o.type = 'P'        /* Object type of Procedure */
  7329.         and c.id = o.id
  7330.         and permissions (o.id)&32 <> 0
  7331.     order by 1, 2, 3
  7332. go
  7333.  
  7334. grant execute on sp_stored_procedures to public
  7335. go
  7336.  
  7337. if (charindex('8.00', @@version) > 0)
  7338.     drop procedure sp_stored_procedures
  7339. else
  7340. begin
  7341.     print ''
  7342.     print ''
  7343.     print 'Warning:'
  7344.     print 'you are installing the stored procedures '
  7345.     print 'on a pre 8.0 SQL Server.'
  7346.     print 'Ignore the following errors.'
  7347. end
  7348. go
  7349.  
  7350.  
  7351. /* 8.0 version */
  7352. create procedure sp_stored_procedures(
  7353.                         @sp_name    nvarchar(390) = null,
  7354.                         @sp_owner   nvarchar(384) = null,
  7355.                         @sp_qualifier   sysname = null)
  7356. as
  7357.     declare @proc_type smallint
  7358.  
  7359.     if @sp_qualifier is not null
  7360.     begin
  7361.         if db_name() <> @sp_qualifier
  7362.         begin
  7363.             if @sp_qualifier = ''
  7364.             begin
  7365.                 /* in this case, we need to return an empty result set */
  7366.                 /* because the user has requested a database with an empty name */
  7367.                 select @sp_name = ''
  7368.                 select @sp_owner = ''
  7369.             end else
  7370.             begin   /* If qualifier doesn't match current database */
  7371.                 raiserror (15250, -1,-1)
  7372.                 return
  7373.             end
  7374.         end
  7375.     end
  7376.  
  7377.     if @sp_name is null
  7378.     begin  /*  If procedure name not supplied, match all */
  7379.         select @sp_name = '%'
  7380.     end
  7381.     else
  7382.     begin
  7383.     if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7384.     begin
  7385.         if exists (select * from sysobjects
  7386.             where uid = user_id()
  7387.                 and name = @sp_name
  7388.                 and type in ('P', 'FN', 'TF', 'IF'))        /* Procedures, scalar UDF, table UDF */
  7389.         begin
  7390.             select @sp_owner = user_name()
  7391.         end
  7392.     end
  7393.     end
  7394.     
  7395.     if @sp_owner is null        /* If procedure owner not supplied, match all */
  7396.     select @sp_owner = '%'
  7397.  
  7398.     select @proc_type=2         /* Return 2 for 4.2 and later servers. */
  7399.  
  7400.     select
  7401.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7402.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7403.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7404.         NUM_INPUT_PARAMS = -1,  /* Constant since value unknown */
  7405.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7406.         NUM_RESULT_SETS = -1,   /* Constant since value unknown */
  7407.         REMARKS = convert(varchar(254),null),   /* Remarks are NULL */
  7408.         PROCEDURE_TYPE = @proc_type
  7409.     from
  7410.         sysobjects o,syscomments c
  7411.     where
  7412.         o.name like @sp_name
  7413.         and c.colid = 1
  7414.         and user_name(o.uid) like @sp_owner
  7415.         and o.type in ('P', 'FN', 'TF', 'IF')       /* Object type of Procedure, scalar UDF, table UDF */
  7416.         and c.id = o.id
  7417.         and permissions (o.id)&32 <> 0
  7418.     order by 1, 2, 3
  7419. go
  7420.  
  7421. grant execute on sp_stored_procedures to public
  7422. go
  7423.  
  7424. dump tran master with no_log
  7425. go
  7426.  
  7427.  
  7428. print 'creating sp_table_privileges'
  7429. go
  7430.  
  7431. /*  Procedure for pre 6.50 server */
  7432. CREATE PROCEDURE sp_table_privileges (
  7433.             @table_name         varchar(90),
  7434.             @table_owner        varchar(90) = null,
  7435.             @table_qualifier    varchar(32) = null)
  7436. as
  7437.     set nocount on
  7438.  
  7439.     declare @table_id    int,
  7440.             @owner_id    int,
  7441.             @full_table_name char(181)
  7442.     declare @refconst int
  7443.  
  7444.     select @refconst = 1
  7445.     if  (charindex('6.00', @@version) = 0)
  7446.         select @refconst = NULL
  7447.  
  7448.     if @table_qualifier is not null
  7449.     begin
  7450.         if db_name() != @table_qualifier
  7451.         begin   /* If qualifier doesn't match current database */
  7452.             raiserror 20001 'Table qualifier must be name of current database'
  7453.             return
  7454.         end
  7455.     end
  7456.     if @table_owner is null
  7457.     begin   /* If unqualified table name */
  7458.         SELECT @full_table_name = @table_name
  7459.     end
  7460.     else
  7461.     begin   /* Qualified table name */
  7462.         SELECT @full_table_name = @table_owner + '.' + @table_name
  7463.     end
  7464.     /*  Get Object ID */
  7465.     SELECT @table_id = object_id(@full_table_name)
  7466.  
  7467.     if @@trancount != 0
  7468.     begin   /* If inside a transaction */
  7469.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  7470.         return
  7471.     end
  7472.     create table #table_priv1(
  7473.         table_qualifier         varchar(32) NOT NULL,
  7474.         table_owner             varchar(32) NOT NULL,
  7475.         table_name              varchar(32) NOT NULL,
  7476.         grantor                 varchar(32) NOT NULL,
  7477.         grantee                 varchar(32) NOT NULL,
  7478.         select_privilege        int NOT NULL,
  7479.         insert_privilege        int NOT NULL,
  7480.         update_privilege        int NOT NULL,
  7481.         delete_privilege        int NOT NULL,
  7482.         references_privilege    int NULL,
  7483.         is_grantable            varchar(3) NOT NULL,
  7484.         uid                     int NOT NULL,
  7485.         gid                     int NOT NULL)
  7486.  
  7487.     insert into #table_priv1
  7488.         select distinct
  7489.             db_name(),
  7490.             user_name(o.uid),
  7491.             o.name,
  7492.             user_name(o.uid),
  7493.             u.name,
  7494.             0,
  7495.             0,
  7496.             0,
  7497.             0,
  7498.             @refconst,
  7499.             'no',
  7500.             u.uid,
  7501.             u.gid
  7502.         from sysusers u, sysobjects o
  7503.         where o.id = @table_id and u.uid != u.gid
  7504.            and sysstat & 0xf in (1,2,3) /* only valid for system tables,
  7505.                                         ** user tables, and views. */
  7506.  
  7507.     /*
  7508.     ** now add row for table owner
  7509.     */
  7510.     if exists (
  7511.         select *
  7512.             from #table_priv1
  7513.             where grantor = grantee)
  7514.     begin
  7515.         update #table_priv1
  7516.         set
  7517.             select_privilege = 1,
  7518.             update_privilege = 1,
  7519.             insert_privilege = 1,
  7520.             delete_privilege = 1,
  7521.             references_privilege = 1,
  7522.             is_grantable = 'yes'
  7523.         where grantor = grantee
  7524.     end
  7525.     else
  7526.     begin
  7527.         insert into #table_priv1
  7528.             select  db_name(),
  7529.                 user_name(o.uid),
  7530.                 o.name,
  7531.                 user_name(o.uid),
  7532.                 user_name(o.uid),
  7533.                 1,
  7534.                 1,
  7535.                 1,
  7536.                 1,
  7537.                 @refconst,
  7538.                 'yes',
  7539.                 o.uid,
  7540.                 u.gid
  7541.             from sysobjects o, sysusers u
  7542.             where o.id = @table_id and u.uid = o.uid
  7543.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7544.                                         ** user tables, and views. */
  7545.  
  7546.     end
  7547.  
  7548.     update #table_priv1
  7549.     set select_privilege = 1
  7550.     where
  7551.         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 = 205
  7559.                 and action = 193)
  7560.         and not exists (
  7561.             select * from sysprotects
  7562.             where
  7563.                 id = @table_id
  7564.                 and (#table_priv1.uid = uid
  7565.                     or #table_priv1.gid = uid
  7566.                     or uid = 0)
  7567.                 and protecttype = 206
  7568.                 and action = 193)
  7569.  
  7570.     update #table_priv1
  7571.     set insert_privilege = 1
  7572.     where
  7573.         exists (
  7574.             select * from sysprotects
  7575.             where
  7576.                 id = @table_id
  7577.                 and (#table_priv1.uid = uid
  7578.                     or #table_priv1.gid = uid
  7579.                     or uid = 0)
  7580.                 and protecttype = 205
  7581.                 and action = 195)
  7582.         and not exists (
  7583.             select * from sysprotects
  7584.             where
  7585.                 id = @table_id
  7586.                 and (#table_priv1.uid = uid
  7587.                     or #table_priv1.gid = uid
  7588.                     or uid = 0)
  7589.                 and protecttype = 206
  7590.                 and action = 195)
  7591.  
  7592.     update #table_priv1
  7593.     set delete_privilege = 1
  7594.     where
  7595.         exists (
  7596.             select * from sysprotects
  7597.             where
  7598.                 id = @table_id
  7599.                 and (#table_priv1.uid = uid
  7600.                     or #table_priv1.gid = uid
  7601.                     or uid = 0)
  7602.                 and protecttype = 205
  7603.                 and action = 196)
  7604.         and not exists (select * from sysprotects
  7605.             where
  7606.                 id = @table_id
  7607.                 and (#table_priv1.uid = uid
  7608.                     or #table_priv1.gid = uid
  7609.                     or uid = 0)
  7610.                 and protecttype = 206
  7611.                 and action = 196)
  7612.  
  7613.     update #table_priv1
  7614.     set update_privilege = 1
  7615.     where
  7616.         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 = 205
  7624.                 and action = 197)
  7625.         and not exists (
  7626.             select * from sysprotects
  7627.             where
  7628.                 id = @table_id
  7629.                 and (#table_priv1.uid = uid
  7630.                     or #table_priv1.gid = uid
  7631.                     or uid = 0)
  7632.                 and protecttype = 206
  7633.                 and action = 197)
  7634.  
  7635.     update #table_priv1
  7636.     set references_privilege = 1
  7637.     where
  7638.         exists (
  7639.             select * from sysprotects
  7640.             where
  7641.                 id = @table_id
  7642.                 and (#table_priv1.uid = uid
  7643.                     or #table_priv1.gid = uid
  7644.                     or uid = 0)
  7645.                 and protecttype = 205
  7646.                 and action = 26)
  7647.         and not exists (
  7648.             select * from sysprotects
  7649.             where
  7650.                 id = @table_id
  7651.                 and (#table_priv1.uid = uid
  7652.                     or #table_priv1.gid = uid
  7653.                     or uid = 0)
  7654.                 and protecttype = 206
  7655.                 and action = 26)
  7656.  
  7657.     create table #table_priv2(
  7658.         table_qualifier varchar(32) NULL,
  7659.         table_owner     varchar(32) NULL,
  7660.         table_name      varchar(32) NOT NULL,
  7661.         grantor         varchar(32) NULL,
  7662.         grantee         varchar(32) NOT NULL,
  7663.         privilege       varchar(32) NOT NULL,
  7664.         is_grantable    varchar(3) NULL)
  7665.  
  7666.     insert into #table_priv2
  7667.         select
  7668.             table_qualifier,
  7669.             table_owner,
  7670.             table_name,
  7671.             grantor,
  7672.             grantee,
  7673.             'SELECT',
  7674.             is_grantable
  7675.         from #table_priv1
  7676.         where select_privilege = 1
  7677.  
  7678.  
  7679.     insert into #table_priv2
  7680.         select
  7681.             table_qualifier,
  7682.             table_owner,
  7683.             table_name,
  7684.             grantor,
  7685.             grantee,
  7686.             'INSERT',
  7687.             is_grantable
  7688.         from #table_priv1
  7689.         where insert_privilege = 1
  7690.  
  7691.  
  7692.     insert into #table_priv2
  7693.         select
  7694.             table_qualifier,
  7695.             table_owner,
  7696.             table_name,
  7697.             grantor,
  7698.             grantee,
  7699.             'DELETE',
  7700.             is_grantable
  7701.         from #table_priv1
  7702.         where delete_privilege = 1
  7703.  
  7704.  
  7705.     insert into #table_priv2
  7706.         select
  7707.             table_qualifier,
  7708.             table_owner,
  7709.             table_name,
  7710.             grantor,
  7711.             grantee,
  7712.             'UPDATE',
  7713.             is_grantable
  7714.         from #table_priv1
  7715.         where update_privilege = 1
  7716.  
  7717.     insert into #table_priv2
  7718.         select
  7719.             table_qualifier,
  7720.             table_owner,
  7721.             table_name,
  7722.             grantor,
  7723.             grantee,
  7724.             'REFERENCES',
  7725.             is_grantable
  7726.         from #table_priv1
  7727.         where references_privilege = 1
  7728.  
  7729.  
  7730.     select * from #table_priv2
  7731.     order by table_owner,table_name,privilege,grantee
  7732. /*  order by 2,3,6,5 Can't use since fails on 4.21a server */
  7733. go
  7734.  
  7735. if (charindex('6.50', @@version) = 0 and
  7736.     charindex('7.00', @@version) = 0 and
  7737.     charindex('8.00', @@version) = 0)
  7738. begin
  7739.     print ''
  7740.     print ''
  7741.     print 'Warning:'
  7742.     print 'you are installing the stored procedures '
  7743.     print 'on a pre 6.50 SQL Server.'
  7744.     print 'Ignore the following errors.'
  7745. end
  7746. else
  7747.     drop proc sp_table_privileges
  7748. go
  7749.  
  7750.  
  7751. /*  Procedure for 6.50 server */
  7752. CREATE PROCEDURE sp_table_privileges (
  7753.             @table_name         varchar(96),
  7754.             @table_owner        varchar(96) = null,
  7755.             @table_qualifier    varchar(32) = null)
  7756. as
  7757.  
  7758.     if @table_qualifier is not null
  7759.     begin
  7760.         if db_name() <> @table_qualifier
  7761.         begin   /* If qualifier doesn't match current database */
  7762.             raiserror (15250, -1,-1)
  7763.             return
  7764.         end
  7765.     end
  7766.     if @table_name is null
  7767.         select @table_name = '%'
  7768.     if @table_owner is null /* If no owner supplied, force wildcard */
  7769.         select @table_owner = '%'
  7770.  
  7771.     select
  7772.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7773.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7774.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7775.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  7776.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  7777.         convert(varchar(32),case p.action
  7778.              when 193 then 'SELECT'
  7779.              when 195 then 'INSERT'
  7780.              when 196 then 'DELETE'
  7781.              when 197 then 'UPDATE'
  7782.              else 'REFERENCES'
  7783.         end) PRIVILEGE,
  7784.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7785.             else 'YES'
  7786.         end) IS_GRANTABLE
  7787.     from sysprotects p, sysobjects o, sysusers u
  7788.     where
  7789.         p.id = o.id
  7790.         and o.type in ('U','V','S')
  7791.         and object_name(o.id) like @table_name
  7792.         and user_name(o.uid) like @table_owner
  7793.             /* expand groups */
  7794.         and ((p.uid = u.uid and u.uid <> u.gid) or
  7795.              (p.uid = u.gid and u.uid <> u.gid))
  7796.         and p.protecttype <> 206    /* only grant rows */
  7797.         and p.action in (26,193,195,196,197)
  7798.         and o.uid <> u.uid          /* no rows for owner */
  7799.         and not exists (            /* exclude revoke'd privileges */
  7800.             select *
  7801.             from sysprotects p1
  7802.             where
  7803.                 p1.protecttype = 206
  7804.                 and p1.action = p.action
  7805.                 and p1.id = p.id
  7806.                 and p1.uid = u.uid)
  7807.     union
  7808.     select  /*  Add rows for table owner */
  7809.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7810.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7811.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7812.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  7813.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  7814.         convert(varchar(32),case v.number
  7815.             when 193 then 'SELECT'
  7816.             when 195 then 'INSERT'
  7817.             when 196 then 'DELETE'
  7818.             when 197 then 'UPDATE'
  7819.             else 'REFERENCES'
  7820.         end) PRIVILEGE,
  7821.         convert(varchar(3),'YES') IS_GRANTABLE
  7822.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7823.     where
  7824.         object_name(o.id) like @table_name
  7825.         and o.type in ('U','V','S')
  7826.         and user_name(o.uid) like @table_owner
  7827.         and u.uid = 1       /* grantor is dbo of database */
  7828.         and v.type = 'P'    /* cross product to get all exposed privileges */
  7829.         and v.number in (26,193,195,196,197)
  7830.         and not exists (    /* exclude revoke'd privileges */
  7831.             select *
  7832.             from sysprotects p1
  7833.             where
  7834.                 p1.protecttype = 206
  7835.                 and p1.action = v.number
  7836.                 and p1.id = o.id
  7837.                 and p1.uid = o.uid)
  7838.     order by 2,3,6,5
  7839. go
  7840.  
  7841.  
  7842. if (charindex('7.00', @@version) > 0 or
  7843.     charindex('8.00', @@version) > 0)
  7844.     drop proc sp_table_privileges
  7845. else
  7846. begin
  7847.     print ''
  7848.     print ''
  7849.     print 'Warning:'
  7850.     print 'you are installing the stored procedures '
  7851.     print 'on a pre 7.0 SQL Server.'
  7852.     print 'Ignore the following errors.'
  7853. end
  7854. go
  7855.  
  7856. /*  Procedure for 7.0 and later servers */
  7857. CREATE PROCEDURE sp_table_privileges (
  7858.             @table_name         nvarchar(384),
  7859.             @table_owner        nvarchar(384) = null,
  7860.             @table_qualifier    sysname = null)
  7861. as
  7862.  
  7863.     if @table_qualifier is not null
  7864.     begin
  7865.         if db_name() <> @table_qualifier
  7866.         begin   /* If qualifier doesn't match current database */
  7867.             raiserror (15250, -1,-1)
  7868.             return
  7869.         end
  7870.     end
  7871.     if @table_name is null
  7872.         select @table_name = '%'
  7873.     if @table_owner is null /* If no owner supplied, force wildcard */
  7874.         select @table_owner = '%'
  7875.  
  7876.     select
  7877.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7878.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7879.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7880.         convert(sysname,user_name(p.grantor)) GRANTOR,
  7881.         convert(sysname,user_name(u.uid)) GRANTEE,
  7882.         convert(varchar(32),case p.action
  7883.              when 193 then 'SELECT'
  7884.              when 195 then 'INSERT'
  7885.              when 196 then 'DELETE'
  7886.              when 197 then 'UPDATE'
  7887.              else 'REFERENCES'
  7888.         end) PRIVILEGE,
  7889.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7890.             else 'YES'
  7891.         end) IS_GRANTABLE
  7892.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  7893.     where
  7894.         p.id = o.id
  7895.         and o.type in ('U','V','S')
  7896.         and object_name(o.id) like @table_name
  7897.         and user_name(o.uid) like @table_owner
  7898.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  7899.         and (u.uid > 0 and u.uid < 16384)
  7900.         and ((p.uid = u.uid) or
  7901.              (p.uid = m.groupuid and u.uid = m.memberuid))
  7902.         and p.protecttype <> 206    /* only grant rows */
  7903.         and p.action in (26,193,195,196,197)
  7904.         and o.uid <> u.uid          /* no rows for owner */
  7905.         and not exists (            /* exclude revoke'd privileges */
  7906.             select *
  7907.             from sysprotects p1
  7908.             where
  7909.                 p1.protecttype = 206
  7910.                 and p1.action = p.action
  7911.                 and p1.id = p.id
  7912.                 and p1.uid = u.uid)
  7913.     union
  7914.     select  /*  Add rows for table owner */
  7915.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7916.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7917.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7918.         convert(sysname,user_name(u.uid)) GRANTOR,
  7919.         convert(sysname,user_name(o.uid)) GRANTEE,
  7920.         convert(varchar(32),case v.number
  7921.             when 193 then 'SELECT'
  7922.             when 195 then 'INSERT'
  7923.             when 196 then 'DELETE'
  7924.             when 197 then 'UPDATE'
  7925.             else 'REFERENCES'
  7926.         end) PRIVILEGE,
  7927.         convert(varchar(3),'YES') IS_GRANTABLE
  7928.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7929.     where
  7930.         object_name(o.id) like @table_name
  7931.         and o.type in ('U','V','S')
  7932.         and user_name(o.uid) like @table_owner
  7933.         and u.uid = 1       /* grantor is 'dbo' of database */
  7934.         and v.type = N'P'   /* cross product to get all exposed privileges */
  7935.         and v.number in (26,193,195,196,197)
  7936.         and not exists (    /* exclude revoke'd privileges */
  7937.             select *
  7938.             from sysprotects p1
  7939.             where
  7940.                 p1.protecttype = 206
  7941.                 and p1.action = v.number
  7942.                 and p1.id = o.id
  7943.                 and p1.uid = o.uid)
  7944.     order by 2,3,6,5
  7945. go
  7946.  
  7947. grant execute on sp_table_privileges to public
  7948. go
  7949.  
  7950. dump tran master with no_log
  7951. go
  7952.  
  7953. print 'creating sp_tables'
  7954. go
  7955.  
  7956. /*  Procedure for 6.50 and earlier servers */
  7957. create procedure sp_tables(
  7958.                @table_name      varchar(96) = null,
  7959.                @table_owner     varchar(96) = null,
  7960.                @table_qualifier varchar(32) = null,
  7961.                @table_type      varchar(100) = null)
  7962. as
  7963.     declare @type1 varchar(3)
  7964.     declare @tableindex int
  7965.  
  7966.  
  7967.     /* Special feature #1:  enumerate databases when owner and name
  7968.          are blank but qualifier is explicitly '%'.  */
  7969.     if @table_qualifier = '%' and
  7970.         @table_owner = '' and
  7971.         @table_name = ''
  7972.     begin   /* If enumerating databases */
  7973.         select
  7974.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  7975.             TABLE_OWNER = convert(varchar(32),null),
  7976.             TABLE_NAME = convert(varchar(32),null),
  7977.             TABLE_TYPE = convert(varchar(32),null),
  7978.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7979.         from master.dbo.sysdatabases d
  7980.         where d.name <> 'model' /* eliminate MODEL database */
  7981.         order by 1
  7982.     end
  7983.  
  7984.     /* Special feature #2:  enumerate owners when qualifier and name
  7985.          are blank but owner is explicitly '%'.  */
  7986.     else if @table_qualifier = '' and
  7987.         @table_owner = '%' and
  7988.         @table_name = ''
  7989.     begin   /* If enumerating owners */
  7990.         select distinct
  7991.             TABLE_QUALIFIER = convert(varchar(32),null),
  7992.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  7993.             TABLE_NAME = convert(varchar(32),null),
  7994.             TABLE_TYPE = convert(varchar(32),null),
  7995.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7996.         from sysobjects
  7997.         order by 2
  7998.     end
  7999.  
  8000.     /* Special feature #3:  enumerate table types when qualifier, owner and
  8001.          name are blank but table type is explicitly '%'.   */
  8002.     else if @table_qualifier = '' and
  8003.         @table_owner = '' and
  8004.         @table_name = '' and
  8005.         @table_type = '%'
  8006.     begin   /* If enumerating table types */
  8007.         select
  8008.             TABLE_QUALIFIER = convert(varchar(32),null),
  8009.             TABLE_OWNER = convert(varchar(32),null),
  8010.             TABLE_NAME = convert(varchar(32),null),
  8011.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  8012.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8013.         from sysobjects o, syscolumns c
  8014.         where o.id=c.id and o.name='sysusers' and colid<=3
  8015.     end
  8016.  
  8017.     else
  8018.     begin /* end of special features - do normal processing */
  8019.         if @table_qualifier is not null
  8020.         begin
  8021.             if db_name() <> @table_qualifier
  8022.             begin
  8023.                 if @table_qualifier = ''
  8024.                 begin  /* If empty qualifier supplied */
  8025.                     /* Force an empty result set */
  8026.                     select @table_name = ''
  8027.                     select @table_owner = ''
  8028.                 end
  8029.                 else
  8030.                 begin   /* If qualifier doesn't match current database */
  8031.                     raiserror 20001 '~~Rush_5~~'
  8032.                     return
  8033.                 end
  8034.             end
  8035.         end
  8036.         if @table_type is null
  8037.         begin   /* Select all ODBC supported table types */
  8038.             select @type1 = 'SUV'
  8039.         end
  8040.         else
  8041.         begin
  8042.             /*  TableType is case sensitive if CS server */
  8043.             select @type1 = null
  8044.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8045.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8046.             if (charindex('''TABLE''',@table_type) <> 0)
  8047.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8048.             if (charindex('''VIEW''',@table_type) <> 0)
  8049.                 select @type1 = @type1 + 'V'    /* Add Views */
  8050.         end
  8051.         if @table_name is null
  8052.         begin   /*  If table name not supplied, match all */
  8053.             select @table_name = '%'
  8054.         end
  8055.         else
  8056.         begin
  8057.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8058.             begin   /* If owner not specified and table is specified */
  8059.                 if exists (select * from sysobjects
  8060.                     where uid = user_id()
  8061.                     and name = @table_name
  8062.                     and (type = 'U' or type = 'V' or type = 'S'))
  8063.                 begin   /* Override supplied owner w/owner of table */
  8064.                     select @table_owner = user_name()
  8065.                 end
  8066.             end
  8067.         end
  8068.         if @table_owner is null /* If no owner supplied, force wildcard */
  8069.             select @table_owner = '%'
  8070.         select
  8071.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  8072.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  8073.             TABLE_NAME = convert(varchar(32),o.name),   /* make nullable */
  8074.             TABLE_TYPE = convert(varchar(32),rtrim(
  8075.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8076.                     (ascii(o.type)-83)*12+1,12))),  /* 'S'=0,'U'=2,'V'=3 */
  8077.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8078.         from sysusers u, sysobjects o
  8079.         where
  8080.             o.name like @table_name
  8081.             and user_name(o.uid) like @table_owner
  8082.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8083.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8084.             and (
  8085.                 suser_id() = 1   /* User is the System Administrator */
  8086.                 or o.uid = user_id()     /* User created the object */
  8087.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8088.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8089.                     from sysprotects p
  8090.                     /* outer join to correlate with all rows in sysobjects */
  8091.                     where p.id =* o.id
  8092.                         /* get rows for public,current user,user's group */
  8093.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  8094.                         /* check for SELECT,EXECUTE privilege */
  8095.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8096.                     ) = 1   /* final magic...compare Grants   */
  8097.             )
  8098.         order by 4, 1, 2, 3
  8099.     end
  8100. go
  8101.  
  8102. if (charindex('7.00', @@version) > 0 or
  8103.     charindex('8.00', @@version) > 0)
  8104.     drop procedure sp_tables
  8105. else
  8106. begin
  8107.     print ''
  8108.     print ''
  8109.     print 'Warning:'
  8110.     print 'you are installing the stored procedures '
  8111.     print 'on a pre 8.0 SQL Server.'
  8112.     print 'Ignore the following errors.'
  8113. end
  8114. go
  8115.  
  8116. /*  Procedure for 8.0 server */
  8117. create procedure sp_tables(
  8118.                @table_name      nvarchar(384)   = null,
  8119.                @table_owner     nvarchar(384)   = null,
  8120.                @table_qualifier sysname = null,
  8121.                @table_type      varchar(100) = null)
  8122. as
  8123.     declare @type1 varchar(3)
  8124.     declare @tableindex int
  8125.  
  8126.  
  8127.     /* Special feature #1:  enumerate databases when owner and name
  8128.          are blank but qualifier is explicitly '%'.  */
  8129.     if @table_qualifier = '%' and
  8130.         @table_owner = '' and
  8131.         @table_name = ''
  8132.     begin   /* If enumerating databases */
  8133.         select
  8134.             TABLE_QUALIFIER = convert(sysname,d.name),
  8135.             TABLE_OWNER = convert(sysname,null),
  8136.             TABLE_NAME = convert(sysname,null),
  8137.             TABLE_TYPE = convert(varchar(32),null),
  8138.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8139.         from master.dbo.sysdatabases d
  8140.         where d.name <> 'model' /* eliminate MODEL database */
  8141.         order by 1
  8142.     end
  8143.  
  8144.     /* Special feature #2:  enumerate owners when qualifier and name
  8145.          are blank but owner is explicitly '%'.  */
  8146.     else if @table_qualifier = '' and
  8147.         @table_owner = '%' and
  8148.         @table_name = ''
  8149.     begin   /* If enumerating owners */
  8150.         select distinct
  8151.             TABLE_QUALIFIER = convert(sysname,null),
  8152.             TABLE_OWNER = convert(sysname,user_name(uid)),
  8153.             TABLE_NAME = convert(sysname,null),
  8154.             TABLE_TYPE = convert(varchar(32),null),
  8155.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8156.         from sysobjects
  8157.         order by 2
  8158.     end
  8159.  
  8160.     /* Special feature #3:  enumerate table types when qualifier, owner and
  8161.          name are blank but table type is explicitly '%'.   */
  8162.     else if @table_qualifier = '' and
  8163.         @table_owner = '' and
  8164.         @table_name = '' and
  8165.         @table_type = '%'
  8166.     begin   /* If enumerating table types */
  8167.         select
  8168.             TABLE_QUALIFIER = convert(sysname,null),
  8169.             TABLE_OWNER = convert(sysname,null),
  8170.             TABLE_NAME = convert(sysname,null),
  8171.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  8172.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8173.         from sysobjects o, syscolumns c
  8174.         where o.id=c.id and o.name='sysusers' and colid<=3
  8175.     end
  8176.  
  8177.     else
  8178.     begin /* end of special features - do normal processing */
  8179.         if @table_qualifier is not null
  8180.         begin
  8181.             if db_name() <> @table_qualifier
  8182.             begin
  8183.                 if @table_qualifier = ''
  8184.                 begin  /* If empty qualifier supplied */
  8185.                     /* Force an empty result set */
  8186.                     select @table_name = ''
  8187.                     select @table_owner = ''
  8188.                 end
  8189.                 else
  8190.                 begin   /* If qualifier doesn't match current database */
  8191.                     raiserror (15250, -1,-1)
  8192.                     return
  8193.                 end
  8194.             end
  8195.         end
  8196.         if @table_type is null
  8197.         begin   /* Select all ODBC supported table types */
  8198.             select @type1 = 'SUV'
  8199.         end
  8200.         else
  8201.         begin
  8202.             /*  TableType is case sensitive if CS server */
  8203.             select @type1 = ''
  8204.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8205.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8206.             if (charindex('''TABLE''',@table_type) <> 0)
  8207.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8208.             if (charindex('''VIEW''',@table_type) <> 0)
  8209.                 select @type1 = @type1 + 'V'    /* Add Views */
  8210.         end
  8211.         if @table_name is null
  8212.         begin   /*  If table name not supplied, match all */
  8213.             select @table_name = '%'
  8214.         end
  8215.         else
  8216.         begin
  8217.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8218.             begin   /* If owner not specified and table is specified */
  8219.                 if exists (select * from sysobjects
  8220.                     where uid = user_id()
  8221.                     and name = @table_name
  8222.                     and (type = 'U' or type = 'V' or type = 'S'))
  8223.                 begin   /* Override supplied owner w/owner of table */
  8224.                     select @table_owner = user_name()
  8225.                 end
  8226.             end
  8227.         end
  8228.         if @table_owner is null /* If no owner supplied, force wildcard */
  8229.             select @table_owner = '%'
  8230.         select
  8231.             TABLE_QUALIFIER = convert(sysname,db_name()),
  8232.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  8233.             TABLE_NAME = convert(sysname,o.name),   /* make nullable */
  8234.             TABLE_TYPE = convert(varchar(32),rtrim(
  8235.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8236.                     (ascii(o.type)-83)*12+1,12))),  /* 'S'=0,'U'=2,'V'=3 */
  8237.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8238.         from sysobjects o
  8239.         where
  8240.             o.name like @table_name
  8241.             and user_name(o.uid) like @table_owner
  8242.             and o.type in ('U','V','S')
  8243.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8244.             and permissions (o.id)&4096 <> 0
  8245.         order by 4, 1, 2, 3
  8246.     end
  8247. go
  8248.  
  8249.  
  8250. grant execute on sp_tables to public
  8251. go
  8252.  
  8253. dump tran master with no_log
  8254. go
  8255.  
  8256. /*-----------------------------------------------------------------------------*/
  8257. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  8258. /*-----------------------------------------------------------------------------*/
  8259.  
  8260. if object_id('sp_linkedservers', 'P') is not null
  8261.     drop proc sp_linkedservers
  8262. go
  8263. raiserror(15339,-1,-1,'sp_linkedservers')
  8264. go
  8265. create proc sp_linkedservers as
  8266.     IF is_srvrolemember('sysadmin') = 1
  8267.     begin
  8268.         select
  8269.             SRV_NAME = srvname,
  8270.             SRV_PROVIDERNAME = providername,
  8271.             SRV_PRODUCT = srvproduct,
  8272.             SRV_DATASOURCE = datasource,
  8273.             SRV_PROVIDERSTRING = providerstring,
  8274.             SRV_LOCATION = location,
  8275.             SRV_CAT = catalog
  8276.         from master.dbo.sysservers
  8277.         order by 1
  8278.     end
  8279.     ELSE
  8280.     begin
  8281.         select
  8282.             SRV_NAME = srvname,
  8283.             SRV_PROVIDERNAME = providername,
  8284.             SRV_PRODUCT = srvproduct,
  8285.             SRV_DATASOURCE = datasource,
  8286.             SRV_PROVIDERSTRING = NULL,
  8287.             SRV_LOCATION = location,
  8288.             SRV_CAT = catalog
  8289.         from master.dbo.sysservers
  8290.         order by 1
  8291.     end
  8292. go
  8293. grant execute on sp_linkedservers to public
  8294. go
  8295.  
  8296.  
  8297. if object_id('sp_catalogs', 'P') is not null
  8298.     drop proc sp_catalogs
  8299. go
  8300. raiserror(15339,-1,-1,'sp_catalogs')
  8301. go
  8302. create procedure sp_catalogs(
  8303.     @server_name        sysname)
  8304. as
  8305.     select
  8306.         CATALOG_NAME,
  8307.         convert (nvarchar(255),DESCRIPTION)
  8308.     from master.dbo.SYSREMOTE_CATALOGS ( @server_name )
  8309.     order by CATALOG_NAME
  8310. go
  8311. grant execute on sp_catalogs to public
  8312. go
  8313.  
  8314.  
  8315. if object_id('sp_tables_ex', 'P') is not null
  8316.     drop proc sp_tables_ex
  8317. go
  8318. raiserror(15339,-1,-1,'sp_tables_ex')
  8319. go
  8320. create procedure sp_tables_ex(
  8321.     @table_server       sysname,
  8322.     @table_name         sysname = null,
  8323.     @table_schema       sysname = null,
  8324.     @table_catalog      sysname = null,
  8325.     @table_type         sysname = null)
  8326. as
  8327.     declare @table_catalog_param        sysname
  8328.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  8329.         (isnull(charindex('[', @table_catalog),0) = 0) and
  8330.         (isnull(charindex('_', @table_catalog),0) = 0))
  8331.         select @table_catalog_param = @table_catalog
  8332.     else
  8333.         select @table_catalog_param = null
  8334.  
  8335.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8336.         (isnull(charindex('[', @table_name),0) = 0) and
  8337.         (isnull(charindex('_', @table_name),0) = 0))
  8338.     begin   /*  If no wild carding */
  8339.         select
  8340.             TABLE_CAT = TABLE_CATALOG,
  8341.             TABLE_SCHEM = TABLE_SCHEMA,
  8342.             TABLE_NAME = TABLE_NAME,
  8343.             TABLE_TYPE = TABLE_TYPE,
  8344.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8345.         from master.dbo.SYSREMOTE_TABLES (
  8346.                     @table_server,
  8347.                     @table_catalog_param,
  8348.                     NULL,
  8349.                     @table_name,
  8350.                     NULL )
  8351.         where (TABLE_SCHEMA like @table_schema
  8352.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8353.             and (TABLE_CATALOG like @table_catalog
  8354.             or   @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8355.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8356.             or   @table_type is NULL)
  8357.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8358.     end
  8359.     else
  8360.     begin   /*  If wild carding */
  8361.         select
  8362.             TABLE_CAT = TABLE_CATALOG,
  8363.             TABLE_SCHEM = TABLE_SCHEMA,
  8364.             TABLE_NAME = TABLE_NAME,
  8365.             TABLE_TYPE = TABLE_TYPE,
  8366.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8367.         from master.dbo.SYSREMOTE_TABLES (
  8368.                     @table_server,
  8369.                     @table_catalog_param,
  8370.                     NULL,
  8371.                     NULL,
  8372.                     NULL )
  8373.         where (TABLE_SCHEMA like @table_schema
  8374.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8375.             and (TABLE_CATALOG like @table_catalog
  8376.             or   @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8377.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8378.             or   @table_type is NULL)
  8379.             and TABLE_NAME like @table_name
  8380.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8381.     end
  8382. go
  8383. grant execute on sp_tables_ex to public
  8384. go
  8385.  
  8386.  
  8387. if object_id('sp_columns_ex', 'P') is not null
  8388.     drop proc sp_columns_ex
  8389. go
  8390.  
  8391. /* 7.0 version */
  8392. raiserror(15339,-1,-1,'sp_columns_ex')
  8393. go
  8394.  
  8395. create procedure sp_columns_ex(
  8396.     @table_server       sysname,
  8397.     @table_name         sysname = null,
  8398.     @table_schema       sysname = null,
  8399.     @table_catalog      sysname = null,
  8400.     @column_name        sysname = null,
  8401.     @ODBCVer            int = 2)
  8402. as
  8403.  
  8404.     set nocount on
  8405.     declare
  8406.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1), @DBCOLUMNFLAGS_ISLONG   binary(1),
  8407.     @DBCOLUMNFLAGS_ISROWVER binary(2)
  8408.  
  8409.     select
  8410.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10, @DBCOLUMNFLAGS_ISLONG   = 0x80,
  8411.     @DBCOLUMNFLAGS_ISROWVER = 0x0200
  8412.  
  8413.     declare 
  8414.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8415.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8416.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8417.     @DBTYPE_BSTR smallint,          @DBTYPE_BOOL smallint,
  8418.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8419.     @DBTYPE_UI1 smallint,           @DBTYPE_BYREF smallint,
  8420.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8421.     @DBTYPE_UI4 smallint,           @DBTYPE_I8 smallint,
  8422.     @DBTYPE_UI8 smallint,           @DBTYPE_GUID smallint,
  8423.     @DBTYPE_BYTES smallint,         @DBTYPE_STR smallint,
  8424.     @DBTYPE_WSTR smallint,          @DBTYPE_NUMERIC smallint,
  8425.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8426.     @DBTYPE_DBTIMESTAMP smallint
  8427.  
  8428.     select
  8429.     @DBTYPE_I2          = 2,    @DBTYPE_I4          = 3,
  8430.     @DBTYPE_R4          = 4,    @DBTYPE_R8          = 5,
  8431.     @DBTYPE_CY          = 6,    @DBTYPE_DATE        = 7,
  8432.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8433.     @DBTYPE_VARNUMERIC  = 139,  @DBTYPE_DECIMAL     = 14,
  8434.     @DBTYPE_UI1         = 17,   @DBTYPE_BYREF       = 16384,
  8435.     @DBTYPE_I1          = 16,   @DBTYPE_UI2         = 18,
  8436.     @DBTYPE_UI4         = 19,   @DBTYPE_I8          = 20,
  8437.     @DBTYPE_UI8         = 21,   @DBTYPE_GUID        = 72,
  8438.     @DBTYPE_BYTES       = 128,  @DBTYPE_STR         = 129,
  8439.     @DBTYPE_WSTR        = 130,  @DBTYPE_NUMERIC     = 131,
  8440.     @DBTYPE_DBDATE      = 133,  @DBTYPE_DBTIME      = 134,  
  8441.     @DBTYPE_DBTIMESTAMP = 135
  8442.  
  8443.     declare 
  8444.     @ODS_SMALLINT_NOT_NULL tinyint, @ODS_INT_NULL tinyint,
  8445.     @ODS_INT_NOT_NULL tinyint,      @ODS_TINYINT_NOT_NULL tinyint,
  8446.     @ODS_REAL_NOT_NULL tinyint,     @ODS_FLOAT_NULL tinyint,
  8447.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8448.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8449.     @ODS_DATETIME_NOT_NULL tinyint, @ODS_TEXT tinyint,
  8450.     @ODS_BIT tinyint,               @ODS_DECIMAL tinyint,
  8451.     @ODS_NUMERIC tinyint,           @ODS_GUID tinyint,
  8452.     @ODS_IMAGE tinyint,             @ODS_TIMESTAMP tinyint,
  8453.     @ODS_BINARY_NULL tinyint,       @ODS_BINARY_NOT_NULL tinyint,
  8454.     @ODS_VARBINARY tinyint,         @ODS_CHAR_NULL tinyint,
  8455.     @ODS_CHAR_NOT_NULL tinyint,     @ODS_VARCHAR tinyint
  8456.  
  8457.     select  
  8458.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8459.     @ODS_GUID = 36,             @ODS_BINARY_NULL = 37,          
  8460.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8461.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8462.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8463.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8464.     @ODS_BIT = 50,              @ODS_SMALLINT_NOT_NULL = 52,    
  8465.     @ODS_INT_NOT_NULL = 56,     @ODS_REAL_NOT_NULL = 59,        
  8466.     @ODS_MONEY_NOT_NULL = 60,   @ODS_DATETIME_NOT_NULL = 61,    
  8467.     @ODS_FLOAT_NOT_NULL = 62,   @ODS_DECIMAL = 106,
  8468.     @ODS_NUMERIC = 108,         @ODS_FLOAT_NULL = 109,
  8469.     @ODS_MONEY_NULL = 110,      @ODS_DATETIME_NULL = 111
  8470.  
  8471.     declare 
  8472.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8473.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8474.     @ODBC_BIT smallint,         @ODBC_TINYINT smallint,
  8475.     @ODBC_IMAGE smallint,       @ODBC_VARBINARY smallint,
  8476.     @ODBC_TIMESTAMP smallint,   @ODBC_BINARY smallint,
  8477.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8478.     @ODBC_NUMERIC smallint,     @ODBC_DECIMAL smallint,
  8479.     @ODBC_MONEY smallint,       @ODBC_INT smallint,     
  8480.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,   
  8481.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8482.     @ODBC_DATETIME smallint
  8483.  
  8484.     select
  8485.     @ODBC_GUID      = -11,  @ODBC_NTEXT     = -10,
  8486.     @ODBC_NVARCHAR  = -9,   @ODBC_NCHAR     = -8,
  8487.     @ODBC_BIT       = -7,   @ODBC_TINYINT   = -6,
  8488.     @ODBC_IMAGE     = -4,   @ODBC_VARBINARY = -3,
  8489.     @ODBC_TIMESTAMP = -2,   @ODBC_BINARY    = -2,
  8490.     @ODBC_TEXT      = -1,   @ODBC_CHAR      = 1,
  8491.     @ODBC_NUMERIC   = 2,    @ODBC_DECIMAL   = 3,
  8492.     @ODBC_MONEY     = 3,    @ODBC_INT       = 4,        
  8493.     @ODBC_SMALLINT  = 5,    @ODBC_FLOAT     = 6,    
  8494.     @ODBC_REAL      = 7,    @ODBC_VARCHAR   = 12,
  8495.     @ODBC_DATETIME  =   
  8496.     case @ODBCVer
  8497.         when 2 then 11
  8498.         else 93
  8499.     end
  8500.  
  8501.     create table #tmp_columns
  8502.     (   TABLE_CAT sysname  NULL,
  8503.         TABLE_SCHEM sysname  NULL,
  8504.         TABLE_NAME sysname NOT NULL,
  8505.         COLUMN_NAME sysname  NULL,
  8506.         DATA_TYPE smallint NOT NULL,
  8507.         TYPE_NAME sysname   NULL,
  8508.         COLUMN_SIZE int NULL,
  8509.         BUFFER_LENGTH int NULL,
  8510.         DECIMAL_DIGITS smallint NULL,
  8511.         NUM_PREC_RADIX smallint NULL,
  8512.         NULLABLE smallint NOT NULL,
  8513.         REMARKS nvarchar(255)  NULL,
  8514.             COLUMN_DEF nvarchar(128)  NULL,
  8515.             SQL_DATA_TYPE smallint null,
  8516.             SQL_DATETIME_SUB smallint NULL,
  8517.         CHAR_OCTET_LENGTH int NULL,
  8518.         ORDINAL_POSITION smallint,
  8519.         IS_NULLABLE varchar(254)  NOT NULL,
  8520.         SS_DATA_TYPE tinyint null,
  8521.         COLUMN_FLAGS int NOT NULL)
  8522.  
  8523.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8524.         (isnull(charindex('[', @table_name),0) = 0) and
  8525.         (isnull(charindex('_', @table_name),0) = 0))
  8526.     begin   /*  If no wild carding */
  8527.         insert into #tmp_columns
  8528.         select
  8529.             TABLE_CAT = c.TABLE_CATALOG,
  8530.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8531.             TABLE_NAME = c.TABLE_NAME,
  8532.             COLUMN_NAME = c.COLUMN_NAME,
  8533.             DATA_TYPE =
  8534.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8535.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  8536.                 when @DBTYPE_I4             then @ODBC_INT
  8537.                 when @DBTYPE_R4             then @ODBC_REAL
  8538.                 when @DBTYPE_R8             then @ODBC_FLOAT
  8539.                 when @DBTYPE_CY             then @ODBC_MONEY
  8540.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  8541.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  8542.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  8543.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8544.                 when @DBTYPE_BOOL           then @ODBC_BIT
  8545.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8546.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8547.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  8548.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8549.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8550.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  8551.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8552.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8553.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  8554.                 when @DBTYPE_GUID           then @ODBC_GUID
  8555.                 when @DBTYPE_BYTES then
  8556.                     case
  8557.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8558.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8559.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8560.                                  p.COLUMN_SIZE = 8
  8561.                             then @ODBC_TIMESTAMP
  8562.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8563.                             then @ODBC_BINARY
  8564.                         else @ODBC_VARBINARY
  8565.                     end
  8566.                 when @DBTYPE_STR then
  8567.                     case
  8568.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8569.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8570.                         else @ODBC_VARCHAR
  8571.                     end
  8572.                 when @DBTYPE_WSTR then
  8573.                     case
  8574.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8575.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8576.                         else @ODBC_NVARCHAR
  8577.                     end
  8578.                 when @DBTYPE_BSTR then
  8579.                     case
  8580.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8581.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8582.                         else @ODBC_NVARCHAR
  8583.                     end
  8584.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  8585.             end,
  8586.             TYPE_NAME = p.TYPE_NAME,
  8587.             COLUMN_SIZE =
  8588.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8589.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8590.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8591.             NUM_PREC_RADIX = NULL,
  8592.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8593.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8594.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8595.             SQL_DATA_TYPE = NULL,
  8596.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8597.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8598.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8599.             IS_NULLABLE =
  8600.             case c.IS_NULLABLE
  8601.                 when 1 then 'YES'
  8602.                 else 'NO'
  8603.             end,
  8604.             SS_DATA_TYPE = NULL,
  8605.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8606.         from master.dbo.SYSREMOTE_COLUMNS (
  8607.                     @table_server,
  8608.                     @table_catalog,
  8609.                     NULL,
  8610.                     @table_name,
  8611.                     NULL ) c,
  8612.                 master.dbo.SYSREMOTE_PROVIDER_TYPES (
  8613.                     @table_server ) p
  8614.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8615.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8616.                 (TABLE_SCHEMA like @table_schema
  8617.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8618.                 and (c.COLUMN_NAME like @column_name
  8619.                 or   @column_name is NULL)
  8620.     end
  8621.     else
  8622.     begin   /*  If wild carding */
  8623.         insert into #tmp_columns
  8624.         select
  8625.             TABLE_CAT = c.TABLE_CATALOG,
  8626.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8627.             TABLE_NAME = c.TABLE_NAME,
  8628.             COLUMN_NAME = c.COLUMN_NAME,
  8629.             DATA_TYPE =
  8630.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8631.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  8632.                 when @DBTYPE_I4             then @ODBC_INT
  8633.                 when @DBTYPE_R4             then @ODBC_REAL
  8634.                 when @DBTYPE_R8             then @ODBC_FLOAT
  8635.                 when @DBTYPE_CY             then @ODBC_MONEY
  8636.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  8637.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  8638.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  8639.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8640.                 when @DBTYPE_BOOL           then @ODBC_BIT
  8641.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8642.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8643.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  8644.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8645.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8646.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  8647.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8648.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8649.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  8650.                 when @DBTYPE_GUID           then @ODBC_GUID
  8651.                 when @DBTYPE_BYTES then
  8652.                     case
  8653.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8654.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8655.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8656.                                  p.COLUMN_SIZE = 8
  8657.                             then @ODBC_TIMESTAMP
  8658.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8659.                             then @ODBC_BINARY
  8660.                         else @ODBC_VARBINARY
  8661.                     end
  8662.                 when @DBTYPE_STR then
  8663.                     case
  8664.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8665.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8666.                         else @ODBC_VARCHAR
  8667.                     end
  8668.                 when @DBTYPE_WSTR then
  8669.                     case
  8670.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8671.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8672.                         else @ODBC_NVARCHAR
  8673.                     end
  8674.                 when @DBTYPE_BSTR then
  8675.                     case
  8676.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8677.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8678.                         else @ODBC_NVARCHAR
  8679.                     end
  8680.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  8681.             end,
  8682.             TYPE_NAME = p.TYPE_NAME,
  8683.             COLUMN_SIZE =
  8684.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8685.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8686.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8687.             NUM_PREC_RADIX = NULL,
  8688.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8689.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8690.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8691.             SQL_DATA_TYPE = NULL,
  8692.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8693.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8694.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8695.             IS_NULLABLE =
  8696.             case c.IS_NULLABLE
  8697.                 when 1 then 'YES'
  8698.                 else 'NO'
  8699.             end,
  8700.             SS_DATA_TYPE = NULL,
  8701.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8702.         from master.dbo.SYSREMOTE_COLUMNS (
  8703.                     @table_server,
  8704.                     @table_catalog,
  8705.                     NULL,
  8706.                     NULL,
  8707.                     NULL ) c,
  8708.                 master.dbo.SYSREMOTE_PROVIDER_TYPES (
  8709.                     @table_server ) p
  8710.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8711.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8712.                 (TABLE_SCHEMA like @table_schema
  8713.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8714.                 and (c.COLUMN_NAME like @column_name
  8715.                 or   @column_name is NULL)
  8716.                 and c.TABLE_NAME like @table_name
  8717.     end
  8718.  
  8719.     update #tmp_columns
  8720.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  8721.         SQL_DATETIME_SUB =
  8722.         case
  8723.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  8724.             else #tmp_columns.SQL_DATETIME_SUB
  8725.         end,
  8726.         TYPE_NAME = 
  8727.         case 
  8728.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8729.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8730.                     BUFFER_LENGTH = 8
  8731.                 then 'timestamp'
  8732.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8733.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8734.                 then 'smalldatetime'
  8735.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  8736.                     #tmp_columns.COLUMN_SIZE = 10
  8737.                 then 'smallmoney'
  8738.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  8739.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  8740.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  8741.                 then spt_dt.TYPE_NAME
  8742.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  8743.         end,
  8744.         COLUMN_SIZE =
  8745.         case
  8746.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  8747.                 spt_dt.data_precision
  8748.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8749.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8750.                 then 16
  8751.             else #tmp_columns.COLUMN_SIZE
  8752.         end,
  8753.         BUFFER_LENGTH =
  8754.         case
  8755.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then  /* decimal/numeric types */
  8756.                 COLUMN_SIZE+2
  8757.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  8758.             else #tmp_columns.BUFFER_LENGTH
  8759.         end,
  8760.         DECIMAL_DIGITS =
  8761.         case
  8762.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  8763.                 then #tmp_columns.SQL_DATETIME_SUB
  8764.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  8765.         end,
  8766.         NUM_PREC_RADIX = spt_dt.RADIX,
  8767.         SS_DATA_TYPE = 
  8768.         case
  8769.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  8770.                 and #tmp_columns.NULLABLE = 1
  8771.                     then @ODS_INT_NULL
  8772.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  8773.                 then @ODS_TINYINT_NOT_NULL
  8774.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  8775.                 then @ODS_SMALLINT_NOT_NULL
  8776.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  8777.                 then @ODS_INT_NOT_NULL
  8778.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  8779.                 then @ODS_FLOAT_NULL
  8780.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  8781.                 then @ODS_REAL_NOT_NULL
  8782.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  8783.                 then @ODS_FLOAT_NOT_NULL
  8784.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  8785.                 then @ODS_MONEY_NULL
  8786.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  8787.                 then @ODS_MONEY_NOT_NULL
  8788.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  8789.                 then @ODS_DATETIME_NULL
  8790.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  8791.                 then @ODS_DATETIME_NOT_NULL
  8792.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  8793.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  8794.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL  
  8795.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  8796.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  8797.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  8798.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  8799.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8800.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8801.                     BUFFER_LENGTH = 8
  8802.                 then @ODS_TIMESTAMP
  8803.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  1
  8804.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8805.                 then  @ODS_BINARY_NULL
  8806.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  0
  8807.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8808.                 then @ODS_BINARY_NOT_NULL
  8809.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  8810.                 then @ODS_VARBINARY 
  8811.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  8812.                 then @ODS_CHAR_NULL
  8813.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  8814.                 then @ODS_CHAR_NOT_NULL
  8815.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  8816.             else null
  8817.         end
  8818.         from master.dbo.spt_datatype_info spt_dt
  8819.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  8820.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  8821.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  8822.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  8823.  
  8824.     select
  8825.             TABLE_CAT,          TABLE_SCHEM,        TABLE_NAME,
  8826.             COLUMN_NAME,        DATA_TYPE,          TYPE_NAME,
  8827.             COLUMN_SIZE,        BUFFER_LENGTH,      DECIMAL_DIGITS,
  8828.             NUM_PREC_RADIX,     NULLABLE,           REMARKS,
  8829.             COLUMN_DEF,         SQL_DATA_TYPE,      SQL_DATETIME_SUB,
  8830.             CHAR_OCTET_LENGTH,  ORDINAL_POSITION,   IS_NULLABLE,
  8831.             SS_DATA_TYPE
  8832.         from #tmp_columns
  8833.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  8834.  
  8835.         drop table #tmp_columns
  8836.  
  8837. go
  8838. if (charindex('8.00', @@version) > 0)
  8839.     drop procedure sp_columns_ex
  8840. else
  8841. begin
  8842.     print ''
  8843.     print ''
  8844.     print 'Warning:'
  8845.     print 'you are installing the stored procedures '
  8846.     print 'on a pre 8.0 SQL Server.'
  8847.     print 'Ignore the following errors.'
  8848. end
  8849. go
  8850.  
  8851. /* 8.0 version */
  8852. raiserror(15339,-1,-1,'sp_columns_ex')
  8853. go
  8854. create procedure sp_columns_ex(
  8855.     @table_server       sysname,
  8856.     @table_name         sysname = null,
  8857.     @table_schema       sysname = null,
  8858.     @table_catalog      sysname = null,
  8859.     @column_name        sysname = null,
  8860.     @ODBCVer            int = 2)
  8861. as
  8862.  
  8863.     set nocount on
  8864.     declare
  8865.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1), @DBCOLUMNFLAGS_ISLONG   binary(1),
  8866.     @DBCOLUMNFLAGS_ISROWVER binary(2)
  8867.  
  8868.     select
  8869.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10, @DBCOLUMNFLAGS_ISLONG   = 0x80,
  8870.     @DBCOLUMNFLAGS_ISROWVER = 0x0200
  8871.  
  8872.     declare 
  8873.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8874.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8875.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8876.     @DBTYPE_BSTR smallint,          @DBTYPE_BOOL smallint,
  8877.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8878.     @DBTYPE_UI1 smallint,           @DBTYPE_BYREF smallint,
  8879.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8880.     @DBTYPE_UI4 smallint,           @DBTYPE_I8 smallint,
  8881.     @DBTYPE_UI8 smallint,           @DBTYPE_GUID smallint,
  8882.     @DBTYPE_BYTES smallint,         @DBTYPE_STR smallint,
  8883.     @DBTYPE_WSTR smallint,          @DBTYPE_NUMERIC smallint,
  8884.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8885.     @DBTYPE_DBTIMESTAMP smallint
  8886.  
  8887.     select
  8888.     @DBTYPE_I2          = 2,    @DBTYPE_I4          = 3,
  8889.     @DBTYPE_R4          = 4,    @DBTYPE_R8          = 5,
  8890.     @DBTYPE_CY          = 6,    @DBTYPE_DATE        = 7,
  8891.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8892.     @DBTYPE_VARNUMERIC  = 139,  @DBTYPE_DECIMAL     = 14,
  8893.     @DBTYPE_UI1         = 17,   @DBTYPE_BYREF       = 16384,
  8894.     @DBTYPE_I1          = 16,   @DBTYPE_UI2         = 18,
  8895.     @DBTYPE_UI4         = 19,   @DBTYPE_I8          = 20,
  8896.     @DBTYPE_UI8         = 21,   @DBTYPE_GUID        = 72,
  8897.     @DBTYPE_BYTES       = 128,  @DBTYPE_STR         = 129,
  8898.     @DBTYPE_WSTR        = 130,  @DBTYPE_NUMERIC     = 131,
  8899.     @DBTYPE_DBDATE      = 133,  @DBTYPE_DBTIME      = 134,  
  8900.     @DBTYPE_DBTIMESTAMP = 135
  8901.  
  8902.     declare 
  8903.     @ODS_SMALLINT_NOT_NULL tinyint, @ODS_INT_NULL tinyint,
  8904.     @ODS_INT_NOT_NULL tinyint,      @ODS_TINYINT_NOT_NULL tinyint,
  8905.     @ODS_REAL_NOT_NULL tinyint,     @ODS_FLOAT_NULL tinyint,
  8906.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8907.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8908.     @ODS_DATETIME_NOT_NULL tinyint, @ODS_TEXT tinyint,
  8909.     @ODS_BIT tinyint,               @ODS_DECIMAL tinyint,
  8910.     @ODS_NUMERIC tinyint,           @ODS_GUID tinyint,
  8911.     @ODS_IMAGE tinyint,             @ODS_TIMESTAMP tinyint,
  8912.     @ODS_BINARY_NULL tinyint,       @ODS_BINARY_NOT_NULL tinyint,
  8913.     @ODS_VARBINARY tinyint,         @ODS_CHAR_NULL tinyint,
  8914.     @ODS_CHAR_NOT_NULL tinyint,     @ODS_VARCHAR tinyint
  8915.  
  8916.     select  
  8917.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8918.     @ODS_GUID = 36,             @ODS_BINARY_NULL = 37,          
  8919.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8920.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8921.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8922.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8923.     @ODS_BIT = 50,              @ODS_SMALLINT_NOT_NULL = 52,    
  8924.     @ODS_INT_NOT_NULL = 56,     @ODS_REAL_NOT_NULL = 59,        
  8925.     @ODS_MONEY_NOT_NULL = 60,   @ODS_DATETIME_NOT_NULL = 61,    
  8926.     @ODS_FLOAT_NOT_NULL = 62,   @ODS_DECIMAL = 106,
  8927.     @ODS_NUMERIC = 108,         @ODS_FLOAT_NULL = 109,
  8928.     @ODS_MONEY_NULL = 110,      @ODS_DATETIME_NULL = 111
  8929.  
  8930.     declare 
  8931.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8932.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8933.     @ODBC_BIT smallint,         @ODBC_TINYINT smallint,
  8934.     @ODBC_IMAGE smallint,       @ODBC_VARBINARY smallint,
  8935.     @ODBC_TIMESTAMP smallint,   @ODBC_BINARY smallint,
  8936.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8937.     @ODBC_NUMERIC smallint,     @ODBC_DECIMAL smallint,
  8938.     @ODBC_MONEY smallint,       @ODBC_INT smallint,     
  8939.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,   
  8940.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8941.     @ODBC_DATETIME smallint
  8942.  
  8943.     select
  8944.     @ODBC_GUID      = -11,  @ODBC_NTEXT     = -10,
  8945.     @ODBC_NVARCHAR  = -9,   @ODBC_NCHAR     = -8,
  8946.     @ODBC_BIT       = -7,   @ODBC_TINYINT   = -6,
  8947.     @ODBC_IMAGE     = -4,   @ODBC_VARBINARY = -3,
  8948.     @ODBC_TIMESTAMP = -2,   @ODBC_BINARY    = -2,
  8949.     @ODBC_TEXT      = -1,   @ODBC_CHAR      = 1,
  8950.     @ODBC_NUMERIC   = 2,    @ODBC_DECIMAL   = 3,
  8951.     @ODBC_MONEY     = 3,    @ODBC_INT       = 4,        
  8952.     @ODBC_SMALLINT  = 5,    @ODBC_FLOAT     = 6,    
  8953.     @ODBC_REAL      = 7,    @ODBC_VARCHAR   = 12,
  8954.     @ODBC_DATETIME  =   
  8955.     case @ODBCVer
  8956.         when 2 then 11
  8957.         else 93
  8958.     end
  8959.  
  8960.     create table #tmp_columns
  8961.     (   TABLE_CAT sysname collate database_default NULL,
  8962.         TABLE_SCHEM sysname collate database_default NULL,
  8963.         TABLE_NAME sysname  collate database_default NOT NULL,
  8964.         COLUMN_NAME sysname collate database_default NULL,
  8965.         DATA_TYPE smallint NOT NULL,
  8966.         TYPE_NAME sysname  collate database_default NULL,
  8967.         COLUMN_SIZE int NULL,
  8968.         BUFFER_LENGTH int NULL,
  8969.         DECIMAL_DIGITS smallint NULL,
  8970.         NUM_PREC_RADIX smallint NULL,
  8971.         NULLABLE smallint NOT NULL,
  8972.         REMARKS nvarchar(255) collate database_default NULL,
  8973.             COLUMN_DEF nvarchar(128) collate database_default NULL,
  8974.             SQL_DATA_TYPE smallint null,
  8975.             SQL_DATETIME_SUB smallint NULL,
  8976.         CHAR_OCTET_LENGTH int NULL,
  8977.         ORDINAL_POSITION smallint,
  8978.         IS_NULLABLE varchar(254) collate database_default NOT NULL,
  8979.         SS_DATA_TYPE tinyint null,
  8980.         COLUMN_FLAGS int NOT NULL)
  8981.  
  8982.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8983.         (isnull(charindex('[', @table_name),0) = 0) and
  8984.         (isnull(charindex('_', @table_name),0) = 0))
  8985.     begin   /*  If no wild carding */
  8986.         insert into #tmp_columns
  8987.         select
  8988.             TABLE_CAT = c.TABLE_CATALOG,
  8989.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8990.             TABLE_NAME = c.TABLE_NAME,
  8991.             COLUMN_NAME = c.COLUMN_NAME,
  8992.             DATA_TYPE =
  8993.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8994.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  8995.                 when @DBTYPE_I4             then @ODBC_INT
  8996.                 when @DBTYPE_R4             then @ODBC_REAL
  8997.                 when @DBTYPE_R8             then @ODBC_FLOAT
  8998.                 when @DBTYPE_CY             then @ODBC_MONEY
  8999.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  9000.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  9001.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  9002.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  9003.                 when @DBTYPE_BOOL           then @ODBC_BIT
  9004.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  9005.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  9006.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  9007.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  9008.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  9009.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  9010.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  9011.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  9012.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  9013.                 when @DBTYPE_GUID           then @ODBC_GUID
  9014.                 when @DBTYPE_BYTES then
  9015.                     case
  9016.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  9017.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9018.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9019.                                  p.COLUMN_SIZE = 8
  9020.                             then @ODBC_TIMESTAMP
  9021.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9022.                             then @ODBC_BINARY
  9023.                         else @ODBC_VARBINARY
  9024.                     end
  9025.                 when @DBTYPE_STR then
  9026.                     case
  9027.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  9028.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  9029.                         else @ODBC_VARCHAR
  9030.                     end
  9031.                 when @DBTYPE_WSTR then
  9032.                     case
  9033.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9034.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9035.                         else @ODBC_NVARCHAR
  9036.                     end
  9037.                 when @DBTYPE_BSTR then
  9038.                     case
  9039.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9040.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9041.                         else @ODBC_NVARCHAR
  9042.                     end
  9043.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  9044.             end,
  9045.             TYPE_NAME = p.TYPE_NAME,
  9046.             COLUMN_SIZE =
  9047.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9048.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9049.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9050.             NUM_PREC_RADIX = NULL,
  9051.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9052.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9053.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9054.             SQL_DATA_TYPE = NULL,
  9055.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9056.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9057.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9058.             IS_NULLABLE =
  9059.             case c.IS_NULLABLE
  9060.                 when 1 then 'YES'
  9061.                 else 'NO'
  9062.             end,
  9063.             SS_DATA_TYPE = NULL,
  9064.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9065.         from master.dbo.SYSREMOTE_COLUMNS (
  9066.                     @table_server,
  9067.                     @table_catalog,
  9068.                     NULL,
  9069.                     @table_name,
  9070.                     NULL ) c,
  9071.                 master.dbo.SYSREMOTE_PROVIDER_TYPES (
  9072.                     @table_server ) p
  9073.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9074.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9075.                 (TABLE_SCHEMA like @table_schema
  9076.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9077.                 and (c.COLUMN_NAME like @column_name
  9078.                 or   @column_name is NULL)
  9079.     end
  9080.     else
  9081.     begin   /*  If wild carding */
  9082.         insert into #tmp_columns
  9083.         select
  9084.             TABLE_CAT = c.TABLE_CATALOG,
  9085.             TABLE_SCHEM = c.TABLE_SCHEMA,
  9086.             TABLE_NAME = c.TABLE_NAME,
  9087.             COLUMN_NAME = c.COLUMN_NAME,
  9088.             DATA_TYPE =
  9089.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  9090.                 when @DBTYPE_I2             then @ODBC_SMALLINT
  9091.                 when @DBTYPE_I4             then @ODBC_INT
  9092.                 when @DBTYPE_R4             then @ODBC_REAL
  9093.                 when @DBTYPE_R8             then @ODBC_FLOAT
  9094.                 when @DBTYPE_CY             then @ODBC_MONEY
  9095.                 when @DBTYPE_DATE           then @ODBC_DATETIME
  9096.                 when @DBTYPE_DBDATE         then @ODBC_DATETIME
  9097.                 when @DBTYPE_DBTIME         then @ODBC_DATETIME
  9098.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  9099.                 when @DBTYPE_BOOL           then @ODBC_BIT
  9100.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  9101.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  9102.                 when @DBTYPE_I1             then @ODBC_NUMERIC
  9103.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  9104.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  9105.                 when @DBTYPE_I8             then @ODBC_NUMERIC
  9106.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  9107.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  9108.                 when @DBTYPE_VARNUMERIC     then @ODBC_NUMERIC
  9109.                 when @DBTYPE_GUID           then @ODBC_GUID
  9110.                 when @DBTYPE_BYTES then
  9111.                     case
  9112.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  9113.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9114.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9115.                                  p.COLUMN_SIZE = 8
  9116.                             then @ODBC_TIMESTAMP
  9117.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9118.                             then @ODBC_BINARY
  9119.                         else @ODBC_VARBINARY
  9120.                     end
  9121.                 when @DBTYPE_STR then
  9122.                     case
  9123.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  9124.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  9125.                         else @ODBC_VARCHAR
  9126.                     end
  9127.                 when @DBTYPE_WSTR then
  9128.                     case
  9129.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9130.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9131.                         else @ODBC_NVARCHAR
  9132.                     end
  9133.                 when @DBTYPE_BSTR then
  9134.                     case
  9135.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9136.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9137.                         else @ODBC_NVARCHAR
  9138.                     end
  9139.                 else @ODBC_NVARCHAR     /*  Unknown OleDB datatype */
  9140.             end,
  9141.             TYPE_NAME = p.TYPE_NAME,
  9142.             COLUMN_SIZE =
  9143.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9144.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9145.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9146.             NUM_PREC_RADIX = NULL,
  9147.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9148.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9149.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9150.             SQL_DATA_TYPE = NULL,
  9151.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9152.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9153.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9154.             IS_NULLABLE =
  9155.             case c.IS_NULLABLE
  9156.                 when 1 then 'YES'
  9157.                 else 'NO'
  9158.             end,
  9159.             SS_DATA_TYPE = NULL,
  9160.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9161.         from master.dbo.SYSREMOTE_COLUMNS (
  9162.                     @table_server,
  9163.                     @table_catalog,
  9164.                     NULL,
  9165.                     NULL,
  9166.                     NULL ) c,
  9167.                 master.dbo.SYSREMOTE_PROVIDER_TYPES (
  9168.                     @table_server ) p
  9169.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9170.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9171.                 (TABLE_SCHEMA like @table_schema
  9172.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9173.                 and (c.COLUMN_NAME like @column_name
  9174.                 or   @column_name is NULL)
  9175.                 and c.TABLE_NAME like @table_name
  9176.     end
  9177.  
  9178.     update #tmp_columns
  9179.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  9180.         SQL_DATETIME_SUB =
  9181.         case
  9182.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  9183.             else #tmp_columns.SQL_DATETIME_SUB
  9184.         end,
  9185.         TYPE_NAME = 
  9186.         case 
  9187.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9188.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9189.                     BUFFER_LENGTH = 8
  9190.                 then 'timestamp'
  9191.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9192.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9193.                 then 'smalldatetime'
  9194.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  9195.                     #tmp_columns.COLUMN_SIZE = 10
  9196.                 then 'smallmoney'
  9197.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  9198.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  9199.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  9200.                 then spt_dt.TYPE_NAME collate database_default
  9201.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME collate database_default)
  9202.         end,
  9203.         COLUMN_SIZE =
  9204.         case
  9205.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  9206.                 spt_dt.data_precision
  9207.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9208.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9209.                 then 16
  9210.             else #tmp_columns.COLUMN_SIZE
  9211.         end,
  9212.         BUFFER_LENGTH =
  9213.         case
  9214.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then  /* decimal/numeric types */
  9215.                 COLUMN_SIZE+2
  9216.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  9217.             else #tmp_columns.BUFFER_LENGTH
  9218.         end,
  9219.         DECIMAL_DIGITS =
  9220.         case
  9221.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  9222.                 then #tmp_columns.SQL_DATETIME_SUB
  9223.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  9224.         end,
  9225.         NUM_PREC_RADIX = spt_dt.RADIX,
  9226.         SS_DATA_TYPE = 
  9227.         case
  9228.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  9229.                 and #tmp_columns.NULLABLE = 1
  9230.                     then @ODS_INT_NULL
  9231.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  9232.                 then @ODS_TINYINT_NOT_NULL
  9233.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  9234.                 then @ODS_SMALLINT_NOT_NULL
  9235.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  9236.                 then @ODS_INT_NOT_NULL
  9237.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  9238.                 then @ODS_FLOAT_NULL
  9239.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  9240.                 then @ODS_REAL_NOT_NULL
  9241.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  9242.                 then @ODS_FLOAT_NOT_NULL
  9243.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  9244.                 then @ODS_MONEY_NULL
  9245.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  9246.                 then @ODS_MONEY_NOT_NULL
  9247.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  9248.                 then @ODS_DATETIME_NULL
  9249.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  9250.                 then @ODS_DATETIME_NOT_NULL
  9251.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  9252.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  9253.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL  
  9254.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  9255.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  9256.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  9257.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  9258.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9259.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9260.                     BUFFER_LENGTH = 8
  9261.                 then @ODS_TIMESTAMP
  9262.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  1
  9263.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9264.                 then  @ODS_BINARY_NULL
  9265.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =  0
  9266.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9267.                 then @ODS_BINARY_NOT_NULL
  9268.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  9269.                 then @ODS_VARBINARY 
  9270.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  9271.                 then @ODS_CHAR_NULL
  9272.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  9273.                 then @ODS_CHAR_NOT_NULL
  9274.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  9275.             else null
  9276.         end
  9277.         from master.dbo.spt_datatype_info spt_dt
  9278.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  9279.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  9280.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  9281.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  9282.  
  9283.     select
  9284.             TABLE_CAT,          TABLE_SCHEM,        TABLE_NAME,
  9285.             COLUMN_NAME,        DATA_TYPE,          TYPE_NAME,
  9286.             COLUMN_SIZE,        BUFFER_LENGTH,      DECIMAL_DIGITS,
  9287.             NUM_PREC_RADIX,     NULLABLE,           REMARKS,
  9288.             COLUMN_DEF,         SQL_DATA_TYPE,      SQL_DATETIME_SUB,
  9289.             CHAR_OCTET_LENGTH,  ORDINAL_POSITION,   IS_NULLABLE,
  9290.             SS_DATA_TYPE
  9291.         from #tmp_columns
  9292.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  9293.  
  9294.         drop table #tmp_columns
  9295.  
  9296. go
  9297. grant execute on sp_columns_ex to public
  9298. go
  9299.  
  9300.  
  9301. if object_id('sp_table_privileges_ex', 'P') is not null
  9302.     drop proc sp_table_privileges_ex
  9303. go
  9304. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  9305. go
  9306. create procedure sp_table_privileges_ex(
  9307.     @table_server       sysname,
  9308.     @table_name         sysname = null,
  9309.     @table_schema       sysname = null,
  9310.     @table_catalog      sysname = null)
  9311. as
  9312.     select
  9313.         TABLE_CAT = TABLE_CATALOG,
  9314.         TABLE_SCHEM = TABLE_SCHEMA,
  9315.         TABLE_NAME = TABLE_NAME,
  9316.         GRANTOR = GRANTOR,
  9317.         GRANTEE = GRANTEE,
  9318.         PRIVILEGE = PRIVILEGE_TYPE,
  9319.         IS_GRANTABLE = 
  9320.         case IS_GRANTABLE
  9321.             when 1 then 'YES'
  9322.             when 0 then 'NO'
  9323.             else null
  9324.         end
  9325.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES (
  9326.                 @table_server,
  9327.                 @table_catalog,
  9328.                 NULL,
  9329.                 NULL )
  9330.     where (TABLE_SCHEMA like @table_schema
  9331.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9332.         and (TABLE_NAME like @table_name
  9333.         or   @table_name is NULL)
  9334.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  9335. go
  9336. grant execute on sp_table_privileges_ex to public
  9337. go
  9338.  
  9339.  
  9340. if object_id('sp_column_privileges_ex', 'P') is not null
  9341.     drop proc sp_column_privileges_ex
  9342. go
  9343. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  9344. go
  9345. create procedure sp_column_privileges_ex(
  9346.     @table_server       sysname,
  9347.     @table_name         sysname = null,
  9348.     @table_schema       sysname = null,
  9349.     @table_catalog      sysname = null,
  9350.     @column_name        sysname = null)
  9351. as
  9352.     select
  9353.         TABLE_CAT = TABLE_CATALOG,
  9354.         TABLE_SCHEM = TABLE_SCHEMA,
  9355.         TABLE_NAME = TABLE_NAME,
  9356.         COLUMN_NAME = COLUMN_NAME,
  9357.         GRANTOR = GRANTOR,
  9358.         GRANTEE = GRANTEE,
  9359.         PRIVILEGE = PRIVILEGE_TYPE,
  9360.         IS_GRANTABLE = 
  9361.         case IS_GRANTABLE
  9362.             when 1 then 'YES'
  9363.             when 0 then 'NO'
  9364.             else null
  9365.         end
  9366.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES (
  9367.         @table_server,
  9368.         @table_catalog,
  9369.         @table_schema,
  9370.         @table_name,
  9371.         NULL )
  9372.     where (COLUMN_NAME like @column_name
  9373.         or @column_name is NULL)
  9374.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  9375. go
  9376. grant execute on sp_column_privileges_ex to public
  9377. go
  9378.  
  9379.  
  9380. if object_id('sp_indexes', 'P') is not null
  9381.     drop proc sp_indexes
  9382. go
  9383. raiserror(15339,-1,-1,'sp_indexes')
  9384. go
  9385. create procedure sp_indexes(
  9386.     @table_server       sysname,
  9387.     @table_name         sysname = null,
  9388.     @table_schema       sysname = null,
  9389.     @table_catalog      sysname = null,
  9390.     @index_name         sysname = null,
  9391.     @is_unique          bit = null)
  9392. as
  9393.     select
  9394.         TABLE_CAT = TABLE_CATALOG,
  9395.         TABLE_SCHEM = TABLE_SCHEMA,
  9396.         TABLE_NAME = TABLE_NAME,
  9397.             NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  9398.         INDEX_QUALIFIER = TABLE_NAME,
  9399.         INDEX_NAME = INDEX_NAME,
  9400.             TYPE =
  9401.         case [CLUSTERED]
  9402.             when 1 then 1
  9403.             else 3
  9404.         end,
  9405.         ORDINAL_POSITION = ORDINAL_POSITION,
  9406.             COLUMN_NAME = COLUMN_NAME,
  9407.         ASC_OR_DESC = 
  9408.         case [COLLATION]
  9409.             when 1 then 'A'
  9410.             when 2 then 'D'
  9411.             else null
  9412.         end,
  9413.             CARDINALITY = CARDINALITY,
  9414.         PAGES = 
  9415.         case [CLUSTERED]
  9416.             when 1 then PAGES
  9417.             else NULL
  9418.         end,
  9419.             FILTER_CONDITION = FILTER_CONDITION
  9420.     from master.dbo.SYSREMOTE_INDEXES (
  9421.                 @table_server,
  9422.                 @table_catalog,
  9423.                 @table_schema,
  9424.                         @index_name,
  9425.                 NULL,           /* TYPE (index type) */
  9426.                 @table_name )
  9427.     where @is_unique is null or @is_unique = [UNIQUE]
  9428.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  9429. go
  9430. grant execute on sp_indexes to public
  9431. go
  9432.  
  9433.  
  9434. if object_id('sp_foreignkeys', 'P') is not null
  9435.     drop proc sp_foreignkeys
  9436. go
  9437. raiserror(15339,-1,-1,'sp_foreignkeys')
  9438. go
  9439. create procedure sp_foreignkeys(
  9440.     @table_server       sysname,
  9441.     @pktab_name         sysname = null,
  9442.     @pktab_schema       sysname = null,
  9443.     @pktab_catalog      sysname = null,
  9444.     @fktab_name         sysname = null,
  9445.     @fktab_schema       sysname = null,
  9446.     @fktab_catalog      sysname = null)
  9447. as
  9448.     select
  9449.         PKTABLE_CAT = PK_TABLE_CATALOG,
  9450.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  9451.         PKTABLE_NAME = PK_TABLE_NAME,
  9452.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  9453.         FKTABLE_CAT = FK_TABLE_CATALOG,
  9454.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  9455.         FKTABLE_NAME = FK_TABLE_NAME,
  9456.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  9457.         KEY_SEQ = ORDINAL,
  9458.         UPDATE_RULE = 
  9459.         case UPDATE_RULE
  9460.             when 'CASCADE' then 0
  9461.             when 'NO ACTION' then 1
  9462.             when 'SET NULL' then 2
  9463.             else null
  9464.         end,
  9465.         DELETE_RULE = 
  9466.         case DELETE_RULE
  9467.             when 'CASCADE' then 0
  9468.             when 'NO ACTION' then 1
  9469.             when 'SET NULL' then 2
  9470.             else null
  9471.         end,
  9472.         FK_NAME = convert(sysname, NULL),
  9473.         PK_NAME = convert(sysname, NULL),
  9474.         DEFERRABILITY = convert(smallint, null)
  9475.  
  9476.     from master.dbo.SYSREMOTE_FOREIGN_KEYS (
  9477.                 @table_server,
  9478.                 @pktab_catalog,
  9479.                 @pktab_schema,
  9480.                 @pktab_name,
  9481.                 @fktab_catalog,
  9482.                 @fktab_schema,
  9483.                 @fktab_name )
  9484.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  9485. go
  9486. grant execute on sp_foreignkeys to public
  9487. go
  9488.  
  9489.  
  9490. if object_id('sp_primarykeys', 'P') is not null
  9491.     drop proc sp_primarykeys
  9492. go
  9493. raiserror(15339,-1,-1,'sp_primarykeys')
  9494. go
  9495. create procedure sp_primarykeys(
  9496.     @table_server       sysname,
  9497.     @table_name         sysname = null,
  9498.     @table_schema       sysname = null,
  9499.     @table_catalog      sysname = null)
  9500. as
  9501.     select
  9502.         TABLE_CAT = TABLE_CATALOG,
  9503.         TABLE_SCHEM = TABLE_SCHEMA,
  9504.         TABLE_NAME = TABLE_NAME,
  9505.         COLUMN_NAME = COLUMN_NAME,
  9506.         KEY_SEQ = ORDINAL,
  9507.         PK_NAME = convert(sysname, NULL)
  9508.     from master.dbo.SYSREMOTE_PRIMARY_KEYS (
  9509.                 @table_server,
  9510.                 @table_catalog,
  9511.                 @table_schema,
  9512.                 @table_name )
  9513.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  9514. go
  9515. grant execute on sp_primarykeys to public
  9516. go
  9517.  
  9518. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  9519.  
  9520.  
  9521. dump tran master with no_log
  9522. go
  9523.  
  9524. if (charindex('6.50', @@version) = 0 and
  9525.     charindex('7.00', @@version) = 0 and
  9526.     charindex('8.00', @@version) = 0)
  9527. begin
  9528.     print ''
  9529.     print ''
  9530.     print 'Warning:'
  9531.     print 'you are installing the stored procedures '
  9532.     print 'on a pre 6.50 SQL Server.'
  9533.     print 'Ignore the following errors.'
  9534. end
  9535.  
  9536. print 'creating sp_ddopen'
  9537. go
  9538.  
  9539. /*  Procedure for pre-6.50 server */
  9540. create procedure sp_ddopen(
  9541.                @handle          int output,
  9542.                @procname        sysname,
  9543.                @scrollopt       int output,
  9544.                @ccopt           int output,
  9545.                @rows            int output,
  9546.                @p1              varchar(255) = null,
  9547.                @p2              varchar(255) = null,
  9548.                @p3              varchar(255) = null,
  9549.                @p4              varchar(255) = null,
  9550.                @p5              varchar(255) = null,
  9551.                @p6              varchar(255) = null,
  9552.                @p7              int = null,
  9553.                @ODBCVer         int = 2)
  9554. as
  9555.     set nocount on
  9556.     declare @ret int
  9557.  
  9558.     if @procname = 'sp_column_privileges'
  9559.     begin
  9560.         create table #spcolpriv (
  9561.             TABLE_QUALIFIER varchar(32) null,
  9562.             TABLE_OWNER varchar(32) null,
  9563.             TABLE_NAME varchar(32)  not null,
  9564.             COLUMN_NAME varchar(32) not null,
  9565.             GRANTOR varchar(32) null,
  9566.             GRANTEE varchar(32) not null,
  9567.             PRIVILEGE varchar(32) not null,
  9568.             IS_GRANTABLE varchar(3) null
  9569.             )
  9570.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9571.         exec @ret = sp_cursoropen @handle output,
  9572.             'select * from #spcolpriv',
  9573.             @scrollopt output, @ccopt output, @rows output
  9574.         drop table #spcolpriv
  9575.     end
  9576.     else if @procname = 'sp_columns'
  9577.     begin
  9578.         create table #spcolumns (
  9579.             TABLE_QUALIFIER varchar(32) null,
  9580.             TABLE_OWNER varchar(32) null,
  9581.             TABLE_NAME varchar(32)  not null,
  9582.             COLUMN_NAME varchar(32) not null,
  9583.             DATA_TYPE smallint not null,
  9584.             TYPE_NAME varchar(32) not null,
  9585.             "PRECISION" int null,
  9586.             LENGTH int null,
  9587.             SCALE smallint null,
  9588.             RADIX smallint null,
  9589.             NULLABLE smallint not null,
  9590.             REMARKS varchar(254) null,
  9591.             COLUMN_DEF varchar(255) null,
  9592.             SQL_DATA_TYPE smallint not null,
  9593.             SQL_DATETIME_SUB smallint null,
  9594.             CHAR_OCTET_LENGTH int null,
  9595.             ORDINAL_POSITION int not null,
  9596.             IS_NULLABLE varchar(254) null,
  9597.             SS_DATA_TYPE tinyint null
  9598.             )
  9599.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  9600.         exec @ret = sp_cursoropen @handle output,
  9601.             'select * from #spcolumns',
  9602.             @scrollopt output, @ccopt output, @rows output
  9603.         drop table #spcolumns
  9604.     end
  9605.     else if @procname = 'sp_datatype_info'
  9606.     begin
  9607.         create table #spdatatypeinfo (
  9608.             TYPE_NAME           varchar(32)  not null,
  9609.             DATA_TYPE           smallint not null,
  9610.             "PRECISION"         int null,
  9611.             LITERAL_PREFIX      varchar(32) null,
  9612.             LITERAL_SUFFIX      varchar(32)    null,
  9613.             CREATE_PARAMS       varchar(32)    null,
  9614.             NULLABLE            smallint   not null,
  9615.             CASE_SENSITIVE      smallint   not null,
  9616.             SEARCHABLE          smallint   not null,
  9617.             UNSIGNED_ATTRIBUTE  smallint   null,
  9618.             MONEY   smallint    not null,
  9619.             AUTO_INCREMENT      smallint    null,
  9620.             LOCAL_TYPE_NAME     varchar(32) null,
  9621.             MINIMUM_SCALE       smallint     null,
  9622.             MAXIMUM_SCALE       smallint   null,
  9623.             SQL_DATA_TYPE       smallint      not null,
  9624.             SQL_DATETIME_SUB    smallint   null,
  9625.             NUM_PREC_RADIX      int  null,
  9626.             INTERVAL_PRECISION  smallint    NULL,
  9627.             USERTYPE            smallint not null)
  9628.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  9629.         exec @ret = sp_cursoropen @handle output,
  9630.             'select * from #spdatatypeinfo',
  9631.             @scrollopt output, @ccopt output, @rows output
  9632.         drop table #spdatatypeinfo
  9633.     end
  9634.     else if @procname = 'sp_fkeys'
  9635.     begin
  9636.         create table #spfkeys (
  9637.             PKTABLE_QUALIFIER varchar(32)    null,
  9638.             PKTABLE_OWNER varchar(32)   null,
  9639.             PKTABLE_NAME varchar(32)  not null,
  9640.             PKCOLUMN_NAME varchar(32)   not null,
  9641.             FKTABLE_QUALIFIER varchar(32)   null,
  9642.             FKTABLE_OWNER varchar(32)   null,
  9643.             FKTABLE_NAME varchar(32)  not null,
  9644.             FKCOLUMN_NAME varchar(32)   not null,
  9645.             KEY_SEQ smallint not null,
  9646.             UPDATE_RULE smallint null,
  9647.             DELETE_RULE smallint null,
  9648.             FK_NAME varchar(32) null,
  9649.             PK_NAME varchar(32) null,
  9650.             DEFERRABILITY smallint null
  9651.             )
  9652.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  9653.         exec @ret = sp_cursoropen @handle output,
  9654.             'select * from #spfkeys',
  9655.             @scrollopt output, @ccopt output, @rows output
  9656.         drop table #spfkeys
  9657.     end
  9658.     else if @procname = 'sp_pkeys'
  9659.     begin
  9660.         create table #sppkeys (
  9661.             TABLE_QUALIFIER varchar(32)   null,
  9662.             TABLE_OWNER varchar(32)   null,
  9663.             TABLE_NAME varchar(32)  not null,
  9664.             COLUMN_NAME varchar(32)  not null,
  9665.             KEY_SEQ smallint not null,
  9666.             PK_NAME varchar(32) null
  9667.             )
  9668.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  9669.         exec @ret = sp_cursoropen @handle output,
  9670.             'select * from #sppkeys',
  9671.             @scrollopt output, @ccopt output, @rows output
  9672.         drop table #sppkeys
  9673.     end
  9674.     else if @procname = 'sp_special_columns'
  9675.     begin
  9676.         create table #spspeccol (
  9677.             SCOPE smallint null,
  9678.             COLUMN_NAME varchar(32) not null,
  9679.             DATA_TYPE smallint not null,
  9680.             TYPE_NAME varchar(32) not null,
  9681.             "PRECISION" int null,
  9682.             LENGTH int null,
  9683.             SCALE smallint null,
  9684.             PSEUDO_COLUMN smallint null
  9685.             )
  9686.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  9687.         exec @ret = sp_cursoropen @handle output,
  9688.             'select * from #spspeccol',
  9689.             @scrollopt output, @ccopt output, @rows output
  9690.         drop table #spspeccol
  9691.     end
  9692.     else if @procname = 'sp_sproc_columns'
  9693.     begin
  9694.         create table #spproccol (
  9695.             PROCEDURE_QUALIFIER varchar(32)  null,
  9696.             PROCEDURE_OWNER varchar(32)  null,
  9697.             PROCEDURE_NAME varchar(32)  not null,
  9698.             COLUMN_NAME varchar(32) not null,
  9699.             COLUMN_TYPE smallint not null,
  9700.             DATA_TYPE smallint not null,
  9701.             TYPE_NAME varchar(32) not null,
  9702.             "PRECISION" int null,
  9703.             LENGTH int null,
  9704.             SCALE smallint null,
  9705.             RADIX smallint null,
  9706.             NULLABLE smallint not null,
  9707.             REMARKS varchar(254) null,
  9708.             COLUMN_DEF varchar(255) null,
  9709.             SQL_DATA_TYPE smallint not null,
  9710.             SQL_DATETIME_SUB smallint null,
  9711.             CHAR_OCTET_LENGTH int null,
  9712.             ORDINAL_POSITION int not null,
  9713.             IS_NULLABLE varchar(254) null,
  9714.             SS_DATA_TYPE tinyint null
  9715.             )
  9716.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  9717.         exec @ret = sp_cursoropen @handle output,
  9718.             'select * from #spproccol',
  9719.             @scrollopt output, @ccopt output, @rows output
  9720.         drop table #spproccol
  9721.     end
  9722.     else if @procname = 'sp_statistics'
  9723.     begin
  9724.         create table #spstatistics (
  9725.             TABLE_QUALIFIER varchar(32)   null,
  9726.             TABLE_OWNER varchar(32)   null,
  9727.             TABLE_NAME varchar(32)  not null,
  9728.             NON_UNIQUE smallint null,
  9729.             INDEX_QUALIFIER varchar(32) null,
  9730.             INDEX_NAME varchar(32)  null,
  9731.             TYPE smallint not null,
  9732.             SEQ_IN_INDEX smallint null,
  9733.             COLUMN_NAME varchar(32) null,
  9734.             COLLATION char(1) null,
  9735.             CARDINALITY int null,
  9736.             PAGES int null,
  9737.             FILTER_CONDITION varchar(128) null
  9738.             )
  9739.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  9740.         exec @ret = sp_cursoropen @handle output,
  9741.             'select * from #spstatistics',
  9742.             @scrollopt output, @ccopt output, @rows output
  9743.         drop table #spstatistics
  9744.     end
  9745.     else if @procname = 'sp_stored_procedures'
  9746.     begin
  9747.         create table #spprocedures (
  9748.             PROCEDURE_QUALIFIER varchar(32)  null,
  9749.             PROCEDURE_OWNER varchar(32)  null,
  9750.             PROCEDURE_NAME varchar(32)  not null,
  9751.             NUM_INPUT_PARAMS int null,
  9752.             NUM_OUTPUT_PARAMS int null,
  9753.             NUM_RESULT_SETS int null,
  9754.             REMARKS varchar(254) null,
  9755.             PROCEDURE_TYPE smallint null
  9756.             )
  9757.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  9758.         exec @ret = sp_cursoropen @handle output,
  9759.             'select * from #spprocedures',
  9760.             @scrollopt output, @ccopt output, @rows output
  9761.         drop table #spprocedures
  9762.     end
  9763.     else if @procname = 'sp_table_privileges'
  9764.     begin
  9765.         create table #sptabpriv (
  9766.             TABLE_QUALIFIER varchar(32) null,
  9767.             TABLE_OWNER varchar(32) null,
  9768.             TABLE_NAME varchar(32)  not null,
  9769.             GRANTOR varchar(32) null,
  9770.             GRANTEE varchar(32) not null,
  9771.             PRIVILEGE varchar(32) not null,
  9772.             IS_GRANTABLE varchar(3) null
  9773.             )
  9774.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  9775.         exec @ret = sp_cursoropen @handle output,
  9776.             'select * from #sptabpriv',
  9777.             @scrollopt output, @ccopt output, @rows output
  9778.         drop table #sptabpriv
  9779.     end
  9780.     else if @procname = 'sp_tables'
  9781.     begin
  9782.         create table #sptables (
  9783.             TABLE_QUALIFIER varchar(32) null,
  9784.             TABLE_OWNER varchar(32) null,
  9785.             TABLE_NAME varchar(32)  null,
  9786.             TABLE_TYPE   varchar(32) null,
  9787.             REMARKS varchar(254) null)
  9788.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  9789.         exec @ret = sp_cursoropen @handle output,
  9790.             'select * from #sptables',
  9791.             @scrollopt output, @ccopt output, @rows output
  9792.         drop table #sptables
  9793.     end
  9794.     else
  9795.         print 'Unknown sp_ddopen procedure'
  9796.     select @ret = isnull(@ret,0)
  9797.     return isnull(@ret,0)
  9798. go
  9799.  
  9800. if (charindex('7.00', @@version) > 0 or
  9801.     charindex('8.00', @@version) > 0)
  9802.     drop procedure sp_ddopen
  9803. else
  9804. begin
  9805.     print ''
  9806.     print ''
  9807.     print 'Warning:'
  9808.     print 'you are installing the stored procedures '
  9809.     print 'on a pre 7.0 SQL Server.'
  9810.     print 'Ignore the following errors.'
  9811. end
  9812. go
  9813.  
  9814. /*  Procedure for 7.0 server */
  9815. create procedure sp_ddopen;1(
  9816.                @handle          int output,
  9817.                @procname        sysname,
  9818.                @scrollopt       int output,
  9819.                @ccopt           int output,
  9820.                @rows            int output,
  9821.                @p1              nvarchar(774) = null,
  9822.                @p2              nvarchar(774) = null,
  9823.                @p3              nvarchar(774) = null,
  9824.                @p4              nvarchar(774) = null,
  9825.                @p5              nvarchar(774) = null,
  9826.                @p6              nvarchar(774) = null,
  9827.                @p7              int = null,
  9828.                @ODBCVer         int = 2)
  9829. as
  9830.     set nocount on
  9831.     declare @ret int
  9832.  
  9833.     if @procname = 'sp_column_privileges'
  9834.     begin
  9835.         exec @ret = sp_ddopen;2 @handle output, 
  9836.                             @scrollopt output, 
  9837.                             @ccopt output, 
  9838.                             @rows output, 
  9839.                             @p1,
  9840.                             @p2,
  9841.                             @p3,
  9842.                             @p4
  9843.     end
  9844.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  9845.     begin
  9846.         exec @ret = sp_ddopen;3 @handle output,
  9847.                             @procname,
  9848.                             @scrollopt output, 
  9849.                             @ccopt output, 
  9850.                             @rows output, 
  9851.                             @p1,
  9852.                             @p2,
  9853.                             @p3,
  9854.                             @p4,
  9855.                             @p5,
  9856.                             @ODBCVer
  9857.     end
  9858.     else if @procname = 'sp_datatype_info'
  9859.     begin
  9860.         exec @ret = sp_ddopen;4 @handle output,
  9861.                             @scrollopt output, 
  9862.                             @ccopt output, 
  9863.                             @rows output, 
  9864.                             @p7,
  9865.                             @ODBCVer
  9866.     end
  9867.     else if @procname = 'sp_fkeys'
  9868.     begin
  9869.         exec @ret = sp_ddopen;5 @handle output, 
  9870.                             @scrollopt output, 
  9871.                             @ccopt output, 
  9872.                             @rows output, 
  9873.                             @p1,
  9874.                             @p2,
  9875.                             @p3,
  9876.                             @p4,
  9877.                             @p5,
  9878.                             @p6
  9879.     end
  9880.     else if @procname = 'sp_pkeys'
  9881.     begin
  9882.         exec @ret = sp_ddopen;6 @handle output, 
  9883.                             @scrollopt output, 
  9884.                             @ccopt output, 
  9885.                             @rows output, 
  9886.                             @p1,
  9887.                             @p2,
  9888.                             @p3
  9889.     end
  9890.     else if @procname = 'sp_special_columns'
  9891.     begin
  9892.         exec @ret = sp_ddopen;7 @handle output, 
  9893.                             @scrollopt output, 
  9894.                             @ccopt output, 
  9895.                             @rows output, 
  9896.                             @p1,
  9897.                             @p2,
  9898.                             @p3,
  9899.                             @p4,
  9900.                             @p5,
  9901.                             @p6,
  9902.                             @ODBCVer
  9903.     end
  9904.     else if @procname = 'sp_sproc_columns'
  9905.     begin
  9906.         exec @ret = sp_ddopen;8 @handle output, 
  9907.                             @scrollopt output, 
  9908.                             @ccopt output, 
  9909.                             @rows output, 
  9910.                             @p1,
  9911.                             @p2,
  9912.                             @p3,
  9913.                             @p4,
  9914.                             @ODBCVer
  9915.     end
  9916.     else if @procname = 'sp_statistics'
  9917.     begin
  9918.         exec @ret = sp_ddopen;9 @handle output, 
  9919.                             @scrollopt output, 
  9920.                             @ccopt output, 
  9921.                             @rows output, 
  9922.                             @p1,
  9923.                             @p2,
  9924.                             @p3,
  9925.                             @p4,
  9926.                             @p5,
  9927.                             @p6
  9928.     end
  9929.     else if @procname = 'sp_stored_procedures'
  9930.     begin
  9931.         exec @ret = sp_ddopen;10 @handle output, 
  9932.                              @scrollopt output, 
  9933.                              @ccopt output, 
  9934.                              @rows output, 
  9935.                              @p1,
  9936.                              @p2,
  9937.                              @p3
  9938.     end
  9939.     else if @procname = 'sp_table_privileges'
  9940.     begin
  9941.         exec @ret = sp_ddopen;11 @handle output, 
  9942.                              @scrollopt output, 
  9943.                              @ccopt output, 
  9944.                              @rows output, 
  9945.                              @p1,
  9946.                              @p2,
  9947.                              @p3
  9948.     end
  9949.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  9950.     begin
  9951.         exec @ret = sp_ddopen;12 @handle output,
  9952.                             @procname,
  9953.                             @scrollopt output, 
  9954.                             @ccopt output, 
  9955.                             @rows output, 
  9956.                             @p1,
  9957.                             @p2,
  9958.                             @p3,
  9959.                             @p4,
  9960.                             @p5
  9961.     end
  9962.     else if @procname = 'sp_tableswc'
  9963.     begin
  9964.         exec @ret = sp_ddopen;13 @handle output,
  9965.                             @procname,
  9966.                             @scrollopt output,
  9967.                             @ccopt output,
  9968.                             @rows output,
  9969.                             @p1,
  9970.                             @p2,
  9971.                             @p3,
  9972.                             @p4
  9973.     end
  9974.     else
  9975.         print 'Unknown sp_ddopen procedure'
  9976.     select @ret = isnull(@ret,0)
  9977.     return isnull(@ret,0)
  9978. go
  9979.  
  9980.  
  9981. if (charindex('7.00', @@version) = 0 and
  9982.     charindex('8.00', @@version) = 0)
  9983. begin
  9984.     print ''
  9985.     print ''
  9986.     print 'Warning:'
  9987.     print 'you are installing the stored procedures '
  9988.     print 'on a pre 7.0 SQL Server.'
  9989.     print 'Ignore the following errors.'
  9990. end
  9991. go
  9992.  
  9993. create procedure sp_ddopen;2(
  9994.                @handle          int output,
  9995.                @scrollopt       int output,
  9996.                @ccopt           int output,
  9997.                @rows            int output,
  9998.                @p1              nvarchar(774),
  9999.                @p2              nvarchar(774),
  10000.                @p3              nvarchar(774),
  10001.                @p4              nvarchar(774))
  10002. as
  10003.     set nocount on
  10004.     declare @ret int
  10005.  
  10006.     create table #spcolpriv (
  10007.         TABLE_QUALIFIER sysname null,
  10008.         TABLE_OWNER sysname null,
  10009.         TABLE_NAME sysname not null,
  10010.         COLUMN_NAME sysname not null,
  10011.         GRANTOR sysname null,
  10012.         GRANTEE sysname not null,
  10013.         PRIVILEGE varchar(32) not null,
  10014.         IS_GRANTABLE varchar(3) null
  10015.         )
  10016.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  10017.     exec @ret = sp_cursoropen @handle output,
  10018.         'select * from #spcolpriv',
  10019.         @scrollopt output, @ccopt output, @rows output
  10020.     drop table #spcolpriv
  10021.     return @ret
  10022. go
  10023.  
  10024.  
  10025. if (charindex('7.00', @@version) = 0 and
  10026.     charindex('8.00', @@version) = 0)
  10027. begin
  10028.     print ''
  10029.     print ''
  10030.     print 'Warning:'
  10031.     print 'you are installing the stored procedures '
  10032.     print 'on a pre 7.0 SQL Server.'
  10033.     print 'Ignore the following errors.'
  10034. end
  10035. go
  10036.  
  10037. create procedure sp_ddopen;3(
  10038.                @handle          int output,
  10039.                @procname        sysname,
  10040.                @scrollopt       int output,
  10041.                @ccopt           int output,
  10042.                @rows            int output,
  10043.                @p1              nvarchar(774),
  10044.                @p2              nvarchar(774),
  10045.                @p3              nvarchar(774),
  10046.                @p4              nvarchar(774),
  10047.                @p5              nvarchar(774),
  10048.                @ODBCVer         int)
  10049. as
  10050.     set nocount on
  10051.     declare @ret int
  10052.  
  10053.     create table #spcolumns (
  10054.         TABLE_QUALIFIER sysname null,
  10055.         TABLE_OWNER sysname null,
  10056.         TABLE_NAME sysname not null,
  10057.         COLUMN_NAME sysname not null,
  10058.         DATA_TYPE smallint not null,
  10059.         TYPE_NAME sysname not null,
  10060.         "PRECISION" int null,
  10061.         LENGTH int null,
  10062.         SCALE smallint null,
  10063.         RADIX smallint null,
  10064.         NULLABLE smallint not null,
  10065.         REMARKS varchar(254) null,
  10066.         COLUMN_DEF nvarchar(3000) null,
  10067.         SQL_DATA_TYPE smallint not null,
  10068.         SQL_DATETIME_SUB smallint null,
  10069.         CHAR_OCTET_LENGTH int null,
  10070.         ORDINAL_POSITION int not null,
  10071.         IS_NULLABLE varchar(254) null,
  10072.         SS_DATA_TYPE tinyint null
  10073.         )
  10074.  
  10075.     if @procname = 'sp_columns'
  10076.     begin
  10077.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10078.     end
  10079.     else
  10080.     begin
  10081.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10082.     end
  10083.     exec @ret = sp_cursoropen @handle output,
  10084.         'select * from #spcolumns',
  10085.         @scrollopt output, @ccopt output, @rows output
  10086.     drop table #spcolumns
  10087.     return @ret
  10088. go
  10089.  
  10090.  
  10091.  
  10092. if (charindex('7.00', @@version) = 0 and
  10093.     charindex('8.00', @@version) = 0)
  10094. begin
  10095.     print ''
  10096.     print ''
  10097.     print 'Warning:'
  10098.     print 'you are installing the stored procedures '
  10099.     print 'on a pre 7.0 SQL Server.'
  10100.     print 'Ignore the following errors.'
  10101. end
  10102. go
  10103.  
  10104. create procedure sp_ddopen;4(
  10105.                @handle          int output,
  10106.                @scrollopt       int output,
  10107.                @ccopt           int output,
  10108.                @rows            int output,
  10109.                @p7              int,
  10110.                @ODBCVer         int)
  10111. as
  10112.     set nocount on
  10113.     declare @ret int
  10114.  
  10115.     create table #spdatatypeinfo (
  10116.         TYPE_NAME           sysname  not null,
  10117.         DATA_TYPE           smallint not null,
  10118.         "PRECISION"         int null,
  10119.         LITERAL_PREFIX      varchar(32) null,
  10120.         LITERAL_SUFFIX      varchar(32)    null,
  10121.         CREATE_PARAMS       varchar(32)    null,
  10122.         NULLABLE            smallint   not null,
  10123.         CASE_SENSITIVE      smallint   not null,
  10124.         SEARCHABLE          smallint   not null,
  10125.         UNSIGNED_ATTRIBUTE  smallint   null,
  10126.         MONEY   smallint    not null,
  10127.         AUTO_INCREMENT      smallint    null,
  10128.         LOCAL_TYPE_NAME     sysname null,
  10129.         MINIMUM_SCALE       smallint     null,
  10130.         MAXIMUM_SCALE       smallint   null,
  10131.         SQL_DATA_TYPE       smallint      not null,
  10132.         SQL_DATETIME_SUB    smallint   null,
  10133.         NUM_PREC_RADIX      int  null,
  10134.         INTERVAL_PRECISION  smallint    NULL,
  10135.         USERTYPE            smallint not null)
  10136.  
  10137.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10138.     exec @ret = sp_cursoropen @handle output,
  10139.         'select * from #spdatatypeinfo',
  10140.         @scrollopt output, @ccopt output, @rows output
  10141.     drop table #spdatatypeinfo
  10142.     return @ret
  10143. go
  10144.  
  10145.  
  10146.  
  10147. if (charindex('7.00', @@version) = 0 and
  10148.     charindex('8.00', @@version) = 0)
  10149. begin
  10150.     print ''
  10151.     print ''
  10152.     print 'Warning:'
  10153.     print 'you are installing the stored procedures '
  10154.     print 'on a pre 7.0 SQL Server.'
  10155.     print 'Ignore the following errors.'
  10156. end
  10157. go
  10158.  
  10159. create procedure sp_ddopen;5(
  10160.                @handle          int output,
  10161.                @scrollopt       int output,
  10162.                @ccopt           int output,
  10163.                @rows            int output,
  10164.                @p1              nvarchar(774),
  10165.                @p2              nvarchar(774),
  10166.                @p3              nvarchar(774),
  10167.                @p4              nvarchar(774),
  10168.                @p5              nvarchar(774),
  10169.                @p6              nvarchar(774))
  10170. as
  10171.     set nocount on
  10172.     declare @ret int
  10173.  
  10174.     create table #spfkeys (
  10175.         PKTABLE_QUALIFIER sysname    null,
  10176.         PKTABLE_OWNER sysname   null,
  10177.         PKTABLE_NAME sysname  not null,
  10178.         PKCOLUMN_NAME sysname  not null,
  10179.         FKTABLE_QUALIFIER sysname   null,
  10180.         FKTABLE_OWNER sysname   null,
  10181.         FKTABLE_NAME sysname  not null,
  10182.         FKCOLUMN_NAME sysname  not null,
  10183.         KEY_SEQ smallint not null,
  10184.         UPDATE_RULE smallint null,
  10185.         DELETE_RULE smallint null,
  10186.         FK_NAME sysname null,
  10187.         PK_NAME sysname null,
  10188.         DEFERRABILITY smallint null
  10189.         )
  10190.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10191.     exec @ret = sp_cursoropen @handle output,
  10192.         'select * from #spfkeys',
  10193.         @scrollopt output, @ccopt output, @rows output
  10194.     drop table #spfkeys
  10195.     return @ret
  10196. go
  10197.  
  10198.  
  10199.  
  10200. if (charindex('7.00', @@version) = 0 and
  10201.     charindex('8.00', @@version) = 0)
  10202. begin
  10203.     print ''
  10204.     print ''
  10205.     print 'Warning:'
  10206.     print 'you are installing the stored procedures '
  10207.     print 'on a pre 7.0 SQL Server.'
  10208.     print 'Ignore the following errors.'
  10209. end
  10210. go
  10211.  
  10212. create procedure sp_ddopen;6(
  10213.                @handle          int output,
  10214.                @scrollopt       int output,
  10215.                @ccopt           int output,
  10216.                @rows            int output,
  10217.                @p1              nvarchar(774),
  10218.                @p2              nvarchar(774),
  10219.                @p3              nvarchar(774))
  10220. as
  10221.     set nocount on
  10222.     declare @ret int
  10223.  
  10224.     create table #sppkeys (
  10225.         TABLE_QUALIFIER sysname   null,
  10226.         TABLE_OWNER sysname   null,
  10227.         TABLE_NAME sysname  not null,
  10228.         COLUMN_NAME sysname  not null,
  10229.         KEY_SEQ smallint not null,
  10230.         PK_NAME sysname null
  10231.         )
  10232.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10233.     exec @ret = sp_cursoropen @handle output,
  10234.         'select * from #sppkeys',
  10235.         @scrollopt output, @ccopt output, @rows output
  10236.     drop table #sppkeys
  10237.     return @ret
  10238. go
  10239.  
  10240.  
  10241.  
  10242. if (charindex('7.00', @@version) = 0 and
  10243.     charindex('8.00', @@version) = 0)
  10244. begin
  10245.     print ''
  10246.     print ''
  10247.     print 'Warning:'
  10248.     print 'you are installing the stored procedures '
  10249.     print 'on a pre 7.0 SQL Server.'
  10250.     print 'Ignore the following errors.'
  10251. end
  10252. go
  10253.  
  10254. create procedure sp_ddopen;7(
  10255.                @handle          int output,
  10256.                @scrollopt       int output,
  10257.                @ccopt           int output,
  10258.                @rows            int output,
  10259.                @p1              nvarchar(774),
  10260.                @p2              nvarchar(774),
  10261.                @p3              nvarchar(774),
  10262.                @p4              nvarchar(774),
  10263.                @p5              nvarchar(774),
  10264.                @p6              nvarchar(774),
  10265.                @ODBCVer         int)
  10266. as
  10267.     set nocount on
  10268.     declare @ret int
  10269.  
  10270.     create table #spspeccol (
  10271.         SCOPE smallint null,
  10272.         COLUMN_NAME sysname not null,
  10273.         DATA_TYPE smallint not null,
  10274.         TYPE_NAME sysname not null,
  10275.         "PRECISION" int null,
  10276.         LENGTH int null,
  10277.         SCALE smallint null,
  10278.         PSEUDO_COLUMN smallint null
  10279.         )
  10280.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  10281.     exec @ret = sp_cursoropen @handle output,
  10282.         'select * from #spspeccol',
  10283.         @scrollopt output, @ccopt output, @rows output
  10284.     drop table #spspeccol
  10285.     return @ret
  10286. go
  10287.  
  10288.  
  10289.  
  10290. if (charindex('7.00', @@version) = 0 and
  10291.     charindex('8.00', @@version) = 0)
  10292. begin
  10293.     print ''
  10294.     print ''
  10295.     print 'Warning:'
  10296.     print 'you are installing the stored procedures '
  10297.     print 'on a pre 7.0 SQL Server.'
  10298.     print 'Ignore the following errors.'
  10299. end
  10300. go
  10301.  
  10302. create procedure sp_ddopen;8(
  10303.                @handle          int output,
  10304.                @scrollopt       int output,
  10305.                @ccopt           int output,
  10306.                @rows            int output,
  10307.                @p1              nvarchar(774),
  10308.                @p2              nvarchar(774),
  10309.                @p3              nvarchar(774),
  10310.                @p4              nvarchar(774),
  10311.                @ODBCVer         int)
  10312. as
  10313.     set nocount on
  10314.     declare @ret int
  10315.  
  10316.     create table #spproccol (
  10317.         PROCEDURE_QUALIFIER sysname  null,
  10318.         PROCEDURE_OWNER sysname  null,
  10319.         PROCEDURE_NAME sysname not null,
  10320.         COLUMN_NAME sysname not null,
  10321.         COLUMN_TYPE smallint not null,
  10322.         DATA_TYPE smallint not null,
  10323.         TYPE_NAME sysname not null,
  10324.         "PRECISION" int null,
  10325.         LENGTH int null,
  10326.         SCALE smallint null,
  10327.         RADIX smallint null,
  10328.         NULLABLE smallint not null,
  10329.         REMARKS varchar(254) null,
  10330.         COLUMN_DEF nvarchar(3000) null,
  10331.         SQL_DATA_TYPE smallint not null,
  10332.         SQL_DATETIME_SUB smallint null,
  10333.         CHAR_OCTET_LENGTH int null,
  10334.         ORDINAL_POSITION int not null,
  10335.         IS_NULLABLE varchar(254) null,
  10336.         SS_DATA_TYPE tinyint null
  10337.         )
  10338.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  10339.     exec @ret = sp_cursoropen @handle output,
  10340.         'select * from #spproccol',
  10341.         @scrollopt output, @ccopt output, @rows output
  10342.     drop table #spproccol
  10343.     return @ret
  10344. go
  10345.  
  10346.  
  10347.  
  10348. if (charindex('7.00', @@version) = 0 and
  10349.     charindex('8.00', @@version) = 0)
  10350. begin
  10351.     print ''
  10352.     print ''
  10353.     print 'Warning:'
  10354.     print 'you are installing the stored procedures '
  10355.     print 'on a pre 7.0 SQL Server.'
  10356.     print 'Ignore the following errors.'
  10357. end
  10358. go
  10359.  
  10360. create procedure sp_ddopen;9(
  10361.                @handle          int output,
  10362.                @scrollopt       int output,
  10363.                @ccopt           int output,
  10364.                @rows            int output,
  10365.                @p1              nvarchar(774),
  10366.                @p2              nvarchar(774),
  10367.                @p3              nvarchar(774),
  10368.                @p4              nvarchar(774),
  10369.                @p5              nvarchar(774),
  10370.                @p6              nvarchar(774))
  10371. as
  10372.     set nocount on
  10373.     declare @ret int
  10374.  
  10375.     create table #spstatistics (
  10376.         TABLE_QUALIFIER sysname   null,
  10377.         TABLE_OWNER sysname   null,
  10378.         TABLE_NAME sysname  not null,
  10379.         NON_UNIQUE smallint null,
  10380.         INDEX_QUALIFIER sysname null,
  10381.         INDEX_NAME sysname null,
  10382.         TYPE smallint not null,
  10383.         SEQ_IN_INDEX smallint null,
  10384.         COLUMN_NAME sysname null,
  10385.         COLLATION char(1) null,
  10386.         CARDINALITY int null,
  10387.         PAGES int null,
  10388.         FILTER_CONDITION varchar(128) null
  10389.         )
  10390.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  10391.     exec @ret = sp_cursoropen @handle output,
  10392.         'select * from #spstatistics',
  10393.         @scrollopt output, @ccopt output, @rows output
  10394.     drop table #spstatistics
  10395.     return @ret
  10396. go
  10397.                
  10398.  
  10399.  
  10400. if (charindex('7.00', @@version) = 0 and
  10401.     charindex('8.00', @@version) = 0)
  10402. begin
  10403.     print ''
  10404.     print ''
  10405.     print 'Warning:'
  10406.     print 'you are installing the stored procedures '
  10407.     print 'on a pre 7.0 SQL Server.'
  10408.     print 'Ignore the following errors.'
  10409. end
  10410. go
  10411.  
  10412. create procedure sp_ddopen;10(
  10413.                @handle          int output,
  10414.                @scrollopt           int output,
  10415.                @ccopt           int output,
  10416.                @rows            int output,
  10417.                @p1              nvarchar(774),
  10418.                @p2              nvarchar(774),
  10419.                @p3              nvarchar(774))
  10420. as
  10421.     set nocount on
  10422.     declare @ret int
  10423.  
  10424.     create table #spprocedures (
  10425.         PROCEDURE_QUALIFIER sysname  null,
  10426.         PROCEDURE_OWNER sysname  null,
  10427.         PROCEDURE_NAME nvarchar(134) not null, /*134=sysname+';'+ltrim(str(c.number,5))*/
  10428.         NUM_INPUT_PARAMS int null,
  10429.         NUM_OUTPUT_PARAMS int null,
  10430.         NUM_RESULT_SETS int null,
  10431.         REMARKS varchar(254) null,
  10432.         PROCEDURE_TYPE smallint null
  10433.         )
  10434.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  10435.     exec @ret = sp_cursoropen @handle output,
  10436.         'select * from #spprocedures',
  10437.         @scrollopt output, @ccopt output, @rows output
  10438.     drop table #spprocedures
  10439.     return @ret
  10440. go
  10441.  
  10442.  
  10443.  
  10444. if (charindex('7.00', @@version) = 0 and
  10445.     charindex('8.00', @@version) = 0)
  10446. begin
  10447.     print ''
  10448.     print ''
  10449.     print 'Warning:'
  10450.     print 'you are installing the stored procedures '
  10451.     print 'on a pre 7.0 SQL Server.'
  10452.     print 'Ignore the following errors.'
  10453. end
  10454. go
  10455.  
  10456. create procedure sp_ddopen;11(
  10457.                @handle          int output,
  10458.                @scrollopt       int output,
  10459.                @ccopt           int output,
  10460.                @rows            int output,
  10461.                @p1              nvarchar(774),
  10462.                @p2              nvarchar(774),
  10463.                @p3              nvarchar(774))
  10464. as
  10465.     set nocount on
  10466.     declare @ret int
  10467.  
  10468.     create table #sptabpriv (
  10469.         TABLE_QUALIFIER sysname null,
  10470.         TABLE_OWNER sysname null,
  10471.         TABLE_NAME sysname not null,
  10472.         GRANTOR sysname null,
  10473.         GRANTEE sysname not null,
  10474.         PRIVILEGE varchar(32) not null,
  10475.         IS_GRANTABLE varchar(3) null
  10476.         )
  10477.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  10478.     exec @ret = sp_cursoropen @handle output,
  10479.         'select * from #sptabpriv',
  10480.         @scrollopt output, @ccopt output, @rows output
  10481.     drop table #sptabpriv
  10482.     return @ret
  10483. go
  10484.  
  10485.  
  10486.  
  10487. if (charindex('7.00', @@version) = 0 and
  10488.     charindex('8.00', @@version) = 0)
  10489. begin
  10490.     print ''
  10491.     print ''
  10492.     print 'Warning:'
  10493.     print 'you are installing the stored procedures '
  10494.     print 'on a pre 7.0 SQL Server.'
  10495.     print 'Ignore the following errors.'
  10496. end
  10497. go
  10498.  
  10499. create procedure sp_ddopen;12(
  10500.                @handle          int output,
  10501.                @procname        sysname,
  10502.                @scrollopt       int output,
  10503.                @ccopt           int output,
  10504.                @rows            int output,
  10505.                @p1              nvarchar(774),
  10506.                @p2              nvarchar(774),
  10507.                @p3              nvarchar(774),
  10508.                @p4              nvarchar(774),
  10509.                @p5              nvarchar(774))
  10510. as
  10511.     set nocount on
  10512.     declare @ret int
  10513.  
  10514.     create table #sptables (
  10515.         TABLE_QUALIFIER sysname null,
  10516.         TABLE_OWNER sysname null,
  10517.         TABLE_NAME sysname null,
  10518.         TABLE_TYPE  varchar(32) null,
  10519.         REMARKS varchar(254) null)
  10520.     if @procname = 'sp_tables'
  10521.     begin
  10522.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  10523.     end
  10524.     else
  10525.     begin
  10526.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  10527.     end
  10528.     exec @ret = sp_cursoropen @handle output,
  10529.         'select * from #sptables',
  10530.         @scrollopt output, @ccopt output, @rows output
  10531.     drop table #sptables
  10532.     return @ret
  10533. go
  10534.  
  10535. if (charindex('7.00', @@version) = 0 and
  10536.     charindex('8.00', @@version) = 0)
  10537. begin
  10538.     print ''
  10539.     print ''
  10540.     print 'Warning:'
  10541.     print 'you are installing the stored procedures '
  10542.     print 'on a pre 7.0 SQL Server.'
  10543.     print 'Ignore the following errors.'
  10544. end
  10545. go
  10546.  
  10547. create procedure sp_ddopen;13(
  10548.                @handle          int output,
  10549.                @procname        sysname,
  10550.                @scrollopt       int output,
  10551.                @ccopt           int output,
  10552.                @rows            int output,
  10553.                @p1              nvarchar(774),  -- @table_name
  10554.                @p2              nvarchar(774),  -- @table_owner
  10555.                @p3              nvarchar(774),  -- @table_qualifier
  10556.                @p4              nvarchar(774))  -- @table_type
  10557. as
  10558.     -- this code was copied from sp_tableswc (for 7,0).  Any changes
  10559.     -- made here need to be evaluated for porting to sp_tableswc.
  10560.     set nocount on
  10561.     declare @ret int
  10562.     declare @databasename   sysname
  10563.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  10564.     
  10565.     create table #sptables (
  10566.         TABLE_QUALIFIER sysname null,
  10567.         TABLE_OWNER sysname null,
  10568.         TABLE_NAME sysname null,
  10569.         TABLE_TYPE varchar(32) null,
  10570.         REMARKS varchar(254) null)
  10571.         
  10572.     declare databases CURSOR FOR
  10573.         select name from master..sysdatabases
  10574.         where name like @p3 and name <> 'model' and has_dbaccess(name)=1
  10575.         for read only
  10576.  
  10577.     open databases
  10578.     fetch next from databases into @databasename
  10579.     while (@@FETCH_STATUS <> -1)
  10580.     begin
  10581.         if (charindex('%', @databasename) = 0)
  10582.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  10583.             select @qualprocname = @databasename + '..sp_tables'
  10584.             insert into #sptables exec @qualprocname
  10585.                 @p1, @p2, @databasename, @p3
  10586.         end
  10587.         fetch next from databases into @databasename
  10588.     end
  10589.     deallocate databases
  10590.     
  10591.     exec @ret = sp_cursoropen @handle output,
  10592.         'select * from #sptables',
  10593.         @scrollopt output, @ccopt output, @rows output
  10594.     drop table #sptables
  10595.     return @ret
  10596. go
  10597.  
  10598. if (charindex('8.00', @@version) > 0)
  10599.     drop procedure sp_ddopen
  10600. else
  10601. begin
  10602.     print ''
  10603.     print ''
  10604.     print 'Warning:'
  10605.     print 'you are installing the stored procedures '
  10606.     print 'on a pre 8.0 SQL Server.'
  10607.     print 'Ignore the following errors.'
  10608. end
  10609. go
  10610.  
  10611. /*  Procedure for 8.0 server */
  10612. create procedure sp_ddopen;1(
  10613.                @handle          int output,
  10614.                @procname        sysname,
  10615.                @scrollopt       int output,
  10616.                @ccopt           int output,
  10617.                @rows            int output,
  10618.                @p1              nvarchar(774) = null,
  10619.                @p2              nvarchar(774) = null,
  10620.                @p3              nvarchar(774) = null,
  10621.                @p4              nvarchar(774) = null,
  10622.                @p5              nvarchar(774) = null,
  10623.                @p6              nvarchar(774) = null,
  10624.                @p7              int = null,
  10625.                @ODBCVer         int = 2)
  10626. as
  10627.     set nocount on
  10628.     declare @ret int
  10629.  
  10630.     if @procname = 'sp_column_privileges'
  10631.     begin
  10632.         exec @ret = sp_ddopen;2 @handle output, 
  10633.                             @scrollopt output, 
  10634.                             @ccopt output, 
  10635.                             @rows output, 
  10636.                             @p1,
  10637.                             @p2,
  10638.                             @p3,
  10639.                             @p4
  10640.     end
  10641.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  10642.     begin
  10643.         exec @ret = sp_ddopen;3 @handle output,
  10644.                             @procname,
  10645.                             @scrollopt output, 
  10646.                             @ccopt output, 
  10647.                             @rows output, 
  10648.                             @p1,
  10649.                             @p2,
  10650.                             @p3,
  10651.                             @p4,
  10652.                             @p5,
  10653.                             @ODBCVer
  10654.     end
  10655.     else if @procname = 'sp_datatype_info'
  10656.     begin
  10657.         exec @ret = sp_ddopen;4 @handle output,
  10658.                             @scrollopt output, 
  10659.                             @ccopt output, 
  10660.                             @rows output, 
  10661.                             @p7,
  10662.                             @ODBCVer
  10663.     end
  10664.     else if @procname = 'sp_fkeys'
  10665.     begin
  10666.         exec @ret = sp_ddopen;5 @handle output, 
  10667.                             @scrollopt output, 
  10668.                             @ccopt output, 
  10669.                             @rows output, 
  10670.                             @p1,
  10671.                             @p2,
  10672.                             @p3,
  10673.                             @p4,
  10674.                             @p5,
  10675.                             @p6
  10676.     end
  10677.     else if @procname = 'sp_pkeys'
  10678.     begin
  10679.         exec @ret = sp_ddopen;6 @handle output, 
  10680.                             @scrollopt output, 
  10681.                             @ccopt output, 
  10682.                             @rows output, 
  10683.                             @p1,
  10684.                             @p2,
  10685.                             @p3
  10686.     end
  10687.     else if @procname = 'sp_special_columns'
  10688.     begin
  10689.         exec @ret = sp_ddopen;7 @handle output, 
  10690.                             @scrollopt output, 
  10691.                             @ccopt output, 
  10692.                             @rows output, 
  10693.                             @p1,
  10694.                             @p2,
  10695.                             @p3,
  10696.                             @p4,
  10697.                             @p5,
  10698.                             @p6,
  10699.                             @ODBCVer
  10700.     end
  10701.     else if @procname = 'sp_sproc_columns'
  10702.     begin
  10703.         exec @ret = sp_ddopen;8 @handle output, 
  10704.                             @scrollopt output, 
  10705.                             @ccopt output, 
  10706.                             @rows output, 
  10707.                             @p1,
  10708.                             @p2,
  10709.                             @p3,
  10710.                             @p4,
  10711.                             @ODBCVer
  10712.     end
  10713.     else if @procname = 'sp_statistics'
  10714.     begin
  10715.         exec @ret = sp_ddopen;9 @handle output, 
  10716.                             @scrollopt output, 
  10717.                             @ccopt output, 
  10718.                             @rows output, 
  10719.                             @p1,
  10720.                             @p2,
  10721.                             @p3,
  10722.                             @p4,
  10723.                             @p5,
  10724.                             @p6
  10725.     end
  10726.     else if @procname = 'sp_stored_procedures'
  10727.     begin
  10728.         exec @ret = sp_ddopen;10 @handle output, 
  10729.                              @scrollopt output, 
  10730.                              @ccopt output, 
  10731.                              @rows output, 
  10732.                              @p1,
  10733.                              @p2,
  10734.                              @p3
  10735.     end
  10736.     else if @procname = 'sp_table_privileges'
  10737.     begin
  10738.         exec @ret = sp_ddopen;11 @handle output, 
  10739.                              @scrollopt output, 
  10740.                              @ccopt output, 
  10741.                              @rows output, 
  10742.                              @p1,
  10743.                              @p2,
  10744.                              @p3
  10745.     end
  10746.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  10747.     begin
  10748.         exec @ret = sp_ddopen;12 @handle output,
  10749.                             @procname,
  10750.                             @scrollopt output, 
  10751.                             @ccopt output, 
  10752.                             @rows output, 
  10753.                             @p1,
  10754.                             @p2,
  10755.                             @p3,
  10756.                             @p4,
  10757.                             @p5
  10758.     end
  10759.     else if @procname = 'sp_tableswc'
  10760.     begin
  10761.         exec @ret = sp_ddopen;13 @handle output,
  10762.                             @procname,
  10763.                             @scrollopt output,
  10764.                             @ccopt output,
  10765.                             @rows output,
  10766.                             @p1,
  10767.                             @p2,
  10768.                             @p3,
  10769.                             @p4
  10770.     end
  10771.     else
  10772.         print 'Unknown sp_ddopen procedure'
  10773.     select @ret = isnull(@ret,0)
  10774.     return isnull(@ret,0)
  10775. go
  10776.  
  10777.  
  10778. if (charindex('8.00', @@version) = 0)
  10779. begin
  10780.     print ''
  10781.     print ''
  10782.     print 'Warning:'
  10783.     print 'you are installing the stored procedures '
  10784.     print 'on a pre 8.0 SQL Server.'
  10785.     print 'Ignore the following errors.'
  10786. end
  10787. go
  10788.  
  10789. create procedure sp_ddopen;2(
  10790.                @handle          int output,
  10791.                @scrollopt       int output,
  10792.                @ccopt           int output,
  10793.                @rows            int output,
  10794.                @p1              nvarchar(774),
  10795.                @p2              nvarchar(774),
  10796.                @p3              nvarchar(774),
  10797.                @p4              nvarchar(774))
  10798. as
  10799.     set nocount on
  10800.     declare @ret int
  10801.  
  10802.     create table #spcolpriv (
  10803.         TABLE_QUALIFIER sysname collate database_default null,
  10804.         TABLE_OWNER sysname collate database_default null,
  10805.         TABLE_NAME sysname collate database_default not null,
  10806.         COLUMN_NAME sysname collate database_default not null,
  10807.         GRANTOR sysname collate database_default null,
  10808.         GRANTEE sysname collate database_default not null,
  10809.         PRIVILEGE varchar(32) collate database_default not null,
  10810.         IS_GRANTABLE varchar(3) collate database_default null
  10811.         )
  10812.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  10813.     exec @ret = sp_cursoropen @handle output,
  10814.         'select * from #spcolpriv',
  10815.         @scrollopt output, @ccopt output, @rows output
  10816.     drop table #spcolpriv
  10817.     return @ret
  10818. go
  10819.  
  10820.  
  10821. if (charindex('8.00', @@version) = 0)
  10822. begin
  10823.     print ''
  10824.     print ''
  10825.     print 'Warning:'
  10826.     print 'you are installing the stored procedures '
  10827.     print 'on a pre 8.0 SQL Server.'
  10828.     print 'Ignore the following errors.'
  10829. end
  10830. go
  10831.  
  10832. create procedure sp_ddopen;3(
  10833.                @handle          int output,
  10834.                @procname        sysname,
  10835.                @scrollopt       int output,
  10836.                @ccopt           int output,
  10837.                @rows            int output,
  10838.                @p1              nvarchar(774),
  10839.                @p2              nvarchar(774),
  10840.                @p3              nvarchar(774),
  10841.                @p4              nvarchar(774),
  10842.                @p5              nvarchar(774),
  10843.                @ODBCVer         int)
  10844. as
  10845.     set nocount on
  10846.     declare @ret int
  10847.  
  10848.     create table #spcolumns (
  10849.         TABLE_QUALIFIER sysname collate database_default null,
  10850.         TABLE_OWNER sysname collate database_default null,
  10851.         TABLE_NAME sysname collate database_default not null,
  10852.         COLUMN_NAME sysname collate database_default not null,
  10853.         DATA_TYPE smallint not null,
  10854.         TYPE_NAME sysname collate database_default not null,
  10855.         "PRECISION" int null,
  10856.         LENGTH int null,
  10857.         SCALE smallint null,
  10858.         RADIX smallint null,
  10859.         NULLABLE smallint not null,
  10860.         REMARKS varchar(254) collate database_default null,
  10861.         COLUMN_DEF nvarchar(3000) collate database_default null,
  10862.         SQL_DATA_TYPE smallint null,
  10863.         SQL_DATETIME_SUB smallint null,
  10864.         CHAR_OCTET_LENGTH int null,
  10865.         ORDINAL_POSITION int not null,
  10866.         IS_NULLABLE varchar(254) collate database_default null,
  10867.         SS_DATA_TYPE tinyint null
  10868.         )
  10869.  
  10870.     if @procname = 'sp_columns'
  10871.     begin
  10872.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10873.     end
  10874.     else
  10875.     begin
  10876.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10877.     end
  10878.     exec @ret = sp_cursoropen @handle output,
  10879.         'select * from #spcolumns',
  10880.         @scrollopt output, @ccopt output, @rows output
  10881.     drop table #spcolumns
  10882.     return @ret
  10883. go
  10884.  
  10885.  
  10886.  
  10887. if (charindex('8.00', @@version) = 0)
  10888. begin
  10889.     print ''
  10890.     print ''
  10891.     print 'Warning:'
  10892.     print 'you are installing the stored procedures '
  10893.     print 'on a pre 8.0 SQL Server.'
  10894.     print 'Ignore the following errors.'
  10895. end
  10896. go
  10897.  
  10898. create procedure sp_ddopen;4(
  10899.                @handle          int output,
  10900.                @scrollopt       int output,
  10901.                @ccopt           int output,
  10902.                @rows            int output,
  10903.                @p7              int,
  10904.                @ODBCVer         int)
  10905. as
  10906.     set nocount on
  10907.     declare @ret int
  10908.  
  10909.     create table #spdatatypeinfo (
  10910.         TYPE_NAME           sysname  collate database_default not null,
  10911.         DATA_TYPE           smallint not null,
  10912.         "PRECISION"         int null,
  10913.         LITERAL_PREFIX      varchar(32) collate database_default null,
  10914.         LITERAL_SUFFIX      varchar(32)    collate database_default null,
  10915.         CREATE_PARAMS       varchar(32)    collate database_default null,
  10916.         NULLABLE            smallint   not null,
  10917.         CASE_SENSITIVE      smallint   not null,
  10918.         SEARCHABLE          smallint   not null,
  10919.         UNSIGNED_ATTRIBUTE  smallint   null,
  10920.         MONEY   smallint    not null,
  10921.         AUTO_INCREMENT      smallint    null,
  10922.         LOCAL_TYPE_NAME     sysname collate database_default null,
  10923.         MINIMUM_SCALE       smallint     null,
  10924.         MAXIMUM_SCALE       smallint   null,
  10925.         SQL_DATA_TYPE       smallint      not null,
  10926.         SQL_DATETIME_SUB    smallint   null,
  10927.         NUM_PREC_RADIX      int  null,
  10928.         INTERVAL_PRECISION  smallint    NULL,
  10929.         USERTYPE            smallint not null)
  10930.  
  10931.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10932.     exec @ret = sp_cursoropen @handle output,
  10933.         'select * from #spdatatypeinfo',
  10934.         @scrollopt output, @ccopt output, @rows output
  10935.     drop table #spdatatypeinfo
  10936.     return @ret
  10937. go
  10938.  
  10939.  
  10940.  
  10941. if (charindex('8.00', @@version) = 0)
  10942. begin
  10943.     print ''
  10944.     print ''
  10945.     print 'Warning:'
  10946.     print 'you are installing the stored procedures '
  10947.     print 'on a pre 8.0 SQL Server.'
  10948.     print 'Ignore the following errors.'
  10949. end
  10950. go
  10951.  
  10952. create procedure sp_ddopen;5(
  10953.                @handle          int output,
  10954.                @scrollopt       int output,
  10955.                @ccopt           int output,
  10956.                @rows            int output,
  10957.                @p1              nvarchar(774),
  10958.                @p2              nvarchar(774),
  10959.                @p3              nvarchar(774),
  10960.                @p4              nvarchar(774),
  10961.                @p5              nvarchar(774),
  10962.                @p6              nvarchar(774))
  10963. as
  10964.     set nocount on
  10965.     declare @ret int
  10966.  
  10967.     create table #spfkeys (
  10968.         PKTABLE_QUALIFIER sysname    collate database_default null,
  10969.         PKTABLE_OWNER sysname   collate database_default null,
  10970.         PKTABLE_NAME sysname  collate database_default not null,
  10971.         PKCOLUMN_NAME sysname  collate database_default not null,
  10972.         FKTABLE_QUALIFIER sysname   collate database_default null,
  10973.         FKTABLE_OWNER sysname   collate database_default null,
  10974.         FKTABLE_NAME sysname  collate database_default not null,
  10975.         FKCOLUMN_NAME sysname  collate database_default not null,
  10976.         KEY_SEQ smallint not null,
  10977.         UPDATE_RULE smallint null,
  10978.         DELETE_RULE smallint null,
  10979.         FK_NAME sysname collate database_default null,
  10980.         PK_NAME sysname collate database_default null,
  10981.         DEFERRABILITY smallint null
  10982.         )
  10983.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10984.     exec @ret = sp_cursoropen @handle output,
  10985.         'select * from #spfkeys',
  10986.         @scrollopt output, @ccopt output, @rows output
  10987.     drop table #spfkeys
  10988.     return @ret
  10989. go
  10990.  
  10991.  
  10992.  
  10993. if (charindex('8.00', @@version) = 0)
  10994. begin
  10995.     print ''
  10996.     print ''
  10997.     print 'Warning:'
  10998.     print 'you are installing the stored procedures '
  10999.     print 'on a pre 8.0 SQL Server.'
  11000.     print 'Ignore the following errors.'
  11001. end
  11002. go
  11003.  
  11004. create procedure sp_ddopen;6(
  11005.                @handle          int output,
  11006.                @scrollopt       int output,
  11007.                @ccopt           int output,
  11008.                @rows            int output,
  11009.                @p1              nvarchar(774),
  11010.                @p2              nvarchar(774),
  11011.                @p3              nvarchar(774))
  11012. as
  11013.     set nocount on
  11014.     declare @ret int
  11015.  
  11016.     create table #sppkeys (
  11017.         TABLE_QUALIFIER sysname   collate database_default null,
  11018.         TABLE_OWNER sysname   collate database_default null,
  11019.         TABLE_NAME sysname  collate database_default not null,
  11020.         COLUMN_NAME sysname  collate database_default not null,
  11021.         KEY_SEQ smallint not null,
  11022.         PK_NAME sysname collate database_default null
  11023.         )
  11024.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  11025.     exec @ret = sp_cursoropen @handle output,
  11026.         'select * from #sppkeys',
  11027.         @scrollopt output, @ccopt output, @rows output
  11028.     drop table #sppkeys
  11029.     return @ret
  11030. go
  11031.  
  11032.  
  11033.  
  11034. if (charindex('8.00', @@version) = 0)
  11035. begin
  11036.     print ''
  11037.     print ''
  11038.     print 'Warning:'
  11039.     print 'you are installing the stored procedures '
  11040.     print 'on a pre 8.0 SQL Server.'
  11041.     print 'Ignore the following errors.'
  11042. end
  11043. go
  11044.  
  11045. create procedure sp_ddopen;7(
  11046.                @handle          int output,
  11047.                @scrollopt       int output,
  11048.                @ccopt           int output,
  11049.                @rows            int output,
  11050.                @p1              nvarchar(774),
  11051.                @p2              nvarchar(774),
  11052.                @p3              nvarchar(774),
  11053.                @p4              nvarchar(774),
  11054.                @p5              nvarchar(774),
  11055.                @p6              nvarchar(774),
  11056.                @ODBCVer         int)
  11057. as
  11058.     set nocount on
  11059.     declare @ret int
  11060.  
  11061.     create table #spspeccol (
  11062.         SCOPE smallint null,
  11063.         COLUMN_NAME sysname collate database_default not null,
  11064.         DATA_TYPE smallint not null,
  11065.         TYPE_NAME sysname collate database_default not null,
  11066.         "PRECISION" int null,
  11067.         LENGTH int null,
  11068.         SCALE smallint null,
  11069.         PSEUDO_COLUMN smallint null
  11070.         )
  11071.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  11072.     exec @ret = sp_cursoropen @handle output,
  11073.         'select * from #spspeccol',
  11074.         @scrollopt output, @ccopt output, @rows output
  11075.     drop table #spspeccol
  11076.     return @ret
  11077. go
  11078.  
  11079.  
  11080.  
  11081. if (charindex('8.00', @@version) = 0)
  11082. begin
  11083.     print ''
  11084.     print ''
  11085.     print 'Warning:'
  11086.     print 'you are installing the stored procedures '
  11087.     print 'on a pre 8.0 SQL Server.'
  11088.     print 'Ignore the following errors.'
  11089. end
  11090. go
  11091.  
  11092. create procedure sp_ddopen;8(
  11093.                @handle          int output,
  11094.                @scrollopt       int output,
  11095.                @ccopt           int output,
  11096.                @rows            int output,
  11097.                @p1              nvarchar(774),
  11098.                @p2              nvarchar(774),
  11099.                @p3              nvarchar(774),
  11100.                @p4              nvarchar(774),
  11101.                @ODBCVer         int)
  11102. as
  11103.     set nocount on
  11104.     declare @ret int
  11105.  
  11106.     create table #spproccol (
  11107.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11108.         PROCEDURE_OWNER sysname  collate database_default null,
  11109.         PROCEDURE_NAME sysname collate database_default not null,
  11110.         COLUMN_NAME sysname collate database_default not null,
  11111.         COLUMN_TYPE smallint not null,
  11112.         DATA_TYPE smallint null,
  11113.         TYPE_NAME sysname collate database_default not null,
  11114.         "PRECISION" int null,
  11115.         LENGTH int null,
  11116.         SCALE smallint null,
  11117.         RADIX smallint null,
  11118.         NULLABLE smallint not null,
  11119.         REMARKS varchar(254) collate database_default null,
  11120.         COLUMN_DEF nvarchar(3000) collate database_default null,
  11121.         SQL_DATA_TYPE smallint null,
  11122.         SQL_DATETIME_SUB smallint null,
  11123.         CHAR_OCTET_LENGTH int null,
  11124.         ORDINAL_POSITION int not null,
  11125.         IS_NULLABLE varchar(254) collate database_default null,
  11126.         SS_DATA_TYPE tinyint null
  11127.         )
  11128.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  11129.     exec @ret = sp_cursoropen @handle output,
  11130.         'select * from #spproccol',
  11131.         @scrollopt output, @ccopt output, @rows output
  11132.     drop table #spproccol
  11133.     return @ret
  11134. go
  11135.  
  11136.  
  11137.  
  11138. if (charindex('8.00', @@version) = 0)
  11139. begin
  11140.     print ''
  11141.     print ''
  11142.     print 'Warning:'
  11143.     print 'you are installing the stored procedures '
  11144.     print 'on a pre 8.0 SQL Server.'
  11145.     print 'Ignore the following errors.'
  11146. end
  11147. go
  11148.  
  11149. create procedure sp_ddopen;9(
  11150.                @handle          int output,
  11151.                @scrollopt       int output,
  11152.                @ccopt           int output,
  11153.                @rows            int output,
  11154.                @p1              nvarchar(774),
  11155.                @p2              nvarchar(774),
  11156.                @p3              nvarchar(774),
  11157.                @p4              nvarchar(774),
  11158.                @p5              nvarchar(774),
  11159.                @p6              nvarchar(774))
  11160. as
  11161.     set nocount on
  11162.     declare @ret int
  11163.  
  11164.     create table #spstatistics (
  11165.         TABLE_QUALIFIER sysname   collate database_default null,
  11166.         TABLE_OWNER sysname   collate database_default null,
  11167.         TABLE_NAME sysname  collate database_default not null,
  11168.         NON_UNIQUE smallint null,
  11169.         INDEX_QUALIFIER sysname collate database_default null,
  11170.         INDEX_NAME sysname collate database_default null,
  11171.         TYPE smallint not null,
  11172.         SEQ_IN_INDEX smallint null,
  11173.         COLUMN_NAME sysname collate database_default null,
  11174.         COLLATION char(1) collate database_default null,
  11175.         CARDINALITY int null,
  11176.         PAGES int null,
  11177.         FILTER_CONDITION varchar(128) collate database_default null
  11178.         )
  11179.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  11180.     exec @ret = sp_cursoropen @handle output,
  11181.         'select * from #spstatistics',
  11182.         @scrollopt output, @ccopt output, @rows output
  11183.     drop table #spstatistics
  11184.     return @ret
  11185. go
  11186.                
  11187.  
  11188.  
  11189. if (charindex('8.00', @@version) = 0)
  11190. begin
  11191.     print ''
  11192.     print ''
  11193.     print 'Warning:'
  11194.     print 'you are installing the stored procedures '
  11195.     print 'on a pre 8.0 SQL Server.'
  11196.     print 'Ignore the following errors.'
  11197. end
  11198. go
  11199.  
  11200. create procedure sp_ddopen;10(
  11201.                @handle          int output,
  11202.                @scrollopt       int output,
  11203.                @ccopt           int output,
  11204.                @rows            int output,
  11205.                @p1              nvarchar(774),
  11206.                @p2              nvarchar(774),
  11207.                @p3              nvarchar(774))
  11208. as
  11209.     set nocount on
  11210.     declare @ret int
  11211.  
  11212.     create table #spprocedures (
  11213.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11214.         PROCEDURE_OWNER sysname  collate database_default null,
  11215.         PROCEDURE_NAME nvarchar(134) collate database_default not null, /*134=sysname+';'+ltrim(str(c.number,5))*/
  11216.         NUM_INPUT_PARAMS int null,
  11217.         NUM_OUTPUT_PARAMS int null,
  11218.         NUM_RESULT_SETS int null,
  11219.         REMARKS varchar(254) collate database_default null,
  11220.         PROCEDURE_TYPE smallint null
  11221.         )
  11222.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  11223.     exec @ret = sp_cursoropen @handle output,
  11224.         'select * from #spprocedures',
  11225.         @scrollopt output, @ccopt output, @rows output
  11226.     drop table #spprocedures
  11227.     return @ret
  11228. go
  11229.  
  11230.  
  11231.  
  11232. if (charindex('8.00', @@version) = 0)
  11233. begin
  11234.     print ''
  11235.     print ''
  11236.     print 'Warning:'
  11237.     print 'you are installing the stored procedures '
  11238.     print 'on a pre 8.0 SQL Server.'
  11239.     print 'Ignore the following errors.'
  11240. end
  11241. go
  11242.  
  11243. create procedure sp_ddopen;11(
  11244.                @handle          int output,
  11245.                @scrollopt       int output,
  11246.                @ccopt           int output,
  11247.                @rows            int output,
  11248.                @p1              nvarchar(774),
  11249.                @p2              nvarchar(774),
  11250.                @p3              nvarchar(774))
  11251. as
  11252.     set nocount on
  11253.     declare @ret int
  11254.  
  11255.     create table #sptabpriv (
  11256.         TABLE_QUALIFIER sysname collate database_default null,
  11257.         TABLE_OWNER sysname collate database_default null,
  11258.         TABLE_NAME sysname collate database_default not null,
  11259.         GRANTOR sysname collate database_default null,
  11260.         GRANTEE sysname collate database_default not null,
  11261.         PRIVILEGE varchar(32) collate database_default not null,
  11262.         IS_GRANTABLE varchar(3) collate database_default null
  11263.         )
  11264.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  11265.     exec @ret = sp_cursoropen @handle output,
  11266.         'select * from #sptabpriv',
  11267.         @scrollopt output, @ccopt output, @rows output
  11268.     drop table #sptabpriv
  11269.     return @ret
  11270. go
  11271.  
  11272.  
  11273.  
  11274. if (charindex('8.00', @@version) = 0)
  11275. begin
  11276.     print ''
  11277.     print ''
  11278.     print 'Warning:'
  11279.     print 'you are installing the stored procedures '
  11280.     print 'on a pre 8.0 SQL Server.'
  11281.     print 'Ignore the following errors.'
  11282. end
  11283. go
  11284.  
  11285. create procedure sp_ddopen;12(
  11286.                @handle          int output,
  11287.                @procname        sysname,
  11288.                @scrollopt       int output,
  11289.                @ccopt           int output,
  11290.                @rows            int output,
  11291.                @p1              nvarchar(774),
  11292.                @p2              nvarchar(774),
  11293.                @p3              nvarchar(774),
  11294.                @p4              nvarchar(774),
  11295.                @p5              nvarchar(774))
  11296. as
  11297.     set nocount on
  11298.     declare @ret int
  11299.  
  11300.     create table #sptables (
  11301.         TABLE_QUALIFIER sysname collate database_default null,
  11302.         TABLE_OWNER sysname collate database_default null,
  11303.         TABLE_NAME sysname collate database_default null,
  11304.         TABLE_TYPE  varchar(32) collate database_default null,
  11305.         REMARKS varchar(254) collate database_default null)
  11306.     if @procname = 'sp_tables'
  11307.     begin
  11308.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  11309.     end
  11310.     else
  11311.     begin
  11312.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  11313.     end
  11314.     exec @ret = sp_cursoropen @handle output,
  11315.         'select * from #sptables',
  11316.         @scrollopt output, @ccopt output, @rows output
  11317.     drop table #sptables
  11318.     return @ret
  11319. go
  11320.  
  11321. create procedure sp_ddopen;13(
  11322.                @handle          int output,
  11323.                @procname        sysname,
  11324.                @scrollopt       int output,
  11325.                @ccopt           int output,
  11326.                @rows            int output,
  11327.                @p1              nvarchar(774),  -- @table_name
  11328.                @p2              nvarchar(774),  -- @table_owner
  11329.                @p3              nvarchar(774),  -- @table_qualifier
  11330.                @p4              nvarchar(774))  -- @table_type
  11331. as
  11332.     -- this code was copied from sp_tableswc (for 8.0).  Any changes
  11333.     -- made here need to be evaluated for porting to sp_tableswc.
  11334.     set nocount on
  11335.     declare @ret int
  11336.     declare @databasename   sysname
  11337.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  11338.  
  11339.     create table #sptables (
  11340.         TABLE_QUALIFIER sysname collate database_default null,
  11341.         TABLE_OWNER sysname collate database_default null,
  11342.         TABLE_NAME sysname collate database_default null,
  11343.         TABLE_TYPE  varchar(32) collate database_default null,
  11344.         REMARKS varchar(254) collate database_default null)
  11345.  
  11346.     declare databases CURSOR FOR
  11347.         select name from master..sysdatabases
  11348.         where name like @p3 and name <> 'model' and has_dbaccess(name)=1
  11349.         for read only
  11350.  
  11351.     open databases
  11352.     fetch next from databases into @databasename
  11353.     while (@@FETCH_STATUS <> -1)
  11354.     begin
  11355.         if (charindex('%', @databasename) = 0)
  11356.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  11357.             select @qualprocname = @databasename + '..sp_tables'
  11358.             insert into #sptables exec @qualprocname
  11359.                 @p1, @p2, @databasename, @p4
  11360.         end
  11361.         fetch next from databases into @databasename
  11362.     end
  11363.     deallocate databases
  11364.     exec @ret = sp_cursoropen @handle output,
  11365.         'select * from #sptables',
  11366.         @scrollopt output, @ccopt output, @rows output
  11367.     drop table #sptables
  11368.     return @ret
  11369. go
  11370.  
  11371. grant execute on sp_ddopen to public
  11372. go
  11373.  
  11374. print 'creating sp_tableswc'
  11375. go
  11376.  
  11377. if (charindex('7.00', @@version) = 0 and
  11378.     charindex('8.00', @@version) = 0)
  11379. begin
  11380.     print ''
  11381.     print ''
  11382.     print 'Warning:'
  11383.     print 'you are installing the stored procedures '
  11384.     print 'on a pre 7.0 SQL Server.'
  11385.     print 'Ignore the following errors.'
  11386. end
  11387. go
  11388.  
  11389. /*  Procedure for 7.0 server */
  11390. create procedure sp_tableswc(
  11391.                @table_name      nvarchar(384)   = null,
  11392.                @table_owner     nvarchar(384)   = null,
  11393.                @table_qualifier sysname = null,
  11394.                @table_type      varchar(100) = null)
  11395. as
  11396.     -- sp_ddopen;13 contains the same code, so changes made here should
  11397.     -- be evaluated for porting to sp_ddopen;13
  11398.     declare @databasename   sysname
  11399.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  11400.  
  11401.     create table #sptables (
  11402.         TABLE_QUALIFIER sysname null,
  11403.         TABLE_OWNER sysname null,
  11404.         TABLE_NAME sysname null,
  11405.         TABLE_TYPE  varchar(32) null,
  11406.         REMARKS varchar(254) null)
  11407.  
  11408.     declare databases CURSOR FOR
  11409.         select name from master..sysdatabases
  11410.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11411.         for read only
  11412.  
  11413.     open databases
  11414.     fetch next from databases into @databasename
  11415.     while (@@FETCH_STATUS <> -1)
  11416.     begin
  11417.         if (charindex('%', @databasename) = 0)
  11418.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  11419.             select @qualprocname = @databasename + '..sp_tables'
  11420.             insert into #sptables exec @qualprocname
  11421.                 @table_name, @table_owner, @databasename, @table_type
  11422.         end
  11423.         fetch next from databases into @databasename
  11424.     end
  11425.     deallocate databases
  11426.     select * from #sptables
  11427.         order by 4, 1, 2, 3
  11428. go
  11429.  
  11430. if (charindex('8.00', @@version) = 0)
  11431. begin
  11432.     print ''
  11433.     print ''
  11434.     print 'Warning:'
  11435.     print 'you are installing the stored procedures '
  11436.     print 'on a pre 8.0 SQL Server.'
  11437.     print 'Ignore the following errors.'
  11438. end
  11439. else
  11440. drop procedure sp_tableswc
  11441. go
  11442.  
  11443. /*  Procedure for 8.0 server */
  11444. create procedure sp_tableswc(
  11445.                @table_name      nvarchar(384)   = null,
  11446.                @table_owner     nvarchar(384)   = null,
  11447.                @table_qualifier sysname = null,
  11448.                @table_type      varchar(100) = null)
  11449. as
  11450.     -- sp_ddopen;13 contains the same code, so changes made here should
  11451.     -- be evaluated for porting to sp_ddopen;13
  11452.     declare @databasename   sysname
  11453.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  11454.  
  11455.     create table #sptables (
  11456.         TABLE_QUALIFIER sysname collate database_default null,
  11457.         TABLE_OWNER sysname collate database_default null,
  11458.         TABLE_NAME sysname collate database_default null,
  11459.         TABLE_TYPE  varchar(32) collate database_default null,
  11460.         REMARKS varchar(254) collate database_default null)
  11461.  
  11462.     declare databases CURSOR FOR
  11463.         select name from master..sysdatabases
  11464.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11465.         for read only
  11466.  
  11467.     open databases
  11468.     fetch next from databases into @databasename
  11469.     while (@@FETCH_STATUS <> -1)
  11470.     begin
  11471.         if (charindex('%', @databasename) = 0)
  11472.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  11473.             select @qualprocname = @databasename + '..sp_tables'
  11474.             insert into #sptables exec @qualprocname
  11475.                 @table_name, @table_owner, @databasename, @table_type
  11476.         end
  11477.         fetch next from databases into @databasename
  11478.     end
  11479.     deallocate databases
  11480.     select * from #sptables
  11481.         order by 4, 1, 2, 3
  11482. go
  11483.  
  11484. grant execute on sp_tableswc to public
  11485. go
  11486.  
  11487. dump tran master with no_log
  11488. go
  11489.  
  11490. /*-------------------------------------------------------------------------*/
  11491. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  11492. /*-------------------------------------------------------------------------*/
  11493.  
  11494. print ''
  11495. print 'creating spt_provider_types'
  11496. go
  11497. if (charindex('7.00', @@version) = 0 and
  11498.     charindex('8.00', @@version) = 0)
  11499. begin /* Pre 8.0 Server */
  11500. create table spt_provider_types
  11501.     (
  11502.     ss_dtype        tinyint     not null,
  11503.     fixlen          int         null,       /* datatype len for variable, else null */
  11504.     type_name       sysname     not null,
  11505.     oledb_data_type     smallint    not null,
  11506.     best_match      bit     not null,
  11507.     is_nullable     tinyint     null,
  11508.     case_sensitive      bit     not null,
  11509.     fixed_prec_scale    bit     not null,
  11510.     is_long         bit     not null,
  11511.     auto_unique_value   tinyint     not null,
  11512.     data_precision      int         null,
  11513.     numeric_scale       smallint    null,       /* min scale if 6.0 */
  11514.     column_size     int         null,
  11515.     literal_prefix      varchar(32) null,
  11516.     literal_suffix      varchar(32)     null,
  11517.     searchable      int     not null,
  11518.     unsigned_attribute  tinyint     null,
  11519.     local_type_name     sysname     null
  11520.     )
  11521. print ''
  11522. print ''
  11523. print 'Warning:'
  11524. print 'you are installing the stored procedures '
  11525. print 'on a pre 8.0 SQL Server.'
  11526. print 'Ignore the following errors.'
  11527. end
  11528. go
  11529. if (charindex('7.00', @@version) > 0 or
  11530.     charindex('8.00', @@version) > 0)
  11531. begin /* 8.0 Server */
  11532. create table spt_provider_types
  11533.     (
  11534.     ss_dtype        tinyint     not null,
  11535.     fixlen          int         null,       /* datatype len for variable, else null */
  11536.     type_name       sysname     not null,
  11537.     oledb_data_type     smallint    not null,
  11538.     best_match      bit     not null,
  11539.     is_nullable     bit     null,
  11540.     case_sensitive      bit     not null,
  11541.     fixed_prec_scale    bit     not null,
  11542.     is_long         bit     not null,
  11543.     auto_unique_value   tinyint     not null,
  11544.     data_precision      int         null,
  11545.     numeric_scale       smallint    null,       /* min scale if 6.0 */
  11546.     column_size     int         null,
  11547.     literal_prefix      nvarchar(32)    null,
  11548.     literal_suffix      nvarchar(32)    null,
  11549.     searchable      int     not null,
  11550.     unsigned_attribute  tinyint     null,
  11551.     local_type_name     sysname     null
  11552.     )
  11553. end
  11554. go
  11555.  
  11556. grant select on spt_provider_types to public
  11557. go
  11558. dump tran master with no_log
  11559. go
  11560.  
  11561. /*
  11562. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  11563. */
  11564. begin tran
  11565.  
  11566. /* Get case sensitivity */
  11567. declare @case_sensitive bit
  11568. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  11569. /* Local Char */
  11570. insert into spt_provider_types values
  11571.     (
  11572.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  11573.     0,              /* fixlen */
  11574.     'char',             /* type_name */
  11575.     129 /*DBTYPE_STR*/,     /* oledb_data_type */
  11576.     0,              /* best_match */
  11577.     null,               /* is_nullable */
  11578.     @case_sensitive,        /* case_sensitive */
  11579.     0,              /* fixed_prec_scale */
  11580.     0,              /* is_long */
  11581.     0,              /* auto_unique_value */
  11582.     null,               /* data_precision */
  11583.     null,               /* numeric_scale */
  11584.     null,               /* column_size */
  11585.     '''',               /* literal_prefix */
  11586.     '''',               /* literal_suffix */
  11587.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11588.     null,               /* unsigned_attribute */
  11589.     'char'              /* local_type_name */
  11590.     )
  11591.  
  11592. /* Local Varchar */
  11593. insert into spt_provider_types values
  11594.     (
  11595.     39 /*SQLVARCHAR*/,      /* ss_dtype */
  11596.     null,               /* fixlen */
  11597.     'varchar',          /* type_name */
  11598.     129 /*DBTYPE_STR*/,     /* oledb_data_type */
  11599.     1,              /* best_match */
  11600.     null,               /* is_nullable */
  11601.     @case_sensitive,        /* case_sensitive */
  11602.     0,              /* fixed_prec_scale */
  11603.     0,              /* is_long */
  11604.     0,              /* auto_unique_value */
  11605.     null,               /* data_precision */
  11606.     null,               /* numeric_scale */
  11607.     null,               /* column_size */
  11608.     '''',               /* literal_prefix */
  11609.     '''',               /* literal_suffix */
  11610.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11611.     null,               /* unsigned_attribute */
  11612.     'varchar'           /* local_type_name */
  11613.     )
  11614.  
  11615. /* Local Text */
  11616. insert into spt_provider_types values
  11617.     (
  11618.     35  /*SQLTEXT*/,        /* ss_dtype */
  11619.     null,               /* fixlen */
  11620.     'text',             /* type_name */
  11621.     129 /*DBTYPE_STR*/,     /* oledb_data_type */
  11622.     0,              /* best_match */
  11623.     null,               /* is_nullable */
  11624.     @case_sensitive,        /* case_sensitive */
  11625.     0,              /* fixed_prec_scale */
  11626.     1,              /* is_long */
  11627.     0,              /* auto_unique_value */
  11628.     null,               /* data_precision */
  11629.     null,               /* numeric_scale */
  11630.     2147483647,         /* column_size */
  11631.     '''',               /* literal_prefix */
  11632.     '''',               /* literal_suffix */
  11633.     2 /*DB_LIKE_ONLY*/,     /* searchable */
  11634.     null,               /* unsigned_attribute */
  11635.     'text'              /* local_type_name */
  11636.     )
  11637. commit tran
  11638. go
  11639. dump tran master with no_log
  11640. go
  11641.  
  11642. /*
  11643. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  11644. */
  11645. begin tran
  11646. /* Local Binary */
  11647. insert into spt_provider_types values
  11648.     (
  11649.     45 /*SQLBINARY*/,       /* ss_dtype */
  11650.     0,              /* fixlen */
  11651.     'binary',           /* type_name */
  11652.     128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  11653.     0,              /* best_match */
  11654.     null,               /* is_nullable */
  11655.     0,              /* case_sensitive */
  11656.     0,              /* fixed_prec_scale */
  11657.     0,              /* is_long */
  11658.     0,              /* auto_unique_value */
  11659.     null,               /* data_precision */
  11660.     null,               /* numeric_scale */
  11661.     null,               /* column_size */
  11662.     '0x',               /* literal_prefix */
  11663.     null,               /* literal_suffix */
  11664.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11665.     null,               /* unsigned_attribute */
  11666.     'binary'            /* local_type_name */
  11667.     )
  11668.  
  11669. /* Local Varbinary */
  11670. insert into spt_provider_types values
  11671.     (
  11672.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  11673.     null,               /* fixlen */
  11674.     'varbinary',            /* type_name */
  11675.     128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  11676.     1,              /* best_match */
  11677.     null,               /* is_nullable */
  11678.     0,              /* case_sensitive */
  11679.     0,              /* fixed_prec_scale */
  11680.     0,              /* is_long */
  11681.     0,              /* auto_unique_value */
  11682.     null,               /* data_precision */
  11683.     null,               /* numeric_scale */
  11684.     null,               /* column_size */
  11685.     '0x',               /* literal_prefix */
  11686.     null,               /* literal_suffix */
  11687.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11688.     null,               /* unsigned_attribute */
  11689.     'varbinary'         /* local_type_name */
  11690.     )
  11691.  
  11692. /* Local Image */
  11693. insert into spt_provider_types values
  11694.     (
  11695.     34 /*SQLIMAGE*/,        /* ss_dtype */
  11696.     null,               /* fixlen */
  11697.     'image',            /* type_name */
  11698.     128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  11699.     0,              /* best_match */
  11700.     null,               /* is_nullable */
  11701.     0,              /* case_sensitive */
  11702.     0,              /* fixed_prec_scale */
  11703.     1,              /* is_long */
  11704.     0,              /* auto_unique_value */
  11705.     null,               /* data_precision */
  11706.     null,               /* numeric_scale */
  11707.     2147483647,         /* column_size */
  11708.     '0x',               /* literal_prefix */
  11709.     null,               /* literal_suffix */
  11710.     1 /*DB_UNSEARCHABLE*/,      /* searchable */
  11711.     null,               /* unsigned_attribute */
  11712.     'image'             /* local_type_name */
  11713.     )
  11714.  
  11715. commit tran
  11716. go
  11717. dump tran master with no_log
  11718. go
  11719.  
  11720. /*
  11721. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  11722. */
  11723. begin tran
  11724.  
  11725. /* Local Datetime */
  11726. insert into spt_provider_types values
  11727.     (
  11728.     61 /*SQLDATETIME*/,     /* ss_dtype */
  11729.     8,              /* fixlen */
  11730.     'datetime',         /* type_name */
  11731.     135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11732.     1,              /* best_match */
  11733.     null,               /* is_nullable */
  11734.     0,              /* case_sensitive */
  11735.     0,              /* fixed_prec_scale */
  11736.     0,              /* is_long */
  11737.     0,              /* auto_unique_value */
  11738.     23,             /* data_precision */
  11739.     null,               /* numeric_scale */
  11740.     null,               /* column_size */
  11741.     '''',               /* literal_prefix */
  11742.     '''',               /* literal_suffix */
  11743.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11744.     null,               /* unsigned_attribute */
  11745.     'datetime'          /* local_type_name */
  11746.     )
  11747.  
  11748. /* Local Smalldatetime */
  11749. insert into spt_provider_types values
  11750.     (
  11751.     58 /*SQLDATETIM4*/,     /* ss_dtype */
  11752.     4,              /* fixlen */
  11753.     'smalldatetime',        /* type_name */
  11754.     135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11755.     0,              /* 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.     16,             /* data_precision */
  11762.     null,               /* numeric_scale */
  11763.     null,               /* column_size */
  11764.     '''',               /* literal_prefix */
  11765.     '''',               /* literal_suffix */
  11766.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11767.     null,               /* unsigned_attribute */
  11768.     'smalldatetime'         /* local_type_name */
  11769.     )
  11770.  
  11771. if (charindex('7.00', @@version) = 0 and
  11772.     charindex('8.00', @@version) = 0)
  11773.     begin   /*  Add nullable type for non-Sphinx server */
  11774.     
  11775.     /* Local Datetimn */
  11776.     insert into spt_provider_types values
  11777.         (
  11778.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11779.         4,              /* fixlen */
  11780.         'smalldatetime',        /* type_name */
  11781.         135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11782.         0,              /* best_match */
  11783.         null,               /* is_nullable */
  11784.         0,              /* case_sensitive */
  11785.         0,              /* fixed_prec_scale */
  11786.         0,              /* is_long */
  11787.         0,              /* auto_unique_value */
  11788.         16,             /* data_precision */
  11789.         null,               /* numeric_scale */
  11790.         null,               /* column_size */
  11791.         '''',               /* literal_prefix */
  11792.         '''',               /* literal_suffix */
  11793.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11794.         null,               /* unsigned_attribute */
  11795.         'smalldatetime'         /* local_type_name */
  11796.         )   
  11797.     insert into spt_provider_types values
  11798.         (
  11799.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11800.         8,              /* fixlen */
  11801.         'datetime',         /* type_name */
  11802.         135 /*DBTYPE_DBTIMESTAMP*/, /* oledb_data_type */
  11803.         0,              /* best_match */
  11804.         null,               /* is_nullable */
  11805.         0,              /* case_sensitive */
  11806.         0,              /* fixed_prec_scale */
  11807.         0,              /* is_long */
  11808.         0,              /* auto_unique_value */
  11809.         23,             /* data_precision */
  11810.         null,               /* numeric_scale */
  11811.         null,               /* column_size */
  11812.         '''',               /* literal_prefix */
  11813.         '''',               /* literal_suffix */
  11814.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11815.         null,               /* unsigned_attribute */
  11816.         'datetime'          /* local_type_name */
  11817.         )
  11818.     end
  11819.  
  11820. commit tran
  11821. go
  11822. dump tran master with no_log
  11823. go
  11824.  
  11825. /*
  11826. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  11827. */
  11828. begin tran
  11829.  
  11830. /* Local Smallmoney */
  11831. insert into spt_provider_types values
  11832.     (
  11833.     122 /*SQLMONEY4*/,      /* ss_dtype */
  11834.     4,              /* fixlen */
  11835.     'smallmoney',           /* type_name */
  11836.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11837.     0,              /* best_match */
  11838.     null,               /* is_nullable */
  11839.     0,              /* case_sensitive */
  11840.     1,              /* fixed_prec_scale */
  11841.     0,              /* is_long */
  11842.     0,              /* auto_unique_value */
  11843.     10,             /* data_precision */
  11844.     null,               /* numeric_scale */
  11845.     null,               /* column_size */
  11846.     '$',                /* literal_prefix */
  11847.     null,               /* literal_suffix */
  11848.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11849.     0,              /* unsigned_attribute */
  11850.     'smallmoney'            /* local_type_name */
  11851.     )   
  11852.     
  11853. /* Local Money */
  11854. insert into spt_provider_types values
  11855.     (
  11856.     60 /*SQLMONEY*/,        /* ss_dtype */
  11857.     8,              /* fixlen */
  11858.     'money',            /* type_name */
  11859.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11860.     1,              /* best_match */
  11861.     null,               /* is_nullable */
  11862.     0,              /* case_sensitive */
  11863.     1,              /* fixed_prec_scale */
  11864.     0,              /* is_long */
  11865.     0,              /* auto_unique_value */
  11866.     19,             /* data_precision */
  11867.     null,               /* numeric_scale */
  11868.     null,               /* column_size */
  11869.     '$',                /* literal_prefix */
  11870.     null,               /* literal_suffix */
  11871.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11872.     0,              /* unsigned_attribute */
  11873.     'money'             /* local_type_name */
  11874.     )   
  11875.  
  11876. if (charindex('7.00', @@version) = 0 and
  11877.     charindex('8.00', @@version) = 0)
  11878.     begin   /*  Add nullable type for non-Sphinx server */
  11879.  
  11880.     /* Local Moneyn */
  11881.     insert into spt_provider_types values
  11882.         (
  11883.         110 /*SQLMONEYN*/,  /* ss_dtype */
  11884.         4,          /* fixlen */
  11885.         'smallmoney',       /* type_name */
  11886.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11887.         0,          /* best_match */
  11888.         null,           /* is_nullable */
  11889.         0,          /* case_sensitive */
  11890.         1,          /* fixed_prec_scale */
  11891.         0,          /* is_long */
  11892.         0,          /* auto_unique_value */
  11893.         10,         /* data_precision */
  11894.         null,           /* numeric_scale */
  11895.         null,           /* column_size */
  11896.         '$',            /* literal_prefix */
  11897.         null,           /* literal_suffix */
  11898.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11899.         0,          /* unsigned_attribute */
  11900.         'smallmoney'        /* local_type_name */
  11901.         )   
  11902.     insert into spt_provider_types values
  11903.         (
  11904.         110 /*SQLMONEYN*/,  /* ss_dtype */
  11905.         8,          /* fixlen */
  11906.         'money',        /* type_name */
  11907.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11908.         0,          /* best_match */
  11909.         null,           /* is_nullable */
  11910.         0,          /* case_sensitive */
  11911.         1,          /* fixed_prec_scale */
  11912.         0,          /* is_long */
  11913.         0,          /* auto_unique_value */
  11914.         19,         /* data_precision */
  11915.         null,           /* numeric_scale */
  11916.         null,           /* column_size */
  11917.         '$',            /* literal_prefix */
  11918.         null,           /* literal_suffix */
  11919.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11920.         null,           /* unsigned_attribute */
  11921.         'money'         /* local_type_name */
  11922.         )   
  11923.     end
  11924.  
  11925. commit tran
  11926. go
  11927. dump tran master with no_log
  11928. go
  11929.  
  11930. /*
  11931. ** Insert the spt_provider_types rows for the numeric data types
  11932. */
  11933. begin tran
  11934.  
  11935. /* Local Float */
  11936. insert into spt_provider_types values
  11937.     (
  11938.     62 /*SQLFLT8*/,         /* ss_dtype */
  11939.     8,              /* fixlen */
  11940.     'float',            /* type_name */
  11941.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  11942.     1,              /* best_match */
  11943.     null,               /* is_nullable */
  11944.     0,              /* case_sensitive */
  11945.     0,              /* fixed_prec_scale */
  11946.     0,              /* is_long */
  11947.     0,              /* auto_unique_value */
  11948.     15,             /* data_precision */
  11949.     null,               /* numeric_scale */
  11950.     null,               /* column_size */
  11951.     null,               /* literal_prefix */
  11952.     null,               /* literal_suffix */
  11953.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11954.     0,              /* unsigned_attribute */
  11955.     'float'             /* local_type_name */
  11956.     )   
  11957.  
  11958. /* Local Real */
  11959. insert into spt_provider_types values
  11960.     (
  11961.     59 /*SQLFLT4*/,         /* ss_dtype */
  11962.     4,              /* fixlen */
  11963.     'real',             /* type_name */
  11964.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  11965.     1,              /* best_match */
  11966.     null,               /* is_nullable */
  11967.     0,              /* case_sensitive */
  11968.     0,              /* fixed_prec_scale */
  11969.     0,              /* is_long */
  11970.     0,              /* auto_unique_value */
  11971.     7,              /* data_precision */
  11972.     null,               /* numeric_scale */
  11973.     null,               /* column_size */
  11974.     null,               /* literal_prefix */
  11975.     null,               /* literal_suffix */
  11976.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  11977.     0,              /* unsigned_attribute */
  11978.     'real'              /* local_type_name */
  11979.     )   
  11980.  
  11981. /* Local Int */
  11982. insert into spt_provider_types values
  11983.     (
  11984.     56 /*SQLINT4*/,         /* ss_dtype */
  11985.     4,              /* fixlen */
  11986.     'int',              /* type_name */
  11987.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  11988.     1,              /* best_match */
  11989.     null,               /* is_nullable */
  11990.     0,              /* case_sensitive */
  11991.     1,              /* fixed_prec_scale */
  11992.     0,              /* is_long */
  11993.     1,              /* auto_unique_value */
  11994.     10,             /* data_precision */
  11995.     null,               /* numeric_scale */
  11996.     null,               /* column_size */
  11997.     null,               /* literal_prefix */
  11998.     null,               /* literal_suffix */
  11999.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12000.     0,              /* unsigned_attribute */
  12001.     'int'               /* local_type_name */
  12002.     )   
  12003.  
  12004. /* Local Smallint */
  12005. insert into spt_provider_types values
  12006.     (
  12007.     52 /*SQLINT2*/,         /* ss_dtype */
  12008.     2,              /* fixlen */
  12009.     'smallint',         /* type_name */
  12010.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  12011.     1,              /* best_match */
  12012.     null,               /* is_nullable */
  12013.     0,              /* case_sensitive */
  12014.     1,              /* fixed_prec_scale */
  12015.     0,              /* is_long */
  12016.     1,              /* auto_unique_value */
  12017.     5,              /* data_precision */
  12018.     null,               /* numeric_scale */
  12019.     null,               /* column_size */
  12020.     null,               /* literal_prefix */
  12021.     null,               /* literal_suffix */
  12022.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12023.     0,              /* unsigned_attribute */
  12024.     'smallint'          /* local_type_name */
  12025.     )   
  12026.  
  12027. /* Local Tinyint */
  12028. insert into spt_provider_types values
  12029.     (
  12030.     48 /*SQLINT1*/,         /* ss_dtype */
  12031.     1,              /* fixlen */
  12032.     'tinyint',          /* type_name */
  12033.     17 /*DBTYPE_UI1*/,      /* oledb_data_type */
  12034.     1,              /* best_match */
  12035.     null,               /* is_nullable */
  12036.     0,              /* case_sensitive */
  12037.     1,              /* fixed_prec_scale */
  12038.     0,              /* is_long */
  12039.     1,              /* auto_unique_value */
  12040.     3,              /* data_precision */
  12041.     null,               /* numeric_scale */
  12042.     null,               /* column_size */
  12043.     null,               /* literal_prefix */
  12044.     null,               /* literal_suffix */
  12045.     3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12046.     1,              /* unsigned_attribute */
  12047.     'tinyint'           /* local_type_name */
  12048.     )   
  12049. commit tran
  12050. go
  12051. dump tran master with no_log
  12052. go
  12053. begin tran  
  12054. if (charindex('6.00', @@version) > 0 or
  12055.     charindex('6.50', @@version) > 0 or
  12056.     charindex('7.00', @@version) > 0 or
  12057.     charindex('8.00', @@version) > 0)
  12058.     begin   /*  Add 6.0 data types */
  12059.     
  12060.     /* Local Decimal */
  12061.     insert into spt_provider_types values
  12062.         (
  12063.         55 /*SQLDECIMAL*/,      /* ss_dtype */
  12064.         0,              /* fixlen */
  12065.         'decimal',          /* type_name */
  12066.         131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12067.         0,              /* best_match */
  12068.         null,               /* is_nullable */
  12069.         0,              /* case_sensitive */
  12070.         1,              /* fixed_prec_scale */
  12071.         0,              /* is_long */
  12072.         1,              /* auto_unique_value */
  12073.         38,             /* data_precision */
  12074.         null,               /* numeric_scale */
  12075.         null,               /* column_size */
  12076.         null,               /* literal_prefix */
  12077.         null,               /* literal_suffix */
  12078.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12079.         0,              /* unsigned_attribute */
  12080.         'decimal'           /* local_type_name */
  12081.         )   
  12082.  
  12083.     /* Local Numeric */
  12084.     insert into spt_provider_types values
  12085.         (
  12086.         63 /*SQLNUMERIC*/,      /* ss_dtype */
  12087.         0,              /* fixlen */
  12088.         'numeric',          /* type_name */
  12089.         131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12090.         1,              /* best_match */
  12091.         null,               /* is_nullable */
  12092.         0,              /* case_sensitive */
  12093.         1,              /* fixed_prec_scale */
  12094.         0,              /* is_long */
  12095.         1,              /* auto_unique_value */
  12096.         38,             /* data_precision */
  12097.         null,               /* numeric_scale */
  12098.         null,               /* column_size */
  12099.         null,               /* literal_prefix */
  12100.         null,               /* literal_suffix */
  12101.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12102.         0,              /* unsigned_attribute */
  12103.         'numeric'           /* local_type_name */
  12104.         )   
  12105.     end
  12106. commit tran
  12107. go
  12108. dump tran master with no_log
  12109. go
  12110. begin tran
  12111. if (charindex('7.00', @@version) = 0 and
  12112.     charindex('8.00', @@version) = 0)
  12113.     begin   /*  Add nullable type for non-Sphinx server */
  12114.     
  12115.     /* Local Floatn */
  12116.     insert into spt_provider_types values
  12117.         (
  12118.         109 /*SQLFLTN*/,        /* ss_dtype */
  12119.         8,              /* fixlen */
  12120.         'float',            /* type_name */
  12121.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  12122.         0,              /* best_match */
  12123.         null,               /* is_nullable */
  12124.         0,              /* case_sensitive */
  12125.         0,              /* fixed_prec_scale */
  12126.         0,              /* is_long */
  12127.         0,              /* auto_unique_value */
  12128.         15,             /* data_precision */
  12129.         null,               /* numeric_scale */
  12130.         null,               /* column_size */
  12131.         null,               /* literal_prefix */
  12132.         null,               /* literal_suffix */
  12133.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12134.         null,               /* unsigned_attribute */
  12135.         'float'             /* local_type_name */
  12136.         )   
  12137.     insert into spt_provider_types values
  12138.         (
  12139.         109 /*SQLFLT4*/,        /* ss_dtype */
  12140.         4,              /* fixlen */
  12141.         'real',             /* type_name */
  12142.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  12143.         0,              /* best_match */
  12144.         null,               /* is_nullable */
  12145.         0,              /* case_sensitive */
  12146.         0,              /* fixed_prec_scale */
  12147.         0,              /* is_long */
  12148.         0,              /* auto_unique_value */
  12149.         7,              /* data_precision */
  12150.         null,               /* numeric_scale */
  12151.         null,               /* column_size */
  12152.         null,               /* literal_prefix */
  12153.         null,               /* literal_suffix */
  12154.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12155.         null,               /* unsigned_attribute */
  12156.         'real'              /* local_type_name */
  12157.         )   
  12158.  
  12159.     /* Local Intn */
  12160.     insert into spt_provider_types values
  12161.         (
  12162.         38 /*SQLINTN*/,         /* ss_dtype */
  12163.         4,              /* fixlen */
  12164.         'int',              /* type_name */
  12165.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  12166.         1,              /* best_match */
  12167.         null,               /* is_nullable */
  12168.         0,              /* case_sensitive */
  12169.         1,              /* fixed_prec_scale */
  12170.         0,              /* is_long */
  12171.         1,              /* auto_unique_value */
  12172.         10,             /* data_precision */
  12173.         null,               /* numeric_scale */
  12174.         null,               /* column_size */
  12175.         null,               /* literal_prefix */
  12176.         null,               /* literal_suffix */
  12177.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12178.         0,              /* unsigned_attribute */
  12179.         'int'               /* local_type_name */
  12180.         )   
  12181.     insert into spt_provider_types values
  12182.         (
  12183.         38 /*SQLINTN*/,         /* ss_dtype */
  12184.         2,              /* fixlen */
  12185.         'smallint',         /* type_name */
  12186.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  12187.         0,              /* best_match */
  12188.         null,               /* is_nullable */
  12189.         0,              /* case_sensitive */
  12190.         1,              /* fixed_prec_scale */
  12191.         0,              /* is_long */
  12192.         1,              /* auto_unique_value */
  12193.         5,              /* data_precision */
  12194.         null,               /* numeric_scale */
  12195.         null,               /* column_size */
  12196.         null,               /* literal_prefix */
  12197.         null,               /* literal_suffix */
  12198.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12199.         0,              /* unsigned_attribute */
  12200.         'smallint'          /* local_type_name */
  12201.         )   
  12202.     insert into spt_provider_types values
  12203.         (
  12204.         38 /*SQLINTN*/,         /* ss_dtype */
  12205.         1,              /* fixlen */
  12206.         'tinyint',          /* type_name */
  12207.         17 /*DBTYPE_UI1*/,      /* oledb_data_type */
  12208.         0,              /* best_match */
  12209.         null,               /* is_nullable */
  12210.         0,              /* case_sensitive */
  12211.         1,              /* fixed_prec_scale */
  12212.         0,              /* is_long */
  12213.         1,              /* auto_unique_value */
  12214.         3,              /* data_precision */
  12215.         null,               /* numeric_scale */
  12216.         null,               /* column_size */
  12217.         null,               /* literal_prefix */
  12218.         null,               /* literal_suffix */
  12219.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12220.         1,              /* unsigned_attribute */
  12221.         'tinyint'           /* local_type_name */
  12222.         )   
  12223.  
  12224.     if (charindex('6.00', @@version) > 0 or charindex('6.50', @@version) > 0)
  12225.         begin   /*  Add 6.0 data types */
  12226.         
  12227.         /* Local Decimaln */
  12228.         insert into spt_provider_types values
  12229.             (
  12230.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  12231.             0,              /* fixlen */
  12232.             'decimal',          /* type_name */
  12233.             131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12234.             0,              /* best_match */
  12235.             null,               /* is_nullable */
  12236.             0,              /* case_sensitive */
  12237.             1,              /* fixed_prec_scale */
  12238.             0,              /* is_long */
  12239.             1,              /* auto_unique_value */
  12240.             38,             /* data_precision */
  12241.             null,               /* numeric_scale */
  12242.             null,               /* column_size */
  12243.             null,               /* literal_prefix */
  12244.             null,               /* literal_suffix */
  12245.             3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12246.             0,              /* unsigned_attribute */
  12247.             'decimal'           /* local_type_name */
  12248.             )   
  12249.  
  12250.         /* Local Numericn */
  12251.         insert into spt_provider_types values
  12252.             (
  12253.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  12254.             0,              /* fixlen */
  12255.             'numeric',          /* type_name */
  12256.             131 /*DBTYPE_NUMERIC*/,     /* oledb_data_type */
  12257.             0,              /* best_match */
  12258.             null,               /* is_nullable */
  12259.             0,              /* case_sensitive */
  12260.             1,              /* fixed_prec_scale */
  12261.             0,              /* is_long */
  12262.             1,              /* auto_unique_value */
  12263.             38,             /* data_precision */
  12264.             null,               /* numeric_scale */
  12265.             null,               /* column_size */
  12266.             null,               /* literal_prefix */
  12267.             null,               /* literal_suffix */
  12268.             3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12269.             0,              /* unsigned_attribute */
  12270.             'numeric'           /* local_type_name */
  12271.             )   
  12272.         end
  12273.     end
  12274. commit tran
  12275. go
  12276. dump tran master with no_log
  12277. go
  12278.  
  12279. /*
  12280. **  Remaining data types
  12281. */
  12282. if (charindex('7.00', @@version) = 0 and
  12283.     charindex('8.00', @@version) = 0)
  12284. begin
  12285.     print ''
  12286.     print ''
  12287.     print 'Warning:'
  12288.     print 'you are installing the stored procedures '
  12289.     print 'on a pre 8.0 SQL Server.'
  12290.     print 'Ignore the following errors.'
  12291. end
  12292. go
  12293. begin tran
  12294.  
  12295. if (charindex('7.00', @@version) > 0 or
  12296.     charindex('8.00', @@version) > 0)
  12297.     begin
  12298.     /* Local Bit */
  12299.     insert into spt_provider_types values
  12300.         (
  12301.         50 /*SQLBIT*/,          /* ss_dtype */
  12302.         0,              /* fixlen */
  12303.         'bit',              /* type_name */
  12304.         11 /*DBTYPE_BOOL*/,     /* oledb_data_type */
  12305.         1,              /* best_match */
  12306.         null,               /* is_nullable */
  12307.         0,              /* case_sensitive */
  12308.         0,              /* fixed_prec_scale */
  12309.         0,              /* is_long */
  12310.         0,              /* auto_unique_value */
  12311.         1,              /* data_precision */
  12312.         null,               /* numeric_scale */
  12313.         null,               /* column_size */
  12314.         null,               /* literal_prefix */
  12315.         null,               /* literal_suffix */
  12316.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12317.         null,               /* unsigned_attribute */
  12318.         'bit'               /* local_type_name */
  12319.         )       
  12320.     
  12321.     /* Local Timestamp */
  12322.     insert into spt_provider_types values
  12323.         (
  12324.         0,              /* ss_dtype */
  12325.         8,              /* fixlen */
  12326.         'timestamp',            /* type_name */
  12327.         128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  12328.         0,              /* best_match */
  12329.         null,               /* is_nullable */
  12330.         0,              /* case_sensitive */
  12331.         0,              /* fixed_prec_scale */
  12332.         0,              /* is_long */
  12333.         0,              /* auto_unique_value */
  12334.         null,               /* data_precision */
  12335.         null,               /* numeric_scale */
  12336.         null,               /* column_size */
  12337.         '0x',               /* literal_prefix */
  12338.         null,               /* literal_suffix */
  12339.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12340.         null,               /* unsigned_attribute */
  12341.         'timestamp'         /* local_type_name */
  12342.         )   
  12343.     /* Local GUID */
  12344.     insert into spt_provider_types values
  12345.         (
  12346.         0,              /* ss_dtype */
  12347.         16,             /* fixlen */
  12348.         'uniqueidentifier',     /* type_name */
  12349.         72 /*DBTYPE_GUID*/,     /* oledb_data_type */
  12350.         1,              /* best_match */
  12351.         null,               /* is_nullable */
  12352.         0,              /* case_sensitive */
  12353.         0,              /* fixed_prec_scale */
  12354.         0,              /* is_long */
  12355.         0,              /* auto_unique_value */
  12356.         null,               /* data_precision */
  12357.         null,               /* numeric_scale */
  12358.         null,               /* column_size */
  12359.         '''',               /* literal_prefix */
  12360.         '''',               /* literal_suffix */
  12361.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12362.         null,               /* unsigned_attribute */
  12363.         'uniqueidentifier'      /* local_type_name */
  12364.         )   
  12365.     
  12366.     /* Get case sensitivity */
  12367.     declare @ncase_sensitive bit
  12368.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12369.  
  12370.     /* Local NChar */
  12371.     insert into spt_provider_types values
  12372.         (
  12373.         0,              /* ss_dtype */
  12374.         0,              /* fixlen */
  12375.         'nchar',            /* type_name */
  12376.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12377.         0,              /* best_match */
  12378.         null,               /* is_nullable */
  12379.         @ncase_sensitive,       /* case_sensitive */
  12380.         0,              /* fixed_prec_scale */
  12381.         0,              /* is_long */
  12382.         0,              /* auto_unique_value */
  12383.         null,               /* data_precision */
  12384.         null,               /* numeric_scale */
  12385.         null,               /* column_size */
  12386.         'N''',              /* literal_prefix */
  12387.         '''',               /* literal_suffix */
  12388.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12389.         null,               /* unsigned_attribute */
  12390.         'nchar'             /* local_type_name */
  12391.         )   
  12392.  
  12393.     /* Local NVarChar */
  12394.     insert into spt_provider_types values
  12395.         (
  12396.         0,              /* ss_dtype */
  12397.         null,               /* fixlen */
  12398.         'nvarchar',         /* type_name */
  12399.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12400.         1,              /* best_match */
  12401.         null,               /* is_nullable */
  12402.         @ncase_sensitive,       /* case_sensitive */
  12403.         0,              /* fixed_prec_scale */
  12404.         0,              /* is_long */
  12405.         0,              /* auto_unique_value */
  12406.         null,               /* data_precision */
  12407.         null,               /* numeric_scale */
  12408.         null,               /* column_size */
  12409.         'N''',              /* literal_prefix */
  12410.         '''',               /* literal_suffix */
  12411.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12412.         null,               /* unsigned_attribute */
  12413.         'nvarchar'          /* local_type_name */
  12414.         )   
  12415.  
  12416.     /* Local NText */
  12417.     insert into spt_provider_types values
  12418.         (
  12419.         0,              /* ss_dtype */
  12420.         null,               /* fixlen */
  12421.         'ntext',            /* type_name */
  12422.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12423.         0,              /* best_match */
  12424.         null,               /* is_nullable */
  12425.         @ncase_sensitive,       /* case_sensitive */
  12426.         0,              /* fixed_prec_scale */
  12427.         1,              /* is_long */
  12428.         0,              /* auto_unique_value */
  12429.         null,               /* data_precision */
  12430.         null,               /* numeric_scale */
  12431.         1073741823,         /* column_size */
  12432.         'N''',              /* literal_prefix */
  12433.         '''',               /* literal_suffix */
  12434.         2 /*DB_LIKE_ONLY*/,     /* searchable */
  12435.         null,               /* unsigned_attribute */
  12436.         'ntext'             /* local_type_name */
  12437.         )   
  12438.  
  12439.     if (charindex('8.00', @@version) > 0)
  12440.     begin
  12441.     /* Local BIGINT */
  12442.     insert into spt_provider_types values
  12443.         (
  12444.         127,                /* ss_dtype */
  12445.         8,              /* fixlen */
  12446.         'bigint',           /* type_name */
  12447.         20 /*DBTYPE_I8*/,       /* oledb_data_type */
  12448.         1,              /* best_match */
  12449.         null,               /* is_nullable */
  12450.         0,              /* case_sensitive */
  12451.         1,              /* fixed_prec_scale */
  12452.         0,              /* is_long */
  12453.         1,              /* auto_unique_value */
  12454.         19,             /* data_precision */
  12455.         null,           /* numeric_scale */
  12456.         null,           /* column_size */
  12457.         null,           /* literal_prefix */
  12458.         null,           /* literal_suffix */
  12459.         3 /*DB_ALL_EXCEPT_LIKE*/,       /* searchable */
  12460.         0,              /* unsigned_attribute */
  12461.         'bigint'        /* local_type_name */
  12462.         )
  12463.  
  12464.     /* sql_variant */
  12465.     insert into spt_provider_types values
  12466.         (
  12467.         98,             /* ss_dtype */
  12468.         null,           /* fixlen */
  12469.         'sql_variant',  /* type_name */
  12470.         12 /*DBTYPE_VARIANT*/,      /* oledb_data_type */
  12471.         1,              /* best_match */
  12472.         null,           /* is_nullable */
  12473.         0,              /* case_sensitive */
  12474.         0,              /* fixed_prec_scale */
  12475.         0,              /* is_long */
  12476.         0,              /* auto_unique_value */
  12477.         16,             /* data_precision sizeof(VARIANT)*/
  12478.         null,           /* numeric_scale */
  12479.         null,           /* column_size */
  12480.         null,           /* literal_prefix */
  12481.         null,           /* literal_suffix */
  12482.         3 ,                 /*DB_ALL_EXCEPT_LIKE*/
  12483.         null,           /* unsigned_attribute */
  12484.         'sql_variant'   /* local_type_name */
  12485.         )
  12486.     end
  12487.  
  12488.  
  12489.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  12490.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  12491.  
  12492.     end
  12493. commit tran
  12494. go
  12495. dump tran master with no_log
  12496. go
  12497. begin tran
  12498. if (charindex('7.00', @@version) = 0 and
  12499.     charindex('8.00', @@version) = 0)
  12500.     begin
  12501.     /* Local Bit */
  12502.     insert into spt_provider_types values
  12503.         (
  12504.         50 /*SQLBIT*/,          /* ss_dtype */
  12505.         0,              /* fixlen */
  12506.         'bit',              /* type_name */
  12507.         11 /*DBTYPE_BOOL*/,     /* oledb_data_type */
  12508.         1,              /* best_match */
  12509.         0,              /* is_nullable */
  12510.         0,              /* case_sensitive */
  12511.         0,              /* fixed_prec_scale */
  12512.         0,              /* is_long */
  12513.         0,              /* auto_unique_value */
  12514.         1,              /* data_precision */
  12515.         null,               /* numeric_scale */
  12516.         null,               /* column_size */
  12517.         null,               /* literal_prefix */
  12518.         null,               /* literal_suffix */
  12519.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12520.         null,               /* unsigned_attribute */
  12521.         'bit'               /* local_type_name */
  12522.         )   
  12523.  
  12524.     /* Local Timestamp */
  12525.     insert into spt_provider_types values
  12526.         (
  12527.         45 /*SQLBINARY*/,       /* ss_dtype */
  12528.         8,              /* fixlen */
  12529.         'timestamp',            /* type_name */
  12530.         128 /*DBTYPE_BYTES*/,       /* oledb_data_type */
  12531.         0,              /* best_match */
  12532.         null,               /* is_nullable */
  12533.         0,              /* case_sensitive */
  12534.         0,              /* fixed_prec_scale */
  12535.         0,              /* is_long */
  12536.         0,              /* auto_unique_value */
  12537.         null,               /* data_precision */
  12538.         null,               /* numeric_scale */
  12539.         null,               /* column_size */
  12540.         '0x',               /* literal_prefix */
  12541.         null,               /* literal_suffix */
  12542.         3 /*DB_ALL_EXCEPT_LIKE*/,   /* searchable */
  12543.         null,               /* unsigned_attribute */
  12544.         'timestamp'         /* local_type_name */
  12545.         )       
  12546. end
  12547.  
  12548. commit tran
  12549. go
  12550. dump tran master with no_log
  12551. go
  12552.  
  12553. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  12554. go
  12555.  
  12556. dump tran master with no_log
  12557. go
  12558. if (charindex('6.00', @@version) > 0)
  12559.     begin
  12560.     if (exists (select * from sysobjects
  12561.         where name = 'spt_provider_types' and type = 'U '))
  12562.         begin
  12563.         drop table spt_provider_types
  12564.         dump tran master with no_log
  12565.         end
  12566.     end
  12567. go
  12568.  
  12569. if (charindex('7.00', @@version) = 0 and
  12570.     charindex('8.00', @@version) = 0)
  12571. begin
  12572.     print ''
  12573.     print ''
  12574.     print 'Warning:'
  12575.     print 'you are installing the stored procedures '
  12576.     print 'on a pre 8.0 SQL Server.'
  12577.     print 'Ignore the following errors.'
  12578. end
  12579. go
  12580.  
  12581.  
  12582. /* 
  12583. ** (2/11/98) 
  12584. ** The following SP is shared by instcat.sql and the server to add information
  12585. ** related to server language collations. The server calls it at the end of
  12586. ** collation change to update the catalog with the new language collations
  12587. ** Everything in this SP should NOT use tempdb.  
  12588. */
  12589.  
  12590. create procedure sp_add_server_sortinfo
  12591. as
  12592. -- spt_server_info has unigue clustered index on attribute_id
  12593. delete spt_server_info where attribute_id in (16,18)
  12594.  
  12595. --insert into spt_server_info
  12596. --  select 18,'COLLATION_SEQ',
  12597. --      'charset='+t2.name+' sort_order='+t1.name
  12598. --      +' charset_num='+rtrim(convert(char(4),t1.csid))+
  12599. --      ' sort_order_num='+rtrim(convert(char(4),t1.id))
  12600. --  from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  12601. --  where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  12602. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12603. declare @case_sensitive bit
  12604. declare @ncase_sensitive bit
  12605. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12606. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12607. select @sortid = value from sysconfigures where config = 1123
  12608. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  12609. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12610.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12611.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12612.         from syscharsets where id = @csid
  12613.  
  12614. insert into spt_server_info
  12615.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12616.  
  12617. if @case_sensitive = 1 /* If case sensitive server */
  12618. begin
  12619.     insert into spt_server_info
  12620.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  12621. end
  12622. else
  12623. begin
  12624.     insert into spt_server_info
  12625.         values (16,'IDENTIFIER_CASE','MIXED')
  12626. end
  12627.  
  12628.  
  12629. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12630.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12631. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12632.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12633. update spt_provider_types set case_sensitive = @case_sensitive 
  12634.     where oledb_data_type = 129 /*DBTYPE_STR*/
  12635. update spt_provider_types set case_sensitive = @ncase_sensitive
  12636.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12637. /* sp_add_server_sortinfo */
  12638. go  
  12639.  
  12640.  
  12641. if (charindex('7.00', @@version) > 0)
  12642. begin
  12643.     exec sp_add_server_sortinfo
  12644. end
  12645. go
  12646.  
  12647. -- 8.0 version of sp_add_server_sortinfo
  12648. --
  12649. create procedure sp_add_server_sortinfo75
  12650. as
  12651. -- spt_server_info has unigue clustered index on attribute_id
  12652.     delete spt_server_info where attribute_id in (16, 18)
  12653.  
  12654.     declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12655.     declare @case_sensitive bit
  12656.     declare @ncase_sensitive bit
  12657.     select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12658.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12659.  
  12660.     select @sortid = convert(int, ServerProperty('sqlsortorder')) 
  12661.     select @csid = convert(int, ServerProperty('sqlcharset')) 
  12662.     select @sortname = name from master.dbo.syscharsets where id = @sortid
  12663.  
  12664.     if @sortid = 0 -- Non-SQL Collations
  12665.         BEGIN
  12666.         select @attribute_value = 'charset=' 
  12667.             + name
  12668.             + ' collation=' + isnull(convert(sysname, ServerProperty('collation')), ' ')
  12669.             from syscharsets where id = @csid
  12670.         END
  12671.     else
  12672.         BEGIN
  12673.         select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12674.             + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12675.             ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12676.             from syscharsets where id = @csid
  12677.         END
  12678.  
  12679.     insert into spt_server_info
  12680.             values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12681.  
  12682.     if @case_sensitive = 1 /* If case sensitive server */
  12683.     begin
  12684.         insert into spt_server_info
  12685.             values (16,'IDENTIFIER_CASE','SENSITIVE')
  12686.     end
  12687.     else
  12688.     begin
  12689.         insert into spt_server_info
  12690.             values (16,'IDENTIFIER_CASE','MIXED')
  12691.     end
  12692.  
  12693.  
  12694.     update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12695.         where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12696.     update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12697.         where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12698.     update spt_provider_types set case_sensitive = @case_sensitive 
  12699.         where oledb_data_type = 129 /*DBTYPE_STR*/
  12700.     update spt_provider_types set case_sensitive = @ncase_sensitive
  12701.         where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12702. /* sp_add_server_info75 */
  12703. go 
  12704.  
  12705.  
  12706.  
  12707. if (charindex('8.00', @@version) > 0)
  12708. begin
  12709.     exec sp_add_server_sortinfo75
  12710. end
  12711. go
  12712.  
  12713. print ''
  12714. print 'creating sp_catalogs_rowset'
  12715. go
  12716.  
  12717. create procedure sp_catalogs_rowset
  12718.     (
  12719.     @catalog_name       varchar(255)
  12720.     )       
  12721. as
  12722.     select
  12723.         CATALOG_NAME    = name,
  12724.         DESCRIPTION = convert(varchar(1),null)
  12725.     from    master.dbo.sysdatabases
  12726.     where   name = @catalog_name AND has_dbaccess(name)=1
  12727. go
  12728. dump tran master with no_log
  12729. go
  12730. create procedure sp_catalogs_rowset;2
  12731.     (
  12732.     @dummy      int /* remove when Hydra bug 17032 is fixed */
  12733.     )
  12734. as
  12735.     select
  12736.         CATALOG_NAME    = name,
  12737.         DESCRIPTION = convert(varchar(1),null)
  12738.     from    master.dbo.sysdatabases
  12739.     where   has_dbaccess(name)=1
  12740.     order by 1
  12741. go
  12742. dump tran master with no_log
  12743. go
  12744.  
  12745. if (charindex('7.00', @@version) = 0 and
  12746.     charindex('8.00', @@version) = 0)
  12747. begin
  12748.     print ''
  12749.     print ''
  12750.     print 'Warning:'
  12751.     print 'you are installing the stored procedures '
  12752.     print 'on a pre 8.0 SQL Server.'
  12753.     print 'Ignore the following errors.'
  12754. end
  12755. else
  12756.     drop proc sp_catalogs_rowset
  12757. go
  12758.  
  12759. /*  Procedure for 8.0 server */
  12760. create procedure sp_catalogs_rowset
  12761.     (
  12762.     @catalog_name       sysname
  12763.     )       
  12764. as
  12765.     select
  12766.         CATALOG_NAME    = name,
  12767.         DESCRIPTION = convert(nvarchar(1),null)
  12768.     from    master.dbo.sysdatabases
  12769.     where   name = @catalog_name AND has_dbaccess(name)=1
  12770. go
  12771. dump tran master with no_log
  12772. go
  12773. create procedure sp_catalogs_rowset;2
  12774.     (
  12775.     @dummy      int /* remove when Hydra bug 17032 is fixed */
  12776.     )
  12777. as
  12778.     select
  12779.         CATALOG_NAME    = name,
  12780.         DESCRIPTION = convert(nvarchar(1),null)
  12781.     from    master.dbo.sysdatabases
  12782.     where   has_dbaccess(name)=1
  12783.     order by 1
  12784. go
  12785. dump tran master with no_log
  12786. go
  12787. create procedure sp_catalogs_rowset;5
  12788.     (
  12789.     @server_name    sysname,
  12790.     @catalog_name   sysname = NULL
  12791.     )
  12792. as
  12793.     select  CATALOG_NAME,
  12794.         DESCRIPTION
  12795.     from master.dbo.SYSREMOTE_CATALOGS ( @server_name, @catalog_name )
  12796.     where   has_dbaccess(CATALOG_NAME)=1
  12797.     order by 1
  12798. go
  12799.  
  12800. grant execute on sp_catalogs_rowset to public
  12801. go
  12802.  
  12803. dump tran master with no_log
  12804. go
  12805. if (charindex('6.00', @@version) > 0)
  12806.     begin
  12807.     if (exists (select * from sysobjects
  12808.             where name = 'sp_catalogs_rowset' and type = 'P '))
  12809.         begin
  12810.         drop procedure sp_catalogs_rowset
  12811.         dump tran master with no_log
  12812.         end
  12813.     end
  12814. go
  12815.  
  12816.  
  12817. print ''
  12818. print 'creating sp_column_privileges_rowset'
  12819. go
  12820.  
  12821. /*  Procedure for 6.0 and 6.5 server */
  12822. CREATE PROCEDURE sp_column_privileges_rowset
  12823.     (
  12824.     @table_name     varchar(255) = null,
  12825.     @table_schema   varchar(255) = null,
  12826.     @column_name    varchar(255) = null,
  12827.     @grantor    varchar(255) = null,
  12828.     @grantee    varchar(255) = null
  12829.     )
  12830. as
  12831. IF @table_name is not null
  12832.     BEGIN
  12833.     select
  12834.         GRANTOR     = user_name(p.grantor),
  12835.         GRANTEE     = user_name(u.uid),
  12836.         TABLE_CATALOG   = db_name(),
  12837.         TABLE_SCHEMA    = user_name(o.uid),
  12838.         TABLE_NAME  = o.name,
  12839.         COLUMN_NAME = c.name,
  12840.         COLUMN_GUID = convert(binary(16),null),
  12841.         COLUMN_PROPID   = convert(int,null),
  12842.         PRIVILEGE_TYPE  = convert(varchar(30),
  12843.                     case p.action
  12844.                     when 193 then 'SELECT'
  12845.                     when 195 then 'INSERT'
  12846.                     when 197 then 'UPDATE'
  12847.                     else 'REFERENCES'
  12848.                     end),
  12849.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12850.     from 
  12851.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12852.     where
  12853.         o.name = @table_name
  12854.     and     o.type in ('U','V','S')
  12855.     and (@table_schema is null or @table_schema = user_name(o.uid))
  12856.     and c.id = o.id
  12857.     and     (@column_name is null or @column_name = c.name)
  12858.     and     c.id = p.id
  12859.     and (@grantor is null or @grantor = user_name(p.grantor))
  12860.     and     case 
  12861.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12862.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12863.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12864.             end & v.high <> 0           /* permission applies to this column */
  12865.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  12866.     and     v.type = 'P'
  12867.     and     v.number = c.colid
  12868.     and (@grantee is null or @grantee = user_name(u.uid))
  12869.             /* expand groups */
  12870.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12871.     and     p.protecttype <> 206    /* only grant rows */
  12872.     and     p.action in (26,193,195,197)
  12873.     and     o.uid <> u.uid          /* no rows for owner */
  12874.     and     not exists (            /* exclude revoke'd privileges */
  12875.             select *
  12876.             from sysprotects p1
  12877.             where
  12878.                 p1.protecttype = 206
  12879.             and     p1.action = p.action
  12880.             and     p1.id = p.id
  12881.             and     p1.uid = u.uid
  12882.             and     case 
  12883.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  12884.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12885.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12886.                 end & v.high <> 0)          /* permission applies to this column */
  12887.     union
  12888.     select  /*  Add rows for table owner */
  12889.         GRANTOR     = user_name(u.uid),
  12890.         GRANTEE     = user_name(o.uid),
  12891.         TABLE_CATALOG   = db_name(),
  12892.         TABLE_SCHEMA    = user_name(o.uid),
  12893.         TABLE_NAME  = o.name,
  12894.         COLUMN_NAME = c.name,
  12895.         COLUMN_GUID = convert(binary(16),null),
  12896.         COLUMN_PROPID   = convert(int,null),
  12897.         PRIVILEGE_TYPE  = convert(varchar(30),
  12898.                     case v.number
  12899.                     when 193 then 'SELECT'
  12900.                     when 195 then 'INSERT'
  12901.                     when 197 then 'UPDATE'
  12902.                     else 'REFERENCES'
  12903.                     end),
  12904.         IS_GRANTABLE    = convert(bit,1)    
  12905.     from 
  12906.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12907.     where
  12908.         o.name = @table_name
  12909.     and     o.type in ('U','V','S')
  12910.     and (@table_schema is null or @table_schema = user_name(o.uid))
  12911.     and (@grantee is null or @grantee = user_name(o.uid))
  12912.     and c.id = o.id
  12913.     and     (@column_name is null or @column_name = c.name)
  12914.     and     u.uid = 1       /* grantor is dbo of database */
  12915.     and (@grantor is null or @grantor = user_name(u.uid))
  12916.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12917.     and     v.number in (26,193,195,197)
  12918.     and     not exists (        /* exclude revoke'd privileges */
  12919.             select *
  12920.             from    sysprotects p1
  12921.             where   p1.protecttype = 206
  12922.             and     p1.action = v.number
  12923.             and     p1.id = o.id
  12924.             and     p1.uid = o.uid)
  12925.     order by 4, 5, 6, 9, 1, 2
  12926.     END
  12927. ELSE
  12928.     BEGIN
  12929.     select
  12930.         GRANTOR     = user_name(p.grantor),
  12931.         GRANTEE     = user_name(u.uid),
  12932.         TABLE_CATALOG   = db_name(),
  12933.         TABLE_SCHEMA    = user_name(o.uid),
  12934.         TABLE_NAME  = o.name,
  12935.         COLUMN_NAME = c.name,
  12936.         COLUMN_GUID = convert(binary(16),null),
  12937.         COLUMN_PROPID   = convert(int,null),
  12938.         PRIVILEGE_TYPE  = convert(varchar(30),
  12939.                     case p.action
  12940.                     when 193 then 'SELECT'
  12941.                     when 195 then 'INSERT'
  12942.                     when 197 then 'UPDATE'
  12943.                     else 'REFERENCES'
  12944.                     end),
  12945.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12946.     from 
  12947.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12948.     where
  12949.         o.type in ('U','V','S')
  12950.     and (@table_schema is null or @table_schema = user_name(o.uid))
  12951.     and c.id = o.id
  12952.     and     (@column_name is null or @column_name = c.name)
  12953.     and     c.id = p.id
  12954.     and (@grantor is null or @grantor = user_name(p.grantor))
  12955.     and     case 
  12956.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12957.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12958.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12959.             end & v.high <> 0           /* permission applies to this column */
  12960.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  12961.     and     v.type = 'P'
  12962.     and     v.number = c.colid
  12963.     and (@grantee is null or @grantee = user_name(u.uid))
  12964.             /* expand groups */
  12965.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12966.     and     p.protecttype <> 206    /* only grant rows */
  12967.     and     p.action in (26,193,195,197)
  12968.     and     o.uid <> u.uid          /* no rows for owner */
  12969.     and     not exists (            /* exclude revoke'd privileges */
  12970.             select *
  12971.             from sysprotects p1
  12972.             where
  12973.                 p1.protecttype = 206
  12974.             and     p1.action = p.action
  12975.             and     p1.id = p.id
  12976.             and     p1.uid = u.uid
  12977.             and     case 
  12978.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  12979.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12980.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12981.                 end & v.high <> 0)          /* permission applies to this column */
  12982.     union
  12983.     select  /*  Add rows for table owner */
  12984.         GRANTOR     = user_name(u.uid),
  12985.         GRANTEE     = user_name(o.uid),
  12986.         TABLE_CATALOG   = db_name(),
  12987.         TABLE_SCHEMA    = user_name(o.uid),
  12988.         TABLE_NAME  = o.name,
  12989.         COLUMN_NAME = c.name,
  12990.         COLUMN_GUID = convert(binary(16),null),
  12991.         COLUMN_PROPID   = convert(int,null),
  12992.         PRIVILEGE_TYPE  = convert(varchar(30),
  12993.                     case v.number
  12994.                     when 193 then 'SELECT'
  12995.                     when 195 then 'INSERT'
  12996.                     when 197 then 'UPDATE'
  12997.                     else 'REFERENCES'
  12998.                     end),
  12999.         IS_GRANTABLE    = convert(bit,1)    
  13000.     from 
  13001.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13002.     where
  13003.         o.type in ('U','V','S')
  13004.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13005.     and (@grantee is null or @grantee = user_name(o.uid))
  13006.     and c.id = o.id
  13007.     and     (@column_name is null or @column_name = c.name)
  13008.     and     u.uid = 1       /* grantor is dbo of database */
  13009.     and (@grantor is null or @grantor = user_name(u.uid))
  13010.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13011.     and     v.number in (26,193,195,197)
  13012.     and     not exists (        /* exclude revoke'd privileges */
  13013.             select *
  13014.             from    sysprotects p1
  13015.             where   p1.protecttype = 206
  13016.             and     p1.action = v.number
  13017.             and     p1.id = o.id
  13018.             and     p1.uid = o.uid)
  13019.     order by 4, 5, 6, 9, 1, 2
  13020.     END
  13021. go
  13022. dump tran master with no_log
  13023. go
  13024. CREATE PROCEDURE sp_column_privileges_rowset;2
  13025.     (
  13026.     @handle     int output,
  13027.     @scrollopt  int output,
  13028.     @ccopt      int output,
  13029.     @rows       int output,
  13030.     @table_name     varchar(255) = null,
  13031.     @table_schema   varchar(255) = null,
  13032.     @column_name    varchar(255) = null,
  13033.     @grantor    varchar(255) = null,
  13034.     @grantee    varchar(255) = null
  13035.     )
  13036. as
  13037.  
  13038. declare @ret int
  13039.  
  13040. SET NOCOUNT ON
  13041.     
  13042. create table #spcprivsrowset1
  13043.     (
  13044.     GRANTOR     sysname not null,
  13045.     GRANTEE     sysname not null,
  13046.     TABLE_CATALOG   sysname not null,
  13047.     TABLE_SCHEMA    sysname not null,
  13048.     TABLE_NAME  sysname not null,
  13049.     COLUMN_NAME sysname not null,
  13050.     COLUMN_GUID binary(16) null,
  13051.     COLUMN_PROPID   int null,
  13052.     PRIVILEGE_TYPE  sysname not null,
  13053.     IS_GRANTABLE    bit not null
  13054.     )
  13055.  
  13056. IF @table_name is not null
  13057.     BEGIN
  13058.     insert into #spcprivsrowset1
  13059.     select
  13060.         GRANTOR     = user_name(p.grantor),
  13061.         GRANTEE     = user_name(u.uid),
  13062.         TABLE_CATALOG   = db_name(),
  13063.         TABLE_SCHEMA    = user_name(o.uid),
  13064.         TABLE_NAME  = o.name,
  13065.         COLUMN_NAME = c.name,
  13066.         COLUMN_GUID = convert(binary(16),null),
  13067.         COLUMN_PROPID   = convert(int,null),
  13068.         PRIVILEGE_TYPE  = convert(varchar(30),
  13069.                     case p.action
  13070.                     when 193 then 'SELECT'
  13071.                     when 195 then 'INSERT'
  13072.                     when 197 then 'UPDATE'
  13073.                     else 'REFERENCES'
  13074.                     end),
  13075.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13076.     from 
  13077.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  13078.     where
  13079.         o.name = @table_name
  13080.     and     o.type in ('U','V','S')
  13081.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13082.     and c.id = o.id
  13083.     and     (@column_name is null or @column_name = c.name)
  13084.     and     c.id = p.id
  13085.     and (@grantor is null or @grantor = user_name(p.grantor))
  13086.     and     case 
  13087.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13088.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13089.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13090.             end & v.high <> 0           /* permission applies to this column */
  13091.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13092.     and     v.type = 'P'
  13093.     and     v.number = c.colid
  13094.     and (@grantee is null or @grantee = user_name(u.uid))
  13095.             /* expand groups */
  13096.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13097.     and     p.protecttype <> 206    /* only grant rows */
  13098.     and     p.action in (26,193,195,197)
  13099.     and     o.uid <> u.uid          /* no rows for owner */
  13100.     and     not exists (            /* exclude revoke'd privileges */
  13101.             select *
  13102.             from sysprotects p1
  13103.             where
  13104.                 p1.protecttype = 206
  13105.             and     p1.action = p.action
  13106.             and     p1.id = p.id
  13107.             and     p1.uid = u.uid
  13108.             and     case 
  13109.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13110.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13111.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13112.                 end & v.high <> 0)          /* permission applies to this column */
  13113.     union
  13114.     select  /*  Add rows for table owner */
  13115.         GRANTOR     = user_name(u.uid),
  13116.         GRANTEE     = user_name(o.uid),
  13117.         TABLE_CATALOG   = db_name(),
  13118.         TABLE_SCHEMA    = user_name(o.uid),
  13119.         TABLE_NAME  = o.name,
  13120.         COLUMN_NAME = c.name,
  13121.         COLUMN_GUID = convert(binary(16),null),
  13122.         COLUMN_PROPID   = convert(int,null),
  13123.         PRIVILEGE_TYPE  = convert(varchar(30),
  13124.                     case v.number
  13125.                     when 193 then 'SELECT'
  13126.                     when 195 then 'INSERT'
  13127.                     when 197 then 'UPDATE'
  13128.                     else 'REFERENCES'
  13129.                     end),
  13130.         IS_GRANTABLE    = convert(bit,1)    
  13131.     from 
  13132.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13133.     where
  13134.         o.name = @table_name
  13135.     and     o.type in ('U','V','S')
  13136.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13137.     and (@grantee is null or @grantee = user_name(o.uid))
  13138.     and c.id = o.id
  13139.     and     (@column_name is null or @column_name = c.name)
  13140.     and     u.uid = 1       /* grantor is dbo of database */
  13141.     and (@grantor is null or @grantor = user_name(u.uid))
  13142.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13143.     and     v.number in (26,193,195,197)
  13144.     and     not exists (        /* exclude revoke'd privileges */
  13145.             select *
  13146.             from    sysprotects p1
  13147.             where   p1.protecttype = 206
  13148.             and     p1.action = v.number
  13149.             and     p1.id = o.id
  13150.             and     p1.uid = o.uid)
  13151.     order by 4, 5, 6, 9, 1, 2
  13152.     END
  13153. ELSE
  13154.     BEGIN
  13155.     insert into #spcprivsrowset1
  13156.     select
  13157.         GRANTOR     = user_name(p.grantor),
  13158.         GRANTEE     = user_name(u.uid),
  13159.         TABLE_CATALOG   = db_name(),
  13160.         TABLE_SCHEMA    = user_name(o.uid),
  13161.         TABLE_NAME  = o.name,
  13162.         COLUMN_NAME = c.name,
  13163.         COLUMN_GUID = convert(binary(16),null),
  13164.         COLUMN_PROPID   = convert(int,null),
  13165.         PRIVILEGE_TYPE  = convert(varchar(30),
  13166.                     case p.action
  13167.                     when 193 then 'SELECT'
  13168.                     when 195 then 'INSERT'
  13169.                     when 197 then 'UPDATE'
  13170.                     else 'REFERENCES'
  13171.                     end),
  13172.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13173.     from 
  13174.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  13175.     where
  13176.         o.type in ('U','V','S')
  13177.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13178.     and c.id = o.id
  13179.     and     (@column_name is null or @column_name = c.name)
  13180.     and     c.id = p.id
  13181.     and (@grantor is null or @grantor = user_name(p.grantor))
  13182.     and     case 
  13183.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13184.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13185.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13186.             end & v.high <> 0           /* permission applies to this column */
  13187.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13188.     and     v.type = 'P'
  13189.     and     v.number = c.colid
  13190.     and (@grantee is null or @grantee = user_name(u.uid))
  13191.             /* expand groups */
  13192.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13193.     and     p.protecttype <> 206    /* only grant rows */
  13194.     and     p.action in (26,193,195,197)
  13195.     and     o.uid <> u.uid          /* no rows for owner */
  13196.     and     not exists (            /* exclude revoke'd privileges */
  13197.             select *
  13198.             from sysprotects p1
  13199.             where
  13200.                 p1.protecttype = 206
  13201.             and     p1.action = p.action
  13202.             and     p1.id = p.id
  13203.             and     p1.uid = u.uid
  13204.             and     case 
  13205.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13206.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13207.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13208.                 end & v.high <> 0)          /* permission applies to this column */
  13209.     union
  13210.     select  /*  Add rows for table owner */
  13211.         GRANTOR     = user_name(u.uid),
  13212.         GRANTEE     = user_name(o.uid),
  13213.         TABLE_CATALOG   = db_name(),
  13214.         TABLE_SCHEMA    = user_name(o.uid),
  13215.         TABLE_NAME  = o.name,
  13216.         COLUMN_NAME = c.name,
  13217.         COLUMN_GUID = convert(binary(16),null),
  13218.         COLUMN_PROPID   = convert(int,null),
  13219.         PRIVILEGE_TYPE  = convert(varchar(30),
  13220.                     case v.number
  13221.                     when 193 then 'SELECT'
  13222.                     when 195 then 'INSERT'
  13223.                     when 197 then 'UPDATE'
  13224.                     else 'REFERENCES'
  13225.                     end),
  13226.         IS_GRANTABLE    = convert(bit,1)    
  13227.     from 
  13228.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13229.     where
  13230.         o.type in ('U','V','S')
  13231.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13232.     and (@grantee is null or @grantee = user_name(o.uid))
  13233.     and c.id = o.id
  13234.     and     (@column_name is null or @column_name = c.name)
  13235.     and     u.uid = 1       /* grantor is dbo of database */
  13236.     and (@grantor is null or @grantor = user_name(u.uid))
  13237.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13238.     and     v.number in (26,193,195,197)
  13239.     and     not exists (        /* exclude revoke'd privileges */
  13240.             select *
  13241.             from    sysprotects p1
  13242.             where   p1.protecttype = 206
  13243.             and     p1.action = v.number
  13244.             and     p1.id = o.id
  13245.             and     p1.uid = o.uid)
  13246.     order by 4, 5, 6, 9, 1, 2
  13247.     END
  13248.  
  13249. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  13250.     @scrollopt output, @ccopt output, @rows output
  13251.  
  13252. drop table #spcprivsrowset1
  13253.  
  13254. return isnull(@ret,0)
  13255. go
  13256. dump tran master with no_log
  13257. go
  13258. CREATE PROCEDURE sp_column_privileges_rowset;3
  13259. as
  13260.     select
  13261.         GRANTOR     = convert(sysname,' '),
  13262.         GRANTEE     = convert(sysname,' '),
  13263.         TABLE_CATALOG   = convert(sysname,' '),
  13264.         TABLE_SCHEMA    = convert(sysname,' '),
  13265.         TABLE_NAME  = convert(sysname,' '),
  13266.         COLUMN_NAME = convert(sysname,' '),
  13267.         COLUMN_GUID = convert(binary(16),null),
  13268.         COLUMN_PROPID   = convert(int,null),
  13269.         PRIVILEGE_TYPE  = convert(varchar(30),' '),
  13270.         IS_GRANTABLE    = convert(bit,1)
  13271.     where   1=0
  13272. go
  13273. dump tran master with no_log
  13274. go
  13275.  
  13276. if (charindex('7.00', @@version) = 0 and
  13277.     charindex('8.00', @@version) = 0)
  13278. begin
  13279.     print ''
  13280.     print ''
  13281.     print 'Warning:'
  13282.     print 'you are installing the stored procedures '
  13283.     print 'on a pre 8.0 SQL Server.'
  13284.     print 'Ignore the following errors.'
  13285. end
  13286. else
  13287.     drop proc sp_column_privileges_rowset
  13288. go
  13289.  
  13290.  
  13291. /*  Procedure for 8.0 server */
  13292. CREATE PROCEDURE sp_column_privileges_rowset
  13293.     (
  13294.     @table_name         sysname,
  13295.     @table_schema       sysname = null,
  13296.     @column_name        sysname = null,
  13297.     @grantor        sysname = null,
  13298.     @grantee        sysname = null
  13299.     )
  13300. as
  13301.     select
  13302.         GRANTOR     = user_name(p.grantor),
  13303.         GRANTEE     = user_name(u.uid),
  13304.         TABLE_CATALOG   = db_name(),
  13305.         TABLE_SCHEMA    = user_name(o.uid),
  13306.         TABLE_NAME  = o.name,
  13307.         COLUMN_NAME = c.name,
  13308.         COLUMN_GUID = convert(uniqueidentifier,null),
  13309.         COLUMN_PROPID   = convert(int,null),
  13310.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13311.                     case p.action
  13312.                     when 193 then N'SELECT'
  13313.                     when 195 then N'INSERT'
  13314.                     when 197 then N'UPDATE'
  13315.                     else N'REFERENCES'
  13316.                     end),
  13317.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13318.     from 
  13319.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13320.     where
  13321.         o.name = @table_name
  13322.     and     o.type in ('U','V','S')
  13323.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13324.     and c.id = o.id
  13325.     and     (@column_name is null or @column_name = c.name)
  13326.     and     c.id = p.id
  13327.     and (@grantor is null or @grantor = user_name(p.grantor))
  13328.     and     case 
  13329.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13330.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13331.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13332.             end & v.high <> 0           /* permission applies to this column */
  13333.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13334.     and     v.type = 'P'
  13335.     and     v.number = c.colid
  13336.     and (@grantee is null or @grantee = user_name(u.uid))
  13337.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13338.     and     (u.uid > 0 and u.uid < 16384)
  13339.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))  
  13340.     and     p.protecttype <> 206    /* only grant rows */
  13341.     and     p.action in (26,193,195,197)
  13342.     and     o.uid <> u.uid          /* no rows for owner */
  13343.     and     not exists (            /* exclude revoke'd privileges */
  13344.             select *
  13345.             from sysprotects p1
  13346.             where
  13347.                 p1.protecttype = 206
  13348.             and     p1.action = p.action
  13349.             and     p1.id = p.id
  13350.             and     p1.uid = u.uid
  13351.             and     case 
  13352.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13353.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13354.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13355.                 end & v.high <> 0)          /* permission applies to this column */
  13356.     union
  13357.     select  /*  Add rows for table owner */
  13358.         GRANTOR     = user_name(u.uid),
  13359.         GRANTEE     = user_name(o.uid),
  13360.         TABLE_CATALOG   = db_name(),
  13361.         TABLE_SCHEMA    = user_name(o.uid),
  13362.         TABLE_NAME  = o.name,
  13363.         COLUMN_NAME = c.name,
  13364.         COLUMN_GUID = convert(uniqueidentifier,null),
  13365.         COLUMN_PROPID   = convert(int,null),
  13366.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13367.                     case v.number
  13368.                     when 193 then N'SELECT'
  13369.                     when 195 then N'INSERT'
  13370.                     when 197 then N'UPDATE'
  13371.                     else N'REFERENCES'
  13372.                     end),
  13373.         IS_GRANTABLE    = convert(bit,1)    
  13374.     from 
  13375.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13376.     where
  13377.         o.name = @table_name
  13378.     and     o.type in ('U','V','S')
  13379.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13380.     and (@grantee is null or @grantee = user_name(o.uid))
  13381.     and c.id = o.id
  13382.     and     (@column_name is null or @column_name = c.name)
  13383.     and     u.uid = 1       /* grantor is dbo of database */
  13384.     and (@grantor is null or @grantor = user_name(u.uid))
  13385.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13386.     and     v.number in (26,193,195,197)
  13387.     and     not exists (        /* exclude revoke'd privileges */
  13388.             select *
  13389.             from    sysprotects p1
  13390.             where   p1.protecttype = 206
  13391.             and     p1.action = v.number
  13392.             and     p1.id = o.id
  13393.             and     p1.uid = o.uid)
  13394.     order by 4, 5, 6, 9, 1, 2
  13395. go
  13396. dump tran master with no_log
  13397. go
  13398. CREATE PROCEDURE sp_column_privileges_rowset;2
  13399.     (
  13400.     @table_schema       sysname = null,
  13401.     @column_name        sysname = null,
  13402.     @grantor        sysname = null,
  13403.     @grantee        sysname = null
  13404.     )
  13405. as
  13406.     select
  13407.         GRANTOR     = user_name(p.grantor),
  13408.         GRANTEE     = user_name(u.uid),
  13409.         TABLE_CATALOG   = db_name(),
  13410.         TABLE_SCHEMA    = user_name(o.uid),
  13411.         TABLE_NAME  = o.name,
  13412.         COLUMN_NAME = c.name,
  13413.         COLUMN_GUID = convert(uniqueidentifier,null),
  13414.         COLUMN_PROPID   = convert(int,null),
  13415.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13416.                     case p.action
  13417.                     when 193 then N'SELECT'
  13418.                     when 195 then N'INSERT'
  13419.                     when 197 then N'UPDATE'
  13420.                     else N'REFERENCES'
  13421.                     end),
  13422.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13423.     from 
  13424.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13425.     where
  13426.         o.type in ('U','V','S')
  13427.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13428.     and c.id = o.id
  13429.     and     (@column_name is null or @column_name = c.name)
  13430.     and     c.id = p.id
  13431.     and (@grantor is null or @grantor = user_name(p.grantor))
  13432.     and     case 
  13433.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13434.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13435.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13436.             end & v.high <> 0           /* permission applies to this column */
  13437.     and     v.number <= (select count(*) from syscolumns where id = o.id)   /* ranges from 1 to # of columns in table */
  13438.     and     v.type = 'P'
  13439.     and     v.number = c.colid
  13440.     and (@grantee is null or @grantee = user_name(u.uid))
  13441.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13442.     and     (u.uid > 0 and u.uid < 16384)
  13443.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))  
  13444.     and     p.protecttype <> 206    /* only grant rows */
  13445.     and     p.action in (26,193,195,197)
  13446.     and     o.uid <> u.uid          /* no rows for owner */
  13447.     and     not exists (            /* exclude revoke'd privileges */
  13448.             select *
  13449.             from sysprotects p1
  13450.             where
  13451.                 p1.protecttype = 206
  13452.             and     p1.action = p.action
  13453.             and     p1.id = p.id
  13454.             and     p1.uid = u.uid
  13455.             and     case 
  13456.                 when substring(p1.columns, 1, 1) & 1 is null then 255   /* all columns have permission */
  13457.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13458.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13459.                 end & v.high <> 0)          /* permission applies to this column */
  13460.     union
  13461.     select  /*  Add rows for table owner */
  13462.         GRANTOR     = user_name(u.uid),
  13463.         GRANTEE     = user_name(o.uid),
  13464.         TABLE_CATALOG   = db_name(),
  13465.         TABLE_SCHEMA    = user_name(o.uid),
  13466.         TABLE_NAME  = o.name,
  13467.         COLUMN_NAME = c.name,
  13468.         COLUMN_GUID = convert(uniqueidentifier,null),
  13469.         COLUMN_PROPID   = convert(int,null),
  13470.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  13471.                     case v.number
  13472.                     when 193 then N'SELECT'
  13473.                     when 195 then N'INSERT'
  13474.                     when 197 then N'UPDATE'
  13475.                     else N'REFERENCES'
  13476.                     end),
  13477.         IS_GRANTABLE    = convert(bit,1)    
  13478.     from 
  13479.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13480.     where
  13481.         o.type in ('U','V','S')
  13482.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13483.     and (@grantee is null or @grantee = user_name(o.uid))
  13484.     and c.id = o.id
  13485.     and     (@column_name is null or @column_name = c.name)
  13486.     and     u.uid = 1       /* grantor is dbo of database */
  13487.     and (@grantor is null or @grantor = user_name(u.uid))
  13488.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13489.     and     v.number in (26,193,195,197)
  13490.     and     not exists (        /* exclude revoke'd privileges */
  13491.             select *
  13492.             from    sysprotects p1
  13493.             where   p1.protecttype = 206
  13494.             and     p1.action = v.number
  13495.             and     p1.id = o.id
  13496.             and     p1.uid = o.uid)
  13497.     order by 4, 5, 6, 9, 1, 2
  13498. go
  13499. dump tran master with no_log
  13500. go
  13501. create procedure sp_column_privileges_rowset;5
  13502.     (
  13503.     @table_server       sysname,
  13504.     @table_catalog      sysname = null,
  13505.     @table_name     sysname = null,
  13506.     @table_schema       sysname = null,
  13507.     @column_name        sysname = null,
  13508.     @grantor        sysname = null,
  13509.     @grantee        sysname = null
  13510.     )
  13511. as
  13512.     select
  13513.         GRANTOR,
  13514.         GRANTEE,
  13515.         TABLE_CATALOG,
  13516.         TABLE_SCHEMA,
  13517.         TABLE_NAME,
  13518.         COLUMN_NAME,
  13519.         COLUMN_GUID,
  13520.         COLUMN_PROPID,  
  13521.         PRIVILEGE_TYPE,
  13522.         IS_GRANTABLE    
  13523.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES (
  13524.                 @table_server,
  13525.                 @table_catalog,
  13526.                 @table_schema,
  13527.                 @table_name,
  13528.                 @column_name,
  13529.                 @grantor,
  13530.                 @grantee )
  13531.     order by 3, 4, 5, 6, 9, 1, 2
  13532. go
  13533.  
  13534. grant execute on sp_column_privileges_rowset to public
  13535. go
  13536.  
  13537. dump tran master with no_log
  13538. go
  13539. if (charindex('6.00', @@version) > 0)
  13540.     begin
  13541.     if (exists (select * from sysobjects
  13542.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  13543.         begin
  13544.         drop procedure sp_column_privileges_rowset
  13545.         dump tran master with no_log
  13546.         end
  13547.     end
  13548. go
  13549.  
  13550.  
  13551. print ''
  13552. print 'creating sp_columns_rowset'
  13553. go
  13554.  
  13555. /*  Procedure for 6.0 and 6.50 server */
  13556. create procedure sp_columns_rowset
  13557.     (
  13558.     @table_name varchar(255),
  13559.     @table_schema   varchar(255) = null,
  13560.     @column_name    varchar(255) = null
  13561.     )
  13562. as  
  13563.     select
  13564.         TABLE_CATALOG       = db_name(),
  13565.         TABLE_SCHEMA        = user_name(o.uid),
  13566.         TABLE_NAME      = o.name,
  13567.         COLUMN_NAME     = c.name,
  13568.         COLUMN_GUID     = convert(binary(16),null),
  13569.         COLUMN_PROPID       = convert(int,null),
  13570.         ORDINAL_POSITION    = convert(int,
  13571.                        (
  13572.                         select count(*)
  13573.                         from syscolumns sc
  13574.                         where sc.id     =  c.id
  13575.                           AND sc.number =  c.number
  13576.                           AND sc.colid  <= c.colid
  13577.                         )),
  13578.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  13579.  
  13580.         COLUMN_DEFAULT      = convert (varchar (255),
  13581.                                   case when substring(m.text,1,1) = '('
  13582.                                   then substring(m.text,2,datalength(m.text)-2)
  13583.                                   else substring(m.text,1,datalength(m.text)-2)
  13584.                               end), 
  13585.         COLUMN_FLAGS        = convert(int,
  13586.                         case when d.is_long = 1 
  13587.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13588.                         else 0
  13589.                         end
  13590.                     |   case when d.fixlen is not null
  13591.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13592.                         else 0
  13593.                         end
  13594.                     |   case when c.status&8 = 8
  13595.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13596.                         else 0
  13597.                         end
  13598.                     |   case 
  13599.                         when d.type_name = 'timestamp'
  13600.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13601.                         when (c.status&128) != 128
  13602.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13603.                         else 0 
  13604.                         end),
  13605.         IS_NULLABLE     = convert(bit,c.status&8),
  13606.         DATA_TYPE       = d.oledb_data_type,
  13607.         TYPE_GUID       = convert(binary(16),null),
  13608.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13609.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13610.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13611.                         then coalesce(d.column_size,c.length)
  13612.                         else null 
  13613.                         end),
  13614.         CHARACTER_OCTET_LENGTH  = convert(int,
  13615.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13616.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13617.                         then coalesce(d.column_size,c.length)
  13618.                         else null 
  13619.                         end),
  13620.         NUMERIC_PRECISION   = convert(smallint,
  13621.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13622.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13623.                                     then d.data_precision else null end),
  13624.         NUMERIC_SCALE       = convert(smallint, 
  13625.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13626.         DATETIME_PRECISION  = convert(int,
  13627.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13628.                             when data_precision = 23 then 3 else 0 end),
  13629.         CHARACTER_SET_CATALOG   = convert(sysname,
  13630.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13631.                         then 'master' 
  13632.                         else null 
  13633.                         end),
  13634.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13635.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13636.                         then 'dbo' 
  13637.                         else null 
  13638.                         end),
  13639.         CHARACTER_SET_NAME  = convert(sysname,
  13640.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13641.                         then a_cha.name 
  13642.                         else null 
  13643.                         end),
  13644.         COLLATION_CATALOG   = convert(sysname,
  13645.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13646.                         then 'master' 
  13647.                         else null 
  13648.                         end),
  13649.         COLLATION_SCHEMA    = convert(sysname,
  13650.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13651.                         then 'dbo' 
  13652.                         else null 
  13653.                         end),
  13654.         COLLATION_NAME      = convert(sysname,
  13655.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13656.                         then b_cha.name 
  13657.                         else null 
  13658.                         end),
  13659.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  13660.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  13661.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  13662.         DESCRIPTION     = convert(varchar(1),null)  
  13663.     
  13664.     from
  13665.         syscolumns c,
  13666.         syscomments m,
  13667.         sysobjects o,
  13668.         master.dbo.spt_provider_types d,
  13669.         systypes t,
  13670.         sysusers u,
  13671.         master.dbo.sysconfigures    cfg,
  13672.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder. */
  13673.         master.dbo.syscharsets      b_cha  /* sortorder/2001, not charset. */
  13674.     where
  13675.         o.name = @table_name
  13676.     and     o.type in ('U','V','S')
  13677.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13678.     and     (
  13679.         suser_id() = 1   /* User is the System Administrator */
  13680.         or o.uid = user_id()     /* User created the object */
  13681.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13682.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13683.             from sysprotects p
  13684.             where p.id = o.id
  13685.             /* get rows for public,current user,user's group */
  13686.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13687.             /* check for SELECT,EXECUTE privilege */
  13688.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13689.             ) = 1   /* final magic...compare Grants   */
  13690.            )
  13691.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13692.     and (@column_name is null or @column_name = c.name)
  13693.     and     o.id = c.id
  13694.     and     t.type = d.ss_dtype
  13695.     and (t.usertype != 80 or d.type_name='timestamp')
  13696.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13697.     and     c.usertype = t.usertype
  13698.     and c.cdefault *= m.id
  13699.     and m.colid = 1
  13700.     and cfg.comment = 'default sortorder ID' 
  13701.     and a_cha.type = 1001 /* type is charset */
  13702.     and b_cha.type = 2001 /* type is sortorder */
  13703.     and a_cha.id = b_cha.csid
  13704.     and     b_cha.id = cfg.value
  13705.     order by 2, 3, c.colid
  13706. go
  13707.  
  13708. dump tran master with no_log
  13709. go
  13710.  
  13711. create procedure sp_columns_rowset;2
  13712.     (
  13713.     @table_schema   varchar(255) = null,
  13714.     @column_name    varchar(255) = null
  13715.     )
  13716. as  
  13717.     select
  13718.         TABLE_CATALOG       = db_name(),
  13719.         TABLE_SCHEMA        = user_name(o.uid),
  13720.         TABLE_NAME      = o.name,
  13721.         COLUMN_NAME     = c.name,
  13722.         COLUMN_GUID     = convert(binary(16),null),
  13723.         COLUMN_PROPID       = convert(int,null),
  13724.         ORDINAL_POSITION    = convert(int,
  13725.                        (
  13726.                         select count(*)
  13727.                         from syscolumns sc
  13728.                         where sc.id     =  c.id
  13729.                           AND sc.number =  c.number
  13730.                           AND sc.colid  <= c.colid
  13731.                         )),
  13732.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  13733.         COLUMN_DEFAULT      = convert (varchar (255),
  13734.                                   case when substring(m.text,1,1) = '('
  13735.                                   then substring(m.text,2,datalength(m.text)-2)
  13736.                                   else substring(m.text,1,datalength(m.text)-2)
  13737.                               end), 
  13738.         COLUMN_FLAGS        = convert(int,
  13739.                         case when d.is_long = 1 
  13740.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13741.                         else 0
  13742.                         end
  13743.                     |   case when d.fixlen is not null
  13744.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13745.                         else 0
  13746.                         end
  13747.                     |   case when c.status&8 = 8
  13748.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13749.                         else 0
  13750.                         end
  13751.                     |   case 
  13752.                         when d.type_name = 'timestamp'
  13753.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13754.                         when (c.status&128) != 128
  13755.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13756.                         else 0 
  13757.                         end),
  13758.         IS_NULLABLE     = convert(bit,c.status&8),
  13759.         DATA_TYPE       = d.oledb_data_type,
  13760.         TYPE_GUID       = convert(binary(16),null),
  13761.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13762.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13763.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13764.                         then coalesce(d.column_size,c.length)
  13765.                         else null 
  13766.                         end),
  13767.         CHARACTER_OCTET_LENGTH  = convert(int,
  13768.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13769.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13770.                         then coalesce(d.column_size,c.length)
  13771.                         else null 
  13772.                         end),
  13773.         NUMERIC_PRECISION   = convert(smallint,
  13774.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13775.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13776.                             then d.data_precision else null end),
  13777.         NUMERIC_SCALE       = convert(smallint, 
  13778.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13779.         DATETIME_PRECISION  = convert(int,
  13780.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13781.                             when data_precision = 23 then 3 else 0 end),
  13782.         CHARACTER_SET_CATALOG   = convert(sysname,
  13783.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13784.                         then 'master' 
  13785.                         else null 
  13786.                         end),
  13787.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13788.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13789.                         then 'dbo' 
  13790.                         else null 
  13791.                         end),
  13792.         CHARACTER_SET_NAME  = convert(sysname,
  13793.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13794.                         then a_cha.name 
  13795.                         else null 
  13796.                         end),
  13797.         COLLATION_CATALOG   = convert(sysname,
  13798.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13799.                         then 'master' 
  13800.                         else null 
  13801.                         end),
  13802.         COLLATION_SCHEMA    = convert(sysname,
  13803.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13804.                         then 'dbo' 
  13805.                         else null 
  13806.                         end),
  13807.         COLLATION_NAME      = convert(sysname,
  13808.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13809.                         then b_cha.name 
  13810.                         else null 
  13811.                         end),
  13812.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  13813.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  13814.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  13815.         DESCRIPTION     = convert(varchar(1),null)  
  13816.     
  13817.     from
  13818.         syscolumns c,
  13819.         syscomments m,
  13820.         sysobjects o,
  13821.         master.dbo.spt_provider_types d,
  13822.         systypes t,
  13823.         sysusers u,
  13824.         master.dbo.sysconfigures    cfg,
  13825.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder. */
  13826.         master.dbo.syscharsets      b_cha /* sortorder/2001, not charset. */
  13827.     where
  13828.         o.type in ('U','V','S')
  13829.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13830.     and     (
  13831.         suser_id() = 1   /* User is the System Administrator */
  13832.         or o.uid = user_id()     /* User created the object */
  13833.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13834.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13835.             from sysprotects p
  13836.             where p.id = o.id
  13837.             /* get rows for public,current user,user's group */
  13838.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13839.             /* check for SELECT,EXECUTE privilege */
  13840.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13841.             ) = 1   /* final magic...compare Grants   */
  13842.            )
  13843.     and (@table_schema is null or @table_schema = user_name(o.uid))
  13844.     and (@column_name is null or @column_name = c.name)
  13845.     and     o.id = c.id
  13846.     and     t.type = d.ss_dtype
  13847.     and (t.usertype != 80 or d.type_name='timestamp')
  13848.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13849.     and     c.usertype = t.usertype
  13850.     and c.cdefault *= m.id
  13851.     and  m.colid = 1
  13852.     and cfg.comment = 'default sortorder ID' 
  13853.     and a_cha.type = 1001 /* type is charset */
  13854.     and b_cha.type = 2001 /* type is sortorder */
  13855.     and a_cha.id = b_cha.csid
  13856.     and     b_cha.id = cfg.value
  13857.     order by 2, 3, c.colid
  13858.     
  13859. go
  13860.  
  13861. dump tran master with no_log
  13862. go
  13863.  
  13864. if (charindex('7.00', @@version) = 0 and
  13865.     charindex('8.00', @@version) = 0)
  13866. begin
  13867.     print ''
  13868.     print ''
  13869.     print 'Warning:'
  13870.     print 'you are installing the stored procedures '
  13871.     print 'on a pre 8.0 SQL Server.'
  13872.     print 'Ignore the following errors.'
  13873. end
  13874. else
  13875.     drop proc sp_columns_rowset
  13876. go
  13877.  
  13878. /*  Procedure for 7.0 server */
  13879. create procedure sp_columns_rowset
  13880.     (
  13881.     @table_name sysname,
  13882.     @table_schema   sysname = NULL,
  13883.     @column_name    sysname = NULL
  13884.     )
  13885. as
  13886.     select
  13887.         TABLE_CATALOG       = db_name(),
  13888.         TABLE_SCHEMA        = user_name(o.uid),
  13889.         TABLE_NAME      = o.name,
  13890.         COLUMN_NAME     = c.name,
  13891.         COLUMN_GUID     = convert(uniqueidentifier,null),
  13892.         COLUMN_PROPID       = convert(int,null),
  13893.         ORDINAL_POSITION    = convert(int,
  13894.                        (
  13895.                         select count(*)
  13896.                         from syscolumns sc
  13897.                         where sc.id     =  c.id
  13898.                           AND sc.number =  c.number
  13899.                           AND sc.colid  <= c.colid
  13900.                         )),
  13901.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  13902.         COLUMN_DEFAULT      =  convert(nvarchar(2000),
  13903.                                         case when substring(m.text,1,1) = '('
  13904.                                             then substring(m.text,2,datalength(m.text)/2-2)
  13905.                                             else substring(m.text,1,datalength(m.text)/2-2)
  13906.                                         end ), 
  13907.         COLUMN_FLAGS        = convert(int,
  13908.                         case when d.is_long = 1 
  13909.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13910.                         else 0
  13911.                         end
  13912.                     |   case when d.fixlen is not null
  13913.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13914.                         else 0
  13915.                         end
  13916.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  13917.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13918.                         else 0
  13919.                         end
  13920.                     |   case 
  13921.                         when d.type_name = 'timestamp'
  13922.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13923.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  13924.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  13925.                         else 0 
  13926.                         end),
  13927.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  13928.         DATA_TYPE       = d.oledb_data_type,
  13929.         TYPE_GUID       = convert(uniqueidentifier,null),
  13930.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13931.                         case 
  13932.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13933.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13934.                         then coalesce(d.column_size,c.length)
  13935.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13936.                         then coalesce(d.column_size,c.length/2)
  13937.                         else null 
  13938.                         end),
  13939.         CHARACTER_OCTET_LENGTH  = convert(int,
  13940.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13941.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13942.                         then coalesce(d.column_size,c.length)
  13943.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13944.                         then coalesce(d.column_size*2,c.length)
  13945.                         else null 
  13946.                         end),
  13947.         NUMERIC_PRECISION   = convert(smallint,
  13948.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  13949.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13950.                             then d.data_precision else null end),
  13951.         NUMERIC_SCALE       = convert(smallint, 
  13952.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13953.         DATETIME_PRECISION  = convert(int,
  13954.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13955.                             when data_precision = 23 then 3 else 0 end),
  13956.         CHARACTER_SET_CATALOG   = convert(sysname,
  13957.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13958.                         then N'master' 
  13959.                         else null 
  13960.                         end),
  13961.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13962.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13963.                         then N'dbo' 
  13964.                         else null 
  13965.                         end),
  13966.         CHARACTER_SET_NAME  = convert(sysname,
  13967.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13968.                         then a_cha.name 
  13969.                         else null 
  13970.                         end),
  13971.         COLLATION_CATALOG   = convert(sysname,
  13972.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13973.                         then N'master' 
  13974.                         else null 
  13975.                         end),
  13976.         COLLATION_SCHEMA    = convert(sysname,
  13977.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13978.                         then N'dbo' 
  13979.                         else null 
  13980.                         end),
  13981.         COLLATION_NAME      = convert(sysname,
  13982.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13983.                         then b_cha.name 
  13984.                         else null 
  13985.                         end),
  13986.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  13987.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  13988.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  13989.         DESCRIPTION     = convert(nvarchar(1),null) 
  13990.  
  13991.     from
  13992.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  13993.         sysobjects o,
  13994.         master.dbo.spt_provider_types d,
  13995.         systypes t,
  13996.         master.dbo.sysconfigures    cfg,
  13997.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder. */
  13998.         master.dbo.syscharsets      b_cha /* sortorder/2001, not charset. */
  13999.     where
  14000.         permissions(o.id, c.name) <> 0
  14001.     and o.name = @table_name
  14002.     and     o.type in ('U','V','S')
  14003.     and (@table_schema is null or @table_schema = user_name(o.uid))
  14004.     and (@column_name is null or @column_name = c.name)
  14005.     and     o.id = c.id
  14006.     and     t.xtype = d.ss_dtype
  14007.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14008.     and     c.xusertype = t.xusertype
  14009.     and cfg.comment = 'default sortorder id' 
  14010.     and a_cha.type = 1001 /* type is charset */
  14011.     and b_cha.type = 2001 /* type is sortorder */
  14012.     and a_cha.id = b_cha.csid
  14013.     and     b_cha.id = cfg.value
  14014.     order by 2, 3, c.colorder
  14015. go
  14016.  
  14017. dump tran master with no_log
  14018. go
  14019.  
  14020. /*  Procedure for 7.0 server */
  14021. create procedure sp_columns_rowset;2
  14022.     (
  14023.     @table_schema   sysname = NULL,
  14024.     @column_name    sysname = NULL
  14025.     )
  14026. as  
  14027.     select
  14028.         TABLE_CATALOG       = db_name(),
  14029.         TABLE_SCHEMA        = user_name(o.uid),
  14030.         TABLE_NAME      = o.name,
  14031.         COLUMN_NAME     = c.name,
  14032.         COLUMN_GUID     = convert(uniqueidentifier,null),
  14033.         COLUMN_PROPID       = convert(int,null),
  14034.         ORDINAL_POSITION    = convert(int,
  14035.                        (
  14036.                         select count(*)
  14037.                         from syscolumns sc
  14038.                         where sc.id     =  c.id
  14039.                           AND sc.number =  c.number
  14040.                           AND sc.colid  <= c.colid
  14041.                         )),
  14042.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  14043.         COLUMN_DEFAULT      =  convert(nvarchar(2000),
  14044.                                         case when substring(m.text,1,1) = '('
  14045.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14046.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14047.                                         end ), 
  14048.         COLUMN_FLAGS        = convert(int,
  14049.                         case when d.is_long = 1 
  14050.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14051.                         else 0
  14052.                         end
  14053.                     |   case when d.fixlen is not null
  14054.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14055.                         else 0
  14056.                         end
  14057.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14058.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14059.                         else 0
  14060.                         end
  14061.                     |   case 
  14062.                         when d.type_name = 'timestamp'
  14063.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14064.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14065.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14066.                         else 0 
  14067.                         end),
  14068.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14069.         DATA_TYPE       = d.oledb_data_type,
  14070.         TYPE_GUID       = convert(uniqueidentifier,null),
  14071.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14072.                         case 
  14073.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14074.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14075.                         then coalesce(d.column_size,c.length)
  14076.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14077.                         then coalesce(d.column_size,c.length/2)
  14078.                         else null 
  14079.                         end),
  14080.         CHARACTER_OCTET_LENGTH  = convert(int,
  14081.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14082.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14083.                         then coalesce(d.column_size,c.length)
  14084.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14085.                         then coalesce(d.column_size*2,c.length)
  14086.                         else null 
  14087.                         end),
  14088.         NUMERIC_PRECISION   = convert(smallint,
  14089.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14090.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14091.                             then d.data_precision else null end),
  14092.         NUMERIC_SCALE       = convert(smallint, 
  14093.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14094.         DATETIME_PRECISION  = convert(int,
  14095.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14096.                             when data_precision = 23 then 3 else 0 end),
  14097.         CHARACTER_SET_CATALOG   = convert(sysname,
  14098.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14099.                         then N'master' 
  14100.                         else null 
  14101.                         end),
  14102.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14103.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14104.                         then N'dbo' 
  14105.                         else null 
  14106.                         end),
  14107.         CHARACTER_SET_NAME  = convert(sysname,
  14108.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14109.                         then a_cha.name 
  14110.                         else null 
  14111.                         end),
  14112.         COLLATION_CATALOG   = convert(sysname,
  14113.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14114.                         then N'master' 
  14115.                         else null 
  14116.                         end),
  14117.         COLLATION_SCHEMA    = convert(sysname,
  14118.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14119.                         then N'dbo' 
  14120.                         else null 
  14121.                         end),
  14122.         COLLATION_NAME      = convert(sysname,
  14123.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14124.                         then b_cha.name 
  14125.                         else null 
  14126.                         end),
  14127.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  14128.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  14129.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  14130.         DESCRIPTION     = convert(nvarchar(1),null) 
  14131.     
  14132.     from
  14133.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14134.         sysobjects o,
  14135.         master.dbo.spt_provider_types d,
  14136.         systypes t,
  14137.         master.dbo.sysconfigures    cfg,
  14138.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder.*/
  14139.         master.dbo.syscharsets      b_cha /* sortorder/2001, not charset.*/
  14140.     where
  14141.         permissions(o.id, c.name) <> 0
  14142.     and     o.type in ('U','V','S')
  14143.     and (@table_schema is null or @table_schema = user_name(o.uid))
  14144.     and (@column_name is null or @column_name = c.name)
  14145.     and     o.id = c.id
  14146.     and     t.xtype = d.ss_dtype
  14147.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14148.     and     c.xusertype = t.xusertype
  14149.     and cfg.comment = 'default sortorder id' 
  14150.     and     a_cha.type = 1001 /* type is charset */
  14151.     and     b_cha.type = 2001 /* type is sortorder */
  14152.     and a_cha.id = b_cha.csid
  14153.     and     b_cha.id = cfg.value
  14154.     order by 2, 3, c.colorder
  14155. go
  14156.  
  14157. dump tran master with no_log
  14158. go
  14159.  
  14160. /*  Procedure for 7.0 server */
  14161. create procedure sp_columns_rowset;5
  14162.     (
  14163.     @table_server       sysname,
  14164.     @table_catalog      sysname = null,
  14165.     @table_name     sysname = null,
  14166.     @table_schema       sysname = null,
  14167.     @column_name        sysname = null
  14168.     )
  14169. as
  14170.     select
  14171.         TABLE_CATALOG,
  14172.         TABLE_SCHEMA,
  14173.         TABLE_NAME,
  14174.         COLUMN_NAME,
  14175.         COLUMN_GUID,
  14176.         COLUMN_PROPID,
  14177.         ORDINAL_POSITION,
  14178.         COLUMN_HASDEFAULT,
  14179.         COLUMN_DEFAULT,
  14180.         COLUMN_FLAGS,
  14181.         IS_NULLABLE,
  14182.         DATA_TYPE,
  14183.         TYPE_GUID,
  14184.         CHARACTER_MAXIMUM_LENGTH,
  14185.         CHARACTER_OCTET_LENGTH,
  14186.         NUMERIC_PRECISION,
  14187.         NUMERIC_SCALE,
  14188.         DATETIME_PRECISION,
  14189.         CHARACTER_SET_CATALOG,
  14190.         CHARACTER_SET_SCHEMA,
  14191.         CHARACTER_SET_NAME,
  14192.         COLLATION_CATALOG,
  14193.         COLLATION_SCHEMA,
  14194.         COLLATION_NAME,
  14195.         DOMAIN_CATALOG,
  14196.         DOMAIN_SCHEMA,
  14197.         DOMAIN_NAME,
  14198.         DESCRIPTION
  14199.     from master.dbo.SYSREMOTE_COLUMNS (
  14200.                 @table_server,
  14201.                 @table_catalog,
  14202.                 @table_schema,
  14203.                 @table_name,
  14204.                 @column_name )
  14205.     order by 1, 2, 3, 7
  14206. go
  14207.  
  14208.  
  14209. dump tran master with no_log
  14210. go
  14211.  
  14212. /*  Procedure for 8.00 server */
  14213. if (charindex('8.00', @@version) = 0)
  14214. begin
  14215.     print ''
  14216.     print ''
  14217.     print 'Warning:'
  14218.     print 'you are installing the stored procedures '
  14219.     print 'on a pre 8.0 SQL Server.'
  14220.     print 'Ignore the following errors.'
  14221. end
  14222. else
  14223.     drop proc sp_columns_rowset
  14224. go
  14225.  
  14226. /*  Procedure for 8.00 server */
  14227. create procedure sp_columns_rowset
  14228.     (
  14229.     @table_name sysname,
  14230.     @table_schema   sysname = NULL,
  14231.     @column_name    sysname = NULL
  14232.     )
  14233. as  
  14234.     select
  14235.         TABLE_CATALOG       = db_name(),
  14236.         TABLE_SCHEMA        = user_name(o.uid),
  14237.         TABLE_NAME      = o.name,
  14238.         COLUMN_NAME     = c.name,
  14239.         COLUMN_GUID     = convert(uniqueidentifier,null),
  14240.         COLUMN_PROPID       = convert(int,null),
  14241.         ORDINAL_POSITION    = convert(int,
  14242.                        (
  14243.                         select count(*)
  14244.                         from syscolumns sc
  14245.                         where sc.id     =  c.id
  14246.                           AND sc.number =  c.number
  14247.                           AND sc.colid  <= c.colid
  14248.                         )),
  14249.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  14250.  
  14251.         /* Get rid of ( if it is first char otherwise display the whole thing */
  14252.         COLUMN_DEFAULT      = convert(nvarchar(2000),
  14253.                                         case when substring(m.text,1,1) = '('
  14254.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14255.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14256.                                         end ), 
  14257.  
  14258.         COLUMN_FLAGS        = convert(int,
  14259.                         case when d.is_long = 1 
  14260.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14261.                         else 0
  14262.                         end
  14263.                     |   case when d.fixlen is not null
  14264.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14265.                         else 0
  14266.                         end
  14267.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14268.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14269.                         else 0
  14270.                         end
  14271.                     |   case 
  14272.                         when d.type_name = 'timestamp'
  14273.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14274.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  14275.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14276.                         else 0 
  14277.                         end),
  14278.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14279.         DATA_TYPE       = d.oledb_data_type,
  14280.         TYPE_GUID       = convert(uniqueidentifier,null),
  14281.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14282.                         case 
  14283.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14284.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14285.                         then coalesce(d.column_size,c.length)
  14286.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14287.                         then coalesce(d.column_size,c.length/2)
  14288.                         else null 
  14289.                         end),
  14290.         CHARACTER_OCTET_LENGTH  = convert(int,
  14291.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14292.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14293.                         then coalesce(d.column_size,c.length)
  14294.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14295.                         then coalesce(d.column_size*2,c.length)
  14296.                         else null 
  14297.                         end),
  14298.         NUMERIC_PRECISION   = convert(smallint,
  14299.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14300.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14301.                             then d.data_precision else null end),
  14302.         NUMERIC_SCALE       = convert(smallint, 
  14303.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14304.         DATETIME_PRECISION  = convert(int,
  14305.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14306.                             when data_precision = 23 then 3 else 0 end),
  14307.         CHARACTER_SET_CATALOG   = convert(sysname,
  14308.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14309.                         then N'master' 
  14310.                         else null 
  14311.                         end),
  14312.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14313.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14314.                         then N'dbo' 
  14315.                         else null 
  14316.                         end),
  14317.         CHARACTER_SET_NAME  = convert(sysname,
  14318.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14319.                         then a_cha.name 
  14320.                         else null 
  14321.                         end),
  14322.         COLLATION_CATALOG   = convert(sysname,
  14323.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14324.                         then N'master' 
  14325.                         else null 
  14326.                         end),
  14327.         COLLATION_SCHEMA    = convert(sysname,
  14328.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14329.                         then N'dbo' 
  14330.                         else null 
  14331.                         end),
  14332.         COLLATION_NAME      = convert(sysname,
  14333.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14334.                         then c.collation 
  14335.                         else null 
  14336.                         end),
  14337.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  14338.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  14339.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  14340.         DESCRIPTION     = convert(nvarchar(1),null),
  14341.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14342.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14343.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14344.                             when ABS(c.collationid) > 0x1000000 
  14345.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14346.                             else null 
  14347.                         end,
  14348.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14349.         IS_COMPUTED = convert(bit, c.iscomputed)    
  14350.     from
  14351.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14352.         sysobjects o,
  14353.         master.dbo.spt_provider_types d,
  14354.         systypes t,
  14355.         master.dbo.syscharsets      a_cha /* charset/1001, not sortorder. */
  14356.     where
  14357.         permissions(o.id, c.name) <> 0
  14358.     and o.name = @table_name
  14359.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14360.     and (@table_schema is null or @table_schema = user_name(o.uid))
  14361.     and (@column_name is null or @column_name = c.name)
  14362.     and     o.id = c.id
  14363.     and     t.xtype = d.ss_dtype
  14364.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14365.     and     c.xusertype = t.xusertype
  14366.     and a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14367.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14368.     order by 2, 3, c.colorder
  14369. go
  14370.  
  14371.  
  14372. /*  Procedure for 8.0 server */
  14373. create procedure sp_columns_rowset;2
  14374.     (
  14375.     @table_schema   sysname = NULL,
  14376.     @column_name    sysname = NULL
  14377.     )
  14378. as  
  14379.     select
  14380.         TABLE_CATALOG       = db_name(),
  14381.         TABLE_SCHEMA        = user_name(o.uid),
  14382.         TABLE_NAME      = o.name,
  14383.         COLUMN_NAME     = c.name,
  14384.         COLUMN_GUID     = convert(uniqueidentifier,null),
  14385.         COLUMN_PROPID       = convert(int,null),
  14386.         ORDINAL_POSITION    = convert(int,
  14387.                        (
  14388.                         select count(*)
  14389.                         from syscolumns sc
  14390.                         where sc.id     =  c.id
  14391.                           AND sc.number =  c.number
  14392.                           AND sc.colid  <= c.colid
  14393.                         )),
  14394.         COLUMN_HASDEFAULT   = convert(bit, case when m.text is null then 0 else 1 end),
  14395.         COLUMN_DEFAULT      = convert(nvarchar(2000),
  14396.                                         case when substring(m.text,1,1) = '('
  14397.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14398.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14399.                                         end ), 
  14400.         COLUMN_FLAGS        = convert(int,
  14401.                         case when d.is_long = 1 
  14402.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14403.                         else 0
  14404.                         end
  14405.                     |   case when d.fixlen is not null
  14406.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14407.                         else 0
  14408.                         end
  14409.                     |   case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14410.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14411.                         else 0
  14412.                         end
  14413.                     |   case 
  14414.                         when d.type_name = 'timestamp'
  14415.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14416.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14417.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14418.                         else 0 
  14419.                         end),
  14420.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14421.         DATA_TYPE       = d.oledb_data_type,
  14422.         TYPE_GUID       = convert(uniqueidentifier,null),
  14423.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14424.                         case 
  14425.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14426.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14427.                         then coalesce(d.column_size,c.length)
  14428.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14429.                         then coalesce(d.column_size,c.length/2)
  14430.                         else null 
  14431.                         end),
  14432.         CHARACTER_OCTET_LENGTH  = convert(int,
  14433.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14434.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14435.                         then coalesce(d.column_size,c.length)
  14436.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14437.                         then coalesce(d.column_size*2,c.length)
  14438.                         else null 
  14439.                         end),
  14440.         NUMERIC_PRECISION   = convert(smallint,
  14441.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14442.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14443.                             then d.data_precision else null end),
  14444.         NUMERIC_SCALE       = convert(smallint, 
  14445.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14446.         DATETIME_PRECISION  = convert(int,
  14447.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14448.                             when data_precision = 23 then 3 else 0 end),
  14449.         CHARACTER_SET_CATALOG   = convert(sysname,
  14450.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14451.                         then N'master' 
  14452.                         else null 
  14453.                         end),
  14454.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14455.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14456.                         then N'dbo' 
  14457.                         else null 
  14458.                         end),
  14459.         CHARACTER_SET_NAME  = convert(sysname,
  14460.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14461.                         then a_cha.name 
  14462.                         else null 
  14463.                         end),
  14464.         COLLATION_CATALOG   = convert(sysname,
  14465.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14466.                         then N'master' 
  14467.                         else null 
  14468.                         end),
  14469.         COLLATION_SCHEMA    = convert(sysname,
  14470.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14471.                         then N'dbo' 
  14472.                         else null 
  14473.                         end),
  14474.         COLLATION_NAME      = convert(sysname,
  14475.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14476.                         then c.collation 
  14477.                         else null 
  14478.                         end),
  14479.         DOMAIN_CATALOG      = case when t.usertype < 256 then null else db_name() end,
  14480.         DOMAIN_SCHEMA       = case when t.usertype < 256 then null else user_name(o.uid) end,
  14481.         DOMAIN_NAME     = case when t.usertype < 256 then null else t.name end,
  14482.         DESCRIPTION     = convert(nvarchar(1),null),    
  14483.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14484.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14485.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14486.                             when ABS(c.collationid) > 0x1000000 
  14487.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14488.                             else null 
  14489.                         end,
  14490.         
  14491.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14492.         IS_COMPUTED = convert(bit, c.iscomputed)
  14493.     
  14494.     from
  14495.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14496.         sysobjects o,
  14497.         master.dbo.spt_provider_types d,
  14498.         systypes t,
  14499.         master.dbo.syscharsets      a_cha /* charset/1001, not sortorder.*/
  14500.     where
  14501.         permissions(o.id, c.name) <> 0
  14502.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14503.     and (@table_schema is null or @table_schema = user_name(o.uid))
  14504.     and (@column_name is null or @column_name = c.name)
  14505.     and     o.id = c.id
  14506.     and     t.xtype = d.ss_dtype
  14507.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14508.     and     c.xusertype = t.xusertype
  14509.     and a_cha.type = 1001 /* type is charset */
  14510.     and a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14511.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14512.     order by 2, 3, c.colorder
  14513. go
  14514.  
  14515. dump tran master with no_log
  14516. go
  14517.  
  14518. /*  Procedure for 8.0 server */
  14519. create procedure sp_columns_rowset;5
  14520.     (
  14521.     @table_server       sysname,
  14522.     @table_catalog      sysname = null,
  14523.     @table_name     sysname = null,
  14524.     @table_schema       sysname = null,
  14525.     @column_name        sysname = null
  14526.     )
  14527. as
  14528.     select
  14529.         TABLE_CATALOG,
  14530.         TABLE_SCHEMA,
  14531.         TABLE_NAME,
  14532.         COLUMN_NAME,
  14533.         COLUMN_GUID,
  14534.         COLUMN_PROPID,
  14535.         ORDINAL_POSITION,
  14536.         COLUMN_HASDEFAULT,
  14537.         COLUMN_DEFAULT,
  14538.         COLUMN_FLAGS,
  14539.         IS_NULLABLE,
  14540.         DATA_TYPE,
  14541.         TYPE_GUID,
  14542.         CHARACTER_MAXIMUM_LENGTH,
  14543.         CHARACTER_OCTET_LENGTH,
  14544.         NUMERIC_PRECISION,
  14545.         NUMERIC_SCALE,
  14546.         DATETIME_PRECISION,
  14547.         CHARACTER_SET_CATALOG,
  14548.         CHARACTER_SET_SCHEMA,
  14549.         CHARACTER_SET_NAME,
  14550.         COLLATION_CATALOG,
  14551.         COLLATION_SCHEMA,
  14552.         COLLATION_NAME,
  14553.         DOMAIN_CATALOG,
  14554.         DOMAIN_SCHEMA,
  14555.         DOMAIN_NAME,
  14556.         DESCRIPTION
  14557.  
  14558.     from master.dbo.SYSREMOTE_COLUMNS (
  14559.                 @table_server,
  14560.                 @table_catalog,
  14561.                 @table_schema,
  14562.                 @table_name,
  14563.                 @column_name )
  14564.     order by 1, 2, 3, 7
  14565. go
  14566.  
  14567.  
  14568. grant execute on sp_columns_rowset to public
  14569. go
  14570.  
  14571. dump tran master with no_log
  14572. go
  14573. if (charindex('6.00', @@version) > 0)
  14574.     begin
  14575.     if (exists (select * from sysobjects
  14576.             where name = 'sp_columns_rowset' and type = 'P '))
  14577.         begin
  14578.         drop procedure sp_columns_rowset
  14579.         dump tran master with no_log
  14580.         end
  14581.     end
  14582. go
  14583.  
  14584. print ''
  14585. print 'creating sp_check_constraints_rowset'
  14586. go
  14587.  
  14588. /*  Procedure for 6.50 and earlier servers */
  14589. create procedure sp_check_constraints_rowset
  14590.     (
  14591.     @constraint_name    varchar(255),
  14592.     @constraint_schema  varchar(255) = null
  14593.     )
  14594. as
  14595.     select
  14596.         CONSTRAINT_CATALOG  = db_name(),
  14597.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14598.         CONSTRAINT_NAME     = c_obj.name,
  14599.         CHECK_CLAUSE        = m.text,   
  14600.         DESCRIPTION         = convert(varchar(1),null)
  14601.     from 
  14602.         sysobjects c_obj, syscomments m
  14603.     where
  14604.             c_obj.type = 'C '
  14605.         and c_obj.name  = @constraint_name
  14606.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14607.         and c_obj.id = m.id
  14608.     order by 1,2,3 
  14609. go
  14610. dump tran master with no_log
  14611. go
  14612.  
  14613. create procedure sp_check_constraints_rowset;2
  14614.     (
  14615.     @constraint_schema  varchar(255) = null
  14616.     )
  14617. as
  14618.     select
  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(varchar(1),null)
  14624.     from 
  14625.         sysobjects c_obj, syscomments m
  14626.     where
  14627.             c_obj.type = 'C '
  14628.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14629.         and c_obj.id = m.id
  14630.     order by 1,2,3 
  14631. go
  14632. dump tran master with no_log
  14633. go
  14634.  
  14635. if (charindex('7.00', @@version) > 0 or
  14636.     charindex('8.00', @@version) > 0)
  14637.     drop procedure sp_check_constraints_rowset
  14638. else
  14639. begin
  14640.     print ''
  14641.     print ''
  14642.     print 'Warning:'
  14643.     print 'you are installing the stored procedures '
  14644.     print 'on a pre 7.0 SQL Server.'
  14645.     print 'Ignore the following errors.'
  14646. end
  14647. go
  14648.  
  14649. /*  Procedure for 7.0 servers and 8.0 servers */
  14650. create procedure sp_check_constraints_rowset
  14651.     (
  14652.     @constraint_name    sysname,
  14653.     @constraint_schema  sysname = null
  14654.     )
  14655. as
  14656.     select
  14657.         CONSTRAINT_CATALOG  = db_name(),
  14658.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14659.         CONSTRAINT_NAME     = c_obj.name,
  14660.         CHECK_CLAUSE        = m.text,   
  14661.         DESCRIPTION         = convert(nvarchar(1), null)
  14662.     from 
  14663.         sysobjects c_obj, syscomments m
  14664.     where
  14665.             c_obj.type = 'C '
  14666.         and c_obj.name  = @constraint_name
  14667.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14668.         and m.id = c_obj.id
  14669.     order by 1,2,3 
  14670. go
  14671. dump tran master with no_log
  14672. go
  14673.  
  14674. create procedure sp_check_constraints_rowset;2
  14675.     (
  14676.     @constraint_schema  sysname = null
  14677.     )
  14678. as
  14679.     select
  14680.         CONSTRAINT_CATALOG  = db_name(),
  14681.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14682.         CONSTRAINT_NAME     = c_obj.name,
  14683.         CHECK_CLAUSE        = m.text,   
  14684.         DESCRIPTION         = convert(nvarchar(1), null)
  14685.     from 
  14686.         sysobjects c_obj, syscomments m
  14687.     where
  14688.             c_obj.type = 'C '
  14689.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14690.         and m.id = c_obj.id
  14691.     order by 1,2,3 
  14692. go
  14693.  
  14694. grant execute on sp_check_constraints_rowset to public
  14695. go
  14696.  
  14697. dump tran master with no_log
  14698. go
  14699.  
  14700. if (charindex('6.00', @@version) > 0)
  14701.     begin
  14702.     if (exists (select * from sysobjects
  14703.             where name = 'sp_check_constraints_rowset' and type = 'P '))
  14704.         begin
  14705.         drop procedure sp_check_constraints_rowset
  14706.         dump tran master with no_log
  14707.         end
  14708.     end
  14709. go
  14710.  
  14711. print ''
  14712. print 'creating sp_check_constbytable_rowset'
  14713. go
  14714.  
  14715. /*  Procedure for 6.50 and earlier servers */
  14716. create procedure sp_check_constbytable_rowset
  14717.     (
  14718.     @table_name         varchar(255), 
  14719.     @table_schema       varchar(255) = null,
  14720.     @constraint_name    varchar(255) = null,
  14721.     @constraint_schema  varchar(255) = null
  14722.     )
  14723. as
  14724.     select
  14725.         TABLE_CATALOG       = db_name(),
  14726.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14727.         TABLE_NAME          = t_obj.name,
  14728.         CONSTRAINT_CATALOG  = db_name(),
  14729.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14730.         CONSTRAINT_NAME     = c_obj.name,
  14731.         CHECK_CLAUSE        = m.text,   
  14732.         DESCRIPTION         = convert(varchar(1),null)
  14733.     from 
  14734.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14735.     where
  14736.             t_obj.name  = @table_name
  14737.         and t_obj.type in ('U ','S ')
  14738.         and (@table_schema is null or @table_schema = user_name(t_obj.uid)) 
  14739.         and t_obj.id = c.id
  14740.         and c.constid = c_obj.id
  14741.         and c_obj.type = 'C '
  14742.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14743.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14744.         and c_obj.id = m.id
  14745.     order by 1,2,3,4,5,6 
  14746. go
  14747. dump tran master with no_log
  14748. go
  14749.  
  14750. create procedure sp_check_constbytable_rowset;2
  14751.     (
  14752.     @table_schema       varchar(255) = null,
  14753.     @constraint_name    varchar(255) = null,
  14754.     @constraint_schema  varchar(255) = null
  14755.     )
  14756. as
  14757.     select
  14758.         TABLE_CATALOG       = db_name(),
  14759.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14760.         TABLE_NAME          = t_obj.name,
  14761.         CONSTRAINT_CATALOG  = db_name(),
  14762.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14763.         CONSTRAINT_NAME     = c_obj.name,
  14764.         CHECK_CLAUSE        = m.text,   
  14765.         DESCRIPTION         = convert(varchar(1),null)
  14766.     from 
  14767.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14768.     where
  14769.             t_obj.type in ('U ','S ')
  14770.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14771.         and c.id = t_obj.id
  14772.         and c.constid = c_obj.id
  14773.         and c_obj.type  = 'C '
  14774.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14775.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14776.         and c_obj.id = m.id
  14777.     order by 1,2,3,4,5,6 
  14778. go
  14779. dump tran master with no_log
  14780. go
  14781.  
  14782. if (charindex('7.00', @@version) > 0 or
  14783.     charindex('8.00', @@version) > 0)
  14784.     drop procedure sp_check_constbytable_rowset
  14785. else
  14786. begin
  14787.     print ''
  14788.     print ''
  14789.     print 'Warning:'
  14790.     print 'you are installing the stored procedures '
  14791.     print 'on a pre 8.0 SQL Server.'
  14792.     print 'Ignore the following errors.'
  14793. end
  14794. go
  14795.  
  14796. /*  Procedure for 8.0 server */
  14797. create procedure sp_check_constbytable_rowset
  14798.     (
  14799.     @table_name         sysname, 
  14800.     @table_schema       sysname = null,
  14801.     @constraint_name    sysname = null,
  14802.     @constraint_schema  sysname = null
  14803.     )
  14804. as
  14805.     select
  14806.         TABLE_CATALOG       = db_name(),
  14807.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14808.         TABLE_NAME          = t_obj.name,
  14809.         CONSTRAINT_CATALOG  = db_name(),
  14810.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14811.         CONSTRAINT_NAME     = c_obj.name,
  14812.         CHECK_CLAUSE        = m.text,   
  14813.         DESCRIPTION         = convert(nvarchar(1), null)
  14814.  
  14815.     from 
  14816.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14817.     where
  14818.             t_obj.name  = @table_name
  14819.         and t_obj.type in ('U ','S ')
  14820.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14821.         and c_obj.parent_obj = t_obj.id
  14822.         and c_obj.type = 'C '
  14823.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14824.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14825.         and m.id = c_obj.id
  14826. order by 1,2,3,4,5,6 
  14827. go
  14828. dump tran master with no_log
  14829. go
  14830. create procedure sp_check_constbytable_rowset;2
  14831.     (
  14832.     @table_schema       sysname = null,
  14833.     @constraint_name    sysname = null,
  14834.     @constraint_schema  sysname = null
  14835.     )
  14836. as
  14837.     select
  14838.         TABLE_CATALOG       = db_name(),
  14839.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14840.         TABLE_NAME          = t_obj.name,
  14841.         CONSTRAINT_CATALOG  = db_name(),
  14842.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  14843.         CONSTRAINT_NAME     = c_obj.name,
  14844.         CHECK_CLAUSE        = m.text,   
  14845.         DESCRIPTION         = convert(nvarchar(1), null)
  14846.     from 
  14847.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14848.     where
  14849.             t_obj.type in ('U ','S ')
  14850.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14851.         and c_obj.parent_obj = t_obj.id
  14852.         and c_obj.type = 'C '
  14853.         and (@constraint_name is null or c_obj.name = @constraint_name)
  14854.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14855.         and m.id = c_obj.id
  14856.     order by 1,2,3,4,5,6 
  14857. go
  14858. dump tran master with no_log
  14859. go
  14860.  
  14861. grant execute on sp_check_constbytable_rowset to public
  14862. go
  14863.  
  14864. dump tran master with no_log
  14865. go
  14866. if (charindex('6.00', @@version) > 0)
  14867.     begin
  14868.     if (exists (select * from sysobjects
  14869.             where name = 'sp_check_constbytable_rowset' and type = 'P '))
  14870.         begin
  14871.         drop procedure sp_check_constbytable_rowset
  14872.         dump tran master with no_log
  14873.         end
  14874.     end
  14875. go
  14876.  
  14877.  
  14878. print ''
  14879. print 'creating sp_foreign_keys_rowset'
  14880. go
  14881.  
  14882. /*  Procedure for 6.0 and 6.5 server */
  14883. CREATE PROCEDURE sp_foreign_keys_rowset
  14884.     (
  14885.     @pk_table_name      varchar(255) = null,
  14886.     @pk_table_schema    varchar(255) = null,
  14887.     @pk_table_catalog   varchar(255) = null,
  14888.     @fk_table_name      varchar(255) = null,
  14889.     @fk_table_schema    varchar(255) = null,
  14890.     @fk_table_catalog   varchar(255) = null
  14891.     )
  14892. as
  14893.     BEGIN
  14894.     select
  14895.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14896.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  14897.         PK_TABLE_NAME       = o1.name,
  14898.         PK_COLUMN_NAME      = c1.name,
  14899.         PK_COLUMN_GUID      = convert(binary(16),null),
  14900.         PK_COLUMN_PROPID    = convert(int,null),
  14901.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14902.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14903.         FK_TABLE_NAME       = o2.name,
  14904.         FK_COLUMN_NAME      = c2.name,
  14905.         FK_COLUMN_GUID      = convert(binary(16),null),
  14906.         FK_COLUMN_PROPID    = convert(int,null),
  14907.         ORDINAL             = convert(int,1),
  14908.         UPDATE_RULE         = 'NO ACTION',
  14909.         DELETE_RULE         = 'NO ACTION',
  14910.         PK_NAME             = i.name,
  14911.         FK_NAME             = object_name(r.constid),
  14912.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14913.     from
  14914.         sysobjects o1, sysobjects o2,
  14915.         syscolumns c1, syscolumns c2,
  14916.         sysreferences r, sysindexes i, sysusers u
  14917.     where   
  14918.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14919.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14920.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14921.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14922.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14923.     and o1.id = r.rkeyid
  14924.     and o1.id = c1.id
  14925.     and c1.colid = r.rkey1
  14926.     and r.fkeyid = o2.id
  14927.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14928.     and o2.id = c2.id   
  14929.     and c2.colid = r.fkey1
  14930.     and i.id = r.rkeyid
  14931.     and i.indid = r.rkeyindid
  14932.     and u.uid = user_id()
  14933.     and ( suser_id() = 1     /* User is the System Administrator */
  14934.     or (
  14935.         o1.uid = user_id()   /* User created the object */
  14936.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14937.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14938.             from sysprotects p
  14939.             where p.id = o1.id
  14940.             /* get rows for public,current user,user's group */
  14941.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14942.             /* check for SELECT,EXECUTE privilege */
  14943.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14944.             ) = 1   /* final magic...compare Grants   */
  14945.         )
  14946.     and (
  14947.         o2.uid = user_id()   /* User created the object */
  14948.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14949.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14950.             from sysprotects p
  14951.             where p.id = o2.id
  14952.             /* get rows for public,current user,user's group */
  14953.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14954.             /* check for SELECT,EXECUTE privilege */
  14955.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14956.             ) = 1   /* final magic...compare Grants   */
  14957.         )
  14958.     )
  14959.     union all
  14960.     select
  14961.         db_name(r.rkeydbid),
  14962.         user_name(o1.uid),
  14963.         o1.name,
  14964.         c1.name,
  14965.         convert(binary(16),null),
  14966.         convert(int,null),
  14967.         db_name(r.fkeydbid),
  14968.         user_name(o2.uid),
  14969.         o2.name,
  14970.         c2.name,
  14971.         convert(binary(16),null),
  14972.         convert(int,null),
  14973.         convert(int,2),
  14974.         'NO ACTION',
  14975.         'NO ACTION',
  14976.         i.name,
  14977.         object_name(r.constid),
  14978.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14979.     from
  14980.         sysobjects o1, sysobjects o2,
  14981.         syscolumns c1, syscolumns c2,
  14982.         sysreferences r, sysindexes i, sysusers u
  14983.     where   
  14984.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14985.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14986.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14987.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14988.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14989.     and o1.id = r.rkeyid
  14990.     and r.keycnt >= 2
  14991.     and o1.id = c1.id
  14992.     and c1.colid = r.rkey2
  14993.     and r.fkeyid = o2.id
  14994.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14995.     and o2.id = c2.id   
  14996.     and c2.colid = r.fkey2
  14997.     and i.id = r.rkeyid
  14998.     and i.indid = r.rkeyindid
  14999.     and u.uid = user_id()
  15000.     and ( suser_id() = 1     /* User is the System Administrator */
  15001.     or (
  15002.         o1.uid = user_id()   /* User created the object */
  15003.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15004.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15005.             from sysprotects p
  15006.             where p.id = o1.id
  15007.             /* get rows for public,current user,user's group */
  15008.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15009.             /* check for SELECT,EXECUTE privilege */
  15010.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15011.             ) = 1   /* final magic...compare Grants   */
  15012.         )
  15013.     and (
  15014.         o2.uid = user_id()   /* User created the object */
  15015.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15016.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15017.             from sysprotects p
  15018.             where p.id = o2.id
  15019.             /* get rows for public,current user,user's group */
  15020.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15021.             /* check for SELECT,EXECUTE privilege */
  15022.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15023.             ) = 1   /* final magic...compare Grants   */
  15024.         )
  15025.     )
  15026.     union all
  15027.     select
  15028.         db_name(r.rkeydbid),
  15029.         user_name(o1.uid),
  15030.         o1.name,
  15031.         c1.name,
  15032.         convert(binary(16),null),
  15033.         convert(int,null),
  15034.         db_name(r.fkeydbid),
  15035.         user_name(o2.uid),
  15036.         o2.name,
  15037.         c2.name,
  15038.         convert(binary(16),null),
  15039.         convert(int,null),
  15040.         convert(int,3),
  15041.         'NO ACTION',
  15042.         'NO ACTION',
  15043.         i.name,
  15044.         object_name(r.constid),
  15045.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15046.     from
  15047.         sysobjects o1, sysobjects o2,
  15048.         syscolumns c1, syscolumns c2,
  15049.         sysreferences r, sysindexes i, sysusers u
  15050.     where   
  15051.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15052.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15053.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15054.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15055.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15056.     and o1.id = r.rkeyid
  15057.     and r.keycnt >= 3
  15058.     and o1.id = c1.id
  15059.     and c1.colid = r.rkey3
  15060.     and r.fkeyid = o2.id
  15061.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15062.     and o2.id = c2.id   
  15063.     and c2.colid = r.fkey3
  15064.     and i.id = r.rkeyid
  15065.     and i.indid = r.rkeyindid
  15066.     and u.uid = user_id()
  15067.     and ( suser_id() = 1     /* User is the System Administrator */
  15068.     or (
  15069.         o1.uid = user_id()   /* User created the object */
  15070.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15071.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15072.             from sysprotects p
  15073.             where p.id = o1.id
  15074.             /* get rows for public,current user,user's group */
  15075.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15076.             /* check for SELECT,EXECUTE privilege */
  15077.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15078.             ) = 1   /* final magic...compare Grants   */
  15079.         )
  15080.     and (
  15081.         o2.uid = user_id()   /* User created the object */
  15082.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15083.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15084.             from sysprotects p
  15085.             where p.id = o2.id
  15086.             /* get rows for public,current user,user's group */
  15087.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15088.             /* check for SELECT,EXECUTE privilege */
  15089.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15090.             ) = 1   /* final magic...compare Grants   */
  15091.         )
  15092.     )
  15093.     union all
  15094.     select
  15095.         db_name(r.rkeydbid),
  15096.         user_name(o1.uid),
  15097.         o1.name,
  15098.         c1.name,
  15099.         convert(binary(16),null),
  15100.         convert(int,null),
  15101.         db_name(r.fkeydbid),
  15102.         user_name(o2.uid),
  15103.         o2.name,
  15104.         c2.name,
  15105.         convert(binary(16),null),
  15106.         convert(int,null),
  15107.         convert(int,4),
  15108.         'NO ACTION',
  15109.         'NO ACTION',
  15110.         i.name,
  15111.         object_name(r.constid),
  15112.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15113.     from
  15114.         sysobjects o1, sysobjects o2,
  15115.         syscolumns c1, syscolumns c2,
  15116.         sysreferences r, sysindexes i, sysusers u
  15117.     where   
  15118.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15119.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15120.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15121.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15122.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15123.     and o1.id = r.rkeyid
  15124.     and r.keycnt >= 4
  15125.     and o1.id = c1.id
  15126.     and c1.colid = r.rkey4
  15127.     and r.fkeyid = o2.id
  15128.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15129.     and o2.id = c2.id   
  15130.     and c2.colid = r.fkey4
  15131.     and i.id = r.rkeyid
  15132.     and i.indid = r.rkeyindid
  15133.     and u.uid = user_id()
  15134.     and ( suser_id() = 1     /* User is the System Administrator */
  15135.     or (
  15136.         o1.uid = user_id()   /* User created the object */
  15137.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15138.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15139.             from sysprotects p
  15140.             where p.id = o1.id
  15141.             /* get rows for public,current user,user's group */
  15142.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15143.             /* check for SELECT,EXECUTE privilege */
  15144.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15145.             ) = 1   /* final magic...compare Grants   */
  15146.         )
  15147.     and (
  15148.         o2.uid = user_id()   /* User created the object */
  15149.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15150.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15151.             from sysprotects p
  15152.             where p.id = o2.id
  15153.             /* get rows for public,current user,user's group */
  15154.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15155.             /* check for SELECT,EXECUTE privilege */
  15156.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15157.             ) = 1   /* final magic...compare Grants   */
  15158.         )
  15159.     )
  15160.     union all
  15161.     select
  15162.         db_name(r.rkeydbid),
  15163.         user_name(o1.uid),
  15164.         o1.name,
  15165.         c1.name,
  15166.         convert(binary(16),null),
  15167.         convert(int,null),
  15168.         db_name(r.fkeydbid),
  15169.         user_name(o2.uid),
  15170.         o2.name,
  15171.         c2.name,
  15172.         convert(binary(16),null),
  15173.         convert(int,null),
  15174.         convert(int,5),
  15175.         'NO ACTION',
  15176.         'NO ACTION',
  15177.         i.name,
  15178.         object_name(r.constid),
  15179.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15180.     from
  15181.         sysobjects o1, sysobjects o2,
  15182.         syscolumns c1, syscolumns c2,
  15183.         sysreferences r, sysindexes i, sysusers u
  15184.     where   
  15185.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15186.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15187.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15188.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15189.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15190.     and o1.id = r.rkeyid
  15191.     and r.keycnt >= 5
  15192.     and o1.id = c1.id
  15193.     and c1.colid = r.rkey5
  15194.     and r.fkeyid = o2.id
  15195.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15196.     and o2.id = c2.id   
  15197.     and c2.colid = r.fkey5
  15198.     and i.id = r.rkeyid
  15199.     and i.indid = r.rkeyindid
  15200.     and u.uid = user_id()
  15201.     and ( suser_id() = 1     /* User is the System Administrator */
  15202.     or (
  15203.         o1.uid = user_id()   /* User created the object */
  15204.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15205.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15206.             from sysprotects p
  15207.             where p.id = o1.id
  15208.             /* get rows for public,current user,user's group */
  15209.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15210.             /* check for SELECT,EXECUTE privilege */
  15211.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15212.             ) = 1   /* final magic...compare Grants   */
  15213.         )
  15214.     and (
  15215.         o2.uid = user_id()   /* User created the object */
  15216.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15217.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15218.             from sysprotects p
  15219.             where p.id = o2.id
  15220.             /* get rows for public,current user,user's group */
  15221.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15222.             /* check for SELECT,EXECUTE privilege */
  15223.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15224.             ) = 1   /* final magic...compare Grants   */
  15225.         )
  15226.     )
  15227.     union all
  15228.     select
  15229.         db_name(r.rkeydbid),
  15230.         user_name(o1.uid),
  15231.         o1.name,
  15232.         c1.name,
  15233.         convert(binary(16),null),
  15234.         convert(int,null),
  15235.         db_name(r.fkeydbid),
  15236.         user_name(o2.uid),
  15237.         o2.name,
  15238.         c2.name,
  15239.         convert(binary(16),null),
  15240.         convert(int,null),
  15241.         convert(int,6),
  15242.         'NO ACTION',
  15243.         'NO ACTION',
  15244.         i.name,
  15245.         object_name(r.constid),
  15246.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15247.     from
  15248.         sysobjects o1, sysobjects o2,
  15249.         syscolumns c1, syscolumns c2,
  15250.         sysreferences r, sysindexes i, sysusers u
  15251.     where   
  15252.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15253.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15254.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15255.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15256.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15257.     and o1.id = r.rkeyid
  15258.     and r.keycnt >= 6
  15259.     and o1.id = c1.id
  15260.     and c1.colid = r.rkey6
  15261.     and r.fkeyid = o2.id
  15262.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15263.     and o2.id = c2.id   
  15264.     and c2.colid = r.fkey6
  15265.     and i.id = r.rkeyid
  15266.     and i.indid = r.rkeyindid
  15267.     and u.uid = user_id()
  15268.     and ( suser_id() = 1     /* User is the System Administrator */
  15269.     or (
  15270.         o1.uid = user_id()   /* User created the object */
  15271.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15272.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15273.             from sysprotects p
  15274.             where p.id = o1.id
  15275.             /* get rows for public,current user,user's group */
  15276.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15277.             /* check for SELECT,EXECUTE privilege */
  15278.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15279.             ) = 1   /* final magic...compare Grants   */
  15280.         )
  15281.     and (
  15282.         o2.uid = user_id()   /* User created the object */
  15283.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15284.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15285.             from sysprotects p
  15286.             where p.id = o2.id
  15287.             /* get rows for public,current user,user's group */
  15288.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15289.             /* check for SELECT,EXECUTE privilege */
  15290.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15291.             ) = 1   /* final magic...compare Grants   */
  15292.         )
  15293.     )
  15294.     union all
  15295.     select
  15296.         db_name(r.rkeydbid),
  15297.         user_name(o1.uid),
  15298.         o1.name,
  15299.         c1.name,
  15300.         convert(binary(16),null),
  15301.         convert(int,null),
  15302.         db_name(r.fkeydbid),
  15303.         user_name(o2.uid),
  15304.         o2.name,
  15305.         c2.name,
  15306.         convert(binary(16),null),
  15307.         convert(int,null),
  15308.         convert(int,7),
  15309.         'NO ACTION',
  15310.         'NO ACTION',
  15311.         i.name,
  15312.         object_name(r.constid),
  15313.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15314.     from
  15315.         sysobjects o1, sysobjects o2,
  15316.         syscolumns c1, syscolumns c2,
  15317.         sysreferences r, sysindexes i, sysusers u
  15318.     where   
  15319.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15320.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15321.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15322.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15323.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15324.     and o1.id = r.rkeyid
  15325.     and r.keycnt >= 7
  15326.     and o1.id = c1.id
  15327.     and c1.colid = r.rkey7
  15328.     and r.fkeyid = o2.id
  15329.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15330.     and o2.id = c2.id   
  15331.     and c2.colid = r.fkey7
  15332.     and i.id = r.rkeyid
  15333.     and i.indid = r.rkeyindid
  15334.     and u.uid = user_id()
  15335.     and ( suser_id() = 1     /* User is the System Administrator */
  15336.     or (
  15337.         o1.uid = user_id()   /* User created the object */
  15338.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15339.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15340.             from sysprotects p
  15341.             where p.id = o1.id
  15342.             /* get rows for public,current user,user's group */
  15343.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15344.             /* check for SELECT,EXECUTE privilege */
  15345.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15346.             ) = 1   /* final magic...compare Grants   */
  15347.         )
  15348.     and (
  15349.         o2.uid = user_id()   /* User created the object */
  15350.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15351.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15352.             from sysprotects p
  15353.             where p.id = o2.id
  15354.             /* get rows for public,current user,user's group */
  15355.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15356.             /* check for SELECT,EXECUTE privilege */
  15357.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15358.             ) = 1   /* final magic...compare Grants   */
  15359.         )
  15360.     )
  15361.     union all
  15362.     select
  15363.         db_name(r.rkeydbid),
  15364.         user_name(o1.uid),
  15365.         o1.name,
  15366.         c1.name,
  15367.         convert(binary(16),null),
  15368.         convert(int,null),
  15369.         db_name(r.fkeydbid),
  15370.         user_name(o2.uid),
  15371.         o2.name,
  15372.         c2.name,
  15373.         convert(binary(16),null),
  15374.         convert(int,null),
  15375.         convert(int,8),
  15376.         'NO ACTION',
  15377.         'NO ACTION',
  15378.         i.name,
  15379.         object_name(r.constid),
  15380.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15381.     from
  15382.         sysobjects o1, sysobjects o2,
  15383.         syscolumns c1, syscolumns c2,
  15384.         sysreferences r, sysindexes i, sysusers u
  15385.     where   
  15386.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15387.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15388.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15389.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15390.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15391.     and o1.id = r.rkeyid
  15392.     and r.keycnt >= 8
  15393.     and o1.id = c1.id
  15394.     and c1.colid = r.rkey8
  15395.     and r.fkeyid = o2.id
  15396.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15397.     and o2.id = c2.id   
  15398.     and c2.colid = r.fkey8
  15399.     and i.id = r.rkeyid
  15400.     and i.indid = r.rkeyindid
  15401.     and u.uid = user_id()
  15402.     and ( suser_id() = 1     /* User is the System Administrator */
  15403.     or (
  15404.         o1.uid = user_id()   /* User created the object */
  15405.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15406.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15407.             from sysprotects p
  15408.             where p.id = o1.id
  15409.             /* get rows for public,current user,user's group */
  15410.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15411.             /* check for SELECT,EXECUTE privilege */
  15412.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15413.             ) = 1   /* final magic...compare Grants   */
  15414.         )
  15415.     and (
  15416.         o2.uid = user_id()   /* User created the object */
  15417.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15418.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15419.             from sysprotects p
  15420.             where p.id = o2.id
  15421.             /* get rows for public,current user,user's group */
  15422.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15423.             /* check for SELECT,EXECUTE privilege */
  15424.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15425.             ) = 1   /* final magic...compare Grants   */
  15426.         )
  15427.     )
  15428.     union all
  15429.     select
  15430.         db_name(r.rkeydbid),
  15431.         user_name(o1.uid),
  15432.         o1.name,
  15433.         c1.name,
  15434.         convert(binary(16),null),
  15435.         convert(int,null),
  15436.         db_name(r.fkeydbid),
  15437.         user_name(o2.uid),
  15438.         o2.name,
  15439.         c2.name,
  15440.         convert(binary(16),null),
  15441.         convert(int,null),
  15442.         convert(int,9),
  15443.         'NO ACTION',
  15444.         'NO ACTION',
  15445.         i.name,
  15446.         object_name(r.constid),
  15447.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15448.     from
  15449.         sysobjects o1, sysobjects o2,
  15450.         syscolumns c1, syscolumns c2,
  15451.         sysreferences r, sysindexes i, sysusers u
  15452.     where   
  15453.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15454.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15455.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15456.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15457.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15458.     and o1.id = r.rkeyid
  15459.     and r.keycnt >= 9
  15460.     and o1.id = c1.id
  15461.     and c1.colid = r.rkey9
  15462.     and r.fkeyid = o2.id
  15463.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15464.     and o2.id = c2.id   
  15465.     and c2.colid = r.fkey9
  15466.     and i.id = r.rkeyid
  15467.     and i.indid = r.rkeyindid
  15468.     and u.uid = user_id()
  15469.     and ( suser_id() = 1     /* User is the System Administrator */
  15470.     or (
  15471.         o1.uid = user_id()   /* User created the object */
  15472.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15473.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15474.             from sysprotects p
  15475.             where p.id = o1.id
  15476.             /* get rows for public,current user,user's group */
  15477.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15478.             /* check for SELECT,EXECUTE privilege */
  15479.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15480.             ) = 1   /* final magic...compare Grants   */
  15481.         )
  15482.     and (
  15483.         o2.uid = user_id()   /* User created the object */
  15484.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15485.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15486.             from sysprotects p
  15487.             where p.id = o2.id
  15488.             /* get rows for public,current user,user's group */
  15489.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15490.             /* check for SELECT,EXECUTE privilege */
  15491.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15492.             ) = 1   /* final magic...compare Grants   */
  15493.         )
  15494.     )
  15495.     union all
  15496.     select
  15497.         db_name(r.rkeydbid),
  15498.         user_name(o1.uid),
  15499.         o1.name,
  15500.         c1.name,
  15501.         convert(binary(16),null),
  15502.         convert(int,null),
  15503.         db_name(r.fkeydbid),
  15504.         user_name(o2.uid),
  15505.         o2.name,
  15506.         c2.name,
  15507.         convert(binary(16),null),
  15508.         convert(int,null),
  15509.         convert(int,10),
  15510.         'NO ACTION',
  15511.         'NO ACTION',
  15512.         i.name,
  15513.         object_name(r.constid),
  15514.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15515.     from
  15516.         sysobjects o1, sysobjects o2,
  15517.         syscolumns c1, syscolumns c2,
  15518.         sysreferences r, sysindexes i, sysusers u
  15519.     where   
  15520.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15521.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15522.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15523.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15524.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15525.     and o1.id = r.rkeyid
  15526.     and r.keycnt >= 10
  15527.     and o1.id = c1.id
  15528.     and c1.colid = r.rkey10
  15529.     and r.fkeyid = o2.id
  15530.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15531.     and o2.id = c2.id   
  15532.     and c2.colid = r.fkey10
  15533.     and i.id = r.rkeyid
  15534.     and i.indid = r.rkeyindid
  15535.     and u.uid = user_id()
  15536.     and ( suser_id() = 1     /* User is the System Administrator */
  15537.     or (
  15538.         o1.uid = user_id()   /* User created the object */
  15539.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15540.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15541.             from sysprotects p
  15542.             where p.id = o1.id
  15543.             /* get rows for public,current user,user's group */
  15544.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15545.             /* check for SELECT,EXECUTE privilege */
  15546.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15547.             ) = 1   /* final magic...compare Grants   */
  15548.         )
  15549.     and (
  15550.         o2.uid = user_id()   /* User created the object */
  15551.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15552.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15553.             from sysprotects p
  15554.             where p.id = o2.id
  15555.             /* get rows for public,current user,user's group */
  15556.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15557.             /* check for SELECT,EXECUTE privilege */
  15558.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15559.             ) = 1   /* final magic...compare Grants   */
  15560.         )
  15561.     )
  15562.     union all
  15563.     select
  15564.         db_name(r.rkeydbid),
  15565.         user_name(o1.uid),
  15566.         o1.name,
  15567.         c1.name,
  15568.         convert(binary(16),null),
  15569.         convert(int,null),
  15570.         db_name(r.fkeydbid),
  15571.         user_name(o2.uid),
  15572.         o2.name,
  15573.         c2.name,
  15574.         convert(binary(16),null),
  15575.         convert(int,null),
  15576.         convert(int,11),
  15577.         'NO ACTION',
  15578.         'NO ACTION',
  15579.         i.name,
  15580.         object_name(r.constid),
  15581.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15582.     from
  15583.         sysobjects o1, sysobjects o2,
  15584.         syscolumns c1, syscolumns c2,
  15585.         sysreferences r, sysindexes i, sysusers u
  15586.     where   
  15587.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15588.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15589.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15590.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15591.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15592.     and o1.id = r.rkeyid
  15593.     and r.keycnt >= 11
  15594.     and o1.id = c1.id
  15595.     and c1.colid = r.rkey11
  15596.     and r.fkeyid = o2.id
  15597.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15598.     and o2.id = c2.id   
  15599.     and c2.colid = r.fkey11
  15600.     and i.id = r.rkeyid
  15601.     and i.indid = r.rkeyindid
  15602.     and u.uid = user_id()
  15603.     and ( suser_id() = 1     /* User is the System Administrator */
  15604.     or (
  15605.         o1.uid = user_id()   /* User created the object */
  15606.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15607.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15608.             from sysprotects p
  15609.             where p.id = o1.id
  15610.             /* get rows for public,current user,user's group */
  15611.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15612.             /* check for SELECT,EXECUTE privilege */
  15613.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15614.             ) = 1   /* final magic...compare Grants   */
  15615.         )
  15616.     and (
  15617.         o2.uid = user_id()   /* User created the object */
  15618.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15619.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15620.             from sysprotects p
  15621.             where p.id = o2.id
  15622.             /* get rows for public,current user,user's group */
  15623.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15624.             /* check for SELECT,EXECUTE privilege */
  15625.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15626.             ) = 1   /* final magic...compare Grants   */
  15627.         )
  15628.     )
  15629.     union all
  15630.     select
  15631.         db_name(r.rkeydbid),
  15632.         user_name(o1.uid),
  15633.         o1.name,
  15634.         c1.name,
  15635.         convert(binary(16),null),
  15636.         convert(int,null),
  15637.         db_name(r.fkeydbid),
  15638.         user_name(o2.uid),
  15639.         o2.name,
  15640.         c2.name,
  15641.         convert(binary(16),null),
  15642.         convert(int,null),
  15643.         convert(int,12),
  15644.         'NO ACTION',
  15645.         'NO ACTION',
  15646.         i.name,
  15647.         object_name(r.constid),
  15648.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15649.     from
  15650.         sysobjects o1, sysobjects o2,
  15651.         syscolumns c1, syscolumns c2,
  15652.         sysreferences r, sysindexes i, sysusers u
  15653.     where   
  15654.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15655.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15656.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15657.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15658.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15659.     and o1.id = r.rkeyid
  15660.     and r.keycnt >= 12
  15661.     and o1.id = c1.id
  15662.     and c1.colid = r.rkey12
  15663.     and r.fkeyid = o2.id
  15664.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15665.     and o2.id = c2.id   
  15666.     and c2.colid = r.fkey12
  15667.     and i.id = r.rkeyid
  15668.     and i.indid = r.rkeyindid
  15669.     and u.uid = user_id()
  15670.     and ( suser_id() = 1     /* User is the System Administrator */
  15671.     or (
  15672.         o1.uid = user_id()   /* User created the object */
  15673.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15674.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15675.             from sysprotects p
  15676.             where p.id = o1.id
  15677.             /* get rows for public,current user,user's group */
  15678.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15679.             /* check for SELECT,EXECUTE privilege */
  15680.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15681.             ) = 1   /* final magic...compare Grants   */
  15682.         )
  15683.     and (
  15684.         o2.uid = user_id()   /* User created the object */
  15685.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15686.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15687.             from sysprotects p
  15688.             where p.id = o2.id
  15689.             /* get rows for public,current user,user's group */
  15690.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15691.             /* check for SELECT,EXECUTE privilege */
  15692.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15693.             ) = 1   /* final magic...compare Grants   */
  15694.         )
  15695.     )
  15696.     union all
  15697.     select
  15698.         db_name(r.rkeydbid),
  15699.         user_name(o1.uid),
  15700.         o1.name,
  15701.         c1.name,
  15702.         convert(binary(16),null),
  15703.         convert(int,null),
  15704.         db_name(r.fkeydbid),
  15705.         user_name(o2.uid),
  15706.         o2.name,
  15707.         c2.name,
  15708.         convert(binary(16),null),
  15709.         convert(int,null),
  15710.         convert(int,13),
  15711.         'NO ACTION',
  15712.         'NO ACTION',
  15713.         i.name,
  15714.         object_name(r.constid),
  15715.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15716.     from
  15717.         sysobjects o1, sysobjects o2,
  15718.         syscolumns c1, syscolumns c2,
  15719.         sysreferences r, sysindexes i, sysusers u
  15720.     where   
  15721.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15722.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15723.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15724.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15725.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15726.     and o1.id = r.rkeyid
  15727.     and r.keycnt >= 13
  15728.     and o1.id = c1.id
  15729.     and c1.colid = r.rkey13
  15730.     and r.fkeyid = o2.id
  15731.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15732.     and o2.id = c2.id   
  15733.     and c2.colid = r.fkey13
  15734.     and i.id = r.rkeyid
  15735.     and i.indid = r.rkeyindid
  15736.     and u.uid = user_id()
  15737.     and ( suser_id() = 1     /* User is the System Administrator */
  15738.     or (
  15739.         o1.uid = user_id()   /* User created the object */
  15740.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15741.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15742.             from sysprotects p
  15743.             where p.id = o1.id
  15744.             /* get rows for public,current user,user's group */
  15745.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15746.             /* check for SELECT,EXECUTE privilege */
  15747.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15748.             ) = 1   /* final magic...compare Grants   */
  15749.         )
  15750.     and (
  15751.         o2.uid = user_id()   /* User created the object */
  15752.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15753.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15754.             from sysprotects p
  15755.             where p.id = o2.id
  15756.             /* get rows for public,current user,user's group */
  15757.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15758.             /* check for SELECT,EXECUTE privilege */
  15759.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15760.             ) = 1   /* final magic...compare Grants   */
  15761.         )
  15762.     )
  15763.     union all
  15764.     select
  15765.         db_name(r.rkeydbid),
  15766.         user_name(o1.uid),
  15767.         o1.name,
  15768.         c1.name,
  15769.         convert(binary(16),null),
  15770.         convert(int,null),
  15771.         db_name(r.fkeydbid),
  15772.         user_name(o2.uid),
  15773.         o2.name,
  15774.         c2.name,
  15775.         convert(binary(16),null),
  15776.         convert(int,null),
  15777.         convert(int,14),
  15778.         'NO ACTION',
  15779.         'NO ACTION',
  15780.         i.name,
  15781.         object_name(r.constid),
  15782.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15783.     from
  15784.         sysobjects o1, sysobjects o2,
  15785.         syscolumns c1, syscolumns c2,
  15786.         sysreferences r, sysindexes i, sysusers u
  15787.     where   
  15788.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15789.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15790.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15791.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15792.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15793.     and o1.id = r.rkeyid
  15794.     and r.keycnt >= 14
  15795.     and o1.id = c1.id
  15796.     and c1.colid = r.rkey14
  15797.     and r.fkeyid = o2.id
  15798.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15799.     and o2.id = c2.id   
  15800.     and c2.colid = r.fkey14
  15801.     and i.id = r.rkeyid
  15802.     and i.indid = r.rkeyindid
  15803.     and u.uid = user_id()
  15804.     and ( suser_id() = 1     /* User is the System Administrator */
  15805.     or (
  15806.         o1.uid = user_id()   /* User created the object */
  15807.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15808.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15809.             from sysprotects p
  15810.             where p.id = o1.id
  15811.             /* get rows for public,current user,user's group */
  15812.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15813.             /* check for SELECT,EXECUTE privilege */
  15814.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15815.             ) = 1   /* final magic...compare Grants   */
  15816.         )
  15817.     and (
  15818.         o2.uid = user_id()   /* User created the object */
  15819.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15820.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15821.             from sysprotects p
  15822.             where p.id = o2.id
  15823.             /* get rows for public,current user,user's group */
  15824.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15825.             /* check for SELECT,EXECUTE privilege */
  15826.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15827.             ) = 1   /* final magic...compare Grants   */
  15828.         )
  15829.     )
  15830.     union all
  15831.     select
  15832.         db_name(r.rkeydbid),
  15833.         user_name(o1.uid),
  15834.         o1.name,
  15835.         c1.name,
  15836.         convert(binary(16),null),
  15837.         convert(int,null),
  15838.         db_name(r.fkeydbid),
  15839.         user_name(o2.uid),
  15840.         o2.name,
  15841.         c2.name,
  15842.         convert(binary(16),null),
  15843.         convert(int,null),
  15844.         convert(int,15),
  15845.         'NO ACTION',
  15846.         'NO ACTION',
  15847.         i.name,
  15848.         object_name(r.constid),
  15849.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15850.     from
  15851.         sysobjects o1, sysobjects o2,
  15852.         syscolumns c1, syscolumns c2,
  15853.         sysreferences r, sysindexes i, sysusers u
  15854.     where   
  15855.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15856.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15857.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15858.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15859.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15860.     and o1.id = r.rkeyid
  15861.     and r.keycnt >= 15
  15862.     and o1.id = c1.id
  15863.     and c1.colid = r.rkey15
  15864.     and r.fkeyid = o2.id
  15865.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15866.     and o2.id = c2.id   
  15867.     and c2.colid = r.fkey15
  15868.     and i.id = r.rkeyid
  15869.     and i.indid = r.rkeyindid
  15870.     and u.uid = user_id()
  15871.     and ( suser_id() = 1     /* User is the System Administrator */
  15872.     or (
  15873.         o1.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 = o1.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.     and (
  15885.         o2.uid = user_id()   /* User created the object */
  15886.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15887.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15888.             from sysprotects p
  15889.             where p.id = o2.id
  15890.             /* get rows for public,current user,user's group */
  15891.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15892.             /* check for SELECT,EXECUTE privilege */
  15893.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15894.             ) = 1   /* final magic...compare Grants   */
  15895.         )
  15896.     )
  15897.     union all
  15898.     select
  15899.         db_name(r.rkeydbid),
  15900.         user_name(o1.uid),
  15901.         o1.name,
  15902.         c1.name,
  15903.         convert(binary(16),null),
  15904.         convert(int,null),
  15905.         db_name(r.fkeydbid),
  15906.         user_name(o2.uid),
  15907.         o2.name,
  15908.         c2.name,
  15909.         convert(binary(16),null),
  15910.         convert(int,null),
  15911.         convert(int,16),
  15912.         'NO ACTION',
  15913.         'NO ACTION',
  15914.         i.name,
  15915.         object_name(r.constid),
  15916.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15917.     from
  15918.         sysobjects o1, sysobjects o2,
  15919.         syscolumns c1, syscolumns c2,
  15920.         sysreferences r, sysindexes i, sysusers u
  15921.     where   
  15922.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15923.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15924.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15925.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15926.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15927.     and o1.id = r.rkeyid
  15928.     and o1.id = c1.id
  15929.     and r.keycnt >= 16
  15930.     and c1.colid = r.rkey16
  15931.     and r.fkeyid = o2.id
  15932.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15933.     and o2.id = c2.id   
  15934.     and c2.colid = r.fkey16
  15935.     and i.id = r.rkeyid
  15936.     and i.indid = r.rkeyindid
  15937.     and u.uid = user_id()
  15938.     and ( suser_id() = 1     /* User is the System Administrator */
  15939.     or (
  15940.         o1.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 = o1.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.     and (
  15952.         o2.uid = user_id()   /* User created the object */
  15953.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15954.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15955.             from sysprotects p
  15956.             where p.id = o2.id
  15957.             /* get rows for public,current user,user's group */
  15958.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15959.             /* check for SELECT,EXECUTE privilege */
  15960.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15961.             ) = 1   /* final magic...compare Grants   */
  15962.         )
  15963.     )
  15964.     order by 8,9,2,3,13
  15965.     END
  15966. go
  15967. dump tran master with no_log
  15968. go
  15969. CREATE PROCEDURE sp_foreign_keys_rowset;2
  15970.     (
  15971.     @handle         int output,
  15972.     @scrollopt      int output,
  15973.     @ccopt          int output,
  15974.     @rows           int output,
  15975.     @pk_table_name      varchar(255) = null,
  15976.     @pk_table_schema    varchar(255) = null,
  15977.     @pk_table_catalog   varchar(255) = null,
  15978.     @fk_table_name      varchar(255) = null,
  15979.     @fk_table_schema    varchar(255) = null,
  15980.     @fk_table_catalog   varchar(255) = null
  15981.     )
  15982. as
  15983.     declare @ret int
  15984.     
  15985.     SET NOCOUNT ON
  15986.  
  15987.     create table #spfkeysrowset1
  15988.         (
  15989.         PK_TABLE_CATALOG    sysname not null,
  15990.         PK_TABLE_SCHEMA     sysname not null,
  15991.         PK_TABLE_NAME       sysname not null,
  15992.         PK_COLUMN_NAME      sysname not null,
  15993.         PK_COLUMN_GUID      binary(16) null,
  15994.         PK_COLUMN_PROPID    int null,
  15995.         FK_TABLE_CATALOG    sysname not null,
  15996.         FK_TABLE_SCHEMA     sysname not null,
  15997.         FK_TABLE_NAME       sysname not null,
  15998.         FK_COLUMN_NAME      sysname not null,
  15999.         FK_COLUMN_GUID      binary(16) null,
  16000.         FK_COLUMN_PROPID    int null,
  16001.         ORDINAL             int not null,
  16002.         UPDATE_RULE         sysname not null,
  16003.         DELETE_RULE         sysname not null,
  16004.         PK_NAME             sysname not null,
  16005.         FK_NAME             sysname not null,
  16006.         DEFERRABILITY       smallint not null       
  16007.         )
  16008.     BEGIN
  16009.     insert into #spfkeysrowset1
  16010.     select
  16011.         db_name(r.rkeydbid),
  16012.         user_name(o1.uid),
  16013.         o1.name,
  16014.         c1.name,
  16015.         convert(binary(16),null),
  16016.         convert(int,null),
  16017.         db_name(r.fkeydbid),
  16018.         user_name(o2.uid),
  16019.         o2.name,
  16020.         c2.name,
  16021.         convert(binary(16),null),
  16022.         convert(int,null),
  16023.         convert(int,1),
  16024.         'NO ACTION',
  16025.         'NO ACTION',
  16026.         i.name,
  16027.         object_name(r.constid),
  16028.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16029.     from
  16030.         sysobjects o1, sysobjects o2,
  16031.         syscolumns c1, syscolumns c2,
  16032.         sysreferences r, sysindexes i, sysusers u
  16033.     where   
  16034.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16035.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16036.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16037.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16038.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16039.     and o1.id = r.rkeyid
  16040.     and o1.id = c1.id
  16041.     and c1.colid = r.rkey1
  16042.     and r.fkeyid = o2.id
  16043.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16044.     and o2.id = c2.id   
  16045.     and c2.colid = r.fkey1
  16046.     and i.id = r.rkeyid
  16047.     and i.indid = r.rkeyindid
  16048.     and u.uid = user_id()
  16049.     and ( suser_id() = 1     /* User is the System Administrator */
  16050.     or (
  16051.         o1.uid = user_id()   /* User created the object */
  16052.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16053.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16054.             from sysprotects p
  16055.             where p.id = o1.id
  16056.             /* get rows for public,current user,user's group */
  16057.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16058.             /* check for SELECT,EXECUTE privilege */
  16059.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16060.             ) = 1   /* final magic...compare Grants   */
  16061.         )
  16062.     and (
  16063.         o2.uid = user_id()   /* User created the object */
  16064.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16065.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16066.             from sysprotects p
  16067.             where p.id = o2.id
  16068.             /* get rows for public,current user,user's group */
  16069.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16070.             /* check for SELECT,EXECUTE privilege */
  16071.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16072.             ) = 1   /* final magic...compare Grants   */
  16073.         )
  16074.     )   
  16075.     union all
  16076.     select
  16077.         db_name(r.rkeydbid),
  16078.         user_name(o1.uid),
  16079.         o1.name,
  16080.         c1.name,
  16081.         convert(binary(16),null),
  16082.         convert(int,null),
  16083.         db_name(r.fkeydbid),
  16084.         user_name(o2.uid),
  16085.         o2.name,
  16086.         c2.name,
  16087.         convert(binary(16),null),
  16088.         convert(int,null),
  16089.         convert(int,2),
  16090.         'NO ACTION',
  16091.         'NO ACTION',
  16092.         i.name,
  16093.         object_name(r.constid),
  16094.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16095.     from
  16096.         sysobjects o1, sysobjects o2,
  16097.         syscolumns c1, syscolumns c2,
  16098.         sysreferences r, sysindexes i, sysusers u
  16099.     where   
  16100.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16101.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16102.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16103.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16104.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16105.     and o1.id = r.rkeyid
  16106.     and r.keycnt >= 2
  16107.     and o1.id = c1.id
  16108.     and c1.colid = r.rkey2
  16109.     and r.fkeyid = o2.id
  16110.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16111.     and o2.id = c2.id   
  16112.     and c2.colid = r.fkey2
  16113.     and i.id = r.rkeyid
  16114.     and i.indid = r.rkeyindid
  16115.     and u.uid = user_id()
  16116.     and ( suser_id() = 1     /* User is the System Administrator */
  16117.     or (
  16118.         o1.uid = user_id()   /* User created the object */
  16119.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16120.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16121.             from sysprotects p
  16122.             where p.id = o1.id
  16123.             /* get rows for public,current user,user's group */
  16124.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16125.             /* check for SELECT,EXECUTE privilege */
  16126.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16127.             ) = 1   /* final magic...compare Grants   */
  16128.         )
  16129.     and (
  16130.         o2.uid = user_id()   /* User created the object */
  16131.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16132.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16133.             from sysprotects p
  16134.             where p.id = o2.id
  16135.             /* get rows for public,current user,user's group */
  16136.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16137.             /* check for SELECT,EXECUTE privilege */
  16138.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16139.             ) = 1   /* final magic...compare Grants   */
  16140.         )
  16141.     )   
  16142.     union all
  16143.     select
  16144.         db_name(r.rkeydbid),
  16145.         user_name(o1.uid),
  16146.         o1.name,
  16147.         c1.name,
  16148.         convert(binary(16),null),
  16149.         convert(int,null),
  16150.         db_name(r.fkeydbid),
  16151.         user_name(o2.uid),
  16152.         o2.name,
  16153.         c2.name,
  16154.         convert(binary(16),null),
  16155.         convert(int,null),
  16156.         convert(int,3),
  16157.         'NO ACTION',
  16158.         'NO ACTION',
  16159.         i.name,
  16160.         object_name(r.constid),
  16161.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16162.     from
  16163.         sysobjects o1, sysobjects o2,
  16164.         syscolumns c1, syscolumns c2,
  16165.         sysreferences r, sysindexes i, sysusers u
  16166.     where   
  16167.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16168.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16169.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16170.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16171.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16172.     and o1.id = r.rkeyid
  16173.     and r.keycnt >= 3
  16174.     and o1.id = c1.id
  16175.     and c1.colid = r.rkey3
  16176.     and r.fkeyid = o2.id
  16177.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16178.     and o2.id = c2.id   
  16179.     and c2.colid = r.fkey3
  16180.     and i.id = r.rkeyid
  16181.     and i.indid = r.rkeyindid
  16182.     and u.uid = user_id()
  16183.     and ( suser_id() = 1     /* User is the System Administrator */
  16184.     or (
  16185.         o1.uid = user_id()   /* User created the object */
  16186.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16187.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16188.             from sysprotects p
  16189.             where p.id = o1.id
  16190.             /* get rows for public,current user,user's group */
  16191.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16192.             /* check for SELECT,EXECUTE privilege */
  16193.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16194.             ) = 1   /* final magic...compare Grants   */
  16195.         )
  16196.     and (
  16197.         o2.uid = user_id()   /* User created the object */
  16198.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16199.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16200.             from sysprotects p
  16201.             where p.id = o2.id
  16202.             /* get rows for public,current user,user's group */
  16203.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16204.             /* check for SELECT,EXECUTE privilege */
  16205.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16206.             ) = 1   /* final magic...compare Grants   */
  16207.         )
  16208.     )   
  16209.     union all
  16210.     select
  16211.         db_name(r.rkeydbid),
  16212.         user_name(o1.uid),
  16213.         o1.name,
  16214.         c1.name,
  16215.         convert(binary(16),null),
  16216.         convert(int,null),
  16217.         db_name(r.fkeydbid),
  16218.         user_name(o2.uid),
  16219.         o2.name,
  16220.         c2.name,
  16221.         convert(binary(16),null),
  16222.         convert(int,null),
  16223.         convert(int,4),
  16224.         'NO ACTION',
  16225.         'NO ACTION',
  16226.         i.name,
  16227.         object_name(r.constid),
  16228.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16229.     from
  16230.         sysobjects o1, sysobjects o2,
  16231.         syscolumns c1, syscolumns c2,
  16232.         sysreferences r, sysindexes i, sysusers u
  16233.     where   
  16234.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16235.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16236.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16237.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16238.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16239.     and o1.id = r.rkeyid
  16240.     and r.keycnt >= 4
  16241.     and o1.id = c1.id
  16242.     and c1.colid = r.rkey4
  16243.     and r.fkeyid = o2.id
  16244.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16245.     and o2.id = c2.id   
  16246.     and c2.colid = r.fkey4
  16247.     and i.id = r.rkeyid
  16248.     and i.indid = r.rkeyindid
  16249.     and u.uid = user_id()
  16250.     and ( suser_id() = 1     /* User is the System Administrator */
  16251.     or (
  16252.         o1.uid = user_id()   /* User created the object */
  16253.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16254.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16255.             from sysprotects p
  16256.             where p.id = o1.id
  16257.             /* get rows for public,current user,user's group */
  16258.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16259.             /* check for SELECT,EXECUTE privilege */
  16260.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16261.             ) = 1   /* final magic...compare Grants   */
  16262.         )
  16263.     and (
  16264.         o2.uid = user_id()   /* User created the object */
  16265.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16266.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16267.             from sysprotects p
  16268.             where p.id = o2.id
  16269.             /* get rows for public,current user,user's group */
  16270.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16271.             /* check for SELECT,EXECUTE privilege */
  16272.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16273.             ) = 1   /* final magic...compare Grants   */
  16274.         )
  16275.     )   
  16276.     union all
  16277.     select
  16278.         db_name(r.rkeydbid),
  16279.         user_name(o1.uid),
  16280.         o1.name,
  16281.         c1.name,
  16282.         convert(binary(16),null),
  16283.         convert(int,null),
  16284.         db_name(r.fkeydbid),
  16285.         user_name(o2.uid),
  16286.         o2.name,
  16287.         c2.name,
  16288.         convert(binary(16),null),
  16289.         convert(int,null),
  16290.         convert(int,5),
  16291.         'NO ACTION',
  16292.         'NO ACTION',
  16293.         i.name,
  16294.         object_name(r.constid),
  16295.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16296.     from
  16297.         sysobjects o1, sysobjects o2,
  16298.         syscolumns c1, syscolumns c2,
  16299.         sysreferences r, sysindexes i, sysusers u
  16300.     where   
  16301.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16302.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16303.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16304.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16305.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16306.     and o1.id = r.rkeyid
  16307.     and r.keycnt >= 5
  16308.     and o1.id = c1.id
  16309.     and c1.colid = r.rkey5
  16310.     and r.fkeyid = o2.id
  16311.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16312.     and o2.id = c2.id   
  16313.     and c2.colid = r.fkey5
  16314.     and i.id = r.rkeyid
  16315.     and i.indid = r.rkeyindid
  16316.     and u.uid = user_id()
  16317.     and ( suser_id() = 1     /* User is the System Administrator */
  16318.     or (
  16319.         o1.uid = user_id()   /* User created the object */
  16320.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16321.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16322.             from sysprotects p
  16323.             where p.id = o1.id
  16324.             /* get rows for public,current user,user's group */
  16325.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16326.             /* check for SELECT,EXECUTE privilege */
  16327.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16328.             ) = 1   /* final magic...compare Grants   */
  16329.         )
  16330.     and (
  16331.         o2.uid = user_id()   /* User created the object */
  16332.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16333.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16334.             from sysprotects p
  16335.             where p.id = o2.id
  16336.             /* get rows for public,current user,user's group */
  16337.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16338.             /* check for SELECT,EXECUTE privilege */
  16339.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16340.             ) = 1   /* final magic...compare Grants   */
  16341.         )
  16342.     )   
  16343.     union all
  16344.     select
  16345.         db_name(r.rkeydbid),
  16346.         user_name(o1.uid),
  16347.         o1.name,
  16348.         c1.name,
  16349.         convert(binary(16),null),
  16350.         convert(int,null),
  16351.         db_name(r.fkeydbid),
  16352.         user_name(o2.uid),
  16353.         o2.name,
  16354.         c2.name,
  16355.         convert(binary(16),null),
  16356.         convert(int,null),
  16357.         convert(int,6),
  16358.         'NO ACTION',
  16359.         'NO ACTION',
  16360.         i.name,
  16361.         object_name(r.constid),
  16362.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16363.     from
  16364.         sysobjects o1, sysobjects o2,
  16365.         syscolumns c1, syscolumns c2,
  16366.         sysreferences r, sysindexes i, sysusers u
  16367.     where   
  16368.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16369.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16370.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16371.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16372.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16373.     and o1.id = r.rkeyid
  16374.     and r.keycnt >= 6
  16375.     and o1.id = c1.id
  16376.     and c1.colid = r.rkey6
  16377.     and r.fkeyid = o2.id
  16378.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16379.     and o2.id = c2.id   
  16380.     and c2.colid = r.fkey6
  16381.     and i.id = r.rkeyid
  16382.     and i.indid = r.rkeyindid
  16383.     and u.uid = user_id()
  16384.     and ( suser_id() = 1     /* User is the System Administrator */
  16385.     or (
  16386.         o1.uid = user_id()   /* User created the object */
  16387.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16388.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16389.             from sysprotects p
  16390.             where p.id = o1.id
  16391.             /* get rows for public,current user,user's group */
  16392.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16393.             /* check for SELECT,EXECUTE privilege */
  16394.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16395.             ) = 1   /* final magic...compare Grants   */
  16396.         )
  16397.     and (
  16398.         o2.uid = user_id()   /* User created the object */
  16399.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16400.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16401.             from sysprotects p
  16402.             where p.id = o2.id
  16403.             /* get rows for public,current user,user's group */
  16404.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16405.             /* check for SELECT,EXECUTE privilege */
  16406.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16407.             ) = 1   /* final magic...compare Grants   */
  16408.         )
  16409.     )   
  16410.     union all
  16411.     select
  16412.         db_name(r.rkeydbid),
  16413.         user_name(o1.uid),
  16414.         o1.name,
  16415.         c1.name,
  16416.         convert(binary(16),null),
  16417.         convert(int,null),
  16418.         db_name(r.fkeydbid),
  16419.         user_name(o2.uid),
  16420.         o2.name,
  16421.         c2.name,
  16422.         convert(binary(16),null),
  16423.         convert(int,null),
  16424.         convert(int,7),
  16425.         'NO ACTION',
  16426.         'NO ACTION',
  16427.         i.name,
  16428.         object_name(r.constid),
  16429.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16430.     from
  16431.         sysobjects o1, sysobjects o2,
  16432.         syscolumns c1, syscolumns c2,
  16433.         sysreferences r, sysindexes i, sysusers u
  16434.     where   
  16435.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16436.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16437.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16438.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16439.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16440.     and o1.id = r.rkeyid
  16441.     and r.keycnt >= 7
  16442.     and o1.id = c1.id
  16443.     and c1.colid = r.rkey7
  16444.     and r.fkeyid = o2.id
  16445.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16446.     and o2.id = c2.id   
  16447.     and c2.colid = r.fkey7
  16448.     and i.id = r.rkeyid
  16449.     and i.indid = r.rkeyindid
  16450.     and u.uid = user_id()
  16451.     and ( suser_id() = 1     /* User is the System Administrator */
  16452.     or (
  16453.         o1.uid = user_id()   /* User created the object */
  16454.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16455.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16456.             from sysprotects p
  16457.             where p.id = o1.id
  16458.             /* get rows for public,current user,user's group */
  16459.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16460.             /* check for SELECT,EXECUTE privilege */
  16461.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16462.             ) = 1   /* final magic...compare Grants   */
  16463.         )
  16464.     and (
  16465.         o2.uid = user_id()   /* User created the object */
  16466.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16467.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16468.             from sysprotects p
  16469.             where p.id = o2.id
  16470.             /* get rows for public,current user,user's group */
  16471.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16472.             /* check for SELECT,EXECUTE privilege */
  16473.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16474.             ) = 1   /* final magic...compare Grants   */
  16475.         )
  16476.     )   
  16477.     union all
  16478.     select
  16479.         db_name(r.rkeydbid),
  16480.         user_name(o1.uid),
  16481.         o1.name,
  16482.         c1.name,
  16483.         convert(binary(16),null),
  16484.         convert(int,null),
  16485.         db_name(r.fkeydbid),
  16486.         user_name(o2.uid),
  16487.         o2.name,
  16488.         c2.name,
  16489.         convert(binary(16),null),
  16490.         convert(int,null),
  16491.         convert(int,8),
  16492.         'NO ACTION',
  16493.         'NO ACTION',
  16494.         i.name,
  16495.         object_name(r.constid),
  16496.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16497.     from
  16498.         sysobjects o1, sysobjects o2,
  16499.         syscolumns c1, syscolumns c2,
  16500.         sysreferences r, sysindexes i, sysusers u
  16501.     where   
  16502.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16503.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16504.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16505.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16506.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16507.     and o1.id = r.rkeyid
  16508.     and r.keycnt >= 8
  16509.     and o1.id = c1.id
  16510.     and c1.colid = r.rkey8
  16511.     and r.fkeyid = o2.id
  16512.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16513.     and o2.id = c2.id   
  16514.     and c2.colid = r.fkey8
  16515.     and i.id = r.rkeyid
  16516.     and i.indid = r.rkeyindid
  16517.     and u.uid = user_id()
  16518.     and ( suser_id() = 1     /* User is the System Administrator */
  16519.     or (
  16520.         o1.uid = user_id()   /* User created the object */
  16521.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16522.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16523.             from sysprotects p
  16524.             where p.id = o1.id
  16525.             /* get rows for public,current user,user's group */
  16526.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16527.             /* check for SELECT,EXECUTE privilege */
  16528.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16529.             ) = 1   /* final magic...compare Grants   */
  16530.         )
  16531.     and (
  16532.         o2.uid = user_id()   /* User created the object */
  16533.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16534.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16535.             from sysprotects p
  16536.             where p.id = o2.id
  16537.             /* get rows for public,current user,user's group */
  16538.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16539.             /* check for SELECT,EXECUTE privilege */
  16540.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16541.             ) = 1   /* final magic...compare Grants   */
  16542.         )
  16543.     )   
  16544.     union all
  16545.     select
  16546.         db_name(r.rkeydbid),
  16547.         user_name(o1.uid),
  16548.         o1.name,
  16549.         c1.name,
  16550.         convert(binary(16),null),
  16551.         convert(int,null),
  16552.         db_name(r.fkeydbid),
  16553.         user_name(o2.uid),
  16554.         o2.name,
  16555.         c2.name,
  16556.         convert(binary(16),null),
  16557.         convert(int,null),
  16558.         convert(int,9),
  16559.         'NO ACTION',
  16560.         'NO ACTION',
  16561.         i.name,
  16562.         object_name(r.constid),
  16563.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16564.     from
  16565.         sysobjects o1, sysobjects o2,
  16566.         syscolumns c1, syscolumns c2,
  16567.         sysreferences r, sysindexes i, sysusers u
  16568.     where   
  16569.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16570.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16571.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16572.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16573.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16574.     and o1.id = r.rkeyid
  16575.     and r.keycnt >= 9
  16576.     and o1.id = c1.id
  16577.     and c1.colid = r.rkey9
  16578.     and r.fkeyid = o2.id
  16579.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16580.     and o2.id = c2.id   
  16581.     and c2.colid = r.fkey9
  16582.     and i.id = r.rkeyid
  16583.     and i.indid = r.rkeyindid
  16584.     and u.uid = user_id()
  16585.     and ( suser_id() = 1     /* User is the System Administrator */
  16586.     or (
  16587.         o1.uid = user_id()   /* User created the object */
  16588.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16589.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16590.             from sysprotects p
  16591.             where p.id = o1.id
  16592.             /* get rows for public,current user,user's group */
  16593.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16594.             /* check for SELECT,EXECUTE privilege */
  16595.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16596.             ) = 1   /* final magic...compare Grants   */
  16597.         )
  16598.     and (
  16599.         o2.uid = user_id()   /* User created the object */
  16600.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16601.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16602.             from sysprotects p
  16603.             where p.id = o2.id
  16604.             /* get rows for public,current user,user's group */
  16605.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16606.             /* check for SELECT,EXECUTE privilege */
  16607.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16608.             ) = 1   /* final magic...compare Grants   */
  16609.         )
  16610.     )   
  16611.     union all
  16612.     select
  16613.         db_name(r.rkeydbid),
  16614.         user_name(o1.uid),
  16615.         o1.name,
  16616.         c1.name,
  16617.         convert(binary(16),null),
  16618.         convert(int,null),
  16619.         db_name(r.fkeydbid),
  16620.         user_name(o2.uid),
  16621.         o2.name,
  16622.         c2.name,
  16623.         convert(binary(16),null),
  16624.         convert(int,null),
  16625.         convert(int,10),
  16626.         'NO ACTION',
  16627.         'NO ACTION',
  16628.         i.name,
  16629.         object_name(r.constid),
  16630.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16631.     from
  16632.         sysobjects o1, sysobjects o2,
  16633.         syscolumns c1, syscolumns c2,
  16634.         sysreferences r, sysindexes i, sysusers u
  16635.     where   
  16636.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16637.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16638.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16639.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16640.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16641.     and o1.id = r.rkeyid
  16642.     and r.keycnt >= 10
  16643.     and o1.id = c1.id
  16644.     and c1.colid = r.rkey10
  16645.     and r.fkeyid = o2.id
  16646.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16647.     and o2.id = c2.id   
  16648.     and c2.colid = r.fkey10
  16649.     and i.id = r.rkeyid
  16650.     and i.indid = r.rkeyindid
  16651.     and u.uid = user_id()
  16652.     and ( suser_id() = 1     /* User is the System Administrator */
  16653.     or (
  16654.         o1.uid = user_id()   /* User created the object */
  16655.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16656.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16657.             from sysprotects p
  16658.             where p.id = o1.id
  16659.             /* get rows for public,current user,user's group */
  16660.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16661.             /* check for SELECT,EXECUTE privilege */
  16662.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16663.             ) = 1   /* final magic...compare Grants   */
  16664.         )
  16665.     and (
  16666.         o2.uid = user_id()   /* User created the object */
  16667.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16668.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16669.             from sysprotects p
  16670.             where p.id = o2.id
  16671.             /* get rows for public,current user,user's group */
  16672.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16673.             /* check for SELECT,EXECUTE privilege */
  16674.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16675.             ) = 1   /* final magic...compare Grants   */
  16676.         )
  16677.     )   
  16678.     union all
  16679.     select
  16680.         db_name(r.rkeydbid),
  16681.         user_name(o1.uid),
  16682.         o1.name,
  16683.         c1.name,
  16684.         convert(binary(16),null),
  16685.         convert(int,null),
  16686.         db_name(r.fkeydbid),
  16687.         user_name(o2.uid),
  16688.         o2.name,
  16689.         c2.name,
  16690.         convert(binary(16),null),
  16691.         convert(int,null),
  16692.         convert(int,11),
  16693.         'NO ACTION',
  16694.         'NO ACTION',
  16695.         i.name,
  16696.         object_name(r.constid),
  16697.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16698.     from
  16699.         sysobjects o1, sysobjects o2,
  16700.         syscolumns c1, syscolumns c2,
  16701.         sysreferences r, sysindexes i, sysusers u
  16702.     where   
  16703.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16704.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16705.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16706.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16707.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16708.     and o1.id = r.rkeyid
  16709.     and r.keycnt >= 11
  16710.     and o1.id = c1.id
  16711.     and c1.colid = r.rkey11
  16712.     and r.fkeyid = o2.id
  16713.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16714.     and o2.id = c2.id   
  16715.     and c2.colid = r.fkey11
  16716.     and i.id = r.rkeyid
  16717.     and i.indid = r.rkeyindid
  16718.     and u.uid = user_id()
  16719.     and ( suser_id() = 1     /* User is the System Administrator */
  16720.     or (
  16721.         o1.uid = user_id()   /* User created the object */
  16722.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16723.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16724.             from sysprotects p
  16725.             where p.id = o1.id
  16726.             /* get rows for public,current user,user's group */
  16727.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16728.             /* check for SELECT,EXECUTE privilege */
  16729.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16730.             ) = 1   /* final magic...compare Grants   */
  16731.         )
  16732.     and (
  16733.         o2.uid = user_id()   /* User created the object */
  16734.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16735.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16736.             from sysprotects p
  16737.             where p.id = o2.id
  16738.             /* get rows for public,current user,user's group */
  16739.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16740.             /* check for SELECT,EXECUTE privilege */
  16741.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16742.             ) = 1   /* final magic...compare Grants   */
  16743.         )
  16744.     )   
  16745.     union all
  16746.     select
  16747.         db_name(r.rkeydbid),
  16748.         user_name(o1.uid),
  16749.         o1.name,
  16750.         c1.name,
  16751.         convert(binary(16),null),
  16752.         convert(int,null),
  16753.         db_name(r.fkeydbid),
  16754.         user_name(o2.uid),
  16755.         o2.name,
  16756.         c2.name,
  16757.         convert(binary(16),null),
  16758.         convert(int,null),
  16759.         convert(int,12),
  16760.         'NO ACTION',
  16761.         'NO ACTION',
  16762.         i.name,
  16763.         object_name(r.constid),
  16764.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16765.     from
  16766.         sysobjects o1, sysobjects o2,
  16767.         syscolumns c1, syscolumns c2,
  16768.         sysreferences r, sysindexes i, sysusers u
  16769.     where   
  16770.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16771.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16772.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16773.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16774.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16775.     and o1.id = r.rkeyid
  16776.     and r.keycnt >= 12
  16777.     and o1.id = c1.id
  16778.     and c1.colid = r.rkey12
  16779.     and r.fkeyid = o2.id
  16780.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16781.     and o2.id = c2.id   
  16782.     and c2.colid = r.fkey12
  16783.     and i.id = r.rkeyid
  16784.     and i.indid = r.rkeyindid
  16785.     and u.uid = user_id()
  16786.     and ( suser_id() = 1     /* User is the System Administrator */
  16787.     or (
  16788.         o1.uid = user_id()   /* User created the object */
  16789.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16790.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16791.             from sysprotects p
  16792.             where p.id = o1.id
  16793.             /* get rows for public,current user,user's group */
  16794.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16795.             /* check for SELECT,EXECUTE privilege */
  16796.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16797.             ) = 1   /* final magic...compare Grants   */
  16798.         )
  16799.     and (
  16800.         o2.uid = user_id()   /* User created the object */
  16801.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16802.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16803.             from sysprotects p
  16804.             where p.id = o2.id
  16805.             /* get rows for public,current user,user's group */
  16806.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16807.             /* check for SELECT,EXECUTE privilege */
  16808.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16809.             ) = 1   /* final magic...compare Grants   */
  16810.         )
  16811.     )   
  16812.     union all
  16813.     select
  16814.         db_name(r.rkeydbid),
  16815.         user_name(o1.uid),
  16816.         o1.name,
  16817.         c1.name,
  16818.         convert(binary(16),null),
  16819.         convert(int,null),
  16820.         db_name(r.fkeydbid),
  16821.         user_name(o2.uid),
  16822.         o2.name,
  16823.         c2.name,
  16824.         convert(binary(16),null),
  16825.         convert(int,null),
  16826.         convert(int,13),
  16827.         'NO ACTION',
  16828.         'NO ACTION',
  16829.         i.name,
  16830.         object_name(r.constid),
  16831.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16832.     from
  16833.         sysobjects o1, sysobjects o2,
  16834.         syscolumns c1, syscolumns c2,
  16835.         sysreferences r, sysindexes i, sysusers u
  16836.     where   
  16837.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16838.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16839.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16840.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16841.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16842.     and o1.id = r.rkeyid
  16843.     and r.keycnt >= 13
  16844.     and o1.id = c1.id
  16845.     and c1.colid = r.rkey13
  16846.     and r.fkeyid = o2.id
  16847.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16848.     and o2.id = c2.id   
  16849.     and c2.colid = r.fkey13
  16850.     and i.id = r.rkeyid
  16851.     and i.indid = r.rkeyindid
  16852.     and u.uid = user_id()
  16853.     and ( suser_id() = 1     /* User is the System Administrator */
  16854.     or (
  16855.         o1.uid = user_id()   /* User created the object */
  16856.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16857.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16858.             from sysprotects p
  16859.             where p.id = o1.id
  16860.             /* get rows for public,current user,user's group */
  16861.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16862.             /* check for SELECT,EXECUTE privilege */
  16863.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16864.             ) = 1   /* final magic...compare Grants   */
  16865.         )
  16866.     and (
  16867.         o2.uid = user_id()   /* User created the object */
  16868.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16869.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16870.             from sysprotects p
  16871.             where p.id = o2.id
  16872.             /* get rows for public,current user,user's group */
  16873.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16874.             /* check for SELECT,EXECUTE privilege */
  16875.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16876.             ) = 1   /* final magic...compare Grants   */
  16877.         )
  16878.     )   
  16879.     union all
  16880.     select
  16881.         db_name(r.rkeydbid),
  16882.         user_name(o1.uid),
  16883.         o1.name,
  16884.         c1.name,
  16885.         convert(binary(16),null),
  16886.         convert(int,null),
  16887.         db_name(r.fkeydbid),
  16888.         user_name(o2.uid),
  16889.         o2.name,
  16890.         c2.name,
  16891.         convert(binary(16),null),
  16892.         convert(int,null),
  16893.         convert(int,14),
  16894.         'NO ACTION',
  16895.         'NO ACTION',
  16896.         i.name,
  16897.         object_name(r.constid),
  16898.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16899.     from
  16900.         sysobjects o1, sysobjects o2,
  16901.         syscolumns c1, syscolumns c2,
  16902.         sysreferences r, sysindexes i, sysusers u
  16903.     where   
  16904.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16905.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16906.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16907.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16908.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16909.     and o1.id = r.rkeyid
  16910.     and r.keycnt >= 14
  16911.     and o1.id = c1.id
  16912.     and c1.colid = r.rkey14
  16913.     and r.fkeyid = o2.id
  16914.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16915.     and o2.id = c2.id   
  16916.     and c2.colid = r.fkey14
  16917.     and i.id = r.rkeyid
  16918.     and i.indid = r.rkeyindid
  16919.     and u.uid = user_id()
  16920.     and ( suser_id() = 1     /* User is the System Administrator */
  16921.     or (
  16922.         o1.uid = user_id()   /* User created the object */
  16923.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16924.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16925.             from sysprotects p
  16926.             where p.id = o1.id
  16927.             /* get rows for public,current user,user's group */
  16928.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16929.             /* check for SELECT,EXECUTE privilege */
  16930.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16931.             ) = 1   /* final magic...compare Grants   */
  16932.         )
  16933.     and (
  16934.         o2.uid = user_id()   /* User created the object */
  16935.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16936.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16937.             from sysprotects p
  16938.             where p.id = o2.id
  16939.             /* get rows for public,current user,user's group */
  16940.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16941.             /* check for SELECT,EXECUTE privilege */
  16942.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16943.             ) = 1   /* final magic...compare Grants   */
  16944.         )
  16945.     )   
  16946.     union all
  16947.     select
  16948.         db_name(r.rkeydbid),
  16949.         user_name(o1.uid),
  16950.         o1.name,
  16951.         c1.name,
  16952.         convert(binary(16),null),
  16953.         convert(int,null),
  16954.         db_name(r.fkeydbid),
  16955.         user_name(o2.uid),
  16956.         o2.name,
  16957.         c2.name,
  16958.         convert(binary(16),null),
  16959.         convert(int,null),
  16960.         convert(int,15),
  16961.         'NO ACTION',
  16962.         'NO ACTION',
  16963.         i.name,
  16964.         object_name(r.constid),
  16965.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16966.     from
  16967.         sysobjects o1, sysobjects o2,
  16968.         syscolumns c1, syscolumns c2,
  16969.         sysreferences r, sysindexes i, sysusers u
  16970.     where   
  16971.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16972.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16973.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16974.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16975.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16976.     and o1.id = r.rkeyid
  16977.     and r.keycnt >= 15
  16978.     and o1.id = c1.id
  16979.     and c1.colid = r.rkey15
  16980.     and r.fkeyid = o2.id
  16981.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16982.     and o2.id = c2.id   
  16983.     and c2.colid = r.fkey15
  16984.     and i.id = r.rkeyid
  16985.     and i.indid = r.rkeyindid
  16986.     and u.uid = user_id()
  16987.     and ( suser_id() = 1     /* User is the System Administrator */
  16988.     or (
  16989.         o1.uid = user_id()   /* User created the object */
  16990.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16991.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16992.             from sysprotects p
  16993.             where p.id = o1.id
  16994.             /* get rows for public,current user,user's group */
  16995.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16996.             /* check for SELECT,EXECUTE privilege */
  16997.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16998.             ) = 1   /* final magic...compare Grants   */
  16999.         )
  17000.     and (
  17001.         o2.uid = user_id()   /* User created the object */
  17002.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17003.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17004.             from sysprotects p
  17005.             where p.id = o2.id
  17006.             /* get rows for public,current user,user's group */
  17007.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17008.             /* check for SELECT,EXECUTE privilege */
  17009.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17010.             ) = 1   /* final magic...compare Grants   */
  17011.         )
  17012.     )   
  17013.     union all
  17014.     select
  17015.         db_name(r.rkeydbid),
  17016.         user_name(o1.uid),
  17017.         o1.name,
  17018.         c1.name,
  17019.         convert(binary(16),null),
  17020.         convert(int,null),
  17021.         db_name(r.fkeydbid),
  17022.         user_name(o2.uid),
  17023.         o2.name,
  17024.         c2.name,
  17025.         convert(binary(16),null),
  17026.         convert(int,null),
  17027.         convert(int,16),
  17028.         'NO ACTION',
  17029.         'NO ACTION',
  17030.         i.name,
  17031.         object_name(r.constid),
  17032.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17033.     from
  17034.         sysobjects o1, sysobjects o2,
  17035.         syscolumns c1, syscolumns c2,
  17036.         sysreferences r, sysindexes i, sysusers u
  17037.     where   
  17038.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17039.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17040.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17041.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17042.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17043.     and o1.id = r.rkeyid
  17044.     and o1.id = c1.id
  17045.     and r.keycnt >= 16
  17046.     and c1.colid = r.rkey16
  17047.     and r.fkeyid = o2.id
  17048.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17049.     and o2.id = c2.id   
  17050.     and c2.colid = r.fkey16
  17051.     and i.id = r.rkeyid
  17052.     and i.indid = r.rkeyindid
  17053.     and u.uid = user_id()
  17054.     and ( suser_id() = 1     /* User is the System Administrator */
  17055.     or (
  17056.         o1.uid = user_id()   /* User created the object */
  17057.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17058.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17059.             from sysprotects p
  17060.             where p.id = o1.id
  17061.             /* get rows for public,current user,user's group */
  17062.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17063.             /* check for SELECT,EXECUTE privilege */
  17064.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17065.             ) = 1   /* final magic...compare Grants   */
  17066.         )
  17067.     and (
  17068.         o2.uid = user_id()   /* User created the object */
  17069.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17070.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17071.             from sysprotects p
  17072.             where p.id = o2.id
  17073.             /* get rows for public,current user,user's group */
  17074.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17075.             /* check for SELECT,EXECUTE privilege */
  17076.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17077.             ) = 1   /* final magic...compare Grants   */
  17078.         )
  17079.     )
  17080.     order by 8,9,2,3,13
  17081.     END
  17082.  
  17083.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  17084.         @scrollopt output, @ccopt output, @rows output
  17085.  
  17086.     drop table #spfkeysrowset1
  17087.     return isnull(@ret,0)
  17088. go
  17089. dump tran master with no_log
  17090. go
  17091. CREATE PROCEDURE sp_foreign_keys_rowset;3
  17092. as
  17093.     select
  17094.         PK_TABLE_CATALOG    = convert(sysname,' '),
  17095.         PK_TABLE_SCHEMA     = convert(sysname,' '),
  17096.         PK_TABLE_NAME       = convert(sysname,' '),
  17097.         PK_COLUMN_NAME      = convert(sysname,' '),
  17098.         PK_COLUMN_GUID      = convert(binary(16),null),
  17099.         PK_COLUMN_PROPID    = convert(int,null),
  17100.         FK_TABLE_CATALOG    = convert(sysname,' '),
  17101.         FK_TABLE_SCHEMA     = convert(sysname,' '),
  17102.         FK_TABLE_NAME       = convert(sysname,' '),
  17103.         FK_COLUMN_NAME      = convert(sysname,' '),
  17104.         FK_COLUMN_GUID      = convert(binary(16),null),
  17105.         FK_COLUMN_PROPID    = convert(int,null),
  17106.         ORDINAL             = convert(int,1),
  17107.         UPDATE_RULE         = 'NO ACTION',
  17108.         DELETE_RULE         = 'NO ACTION',
  17109.         PK_NAME             = convert(sysname, ' '),
  17110.         FK_NAME             = convert(sysname, ' '),
  17111.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17112.     where   1=0
  17113. go
  17114. dump tran master with no_log
  17115. go
  17116.  
  17117.  
  17118. if (charindex('7.00', @@version) = 0 and
  17119.     charindex('8.00', @@version) = 0)
  17120. begin
  17121.     print ''
  17122.     print ''
  17123.     print 'Warning:'
  17124.     print 'you are installing the stored procedures '
  17125.     print 'on a pre 8.0 SQL Server.'
  17126.     print 'Ignore the following errors.'
  17127. end
  17128. else
  17129.     drop proc sp_foreign_keys_rowset
  17130. go
  17131.  
  17132.  
  17133. /*  Procedure for 8.0 server */
  17134. CREATE PROCEDURE sp_foreign_keys_rowset
  17135.     (
  17136.     @pk_table_name      sysname,
  17137.     @pk_table_schema    sysname = null,
  17138.     @fk_table_name      sysname = null,
  17139.     @fk_table_schema    sysname = null,
  17140.     @fk_table_catalog   sysname = null
  17141.     )
  17142. as
  17143.     select
  17144.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17145.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17146.         PK_TABLE_NAME       = o1.name,
  17147.         PK_COLUMN_NAME      = c1.name,
  17148.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17149.         PK_COLUMN_PROPID    = convert(int,null),
  17150.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17151.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17152.         FK_TABLE_NAME       = o2.name,
  17153.         FK_COLUMN_NAME      = c2.name,
  17154.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17155.         FK_COLUMN_PROPID    = convert(int,null),
  17156.         ORDINAL             = convert(int,1),
  17157.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17158.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17159.         PK_NAME             = i.name,
  17160.         FK_NAME             = object_name(r.constid),
  17161.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17162.     from
  17163.         sysobjects o1, sysobjects o2,
  17164.         syscolumns c1, syscolumns c2,
  17165.         sysreferences r, sysindexes i
  17166.     where   
  17167.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17168.     and o1.name = @pk_table_name
  17169.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17170.     and o1.id = r.rkeyid
  17171.     and o1.id = c1.id
  17172.     and c1.colid = r.rkey1
  17173.     and r.fkeyid = o2.id
  17174.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17175.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17176.     and o2.id = c2.id   
  17177.     and c2.colid = r.fkey1
  17178.     and permissions(o1.id) <> 0
  17179.     and permissions(o2.id) <> 0
  17180.     and i.id = r.rkeyid
  17181.     and i.indid = r.rkeyindid
  17182.     union all
  17183.     select
  17184.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17185.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17186.         PK_TABLE_NAME       = o1.name,
  17187.         PK_COLUMN_NAME      = c1.name,
  17188.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17189.         PK_COLUMN_PROPID    = convert(int,null),
  17190.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17191.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17192.         FK_TABLE_NAME       = o2.name,
  17193.         FK_COLUMN_NAME      = c2.name,
  17194.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17195.         FK_COLUMN_PROPID    = convert(int,null),
  17196.         ORDINAL             = convert(int,2),
  17197.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17198.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17199.         PK_NAME             = i.name,
  17200.         FK_NAME             = object_name(r.constid),
  17201.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17202.     from
  17203.         sysobjects o1, sysobjects o2,
  17204.         syscolumns c1, syscolumns c2,
  17205.         sysreferences r, sysindexes i
  17206.     where   
  17207.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17208.     and o1.name = @pk_table_name
  17209.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17210.     and o1.id = r.rkeyid
  17211.     and o1.id = c1.id
  17212.     and c1.colid = r.rkey2
  17213.     and r.keycnt >= 2
  17214.     and r.fkeyid = o2.id
  17215.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17216.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17217.     and o2.id = c2.id   
  17218.     and c2.colid = r.fkey2
  17219.     and permissions(o1.id) <> 0
  17220.     and permissions(o2.id) <> 0
  17221.     and i.id = r.rkeyid
  17222.     and i.indid = r.rkeyindid
  17223.     union all
  17224.     select
  17225.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17226.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17227.         PK_TABLE_NAME       = o1.name,
  17228.         PK_COLUMN_NAME      = c1.name,
  17229.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17230.         PK_COLUMN_PROPID    = convert(int,null),
  17231.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17232.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17233.         FK_TABLE_NAME       = o2.name,
  17234.         FK_COLUMN_NAME      = c2.name,
  17235.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17236.         FK_COLUMN_PROPID    = convert(int,null),
  17237.         ORDINAL             = convert(int,3),
  17238.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17239.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17240.         PK_NAME             = i.name,
  17241.         FK_NAME             = object_name(r.constid),
  17242.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17243.     from
  17244.         sysobjects o1, sysobjects o2,
  17245.         syscolumns c1, syscolumns c2,
  17246.         sysreferences r, sysindexes i
  17247.     where   
  17248.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17249.     and o1.name = @pk_table_name
  17250.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17251.     and o1.id = r.rkeyid
  17252.     and o1.id = c1.id
  17253.     and c1.colid = r.rkey3
  17254.     and r.keycnt >= 3
  17255.     and r.fkeyid = o2.id
  17256.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17257.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17258.     and o2.id = c2.id   
  17259.     and c2.colid = r.fkey3
  17260.     and permissions(o1.id) <> 0
  17261.     and permissions(o2.id) <> 0 
  17262.     and i.id = r.rkeyid
  17263.     and i.indid = r.rkeyindid
  17264.     union all
  17265.     select
  17266.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17267.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17268.         PK_TABLE_NAME       = o1.name,
  17269.         PK_COLUMN_NAME      = c1.name,
  17270.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17271.         PK_COLUMN_PROPID    = convert(int,null),
  17272.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17273.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17274.         FK_TABLE_NAME       = o2.name,
  17275.         FK_COLUMN_NAME      = c2.name,
  17276.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17277.         FK_COLUMN_PROPID    = convert(int,null),
  17278.         ORDINAL             = convert(int,4),
  17279.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17280.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17281.         PK_NAME             = i.name,
  17282.         FK_NAME             = object_name(r.constid),
  17283.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17284.     from
  17285.         sysobjects o1, sysobjects o2,
  17286.         syscolumns c1, syscolumns c2,
  17287.         sysreferences r, sysindexes i
  17288.     where   
  17289.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17290.     and o1.name = @pk_table_name
  17291.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17292.     and o1.id = r.rkeyid
  17293.     and r.keycnt >= 4
  17294.     and o1.id = c1.id
  17295.     and c1.colid = r.rkey4
  17296.     and r.fkeyid = o2.id
  17297.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17298.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17299.     and o2.id = c2.id   
  17300.     and c2.colid = r.fkey4
  17301.     and permissions(o1.id) <> 0
  17302.     and permissions(o2.id) <> 0
  17303.     and i.id = r.rkeyid
  17304.     and i.indid = r.rkeyindid   
  17305.     union all
  17306.     select
  17307.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17308.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17309.         PK_TABLE_NAME       = o1.name,
  17310.         PK_COLUMN_NAME      = c1.name,
  17311.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17312.         PK_COLUMN_PROPID    = convert(int,null),
  17313.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17314.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17315.         FK_TABLE_NAME       = o2.name,
  17316.         FK_COLUMN_NAME      = c2.name,
  17317.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17318.         FK_COLUMN_PROPID    = convert(int,null),
  17319.         ORDINAL             = convert(int,5),
  17320.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17321.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17322.         PK_NAME             = i.name,
  17323.         FK_NAME             = object_name(r.constid),
  17324.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17325.     from
  17326.         sysobjects o1, sysobjects o2,
  17327.         syscolumns c1, syscolumns c2,
  17328.         sysreferences r, sysindexes i
  17329.     where   
  17330.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17331.     and o1.name = @pk_table_name
  17332.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17333.     and o1.id = r.rkeyid
  17334.     and r.keycnt >= 5
  17335.     and o1.id = c1.id
  17336.     and c1.colid = r.rkey5
  17337.     and r.fkeyid = o2.id
  17338.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17339.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17340.     and o2.id = c2.id   
  17341.     and c2.colid = r.fkey5
  17342.     and permissions(o1.id) <> 0
  17343.     and permissions(o2.id) <> 0
  17344.     and i.id = r.rkeyid
  17345.     and i.indid = r.rkeyindid       
  17346.     union all
  17347.     select
  17348.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17349.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17350.         PK_TABLE_NAME       = o1.name,
  17351.         PK_COLUMN_NAME      = c1.name,
  17352.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17353.         PK_COLUMN_PROPID    = convert(int,null),
  17354.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17355.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17356.         FK_TABLE_NAME       = o2.name,
  17357.         FK_COLUMN_NAME      = c2.name,
  17358.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17359.         FK_COLUMN_PROPID    = convert(int,null),
  17360.         ORDINAL             = convert(int,6),
  17361.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17362.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17363.         PK_NAME             = i.name,
  17364.         FK_NAME             = object_name(r.constid),
  17365.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17366.     from
  17367.         sysobjects o1, sysobjects o2,
  17368.         syscolumns c1, syscolumns c2,
  17369.         sysreferences r, sysindexes i
  17370.     where   
  17371.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17372.     and o1.name = @pk_table_name
  17373.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17374.     and o1.id = r.rkeyid
  17375.     and r.keycnt >= 6
  17376.     and o1.id = c1.id
  17377.     and c1.colid = r.rkey6
  17378.     and r.fkeyid = o2.id
  17379.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17380.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17381.     and o2.id = c2.id   
  17382.     and c2.colid = r.fkey6
  17383.     and permissions(o1.id) <> 0
  17384.     and permissions(o2.id) <> 0
  17385.     and i.id = r.rkeyid
  17386.     and i.indid = r.rkeyindid           
  17387.     union all
  17388.     select
  17389.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17390.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17391.         PK_TABLE_NAME       = o1.name,
  17392.         PK_COLUMN_NAME      = c1.name,
  17393.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17394.         PK_COLUMN_PROPID    = convert(int,null),
  17395.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17396.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17397.         FK_TABLE_NAME       = o2.name,
  17398.         FK_COLUMN_NAME      = c2.name,
  17399.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17400.         FK_COLUMN_PROPID    = convert(int,null),
  17401.         ORDINAL             = convert(int,7),
  17402.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17403.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17404.         PK_NAME             = i.name,
  17405.         FK_NAME             = object_name(r.constid),
  17406.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17407.     from
  17408.         sysobjects o1, sysobjects o2,
  17409.         syscolumns c1, syscolumns c2,
  17410.         sysreferences r, sysindexes i
  17411.     where   
  17412.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17413.     and o1.name = @pk_table_name
  17414.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17415.     and o1.id = r.rkeyid
  17416.     and r.keycnt >= 7
  17417.     and o1.id = c1.id
  17418.     and c1.colid = r.rkey7
  17419.     and r.fkeyid = o2.id
  17420.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17421.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17422.     and o2.id = c2.id   
  17423.     and c2.colid = r.fkey7
  17424.     and permissions(o1.id) <> 0
  17425.     and permissions(o2.id) <> 0
  17426.     and i.id = r.rkeyid
  17427.     and i.indid = r.rkeyindid           
  17428.     union all
  17429.     select
  17430.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17431.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17432.         PK_TABLE_NAME       = o1.name,
  17433.         PK_COLUMN_NAME      = c1.name,
  17434.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17435.         PK_COLUMN_PROPID    = convert(int,null),
  17436.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17437.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17438.         FK_TABLE_NAME       = o2.name,
  17439.         FK_COLUMN_NAME      = c2.name,
  17440.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17441.         FK_COLUMN_PROPID    = convert(int,null),
  17442.         ORDINAL             = convert(int,8),
  17443.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17444.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17445.         PK_NAME             = i.name,
  17446.         FK_NAME             = object_name(r.constid),
  17447.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17448.     from
  17449.         sysobjects o1, sysobjects o2,
  17450.         syscolumns c1, syscolumns c2,
  17451.         sysreferences r, sysindexes i
  17452.     where   
  17453.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17454.     and o1.name = @pk_table_name
  17455.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17456.     and o1.id = r.rkeyid
  17457.     and r.keycnt >= 8
  17458.     and o1.id = c1.id
  17459.     and c1.colid = r.rkey8
  17460.     and r.fkeyid = o2.id
  17461.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17462.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17463.     and o2.id = c2.id   
  17464.     and c2.colid = r.fkey8
  17465.     and permissions(o1.id) <> 0
  17466.     and permissions(o2.id) <> 0
  17467.     and i.id = r.rkeyid
  17468.     and i.indid = r.rkeyindid           
  17469.     union all
  17470.     select
  17471.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17472.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17473.         PK_TABLE_NAME       = o1.name,
  17474.         PK_COLUMN_NAME      = c1.name,
  17475.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17476.         PK_COLUMN_PROPID    = convert(int,null),
  17477.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17478.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17479.         FK_TABLE_NAME       = o2.name,
  17480.         FK_COLUMN_NAME      = c2.name,
  17481.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17482.         FK_COLUMN_PROPID    = convert(int,null),
  17483.         ORDINAL             = convert(int,9),
  17484.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17485.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17486.         PK_NAME             = i.name,
  17487.         FK_NAME             = object_name(r.constid),
  17488.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17489.     from
  17490.         sysobjects o1, sysobjects o2,
  17491.         syscolumns c1, syscolumns c2,
  17492.         sysreferences r, sysindexes i
  17493.     where   
  17494.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17495.     and o1.name = @pk_table_name
  17496.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17497.     and o1.id = r.rkeyid
  17498.     and r.keycnt >= 9
  17499.     and o1.id = c1.id
  17500.     and c1.colid = r.rkey9
  17501.     and r.fkeyid = o2.id
  17502.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17503.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17504.     and o2.id = c2.id   
  17505.     and c2.colid = r.fkey9
  17506.     and permissions(o1.id) <> 0
  17507.     and permissions(o2.id) <> 0
  17508.     and i.id = r.rkeyid
  17509.     and i.indid = r.rkeyindid           
  17510.     union all
  17511.     select
  17512.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17513.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17514.         PK_TABLE_NAME       = o1.name,
  17515.         PK_COLUMN_NAME      = c1.name,
  17516.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17517.         PK_COLUMN_PROPID    = convert(int,null),
  17518.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17519.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17520.         FK_TABLE_NAME       = o2.name,
  17521.         FK_COLUMN_NAME      = c2.name,
  17522.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17523.         FK_COLUMN_PROPID    = convert(int,null),
  17524.         ORDINAL             = convert(int,10),
  17525.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17526.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17527.         PK_NAME             = i.name,
  17528.         FK_NAME             = object_name(r.constid),
  17529.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17530.     from
  17531.         sysobjects o1, sysobjects o2,
  17532.         syscolumns c1, syscolumns c2,
  17533.         sysreferences r, sysindexes i
  17534.     where   
  17535.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17536.     and o1.name = @pk_table_name
  17537.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17538.     and o1.id = r.rkeyid
  17539.     and r.keycnt >= 10
  17540.     and o1.id = c1.id
  17541.     and c1.colid = r.rkey10
  17542.     and r.fkeyid = o2.id
  17543.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17544.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17545.     and o2.id = c2.id   
  17546.     and c2.colid = r.fkey10
  17547.     and permissions(o1.id) <> 0
  17548.     and permissions(o2.id) <> 0
  17549.     and i.id = r.rkeyid
  17550.     and i.indid = r.rkeyindid           
  17551.     union all
  17552.     select
  17553.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17554.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17555.         PK_TABLE_NAME       = o1.name,
  17556.         PK_COLUMN_NAME      = c1.name,
  17557.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17558.         PK_COLUMN_PROPID    = convert(int,null),
  17559.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17560.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17561.         FK_TABLE_NAME       = o2.name,
  17562.         FK_COLUMN_NAME      = c2.name,
  17563.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17564.         FK_COLUMN_PROPID    = convert(int,null),
  17565.         ORDINAL             = convert(int,11),
  17566.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17567.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17568.         PK_NAME             = i.name,
  17569.         FK_NAME             = object_name(r.constid),
  17570.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17571.     from
  17572.         sysobjects o1, sysobjects o2,
  17573.         syscolumns c1, syscolumns c2,
  17574.         sysreferences r, sysindexes i
  17575.     where   
  17576.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17577.     and o1.name = @pk_table_name
  17578.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17579.     and o1.id = r.rkeyid
  17580.     and r.keycnt >= 11
  17581.     and o1.id = c1.id
  17582.     and c1.colid = r.rkey11
  17583.     and r.fkeyid = o2.id
  17584.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17585.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17586.     and o2.id = c2.id   
  17587.     and c2.colid = r.fkey11
  17588.     and permissions(o1.id) <> 0
  17589.     and permissions(o2.id) <> 0
  17590.     and i.id = r.rkeyid
  17591.     and i.indid = r.rkeyindid           
  17592.     union all
  17593.     select
  17594.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17595.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17596.         PK_TABLE_NAME       = o1.name,
  17597.         PK_COLUMN_NAME      = c1.name,
  17598.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17599.         PK_COLUMN_PROPID    = convert(int,null),
  17600.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17601.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17602.         FK_TABLE_NAME       = o2.name,
  17603.         FK_COLUMN_NAME      = c2.name,
  17604.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17605.         FK_COLUMN_PROPID    = convert(int,null),
  17606.         ORDINAL             = convert(int,12),
  17607.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17608.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17609.         PK_NAME             = i.name,
  17610.         FK_NAME             = object_name(r.constid),
  17611.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17612.     from
  17613.         sysobjects o1, sysobjects o2,
  17614.         syscolumns c1, syscolumns c2,
  17615.         sysreferences r, sysindexes i
  17616.     where   
  17617.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17618.     and o1.name = @pk_table_name
  17619.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17620.     and o1.id = r.rkeyid
  17621.     and r.keycnt >= 12
  17622.     and o1.id = c1.id
  17623.     and c1.colid = r.rkey12
  17624.     and r.fkeyid = o2.id
  17625.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17626.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17627.     and o2.id = c2.id   
  17628.     and c2.colid = r.fkey12
  17629.     and permissions(o1.id) <> 0
  17630.     and permissions(o2.id) <> 0
  17631.     and i.id = r.rkeyid
  17632.     and i.indid = r.rkeyindid           
  17633.     union all
  17634.     select
  17635.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17636.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17637.         PK_TABLE_NAME       = o1.name,
  17638.         PK_COLUMN_NAME      = c1.name,
  17639.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17640.         PK_COLUMN_PROPID    = convert(int,null),
  17641.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17642.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17643.         FK_TABLE_NAME       = o2.name,
  17644.         FK_COLUMN_NAME      = c2.name,
  17645.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17646.         FK_COLUMN_PROPID    = convert(int,null),
  17647.         ORDINAL             = convert(int,13),
  17648.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17649.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17650.         PK_NAME             = i.name,
  17651.         FK_NAME             = object_name(r.constid),
  17652.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17653.     from
  17654.         sysobjects o1, sysobjects o2,
  17655.         syscolumns c1, syscolumns c2,
  17656.         sysreferences r, sysindexes i
  17657.     where   
  17658.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17659.     and o1.name = @pk_table_name
  17660.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17661.     and o1.id = r.rkeyid
  17662.     and r.keycnt >= 13
  17663.     and o1.id = c1.id
  17664.     and c1.colid = r.rkey13
  17665.     and r.fkeyid = o2.id
  17666.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17667.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17668.     and o2.id = c2.id   
  17669.     and c2.colid = r.fkey13
  17670.     and permissions(o1.id) <> 0
  17671.     and permissions(o2.id) <> 0
  17672.     and i.id = r.rkeyid
  17673.     and i.indid = r.rkeyindid           
  17674.     union all
  17675.     select
  17676.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17677.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17678.         PK_TABLE_NAME       = o1.name,
  17679.         PK_COLUMN_NAME      = c1.name,
  17680.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17681.         PK_COLUMN_PROPID    = convert(int,null),
  17682.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17683.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17684.         FK_TABLE_NAME       = o2.name,
  17685.         FK_COLUMN_NAME      = c2.name,
  17686.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17687.         FK_COLUMN_PROPID    = convert(int,null),
  17688.         ORDINAL             = convert(int,14),
  17689.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17690.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17691.         PK_NAME             = i.name,
  17692.         FK_NAME             = object_name(r.constid),
  17693.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17694.     from
  17695.         sysobjects o1, sysobjects o2,
  17696.         syscolumns c1, syscolumns c2,
  17697.         sysreferences r, sysindexes i
  17698.     where   
  17699.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17700.     and o1.name = @pk_table_name
  17701.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17702.     and o1.id = r.rkeyid
  17703.     and r.keycnt >= 14
  17704.     and o1.id = c1.id
  17705.     and c1.colid = r.rkey14
  17706.     and r.fkeyid = o2.id
  17707.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17708.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17709.     and o2.id = c2.id   
  17710.     and c2.colid = r.fkey14
  17711.     and permissions(o1.id) <> 0
  17712.     and permissions(o2.id) <> 0
  17713.     and i.id = r.rkeyid
  17714.     and i.indid = r.rkeyindid           
  17715.     union all
  17716.     select
  17717.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17718.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17719.         PK_TABLE_NAME       = o1.name,
  17720.         PK_COLUMN_NAME      = c1.name,
  17721.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17722.         PK_COLUMN_PROPID    = convert(int,null),
  17723.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17724.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17725.         FK_TABLE_NAME       = o2.name,
  17726.         FK_COLUMN_NAME      = c2.name,
  17727.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17728.         FK_COLUMN_PROPID    = convert(int,null),
  17729.         ORDINAL             = convert(int,15),
  17730.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17731.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17732.         PK_NAME             = i.name,
  17733.         FK_NAME             = object_name(r.constid),
  17734.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17735.     from
  17736.         sysobjects o1, sysobjects o2,
  17737.         syscolumns c1, syscolumns c2,
  17738.         sysreferences r, sysindexes i
  17739.     where   
  17740.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17741.     and o1.name = @pk_table_name
  17742.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17743.     and o1.id = r.rkeyid
  17744.     and r.keycnt >= 15
  17745.     and o1.id = c1.id
  17746.     and c1.colid = r.rkey15
  17747.     and r.fkeyid = o2.id
  17748.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17749.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17750.     and o2.id = c2.id   
  17751.     and c2.colid = r.fkey15
  17752.     and permissions(o1.id) <> 0
  17753.     and permissions(o2.id) <> 0
  17754.     and i.id = r.rkeyid
  17755.     and i.indid = r.rkeyindid           
  17756.     union all
  17757.     select
  17758.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17759.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17760.         PK_TABLE_NAME       = o1.name,
  17761.         PK_COLUMN_NAME      = c1.name,
  17762.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17763.         PK_COLUMN_PROPID    = convert(int,null),
  17764.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17765.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17766.         FK_TABLE_NAME       = o2.name,
  17767.         FK_COLUMN_NAME      = c2.name,
  17768.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17769.         FK_COLUMN_PROPID    = convert(int,null),
  17770.         ORDINAL             = convert(int,16),
  17771.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17772.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17773.         PK_NAME             = i.name,
  17774.         FK_NAME             = object_name(r.constid),
  17775.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17776.     from
  17777.         sysobjects o1, sysobjects o2,
  17778.         syscolumns c1, syscolumns c2,
  17779.         sysreferences r, sysindexes i
  17780.     where   
  17781.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17782.     and o1.name = @pk_table_name
  17783.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17784.     and o1.id = r.rkeyid
  17785.     and r.keycnt >= 16
  17786.     and o1.id = c1.id
  17787.     and c1.colid = r.rkey16
  17788.     and r.fkeyid = o2.id
  17789.     and (@fk_table_name is null or @fk_table_name = o2.name)
  17790.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17791.     and o2.id = c2.id   
  17792.     and c2.colid = r.fkey16
  17793.     and permissions(o1.id) <> 0
  17794.     and permissions(o2.id) <> 0
  17795.     and i.id = r.rkeyid
  17796.     and i.indid = r.rkeyindid       
  17797.     order by 8,9,2,3,13
  17798. go
  17799. dump tran master with no_log
  17800. go
  17801. CREATE PROCEDURE sp_foreign_keys_rowset;2
  17802.     (
  17803.     @fk_table_name      sysname,
  17804.     @fk_table_schema    sysname = null,
  17805.     @pk_table_name      sysname = null,
  17806.     @pk_table_schema    sysname = null,
  17807.     @pk_table_catalog   sysname = null
  17808.     )
  17809. as
  17810.     select
  17811.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17812.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17813.         PK_TABLE_NAME       = o1.name,
  17814.         PK_COLUMN_NAME      = c1.name,
  17815.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17816.         PK_COLUMN_PROPID    = convert(int,null),
  17817.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17818.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17819.         FK_TABLE_NAME       = o2.name,
  17820.         FK_COLUMN_NAME      = c2.name,
  17821.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17822.         FK_COLUMN_PROPID    = convert(int,null),
  17823.         ORDINAL             = convert(int,1),
  17824.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17825.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17826.         PK_NAME             = i.name,
  17827.         FK_NAME             = object_name(r.constid),
  17828.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17829.     from
  17830.         sysobjects o1, sysobjects o2,
  17831.         syscolumns c1, syscolumns c2,
  17832.         sysreferences r, sysindexes i
  17833.     where   
  17834.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17835.     and o2.name = @fk_table_name
  17836.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17837.     and o2.id = r.fkeyid
  17838.     and o2.id = c2.id
  17839.     and c2.colid = r.fkey1
  17840.     and r.rkeyid = o1.id
  17841.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17842.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17843.     and o1.id = c1.id   
  17844.     and c1.colid = r.rkey1
  17845.     and permissions(o1.id) <> 0
  17846.     and permissions(o2.id) <> 0
  17847.     and i.id = r.rkeyid
  17848.     and i.indid = r.rkeyindid       
  17849.     union   all
  17850.     select
  17851.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17852.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17853.         PK_TABLE_NAME       = o1.name,
  17854.         PK_COLUMN_NAME      = c1.name,
  17855.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17856.         PK_COLUMN_PROPID    = convert(int,null),
  17857.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17858.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17859.         FK_TABLE_NAME       = o2.name,
  17860.         FK_COLUMN_NAME      = c2.name,
  17861.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17862.         FK_COLUMN_PROPID    = convert(int,null),
  17863.         ORDINAL             = convert(int,2),
  17864.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17865.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17866.         PK_NAME             = i.name,
  17867.         FK_NAME             = object_name(r.constid),
  17868.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17869.     from
  17870.         sysobjects o1, sysobjects o2,
  17871.         syscolumns c1, syscolumns c2,
  17872.         sysreferences r, sysindexes i
  17873.     where   
  17874.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17875.     and o2.name = @fk_table_name
  17876.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17877.     and o2.id = r.fkeyid
  17878.     and r.keycnt >= 2
  17879.     and o2.id = c2.id
  17880.     and c2.colid = r.fkey2
  17881.     and r.rkeyid = o1.id
  17882.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17883.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17884.     and o1.id = c1.id   
  17885.     and c1.colid = r.rkey2
  17886.     and permissions(o1.id) <> 0
  17887.     and permissions(o2.id) <> 0
  17888.     and i.id = r.rkeyid
  17889.     and i.indid = r.rkeyindid           
  17890.     union   all
  17891.     select
  17892.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17893.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17894.         PK_TABLE_NAME       = o1.name,
  17895.         PK_COLUMN_NAME      = c1.name,
  17896.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17897.         PK_COLUMN_PROPID    = convert(int,null),
  17898.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17899.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17900.         FK_TABLE_NAME       = o2.name,
  17901.         FK_COLUMN_NAME      = c2.name,
  17902.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17903.         FK_COLUMN_PROPID    = convert(int,null),
  17904.         ORDINAL             = convert(int,3),
  17905.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17906.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17907.         PK_NAME             = i.name,
  17908.         FK_NAME             = object_name(r.constid),
  17909.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17910.     from
  17911.         sysobjects o1, sysobjects o2,
  17912.         syscolumns c1, syscolumns c2,
  17913.         sysreferences r, sysindexes i
  17914.     where   
  17915.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17916.     and o2.name = @fk_table_name
  17917.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17918.     and o2.id = r.fkeyid
  17919.     and r.keycnt >= 3
  17920.     and o2.id = c2.id
  17921.     and c2.colid = r.fkey3
  17922.     and r.rkeyid = o1.id
  17923.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17924.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17925.     and o1.id = c1.id   
  17926.     and c1.colid = r.rkey3
  17927.     and permissions(o1.id) <> 0
  17928.     and permissions(o2.id) <> 0
  17929.     and i.id = r.rkeyid
  17930.     and i.indid = r.rkeyindid           
  17931.     union   all
  17932.     select
  17933.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17934.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17935.         PK_TABLE_NAME       = o1.name,
  17936.         PK_COLUMN_NAME      = c1.name,
  17937.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17938.         PK_COLUMN_PROPID    = convert(int,null),
  17939.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17940.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17941.         FK_TABLE_NAME       = o2.name,
  17942.         FK_COLUMN_NAME      = c2.name,
  17943.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17944.         FK_COLUMN_PROPID    = convert(int,null),
  17945.         ORDINAL             = convert(int,4),
  17946.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17947.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17948.         PK_NAME             = i.name,
  17949.         FK_NAME             = object_name(r.constid),
  17950.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17951.     from
  17952.         sysobjects o1, sysobjects o2,
  17953.         syscolumns c1, syscolumns c2,
  17954.         sysreferences r, sysindexes i
  17955.     where   
  17956.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17957.     and o2.name = @fk_table_name
  17958.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17959.     and o2.id = r.fkeyid
  17960.     and r.keycnt >= 4
  17961.     and o2.id = c2.id
  17962.     and c2.colid = r.fkey4
  17963.     and r.rkeyid = o1.id
  17964.     and (@pk_table_name is null or @pk_table_name = o1.name)
  17965.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17966.     and o1.id = c1.id   
  17967.     and c1.colid = r.rkey4
  17968.     and permissions(o1.id) <> 0
  17969.     and permissions(o2.id) <> 0
  17970.     and i.id = r.rkeyid
  17971.     and i.indid = r.rkeyindid           
  17972.     union   all
  17973.     select
  17974.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17975.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  17976.         PK_TABLE_NAME       = o1.name,
  17977.         PK_COLUMN_NAME      = c1.name,
  17978.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17979.         PK_COLUMN_PROPID    = convert(int,null),
  17980.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17981.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17982.         FK_TABLE_NAME       = o2.name,
  17983.         FK_COLUMN_NAME      = c2.name,
  17984.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  17985.         FK_COLUMN_PROPID    = convert(int,null),
  17986.         ORDINAL             = convert(int,5),
  17987.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17988.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17989.         PK_NAME             = i.name,
  17990.         FK_NAME             = object_name(r.constid),
  17991.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17992.     from
  17993.         sysobjects o1, sysobjects o2,
  17994.         syscolumns c1, syscolumns c2,
  17995.         sysreferences r, sysindexes i
  17996.     where   
  17997.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17998.     and o2.name = @fk_table_name
  17999.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18000.     and o2.id = r.fkeyid
  18001.     and r.keycnt >= 5
  18002.     and o2.id = c2.id
  18003.     and c2.colid = r.fkey5
  18004.     and r.rkeyid = o1.id
  18005.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18006.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18007.     and o1.id = c1.id   
  18008.     and c1.colid = r.rkey5
  18009.     and permissions(o1.id) <> 0
  18010.     and permissions(o2.id) <> 0
  18011.     and i.id = r.rkeyid
  18012.     and i.indid = r.rkeyindid           
  18013.     union   all
  18014.     select
  18015.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18016.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18017.         PK_TABLE_NAME       = o1.name,
  18018.         PK_COLUMN_NAME      = c1.name,
  18019.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18020.         PK_COLUMN_PROPID    = convert(int,null),
  18021.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18022.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18023.         FK_TABLE_NAME       = o2.name,
  18024.         FK_COLUMN_NAME      = c2.name,
  18025.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18026.         FK_COLUMN_PROPID    = convert(int,null),
  18027.         ORDINAL             = convert(int,6),
  18028.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18029.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18030.         PK_NAME             = i.name,
  18031.         FK_NAME             = object_name(r.constid),
  18032.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18033.     from
  18034.         sysobjects o1, sysobjects o2,
  18035.         syscolumns c1, syscolumns c2,
  18036.         sysreferences r, sysindexes i
  18037.     where   
  18038.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18039.     and o2.name = @fk_table_name
  18040.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18041.     and o2.id = r.fkeyid
  18042.     and r.keycnt >= 6
  18043.     and o2.id = c2.id
  18044.     and c2.colid = r.fkey6
  18045.     and r.rkeyid = o1.id
  18046.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18047.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18048.     and o1.id = c1.id       
  18049.     and c1.colid = r.rkey6
  18050.     and permissions(o1.id) <> 0
  18051.     and permissions(o2.id) <> 0
  18052.     and i.id = r.rkeyid
  18053.     and i.indid = r.rkeyindid       
  18054.     union   all
  18055.     select
  18056.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18057.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18058.         PK_TABLE_NAME       = o1.name,
  18059.         PK_COLUMN_NAME      = c1.name,
  18060.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18061.         PK_COLUMN_PROPID    = convert(int,null),
  18062.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18063.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18064.         FK_TABLE_NAME       = o2.name,
  18065.         FK_COLUMN_NAME      = c2.name,
  18066.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18067.         FK_COLUMN_PROPID    = convert(int,null),
  18068.         ORDINAL             = convert(int,7),
  18069.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18070.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18071.         PK_NAME             = i.name,
  18072.         FK_NAME             = object_name(r.constid),
  18073.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18074.     from
  18075.         sysobjects o1, sysobjects o2,
  18076.         syscolumns c1, syscolumns c2,
  18077.         sysreferences r, sysindexes i
  18078.     where   
  18079.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18080.     and o2.name = @fk_table_name
  18081.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18082.     and o2.id = r.fkeyid
  18083.     and r.keycnt >= 7
  18084.     and o2.id = c2.id
  18085.     and c2.colid = r.fkey7
  18086.     and r.rkeyid = o1.id
  18087.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18088.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18089.     and o1.id = c1.id   
  18090.     and c1.colid = r.rkey7
  18091.     and permissions(o1.id) <> 0
  18092.     and permissions(o2.id) <> 0
  18093.     and i.id = r.rkeyid
  18094.     and i.indid = r.rkeyindid           
  18095.     union   all
  18096.     select
  18097.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18098.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18099.         PK_TABLE_NAME       = o1.name,
  18100.         PK_COLUMN_NAME      = c1.name,
  18101.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18102.         PK_COLUMN_PROPID    = convert(int,null),
  18103.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18104.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18105.         FK_TABLE_NAME       = o2.name,
  18106.         FK_COLUMN_NAME      = c2.name,
  18107.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18108.         FK_COLUMN_PROPID    = convert(int,null),
  18109.         ORDINAL             = convert(int,8),
  18110.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18111.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18112.         PK_NAME             = i.name,
  18113.         FK_NAME             = object_name(r.constid),
  18114.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18115.     from
  18116.         sysobjects o1, sysobjects o2,
  18117.         syscolumns c1, syscolumns c2,
  18118.         sysreferences r, sysindexes i
  18119.     where   
  18120.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18121.     and o2.name = @fk_table_name
  18122.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18123.     and o2.id = r.fkeyid
  18124.     and r.keycnt >= 8
  18125.     and o2.id = c2.id
  18126.     and c2.colid = r.fkey8
  18127.     and r.rkeyid = o1.id
  18128.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18129.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18130.     and o1.id = c1.id   
  18131.     and c1.colid = r.rkey8
  18132.     and permissions(o1.id) <> 0
  18133.     and permissions(o2.id) <> 0
  18134.     and i.id = r.rkeyid
  18135.     and i.indid = r.rkeyindid           
  18136.     union   all
  18137.     select
  18138.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18139.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18140.         PK_TABLE_NAME       = o1.name,
  18141.         PK_COLUMN_NAME      = c1.name,
  18142.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18143.         PK_COLUMN_PROPID    = convert(int,null),
  18144.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18145.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18146.         FK_TABLE_NAME       = o2.name,
  18147.         FK_COLUMN_NAME      = c2.name,
  18148.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18149.         FK_COLUMN_PROPID    = convert(int,null),
  18150.         ORDINAL             = convert(int,9),
  18151.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18152.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18153.         PK_NAME             = i.name,
  18154.         FK_NAME             = object_name(r.constid),
  18155.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18156.     from
  18157.         sysobjects o1, sysobjects o2,
  18158.         syscolumns c1, syscolumns c2,
  18159.         sysreferences r, sysindexes i
  18160.     where   
  18161.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18162.     and o2.name = @fk_table_name
  18163.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18164.     and o2.id = r.fkeyid
  18165.     and r.keycnt >= 9
  18166.     and o2.id = c2.id
  18167.     and c2.colid = r.fkey9
  18168.     and r.rkeyid = o1.id
  18169.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18170.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18171.     and o1.id = c1.id   
  18172.     and c1.colid = r.rkey9
  18173.     and permissions(o1.id) <> 0
  18174.     and permissions(o2.id) <> 0
  18175.     and i.id = r.rkeyid
  18176.     and i.indid = r.rkeyindid           
  18177.     union   all
  18178.     select
  18179.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18180.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18181.         PK_TABLE_NAME       = o1.name,
  18182.         PK_COLUMN_NAME      = c1.name,
  18183.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18184.         PK_COLUMN_PROPID    = convert(int,null),
  18185.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18186.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18187.         FK_TABLE_NAME       = o2.name,
  18188.         FK_COLUMN_NAME      = c2.name,
  18189.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18190.         FK_COLUMN_PROPID    = convert(int,null),
  18191.         ORDINAL             = convert(int,10),
  18192.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18193.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18194.         PK_NAME             = i.name,
  18195.         FK_NAME             = object_name(r.constid),
  18196.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18197.     from
  18198.         sysobjects o1, sysobjects o2,
  18199.         syscolumns c1, syscolumns c2,
  18200.         sysreferences r, sysindexes i
  18201.     where   
  18202.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18203.     and o2.name = @fk_table_name
  18204.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18205.     and o2.id = r.fkeyid
  18206.     and r.keycnt >= 10
  18207.     and o2.id = c2.id
  18208.     and c2.colid = r.fkey10
  18209.     and r.rkeyid = o1.id
  18210.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18211.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18212.     and o1.id = c1.id   
  18213.     and c1.colid = r.rkey10
  18214.     and permissions(o1.id) <> 0
  18215.     and permissions(o2.id) <> 0
  18216.     and i.id = r.rkeyid
  18217.     and i.indid = r.rkeyindid           
  18218.     union   all
  18219.     select
  18220.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18221.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18222.         PK_TABLE_NAME       = o1.name,
  18223.         PK_COLUMN_NAME      = c1.name,
  18224.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18225.         PK_COLUMN_PROPID    = convert(int,null),
  18226.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18227.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18228.         FK_TABLE_NAME       = o2.name,
  18229.         FK_COLUMN_NAME      = c2.name,
  18230.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18231.         FK_COLUMN_PROPID    = convert(int,null),
  18232.         ORDINAL             = convert(int,11),
  18233.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18234.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18235.         PK_NAME             = i.name,
  18236.         FK_NAME             = object_name(r.constid),
  18237.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18238.     from
  18239.         sysobjects o1, sysobjects o2,
  18240.         syscolumns c1, syscolumns c2,
  18241.         sysreferences r, sysindexes i
  18242.     where   
  18243.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18244.     and o2.name = @fk_table_name
  18245.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18246.     and o2.id = r.fkeyid
  18247.     and r.keycnt >= 11
  18248.     and o2.id = c2.id
  18249.     and c2.colid = r.fkey11
  18250.     and r.rkeyid = o1.id
  18251.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18252.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18253.     and o1.id = c1.id   
  18254.     and c1.colid = r.rkey11
  18255.     and permissions(o1.id) <> 0
  18256.     and permissions(o2.id) <> 0
  18257.     and i.id = r.rkeyid
  18258.     and i.indid = r.rkeyindid           
  18259.     union   all
  18260.     select
  18261.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18262.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18263.         PK_TABLE_NAME       = o1.name,
  18264.         PK_COLUMN_NAME      = c1.name,
  18265.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18266.         PK_COLUMN_PROPID    = convert(int,null),
  18267.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18268.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18269.         FK_TABLE_NAME       = o2.name,
  18270.         FK_COLUMN_NAME      = c2.name,
  18271.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18272.         FK_COLUMN_PROPID    = convert(int,null),
  18273.         ORDINAL             = convert(int,12),
  18274.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18275.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18276.         PK_NAME             = i.name,
  18277.         FK_NAME             = object_name(r.constid),
  18278.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18279.     from
  18280.         sysobjects o1, sysobjects o2,
  18281.         syscolumns c1, syscolumns c2,
  18282.         sysreferences r, sysindexes i
  18283.     where   
  18284.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18285.     and o2.name = @fk_table_name
  18286.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18287.     and o2.id = r.fkeyid
  18288.     and r.keycnt >= 12
  18289.     and o2.id = c2.id
  18290.     and c2.colid = r.fkey12
  18291.     and r.rkeyid = o1.id
  18292.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18293.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18294.     and o1.id = c1.id   
  18295.     and c1.colid = r.rkey12
  18296.     and permissions(o1.id) <> 0
  18297.     and permissions(o2.id) <> 0
  18298.     and i.id = r.rkeyid
  18299.     and i.indid = r.rkeyindid           
  18300.     union   all
  18301.     select
  18302.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18303.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18304.         PK_TABLE_NAME       = o1.name,
  18305.         PK_COLUMN_NAME      = c1.name,
  18306.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18307.         PK_COLUMN_PROPID    = convert(int,null),
  18308.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18309.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18310.         FK_TABLE_NAME       = o2.name,
  18311.         FK_COLUMN_NAME      = c2.name,
  18312.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18313.         FK_COLUMN_PROPID    = convert(int,null),
  18314.         ORDINAL             = convert(int,13),
  18315.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18316.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18317.         PK_NAME             = i.name,
  18318.         FK_NAME             = object_name(r.constid),
  18319.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18320.     from
  18321.         sysobjects o1, sysobjects o2,
  18322.         syscolumns c1, syscolumns c2,
  18323.         sysreferences r, sysindexes i
  18324.     where   
  18325.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18326.     and o2.name = @fk_table_name
  18327.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18328.     and o2.id = r.fkeyid
  18329.     and r.keycnt >= 13
  18330.     and o2.id = c2.id
  18331.     and c2.colid = r.fkey13
  18332.     and r.rkeyid = o1.id
  18333.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18334.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18335.     and o1.id = c1.id   
  18336.     and c1.colid = r.rkey13
  18337.     and permissions(o1.id) <> 0
  18338.     and permissions(o2.id) <> 0
  18339.     and i.id = r.rkeyid
  18340.     and i.indid = r.rkeyindid           
  18341.     union   all
  18342.     select
  18343.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18344.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18345.         PK_TABLE_NAME       = o1.name,
  18346.         PK_COLUMN_NAME      = c1.name,
  18347.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18348.         PK_COLUMN_PROPID    = convert(int,null),
  18349.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18350.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18351.         FK_TABLE_NAME       = o2.name,
  18352.         FK_COLUMN_NAME      = c2.name,
  18353.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18354.         FK_COLUMN_PROPID    = convert(int,null),
  18355.         ORDINAL             = convert(int,14),
  18356.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18357.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18358.         PK_NAME             = i.name,
  18359.         FK_NAME             = object_name(r.constid),
  18360.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18361.     from
  18362.         sysobjects o1, sysobjects o2,
  18363.         syscolumns c1, syscolumns c2,
  18364.         sysreferences r, sysindexes i
  18365.     where   
  18366.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18367.     and o2.name = @fk_table_name
  18368.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18369.     and o2.id = r.fkeyid
  18370.     and r.keycnt >= 14
  18371.     and o2.id = c2.id
  18372.     and c2.colid = r.fkey14
  18373.     and r.rkeyid = o1.id
  18374.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18375.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18376.     and o1.id = c1.id   
  18377.     and c1.colid = r.rkey14
  18378.     and permissions(o1.id) <> 0
  18379.     and permissions(o2.id) <> 0
  18380.     and i.id = r.rkeyid
  18381.     and i.indid = r.rkeyindid           
  18382.     union   all
  18383.     select
  18384.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18385.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18386.         PK_TABLE_NAME       = o1.name,
  18387.         PK_COLUMN_NAME      = c1.name,
  18388.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18389.         PK_COLUMN_PROPID    = convert(int,null),
  18390.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18391.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18392.         FK_TABLE_NAME       = o2.name,
  18393.         FK_COLUMN_NAME      = c2.name,
  18394.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18395.         FK_COLUMN_PROPID    = convert(int,null),
  18396.         ORDINAL             = convert(int,15),
  18397.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18398.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18399.         PK_NAME             = i.name,
  18400.         FK_NAME             = object_name(r.constid),
  18401.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/      
  18402.     from
  18403.         sysobjects o1, sysobjects o2,
  18404.         syscolumns c1, syscolumns c2,
  18405.         sysreferences r, sysindexes i
  18406.     where   
  18407.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18408.     and o2.name = @fk_table_name
  18409.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18410.     and o2.id = r.fkeyid
  18411.     and r.keycnt >= 15
  18412.     and o2.id = c2.id
  18413.     and c2.colid = r.fkey15
  18414.     and r.rkeyid = o1.id
  18415.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18416.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18417.     and o1.id = c1.id   
  18418.     and c1.colid = r.rkey15
  18419.     and permissions(o1.id) <> 0
  18420.     and permissions(o2.id) <> 0
  18421.     and i.id = r.rkeyid
  18422.     and i.indid = r.rkeyindid           
  18423.     union   all
  18424.     select
  18425.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18426.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18427.         PK_TABLE_NAME       = o1.name,
  18428.         PK_COLUMN_NAME      = c1.name,
  18429.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18430.         PK_COLUMN_PROPID    = convert(int,null),
  18431.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18432.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18433.         FK_TABLE_NAME       = o2.name,
  18434.         FK_COLUMN_NAME      = c2.name,
  18435.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18436.         FK_COLUMN_PROPID    = convert(int,null),
  18437.         ORDINAL             = convert(int,16),
  18438.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18439.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18440.         PK_NAME             = i.name,
  18441.         FK_NAME             = object_name(r.constid),
  18442.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/      
  18443.     from
  18444.         sysobjects o1, sysobjects o2,
  18445.         syscolumns c1, syscolumns c2,
  18446.         sysreferences r, sysindexes i
  18447.     where   
  18448.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18449.     and o2.name = @fk_table_name
  18450.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18451.     and o2.id = r.fkeyid
  18452.     and r.keycnt >= 16
  18453.     and o2.id = c2.id
  18454.     and c2.colid = r.fkey16
  18455.     and r.rkeyid = o1.id
  18456.     and (@pk_table_name is null or @pk_table_name = o1.name)
  18457.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18458.     and o1.id = c1.id   
  18459.     and c1.colid = r.rkey16
  18460.     and permissions(o1.id) <> 0
  18461.     and permissions(o2.id) <> 0
  18462.     and i.id = r.rkeyid
  18463.     and i.indid = r.rkeyindid       
  18464.     order by 8,9,2,3,13
  18465. go
  18466. dump tran master with no_log
  18467. go
  18468. CREATE PROCEDURE sp_foreign_keys_rowset;3
  18469.     (
  18470.     @pk_table_schema    sysname = null,
  18471.     @pk_table_catalog   sysname = null,
  18472.     @fk_table_schema    sysname = null,
  18473.     @fk_table_catalog   sysname = null
  18474.     )
  18475. as
  18476.     select
  18477.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18478.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18479.         PK_TABLE_NAME       = o1.name,
  18480.         PK_COLUMN_NAME      = c1.name,
  18481.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18482.         PK_COLUMN_PROPID    = convert(int,null),
  18483.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18484.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18485.         FK_TABLE_NAME       = o2.name,
  18486.         FK_COLUMN_NAME      = c2.name,
  18487.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18488.         FK_COLUMN_PROPID    = convert(int,null),
  18489.         ORDINAL             = convert(int,1),
  18490.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18491.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18492.         PK_NAME             = i.name,
  18493.         FK_NAME             = object_name(r.constid),
  18494.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/      
  18495.     from
  18496.         sysobjects o1, sysobjects o2,
  18497.         syscolumns c1, syscolumns c2,
  18498.         sysreferences r, sysindexes i
  18499.     where   
  18500.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18501.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18502.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18503.     and o1.id = r.rkeyid
  18504.     and o1.id = c1.id
  18505.     and c1.colid = r.rkey1
  18506.     and r.fkeyid = o2.id
  18507.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18508.     and o2.id = c2.id   
  18509.     and c2.colid = r.fkey1
  18510.     and permissions(o1.id) <> 0
  18511.     and permissions(o2.id) <> 0
  18512.     and i.id = r.rkeyid
  18513.     and i.indid = r.rkeyindid       
  18514.     union all
  18515.     select
  18516.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18517.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18518.         PK_TABLE_NAME       = o1.name,
  18519.         PK_COLUMN_NAME      = c1.name,
  18520.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18521.         PK_COLUMN_PROPID    = convert(int,null),
  18522.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18523.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18524.         FK_TABLE_NAME       = o2.name,
  18525.         FK_COLUMN_NAME      = c2.name,
  18526.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18527.         FK_COLUMN_PROPID    = convert(int,null),
  18528.         ORDINAL             = convert(int,2),
  18529.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18530.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18531.         PK_NAME             = i.name,
  18532.         FK_NAME             = object_name(r.constid),
  18533.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18534.     from
  18535.         sysobjects o1, sysobjects o2,
  18536.         syscolumns c1, syscolumns c2,
  18537.         sysreferences r, sysindexes i
  18538.     where   
  18539.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18540.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18541.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18542.     and o1.id = r.rkeyid
  18543.     and r.keycnt >= 2
  18544.     and o1.id = c1.id
  18545.     and c1.colid = r.rkey2
  18546.     and r.fkeyid = o2.id
  18547.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18548.     and o2.id = c2.id   
  18549.     and c2.colid = r.fkey2
  18550.     and permissions(o1.id) <> 0
  18551.     and permissions(o2.id) <> 0
  18552.     and i.id = r.rkeyid
  18553.     and i.indid = r.rkeyindid       
  18554.     union all
  18555.     select
  18556.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18557.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18558.         PK_TABLE_NAME       = o1.name,
  18559.         PK_COLUMN_NAME      = c1.name,
  18560.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18561.         PK_COLUMN_PROPID    = convert(int,null),
  18562.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18563.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18564.         FK_TABLE_NAME       = o2.name,
  18565.         FK_COLUMN_NAME      = c2.name,
  18566.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18567.         FK_COLUMN_PROPID    = convert(int,null),
  18568.         ORDINAL             = convert(int,3),
  18569.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18570.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18571.         PK_NAME             = i.name,
  18572.         FK_NAME             = object_name(r.constid),
  18573.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18574.     from
  18575.         sysobjects o1, sysobjects o2,
  18576.         syscolumns c1, syscolumns c2,
  18577.         sysreferences r, sysindexes i
  18578.     where   
  18579.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18580.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18581.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18582.     and o1.id = r.rkeyid
  18583.     and r.keycnt >= 3
  18584.     and o1.id = c1.id
  18585.     and c1.colid = r.rkey3
  18586.     and r.fkeyid = o2.id
  18587.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18588.     and o2.id = c2.id   
  18589.     and c2.colid = r.fkey3
  18590.     and permissions(o1.id) <> 0
  18591.     and permissions(o2.id) <> 0
  18592.     and i.id = r.rkeyid
  18593.     and i.indid = r.rkeyindid       
  18594.     union all
  18595.     select
  18596.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18597.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18598.         PK_TABLE_NAME       = o1.name,
  18599.         PK_COLUMN_NAME      = c1.name,
  18600.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18601.         PK_COLUMN_PROPID    = convert(int,null),
  18602.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18603.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18604.         FK_TABLE_NAME       = o2.name,
  18605.         FK_COLUMN_NAME      = c2.name,
  18606.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18607.         FK_COLUMN_PROPID    = convert(int,null),
  18608.         ORDINAL             = convert(int,4),
  18609.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18610.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18611.         PK_NAME             = i.name,
  18612.         FK_NAME             = object_name(r.constid),
  18613.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18614.     from
  18615.         sysobjects o1, sysobjects o2,
  18616.         syscolumns c1, syscolumns c2,
  18617.         sysreferences r, sysindexes i
  18618.     where   
  18619.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18620.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18621.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18622.     and o1.id = r.rkeyid
  18623.     and r.keycnt >= 4
  18624.     and o1.id = c1.id
  18625.     and c1.colid = r.rkey4
  18626.     and r.fkeyid = o2.id
  18627.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18628.     and o2.id = c2.id   
  18629.     and c2.colid = r.fkey4
  18630.     and permissions(o1.id) <> 0
  18631.     and permissions(o2.id) <> 0
  18632.     and i.id = r.rkeyid
  18633.     and i.indid = r.rkeyindid       
  18634.     union all
  18635.     select
  18636.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18637.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18638.         PK_TABLE_NAME       = o1.name,
  18639.         PK_COLUMN_NAME      = c1.name,
  18640.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18641.         PK_COLUMN_PROPID    = convert(int,null),
  18642.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18643.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18644.         FK_TABLE_NAME       = o2.name,
  18645.         FK_COLUMN_NAME      = c2.name,
  18646.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18647.         FK_COLUMN_PROPID    = convert(int,null),
  18648.         ORDINAL             = convert(int,5),
  18649.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18650.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18651.         PK_NAME             = i.name,
  18652.         FK_NAME             = object_name(r.constid),
  18653.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18654.     from
  18655.         sysobjects o1, sysobjects o2,
  18656.         syscolumns c1, syscolumns c2,
  18657.         sysreferences r, sysindexes i
  18658.     where   
  18659.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18660.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18661.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18662.     and o1.id = r.rkeyid
  18663.     and r.keycnt >= 5
  18664.     and o1.id = c1.id
  18665.     and c1.colid = r.rkey5
  18666.     and r.fkeyid = o2.id
  18667.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18668.     and o2.id = c2.id   
  18669.     and c2.colid = r.fkey5
  18670.     and permissions(o1.id) <> 0
  18671.     and permissions(o2.id) <> 0
  18672.     and i.id = r.rkeyid
  18673.     and i.indid = r.rkeyindid       
  18674.     union all
  18675.     select
  18676.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18677.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18678.         PK_TABLE_NAME       = o1.name,
  18679.         PK_COLUMN_NAME      = c1.name,
  18680.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18681.         PK_COLUMN_PROPID    = convert(int,null),
  18682.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18683.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18684.         FK_TABLE_NAME       = o2.name,
  18685.         FK_COLUMN_NAME      = c2.name,
  18686.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18687.         FK_COLUMN_PROPID    = convert(int,null),
  18688.         ORDINAL             = convert(int,6),
  18689.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18690.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18691.         PK_NAME             = i.name,
  18692.         FK_NAME             = object_name(r.constid),
  18693.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18694.     from
  18695.         sysobjects o1, sysobjects o2,
  18696.         syscolumns c1, syscolumns c2,
  18697.         sysreferences r, sysindexes i
  18698.     where   
  18699.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18700.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18701.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18702.     and o1.id = r.rkeyid
  18703.     and r.keycnt >= 6
  18704.     and o1.id = c1.id
  18705.     and c1.colid = r.rkey6
  18706.     and r.fkeyid = o2.id
  18707.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18708.     and o2.id = c2.id   
  18709.     and c2.colid = r.fkey6
  18710.     and permissions(o1.id) <> 0
  18711.     and permissions(o2.id) <> 0
  18712.     and i.id = r.rkeyid
  18713.     and i.indid = r.rkeyindid       
  18714.     union all
  18715.     select
  18716.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18717.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18718.         PK_TABLE_NAME       = o1.name,
  18719.         PK_COLUMN_NAME      = c1.name,
  18720.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18721.         PK_COLUMN_PROPID    = convert(int,null),
  18722.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18723.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18724.         FK_TABLE_NAME       = o2.name,
  18725.         FK_COLUMN_NAME      = c2.name,
  18726.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18727.         FK_COLUMN_PROPID    = convert(int,null),
  18728.         ORDINAL             = convert(int,7),
  18729.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18730.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18731.         PK_NAME             = i.name,
  18732.         FK_NAME             = object_name(r.constid),
  18733.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18734.     from
  18735.         sysobjects o1, sysobjects o2,
  18736.         syscolumns c1, syscolumns c2,
  18737.         sysreferences r, sysindexes i
  18738.     where   
  18739.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18740.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18741.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18742.     and o1.id = r.rkeyid
  18743.     and r.keycnt >= 7
  18744.     and o1.id = c1.id
  18745.     and c1.colid = r.rkey7
  18746.     and r.fkeyid = o2.id
  18747.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18748.     and o2.id = c2.id   
  18749.     and c2.colid = r.fkey7
  18750.     and permissions(o1.id) <> 0
  18751.     and permissions(o2.id) <> 0
  18752.     and i.id = r.rkeyid
  18753.     and i.indid = r.rkeyindid       
  18754.     union all
  18755.     select
  18756.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18757.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18758.         PK_TABLE_NAME       = o1.name,
  18759.         PK_COLUMN_NAME      = c1.name,
  18760.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18761.         PK_COLUMN_PROPID    = convert(int,null),
  18762.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18763.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18764.         FK_TABLE_NAME       = o2.name,
  18765.         FK_COLUMN_NAME      = c2.name,
  18766.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18767.         FK_COLUMN_PROPID    = convert(int,null),
  18768.         ORDINAL             = convert(int,8),
  18769.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18770.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18771.         PK_NAME             = i.name,
  18772.         FK_NAME             = object_name(r.constid),
  18773.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18774.     from
  18775.         sysobjects o1, sysobjects o2,
  18776.         syscolumns c1, syscolumns c2,
  18777.         sysreferences r, sysindexes i
  18778.     where   
  18779.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18780.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18781.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18782.     and o1.id = r.rkeyid
  18783.     and r.keycnt >= 8
  18784.     and o1.id = c1.id
  18785.     and c1.colid = r.rkey8
  18786.     and r.fkeyid = o2.id
  18787.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18788.     and o2.id = c2.id   
  18789.     and c2.colid = r.fkey8
  18790.     and permissions(o1.id) <> 0
  18791.     and permissions(o2.id) <> 0
  18792.     and i.id = r.rkeyid
  18793.     and i.indid = r.rkeyindid       
  18794.     union all
  18795.     select
  18796.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18797.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18798.         PK_TABLE_NAME       = o1.name,
  18799.         PK_COLUMN_NAME      = c1.name,
  18800.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18801.         PK_COLUMN_PROPID    = convert(int,null),
  18802.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18803.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18804.         FK_TABLE_NAME       = o2.name,
  18805.         FK_COLUMN_NAME      = c2.name,
  18806.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18807.         FK_COLUMN_PROPID    = convert(int,null),
  18808.         ORDINAL             = convert(int,9),
  18809.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18810.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18811.         PK_NAME             = i.name,
  18812.         FK_NAME             = object_name(r.constid),
  18813.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18814.     from
  18815.         sysobjects o1, sysobjects o2,
  18816.         syscolumns c1, syscolumns c2,
  18817.         sysreferences r, sysindexes i
  18818.     where   
  18819.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18820.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18821.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18822.     and o1.id = r.rkeyid
  18823.     and r.keycnt >= 9
  18824.     and o1.id = c1.id
  18825.     and c1.colid = r.rkey9
  18826.     and r.fkeyid = o2.id
  18827.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18828.     and o2.id = c2.id   
  18829.     and c2.colid = r.fkey9
  18830.     and permissions(o1.id) <> 0
  18831.     and permissions(o2.id) <> 0
  18832.     and i.id = r.rkeyid
  18833.     and i.indid = r.rkeyindid       
  18834.     union all
  18835.     select
  18836.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18837.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18838.         PK_TABLE_NAME       = o1.name,
  18839.         PK_COLUMN_NAME      = c1.name,
  18840.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18841.         PK_COLUMN_PROPID    = convert(int,null),
  18842.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18843.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18844.         FK_TABLE_NAME       = o2.name,
  18845.         FK_COLUMN_NAME      = c2.name,
  18846.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18847.         FK_COLUMN_PROPID    = convert(int,null),
  18848.         ORDINAL             = convert(int,10),
  18849.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18850.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18851.         PK_NAME             = i.name,
  18852.         FK_NAME             = object_name(r.constid),
  18853.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18854.     from
  18855.         sysobjects o1, sysobjects o2,
  18856.         syscolumns c1, syscolumns c2,
  18857.         sysreferences r, sysindexes i
  18858.     where   
  18859.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18860.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18861.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18862.     and o1.id = r.rkeyid
  18863.     and r.keycnt >= 10
  18864.     and o1.id = c1.id
  18865.     and c1.colid = r.rkey10
  18866.     and r.fkeyid = o2.id
  18867.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18868.     and o2.id = c2.id   
  18869.     and c2.colid = r.fkey10
  18870.     and permissions(o1.id) <> 0
  18871.     and permissions(o2.id) <> 0
  18872.     and i.id = r.rkeyid
  18873.     and i.indid = r.rkeyindid       
  18874.     union all
  18875.     select
  18876.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18877.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18878.         PK_TABLE_NAME       = o1.name,
  18879.         PK_COLUMN_NAME      = c1.name,
  18880.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18881.         PK_COLUMN_PROPID    = convert(int,null),
  18882.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18883.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18884.         FK_TABLE_NAME       = o2.name,
  18885.         FK_COLUMN_NAME      = c2.name,
  18886.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18887.         FK_COLUMN_PROPID    = convert(int,null),
  18888.         ORDINAL             = convert(int,11),
  18889.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18890.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18891.         PK_NAME             = i.name,
  18892.         FK_NAME             = object_name(r.constid),
  18893.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18894.     from
  18895.         sysobjects o1, sysobjects o2,
  18896.         syscolumns c1, syscolumns c2,
  18897.         sysreferences r, sysindexes i
  18898.     where   
  18899.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18900.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18901.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18902.     and o1.id = r.rkeyid
  18903.     and r.keycnt >= 11
  18904.     and o1.id = c1.id
  18905.     and c1.colid = r.rkey11
  18906.     and r.fkeyid = o2.id
  18907.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18908.     and o2.id = c2.id   
  18909.     and c2.colid = r.fkey11
  18910.     and permissions(o1.id) <> 0
  18911.     and permissions(o2.id) <> 0
  18912.     and i.id = r.rkeyid
  18913.     and i.indid = r.rkeyindid       
  18914.     union all
  18915.     select
  18916.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18917.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18918.         PK_TABLE_NAME       = o1.name,
  18919.         PK_COLUMN_NAME      = c1.name,
  18920.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18921.         PK_COLUMN_PROPID    = convert(int,null),
  18922.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18923.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18924.         FK_TABLE_NAME       = o2.name,
  18925.         FK_COLUMN_NAME      = c2.name,
  18926.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18927.         FK_COLUMN_PROPID    = convert(int,null),
  18928.         ORDINAL             = convert(int,12),
  18929.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18930.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18931.         PK_NAME             = i.name,
  18932.         FK_NAME             = object_name(r.constid),
  18933.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18934.     from
  18935.         sysobjects o1, sysobjects o2,
  18936.         syscolumns c1, syscolumns c2,
  18937.         sysreferences r, sysindexes i
  18938.     where   
  18939.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18940.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18941.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18942.     and o1.id = r.rkeyid
  18943.     and r.keycnt >= 12
  18944.     and o1.id = c1.id
  18945.     and c1.colid = r.rkey12
  18946.     and r.fkeyid = o2.id
  18947.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18948.     and o2.id = c2.id   
  18949.     and c2.colid = r.fkey12
  18950.     and permissions(o1.id) <> 0
  18951.     and permissions(o2.id) <> 0
  18952.     and i.id = r.rkeyid
  18953.     and i.indid = r.rkeyindid       
  18954.     union all
  18955.     select
  18956.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18957.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18958.         PK_TABLE_NAME       = o1.name,
  18959.         PK_COLUMN_NAME      = c1.name,
  18960.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18961.         PK_COLUMN_PROPID    = convert(int,null),
  18962.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18963.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18964.         FK_TABLE_NAME       = o2.name,
  18965.         FK_COLUMN_NAME      = c2.name,
  18966.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  18967.         FK_COLUMN_PROPID    = convert(int,null),
  18968.         ORDINAL             = convert(int,13),
  18969.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18970.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18971.         PK_NAME             = i.name,
  18972.         FK_NAME             = object_name(r.constid),
  18973.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18974.     from
  18975.         sysobjects o1, sysobjects o2,
  18976.         syscolumns c1, syscolumns c2,
  18977.         sysreferences r, sysindexes i
  18978.     where   
  18979.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18980.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18981.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18982.     and o1.id = r.rkeyid
  18983.     and r.keycnt >= 13
  18984.     and o1.id = c1.id
  18985.     and c1.colid = r.rkey13
  18986.     and r.fkeyid = o2.id
  18987.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18988.     and o2.id = c2.id   
  18989.     and c2.colid = r.fkey13
  18990.     and permissions(o1.id) <> 0
  18991.     and permissions(o2.id) <> 0
  18992.     and i.id = r.rkeyid
  18993.     and i.indid = r.rkeyindid       
  18994.     union all
  18995.     select
  18996.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18997.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  18998.         PK_TABLE_NAME       = o1.name,
  18999.         PK_COLUMN_NAME      = c1.name,
  19000.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19001.         PK_COLUMN_PROPID    = convert(int,null),
  19002.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  19003.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  19004.         FK_TABLE_NAME       = o2.name,
  19005.         FK_COLUMN_NAME      = c2.name,
  19006.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19007.         FK_COLUMN_PROPID    = convert(int,null),
  19008.         ORDINAL             = convert(int,14),
  19009.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19010.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19011.         PK_NAME             = i.name,
  19012.         FK_NAME             = object_name(r.constid),
  19013.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19014.     from
  19015.         sysobjects o1, sysobjects o2,
  19016.         syscolumns c1, syscolumns c2,
  19017.         sysreferences r, sysindexes i
  19018.     where   
  19019.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19020.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19021.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19022.     and o1.id = r.rkeyid
  19023.     and r.keycnt >= 14
  19024.     and o1.id = c1.id
  19025.     and c1.colid = r.rkey14
  19026.     and r.fkeyid = o2.id
  19027.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19028.     and o2.id = c2.id   
  19029.     and c2.colid = r.fkey14
  19030.     and permissions(o1.id) <> 0
  19031.     and permissions(o2.id) <> 0
  19032.     and i.id = r.rkeyid
  19033.     and i.indid = r.rkeyindid       
  19034.     union all
  19035.     select
  19036.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  19037.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  19038.         PK_TABLE_NAME       = o1.name,
  19039.         PK_COLUMN_NAME      = c1.name,
  19040.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19041.         PK_COLUMN_PROPID    = convert(int,null),
  19042.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  19043.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  19044.         FK_TABLE_NAME       = o2.name,
  19045.         FK_COLUMN_NAME      = c2.name,
  19046.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19047.         FK_COLUMN_PROPID    = convert(int,null),
  19048.         ORDINAL             = convert(int,15),
  19049.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19050.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19051.         PK_NAME             = i.name,
  19052.         FK_NAME             = object_name(r.constid),
  19053.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19054.     from
  19055.         sysobjects o1, sysobjects o2,
  19056.         syscolumns c1, syscolumns c2,
  19057.         sysreferences r, sysindexes i
  19058.     where   
  19059.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19060.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19061.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19062.     and o1.id = r.rkeyid
  19063.     and r.keycnt >= 15
  19064.     and o1.id = c1.id
  19065.     and c1.colid = r.rkey15
  19066.     and r.fkeyid = o2.id
  19067.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19068.     and o2.id = c2.id   
  19069.     and c2.colid = r.fkey15
  19070.     and permissions(o1.id) <> 0
  19071.     and permissions(o2.id) <> 0
  19072.     and i.id = r.rkeyid
  19073.     and i.indid = r.rkeyindid       
  19074.     union all
  19075.     select
  19076.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  19077.         PK_TABLE_SCHEMA     = user_name(o1.uid),
  19078.         PK_TABLE_NAME       = o1.name,
  19079.         PK_COLUMN_NAME      = c1.name,
  19080.         PK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19081.         PK_COLUMN_PROPID    = convert(int,null),
  19082.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  19083.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  19084.         FK_TABLE_NAME       = o2.name,
  19085.         FK_COLUMN_NAME      = c2.name,
  19086.         FK_COLUMN_GUID      = convert(uniqueidentifier,null),
  19087.         FK_COLUMN_PROPID    = convert(int,null),
  19088.         ORDINAL             = convert(int,16),
  19089.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19090.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19091.         PK_NAME             = i.name,
  19092.         FK_NAME             = object_name(r.constid),
  19093.         DEFERRABILITY       = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19094.     from
  19095.         sysobjects o1, sysobjects o2,
  19096.         syscolumns c1, syscolumns c2,
  19097.         sysreferences r, sysindexes i
  19098.     where   
  19099.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19100.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19101.     and (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19102.     and o1.id = r.rkeyid
  19103.     and o1.id = c1.id
  19104.     and r.keycnt >= 16
  19105.     and c1.colid = r.rkey16
  19106.     and r.fkeyid = o2.id
  19107.     and (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19108.     and o2.id = c2.id   
  19109.     and c2.colid = r.fkey16
  19110.     and permissions(o1.id) <> 0
  19111.     and permissions(o2.id) <> 0
  19112.     and i.id = r.rkeyid
  19113.     and i.indid = r.rkeyindid       
  19114.     order by 8,9,2,3,13
  19115. go
  19116. dump tran master with no_log
  19117. go
  19118. create procedure sp_foreign_keys_rowset;5
  19119.     (
  19120.     @server_name        sysname,
  19121.     @pk_catalog_name    sysname = null,
  19122.     @fk_catalog_name    sysname = null,
  19123.     @pk_table_name      sysname = null,
  19124.     @pk_table_schema    sysname = null,
  19125.     @fk_table_name      sysname = null,
  19126.     @fk_table_schema    sysname = null
  19127.     )
  19128. as
  19129.     select
  19130.         PK_TABLE_CATALOG,
  19131.         PK_TABLE_SCHEMA,    
  19132.         PK_TABLE_NAME,  
  19133.         PK_COLUMN_NAME,     
  19134.         PK_COLUMN_GUID, 
  19135.         PK_COLUMN_PROPID,
  19136.         FK_TABLE_CATALOG,
  19137.         FK_TABLE_SCHEMA, 
  19138.         FK_TABLE_NAME,  
  19139.         FK_COLUMN_NAME,     
  19140.         FK_COLUMN_GUID, 
  19141.         FK_COLUMN_PROPID,
  19142.         ORDINAL,        
  19143.         UPDATE_RULE,    
  19144.         DELETE_RULE
  19145.         --PK_NAME,      
  19146.         --FK_NAME,
  19147.         --DEFERRABILITY 
  19148.     from master.dbo.SYSREMOTE_FOREIGN_KEYS (
  19149.                 @server_name,
  19150.                 @pk_catalog_name,
  19151.                 @pk_table_schema,
  19152.                 @pk_table_name,
  19153.                 @fk_catalog_name,
  19154.                 @fk_table_schema,
  19155.                 @fk_table_name )
  19156.     order by 7,8,9,1,2,3,13
  19157. go
  19158.  
  19159. grant execute on sp_foreign_keys_rowset to public
  19160. go
  19161.  
  19162. dump tran master with no_log
  19163. go
  19164. if (charindex('6.00', @@version) > 0)
  19165.     begin
  19166.     if (exists (select * from sysobjects
  19167.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  19168.         begin
  19169.         drop procedure sp_foreign_keys_rowset
  19170.         dump tran master with no_log
  19171.         end
  19172.     end
  19173. go
  19174.  
  19175.  
  19176. print ''
  19177. print 'creating sp_indexes_rowset'
  19178. go
  19179.  
  19180. /*  6.0 and 6.5 version */
  19181. create procedure sp_indexes_rowset
  19182.     (
  19183.     @table_name varchar(255), 
  19184.     @index_name varchar(255) = null,
  19185.     @table_schema   varchar(255) = null  
  19186.     )
  19187. as
  19188.     select  TABLE_CATALOG       = db_name(),
  19189.         TABLE_SCHEMA        = user_name(o.uid),
  19190.         TABLE_NAME      = o.name,
  19191.         INDEX_CATALOG       = db_name(),        
  19192.         INDEX_SCHEMA        = user_name(o.uid),
  19193.         INDEX_NAME      = x.name,
  19194.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19195.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19196.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19197.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19198.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19199.         INITIAL_SIZE        = convert(int,null),
  19200.         NULLS           = convert(int,null),
  19201.         SORT_BOOKMARKS      = convert(bit,0),
  19202.         AUTO_UPDATE     = convert(bit,1),
  19203.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19204.         ORDINAL_POSITION    = convert(int,
  19205.                        (
  19206.                         select count(*)
  19207.                         from syscolumns sc
  19208.                         where sc.id     =  c.id
  19209.                           AND sc.number =  c.number
  19210.                           AND sc.colid  <= c.colid
  19211.                         )),
  19212.         COLUMN_NAME     = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19213.         COLUMN_GUID     = convert(binary(16),null),
  19214.         COLUMN_PROPID       = convert(int,null),
  19215.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19216.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19217.         PAGES           = convert(int, x.dpages),
  19218.         FILTER_CONDITION    = convert(varchar(1),null),
  19219.         INTEGRATED      = convert(bit,(x.status & 16)/16)
  19220.         
  19221.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19222.     where   o.type in ('U')
  19223.     and     o.name = @table_name
  19224.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19225.     and (@index_name is null or @index_name = x.name)
  19226.     and     x.id = o.id
  19227.     and     x.id = c.id
  19228.     and     c.colid < x.keycnt+(x.status&16)/16
  19229.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19230.     and     (
  19231.         suser_id() = 1   /* User is the System Administrator */
  19232.         or o.uid = user_id()     /* User created the object */
  19233.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19234.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19235.             from sysprotects p
  19236.             where p.id = o.id
  19237.             /* get rows for public,current user,user's group */
  19238.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19239.             /* check for SELECT,EXECUTE privilege */
  19240.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19241.             ) = 1   /* final magic...compare Grants   */
  19242.            )
  19243.     order by 8 desc, 4, 5, 6, 17
  19244. go
  19245. dump tran master with no_log
  19246. go
  19247. create procedure sp_indexes_rowset;2
  19248.     (
  19249.     @index_name varchar(255) = null,
  19250.     @table_schema   varchar(255) = null  
  19251.     )
  19252. as
  19253.     select  TABLE_CATALOG       = db_name(),
  19254.         TABLE_SCHEMA        = user_name(o.uid),
  19255.         TABLE_NAME      = o.name,
  19256.         INDEX_CATALOG       = db_name(),        
  19257.         INDEX_SCHEMA        = user_name(o.uid),
  19258.         INDEX_NAME      = x.name,
  19259.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19260.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19261.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19262.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19263.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19264.         INITIAL_SIZE        = convert(int,null),
  19265.         NULLS           = convert(int,null),
  19266.         SORT_BOOKMARKS      = convert(bit,0),
  19267.         AUTO_UPDATE     = convert(bit,1),
  19268.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19269.         ORDINAL_POSITION    = convert(int,
  19270.                        (
  19271.                         select count(*)
  19272.                         from syscolumns sc
  19273.                         where sc.id     =  c.id
  19274.                           AND sc.number =  c.number
  19275.                           AND sc.colid  <= c.colid
  19276.                         )),
  19277.         COLUMN_NAME     = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19278.         COLUMN_GUID     = convert(binary(16),null),
  19279.         COLUMN_PROPID       = convert(int,null),
  19280.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19281.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19282.         PAGES           = convert(int, x.dpages),
  19283.         FILTER_CONDITION    = convert(varchar(1),null),
  19284.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19285.         
  19286.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19287.     where   o.type in ('U')
  19288.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19289.     and (@index_name is null or @index_name = x.name)
  19290.     and     x.id = o.id
  19291.     and     x.id = c.id
  19292.     and     c.colid < x.keycnt+(x.status&16)/16
  19293.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19294.     and     (
  19295.         suser_id() = 1   /* User is the System Administrator */
  19296.         or o.uid = user_id()     /* User created the object */
  19297.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19298.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19299.             from sysprotects p
  19300.             where p.id = o.id
  19301.             /* get rows for public,current user,user's group */
  19302.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19303.             /* check for SELECT,EXECUTE privilege */
  19304.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19305.             ) = 1   /* final magic...compare Grants   */
  19306.            )
  19307.     order by 8 desc, 4, 5, 6, 17
  19308. go
  19309. dump tran master with no_log
  19310. go
  19311.  
  19312. if (charindex('7.00', @@version) > 0 or
  19313.     charindex('8.00', @@version) > 0)
  19314.     drop procedure sp_indexes_rowset
  19315. else
  19316. begin
  19317.     print ''
  19318.     print ''
  19319.     print 'Warning:'
  19320.     print 'you are installing the stored procedures '
  19321.     print 'on a pre 7.0 SQL Server.'
  19322.     print 'Ignore the following errors.'
  19323. end
  19324. go
  19325.  
  19326. /*  7.0 version */
  19327. create procedure sp_indexes_rowset
  19328.     (
  19329.     @table_name sysname, 
  19330.     @index_name sysname = null,
  19331.     @table_schema   sysname = null   
  19332.     )
  19333. as
  19334.     select  TABLE_CATALOG       = db_name(),
  19335.         TABLE_SCHEMA        = user_name(o.uid),
  19336.         TABLE_NAME      = o.name,
  19337.         INDEX_CATALOG       = db_name(),        
  19338.         INDEX_SCHEMA        = user_name(o.uid),
  19339.         INDEX_NAME      = x.name,
  19340.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19341.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19342.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19343.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19344.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19345.         INITIAL_SIZE        = convert(int,null),
  19346.         NULLS           = convert(int,null),
  19347.         SORT_BOOKMARKS      = convert(bit,0),
  19348.         AUTO_UPDATE     = convert(bit,1),
  19349.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19350.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19351.         COLUMN_NAME     = c.name,
  19352.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19353.         COLUMN_PROPID       = convert(int,null),
  19354.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19355.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19356.         PAGES           = convert(int, x.dpages),
  19357.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19358.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19359.         
  19360.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19361.     where   o.type in ('U')
  19362.     and     o.name = @table_name
  19363.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19364.     and (@index_name is null or @index_name = x.name)
  19365.     and x.id = o.id
  19366.     and o.id = c.id
  19367.     and o.id = xk.id
  19368.     and x.indid = xk.indid
  19369.     and c.colid = xk.colid
  19370.     and xk.keyno <= x.keycnt
  19371.     and permissions(o.id, c.name) <> 0
  19372.     and     (x.status&32) = 0  -- No hypothetical indexes
  19373.     order by 8 desc, 4, 5, 6, 17
  19374. go
  19375. dump tran master with no_log
  19376. go
  19377. create procedure sp_indexes_rowset;2
  19378.     (
  19379.     @index_name sysname = null,
  19380.     @table_schema   sysname = null   
  19381.     )
  19382. as
  19383.     select  TABLE_CATALOG       = db_name(),
  19384.         TABLE_SCHEMA        = user_name(o.uid),
  19385.         TABLE_NAME      = o.name,
  19386.         INDEX_CATALOG       = db_name(),        
  19387.         INDEX_SCHEMA        = user_name(o.uid),
  19388.         INDEX_NAME      = x.name,
  19389.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19390.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19391.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19392.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19393.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19394.         INITIAL_SIZE        = convert(int,null),
  19395.         NULLS           = convert(int,null),
  19396.         SORT_BOOKMARKS      = convert(bit,0),
  19397.         AUTO_UPDATE     = convert(bit,1),
  19398.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19399.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19400.         COLUMN_NAME     = c.name,
  19401.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19402.         COLUMN_PROPID       = convert(int,null),
  19403.         COLLATION   = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19404.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19405.         PAGES           = convert(int, x.dpages),
  19406.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19407.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19408.         
  19409.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19410.     where   o.type in ('U')
  19411.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19412.     and (@index_name is null or @index_name = x.name)
  19413.     and x.id = o.id
  19414.     and o.id = c.id
  19415.     and o.id = xk.id
  19416.     and x.indid = xk.indid
  19417.     and c.colid = xk.colid
  19418.     and xk.keyno <= x.keycnt
  19419.     and permissions(o.id, c.name) <> 0
  19420.     and (x.status&32) = 0  -- No hypothetical indexes
  19421.     order by 8 desc, 4, 5, 6, 17
  19422. go
  19423. dump tran master with no_log
  19424. go
  19425. create procedure sp_indexes_rowset;5
  19426.     (
  19427.     @table_server       sysname,
  19428.     @table_catalog      sysname = null,
  19429.     @table_name     sysname = null,
  19430.     @index_name     sysname = null,
  19431.     @table_schema       sysname = null
  19432.     )
  19433. as
  19434.     select
  19435.         TABLE_CATALOG,  
  19436.         TABLE_SCHEMA,   
  19437.         TABLE_NAME, 
  19438.         INDEX_CATALOG,      
  19439.         INDEX_SCHEMA,   
  19440.         INDEX_NAME, 
  19441.         PRIMARY_KEY,    
  19442.         "UNIQUE",   
  19443.         "CLUSTERED",    
  19444.         "TYPE",     
  19445.         FILL_FACTOR,    
  19446.         INITIAL_SIZE,   
  19447.         NULLS,      
  19448.         SORT_BOOKMARKS, 
  19449.         AUTO_UPDATE,    
  19450.         NULL_COLLATION, 
  19451.         ORDINAL_POSITION,
  19452.         COLUMN_NAME,    
  19453.         COLUMN_GUID,    
  19454.         COLUMN_PROPID,  
  19455.         COLLATION,  
  19456.         CARDINALITY,    
  19457.         PAGES,      
  19458.         FILTER_CONDITION
  19459.     --  INTEGRATED
  19460.     from master.dbo.SYSREMOTE_INDEXES (
  19461.                 @table_server,
  19462.                 @table_catalog,
  19463.                 @table_schema,
  19464.                 @index_name,
  19465.                 NULL,           /* TYPE (index type) */
  19466.                 @table_name )
  19467.     order by 8 desc, 4, 5, 6, 17
  19468. go
  19469.  
  19470. grant execute on sp_indexes_rowset to public
  19471. go
  19472.  
  19473. dump tran master with no_log
  19474. go
  19475.  
  19476. if (charindex('8.00', @@version) > 0)
  19477.     drop procedure sp_indexes_rowset
  19478. else
  19479. begin
  19480.     print ''
  19481.     print ''
  19482.     print 'Warning:'
  19483.     print 'you are installing the stored procedures '
  19484.     print 'on a pre 8.0 SQL Server.'
  19485.     print 'Ignore the following errors.'
  19486. end
  19487. go
  19488.  
  19489. /*  8.0 version */
  19490. create procedure sp_indexes_rowset
  19491.     (
  19492.     @table_name sysname, 
  19493.     @index_name sysname = null,
  19494.     @table_schema   sysname = null   
  19495.     )
  19496. as
  19497.     select  TABLE_CATALOG       = db_name(),
  19498.         TABLE_SCHEMA        = user_name(o.uid),
  19499.         TABLE_NAME      = o.name,
  19500.         INDEX_CATALOG       = db_name(),        
  19501.         INDEX_SCHEMA        = user_name(o.uid),
  19502.         INDEX_NAME      = x.name,
  19503.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19504.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19505.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19506.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19507.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19508.         INITIAL_SIZE        = convert(int,null),
  19509.         NULLS           = convert(int,null),
  19510.         SORT_BOOKMARKS      = convert(bit,0),
  19511.         AUTO_UPDATE     = convert(bit,1),
  19512.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19513.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19514.         COLUMN_NAME     = c.name,
  19515.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19516.         COLUMN_PROPID       = convert(int,null),
  19517.         COLLATION   = convert(smallint,
  19518.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19519.                         then 2      /* DB_COLLATION_DESC */ 
  19520.                         else 1      /* DB_COLLATION_ASC */ 
  19521.                         end),
  19522.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19523.         PAGES           = convert(int, x.dpages),
  19524.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19525.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19526.         
  19527.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19528.     where   o.type in ('U')
  19529.     and     o.name = @table_name
  19530.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19531.     and (@index_name is null or @index_name = x.name)
  19532.     and x.id = o.id
  19533.     and o.id = c.id
  19534.     and o.id = xk.id
  19535.     and x.indid = xk.indid
  19536.     and c.colid = xk.colid
  19537.     and xk.keyno <= x.keycnt
  19538.     and permissions(o.id, c.name) <> 0
  19539.     and     (x.status&32) = 0  -- No hypothetical indexes
  19540.     order by 8 desc, 4, 5, 6, 17
  19541. go
  19542. dump tran master with no_log
  19543. go
  19544. create procedure sp_indexes_rowset;2
  19545.     (
  19546.     @index_name sysname = null,
  19547.     @table_schema   sysname = null   
  19548.     )
  19549. as
  19550.     select  TABLE_CATALOG       = db_name(),
  19551.         TABLE_SCHEMA        = user_name(o.uid),
  19552.         TABLE_NAME      = o.name,
  19553.         INDEX_CATALOG       = db_name(),        
  19554.         INDEX_SCHEMA        = user_name(o.uid),
  19555.         INDEX_NAME      = x.name,
  19556.         PRIMARY_KEY     = convert(bit,(x.status & 0x800)/0x800),
  19557.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19558.         "CLUSTERED"     = convert(bit,(x.status & 16)/16),
  19559.         "TYPE"          = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19560.         FILL_FACTOR     = convert(int, x.OrigFillFactor),
  19561.         INITIAL_SIZE        = convert(int,null),
  19562.         NULLS           = convert(int,null),
  19563.         SORT_BOOKMARKS      = convert(bit,0),
  19564.         AUTO_UPDATE     = convert(bit,1),
  19565.         NULL_COLLATION      = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19566.         ORDINAL_POSITION    = convert(int, xk.keyno),
  19567.         COLUMN_NAME     = c.name,
  19568.         COLUMN_GUID     = convert(uniqueidentifier,null),
  19569.         COLUMN_PROPID       = convert(int,null),
  19570.         COLLATION   = convert(smallint,
  19571.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19572.                         then 2      /* DB_COLLATION_DESC */ 
  19573.                         else 1      /* DB_COLLATION_ASC */ 
  19574.                         end),
  19575.         CARDINALITY     = case when (x.status & 2) = 2 then x.rows else null end,
  19576.         PAGES           = convert(int, x.dpages),
  19577.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19578.         INTEGRATED      = convert(bit,(x.status & 16)/16) 
  19579.         
  19580.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19581.     where   o.type in ('U')
  19582.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19583.     and (@index_name is null or @index_name = x.name)
  19584.     and x.id = o.id
  19585.     and o.id = c.id
  19586.     and o.id = xk.id
  19587.     and x.indid = xk.indid
  19588.     and c.colid = xk.colid
  19589.     and xk.keyno <= x.keycnt
  19590.     and permissions(o.id, c.name) <> 0
  19591.     and (x.status&32) = 0  -- No hypothetical indexes
  19592.     order by 8 desc, 4, 5, 6, 17
  19593. go
  19594. dump tran master with no_log
  19595. go
  19596. create procedure sp_indexes_rowset;5
  19597.     (
  19598.     @table_server       sysname,
  19599.     @table_catalog      sysname = null,
  19600.     @table_name     sysname = null,
  19601.     @index_name     sysname = null,
  19602.     @table_schema       sysname = null
  19603.     )
  19604. as
  19605.     select
  19606.         TABLE_CATALOG,  
  19607.         TABLE_SCHEMA,   
  19608.         TABLE_NAME, 
  19609.         INDEX_CATALOG,      
  19610.         INDEX_SCHEMA,   
  19611.         INDEX_NAME, 
  19612.         PRIMARY_KEY,    
  19613.         "UNIQUE",   
  19614.         "CLUSTERED",    
  19615.         "TYPE",     
  19616.         FILL_FACTOR,    
  19617.         INITIAL_SIZE,   
  19618.         NULLS,      
  19619.         SORT_BOOKMARKS, 
  19620.         AUTO_UPDATE,    
  19621.         NULL_COLLATION, 
  19622.         ORDINAL_POSITION,
  19623.         COLUMN_NAME,    
  19624.         COLUMN_GUID,    
  19625.         COLUMN_PROPID,  
  19626.         COLLATION,  
  19627.         CARDINALITY,    
  19628.         PAGES,      
  19629.         FILTER_CONDITION
  19630.     --  INTEGRATED
  19631.     from master.dbo.SYSREMOTE_INDEXES (
  19632.                 @table_server,
  19633.                 @table_catalog,
  19634.                 @table_schema,
  19635.                 @index_name,
  19636.                 NULL,           /* TYPE (index type) */
  19637.                 @table_name )
  19638.     order by 8 desc, 4, 5, 6, 17
  19639. go
  19640.  
  19641. grant execute on sp_indexes_rowset to public
  19642. go
  19643.  
  19644. dump tran master with no_log
  19645. go
  19646. if (charindex('6.00', @@version) > 0)
  19647.     begin
  19648.     if (exists (select * from sysobjects
  19649.             where name = 'sp_indexes_rowset' and type = 'P '))
  19650.         begin
  19651.         drop procedure sp_indexes_rowset
  19652.         dump tran master with no_log
  19653.         end
  19654.     end
  19655. go
  19656.  
  19657.  
  19658. print ''
  19659. print 'creating sp_primary_keys_rowset'
  19660. go
  19661.  
  19662. /*  Procedure for 6.0 and 6.5 servers */
  19663. create procedure sp_primary_keys_rowset
  19664.     (
  19665.     @table_name     varchar(255),
  19666.     @table_schema       varchar(244) = null
  19667.     )
  19668. as
  19669.     select  TABLE_CATALOG   = db_name(),
  19670.             TABLE_SCHEMA    = user_name(o.uid),
  19671.             TABLE_NAME      = o.name,   
  19672.             COLUMN_NAME     = c.name,
  19673.             COLUMN_GUID     = convert(binary(16),null),
  19674.             COLUMN_PROPID   = convert(int,null),
  19675.             ORDINAL         = convert(int,
  19676.                                       case
  19677.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19678.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19679.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19680.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19681.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19682.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19683.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19684.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19685.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19686.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19687.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19688.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19689.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19690.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19691.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19692.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19693.                                       end
  19694.                                      ),
  19695.             PK_NAME         = i.name
  19696.     from    sysindexes i, syscolumns c, sysobjects o, sysusers u
  19697.     where   o.type in ('U')
  19698.     and     o.name = @table_name
  19699.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19700.     and     o.id = c.id
  19701.     and     o.id = i.id
  19702.     and     (i.status & 0x800) = 0x800
  19703.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19704.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19705.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19706.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19707.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19708.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19709.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19710.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19711.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19712.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19713.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19714.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19715.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19716.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19717.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19718.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19719.         )
  19720.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19721.     and     (
  19722.         suser_id() = 1   /* User is the System Administrator */
  19723.         or o.uid = user_id()     /* User created the object */
  19724.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19725.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19726.             from sysprotects p
  19727.             where p.id = o.id
  19728.             /* get rows for public,current user,user's group */
  19729.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19730.             /* check for SELECT,EXECUTE privilege */
  19731.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19732.             ) = 1   /* final magic...compare Grants   */
  19733.            )
  19734.     order by 2, 3
  19735. go
  19736. dump tran master with no_log
  19737. go
  19738. create procedure sp_primary_keys_rowset;2
  19739.     (
  19740.     @table_schema       varchar(244) = null
  19741.     )
  19742. as
  19743.     select  TABLE_CATALOG   = db_name(),
  19744.             TABLE_SCHEMA    = user_name(o.uid),
  19745.             TABLE_NAME      = o.name,   
  19746.             COLUMN_NAME     = c.name,
  19747.             COLUMN_GUID     = convert(binary(16),null),
  19748.             COLUMN_PROPID   = convert(int,null),
  19749.             ORDINAL         = convert(int,
  19750.                                       case
  19751.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19752.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19753.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19754.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19755.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19756.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19757.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19758.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19759.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19760.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19761.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19762.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19763.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19764.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19765.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19766.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19767.                                       end
  19768.                                      ),
  19769.             PK_NAME         = i.name
  19770.     from    sysindexes i, syscolumns c, sysobjects o, sysusers u
  19771.     where   o.type in ('U')
  19772.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19773.     and     o.id = c.id
  19774.     and     o.id = i.id
  19775.     and     (i.status & 0x800) = 0x800
  19776.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19777.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19778.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19779.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19780.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19781.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19782.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19783.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19784.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19785.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19786.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19787.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19788.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19789.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19790.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19791.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19792.         )
  19793.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19794.     and     (
  19795.         suser_id() = 1   /* User is the System Administrator */
  19796.         or o.uid = user_id()     /* User created the object */
  19797.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19798.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19799.             from sysprotects p
  19800.             where p.id = o.id
  19801.             /* get rows for public,current user,user's group */
  19802.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19803.             /* check for SELECT,EXECUTE privilege */
  19804.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19805.             ) = 1   /* final magic...compare Grants   */
  19806.            )
  19807.     order by 2, 3
  19808. go
  19809.  
  19810. if (charindex('7.00', @@version) = 0 and
  19811.     charindex('8.00', @@version) = 0)
  19812. begin
  19813.     print ''
  19814.     print ''
  19815.     print 'Warning:'
  19816.     print 'you are installing the stored procedures '
  19817.     print 'on a pre 8.0 SQL Server.'
  19818.     print 'Ignore the following errors.'
  19819. end
  19820. else
  19821.     drop proc sp_primary_keys_rowset
  19822. go
  19823.  
  19824. /*  Procedure for 7.0 & 8.0 servers */
  19825. create procedure sp_primary_keys_rowset
  19826.     (
  19827.     @table_name     sysname,
  19828.     @table_schema   sysname = null
  19829.     )
  19830. as
  19831.     select  TABLE_CATALOG   = db_name(),
  19832.             TABLE_SCHEMA    = user_name(o.uid),
  19833.             TABLE_NAME      = o.name,   
  19834.             COLUMN_NAME     = c.name,
  19835.             COLUMN_GUID     = convert(uniqueidentifier,null),
  19836.             COLUMN_PROPID   = convert(int,null),
  19837.             ORDINAL         = convert(int,
  19838.                                       case
  19839.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19840.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19841.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19842.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19843.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19844.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19845.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19846.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19847.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19848.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19849.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19850.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19851.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19852.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19853.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19854.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19855.                                       end
  19856.                                      ),
  19857.             PK_NAME         = i.name
  19858.     from    sysindexes i, syscolumns c, sysobjects o
  19859.     where   o.type in ('U')
  19860.     and     o.name = @table_name
  19861.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19862.     and     o.id = c.id
  19863.     and     o.id = i.id
  19864.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  19865.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19866.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19867.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19868.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19869.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19870.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19871.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19872.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19873.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19874.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19875.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19876.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19877.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19878.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19879.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19880.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19881.         )
  19882.     and permissions(o.id) <> 0
  19883.     order by 2, 3
  19884. go
  19885. dump tran master with no_log
  19886. go
  19887. create procedure sp_primary_keys_rowset;2
  19888.     (
  19889.     @table_schema   sysname = null
  19890.     )
  19891. as
  19892.     select  TABLE_CATALOG   = db_name(),
  19893.             TABLE_SCHEMA    = user_name(o.uid),
  19894.             TABLE_NAME      = o.name,   
  19895.             COLUMN_NAME     = c.name,
  19896.             COLUMN_GUID     = convert(uniqueidentifier,null),
  19897.             COLUMN_PROPID   = convert(int,null),
  19898.             ORDINAL         = convert(int,
  19899.                                       case
  19900.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19901.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19902.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19903.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19904.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19905.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19906.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19907.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19908.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19909.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19910.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19911.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19912.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19913.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19914.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19915.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19916.                                       end
  19917.                                      ),
  19918.             PK_NAME         = i.name
  19919.     from    sysindexes i, syscolumns c, sysobjects o
  19920.     where   o.type in ('U')
  19921.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19922.     and     o.id = c.id
  19923.     and     o.id = i.id
  19924.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  19925.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19926.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19927.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19928.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19929.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19930.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19931.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19932.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19933.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19934.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19935.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  19936.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  19937.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  19938.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  19939.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  19940.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  19941.         )
  19942.     and permissions(o.id) <> 0
  19943.     order by 2, 3
  19944. go
  19945. dump tran master with no_log
  19946. go
  19947. create procedure sp_primary_keys_rowset;3
  19948.     (
  19949.     @table_name     sysname = null,
  19950.     @table_schema       sysname = null
  19951.     )
  19952. as
  19953. IF @table_name is not NULL
  19954.     BEGIN
  19955.     select  TABLE_CATALOG   = db_name(),
  19956.             TABLE_SCHEMA    = user_name(o.uid),
  19957.             TABLE_NAME      = o.name,   
  19958.             COLUMN_NAME     = c.name,
  19959.             COLUMN_GUID     = convert(uniqueidentifier,null),
  19960.             COLUMN_PROPID   = convert(int,null),
  19961.             ORDINAL         = convert(int,
  19962.                                       case
  19963.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  19964.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  19965.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  19966.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  19967.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  19968.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  19969.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  19970.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  19971.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  19972.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  19973.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  19974.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  19975.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  19976.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  19977.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  19978.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  19979.                                       end
  19980.                                      ),
  19981.             PK_NAME         = i.name
  19982.     from    sysindexes i, syscolumns c, sysobjects o
  19983.     where   o.type in ('U')
  19984.     and     o.name = @table_name
  19985.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19986.     and     o.id = c.id
  19987.     and     o.id = i.id
  19988.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  19989.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  19990.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  19991.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  19992.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  19993.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  19994.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  19995.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  19996.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  19997.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  19998.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  19999.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  20000.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  20001.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  20002.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  20003.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  20004.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  20005.         )
  20006.     and permissions(o.id) <> 0
  20007.     END
  20008. ELSE
  20009.     BEGIN
  20010.     select  TABLE_CATALOG   = db_name(),
  20011.             TABLE_SCHEMA    = user_name(o.uid),
  20012.             TABLE_NAME      = o.name,   
  20013.             COLUMN_NAME     = c.name,
  20014.             COLUMN_GUID     = convert(uniqueidentifier,null),
  20015.             COLUMN_PROPID   = convert(int,null),
  20016.             ORDINAL         = convert(int,
  20017.                                       case
  20018.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  1) then 1
  20019.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  2) then 2
  20020.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  3) then 3
  20021.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  4) then 4
  20022.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  5) then 5
  20023.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  6) then 6
  20024.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  7) then 7
  20025.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  8) then 8
  20026.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid,  9) then 9
  20027.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 10) then 10
  20028.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 11) then 11
  20029.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 12) then 12
  20030.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 13) then 13
  20031.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 14) then 14
  20032.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 15) then 15
  20033.                                       when c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, 16) then 16
  20034.                                       end
  20035.                                      ),
  20036.             PK_NAME         = i.name
  20037.     from    sysindexes i, syscolumns c, sysobjects o
  20038.     where   o.type in ('U')
  20039.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  20040.     and     o.id = c.id
  20041.     and     o.id = i.id
  20042.     and     (i.status & 0x800) = 0x800 -- PRIMARY KEY
  20043.     and (c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  1) or
  20044.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  2) or
  20045.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  3) or
  20046.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  4) or
  20047.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  5) or
  20048.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  6) or
  20049.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  7) or
  20050.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  8) or
  20051.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid,  9) or
  20052.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 10) or
  20053.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 11) or
  20054.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 12) or
  20055.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 13) or
  20056.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 14) or
  20057.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 15) or
  20058.          c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, 16)
  20059.         )
  20060.     and permissions(o.id) <> 0
  20061.     END
  20062. go
  20063. dump tran master with no_log
  20064. go
  20065. create procedure sp_primary_keys_rowset;5
  20066.     (
  20067.     @table_server       sysname,
  20068.     @table_catalog      sysname = null,
  20069.     @table_name         sysname = null,
  20070.     @table_schema       sysname = null
  20071.     )
  20072. as
  20073.     select
  20074.         TABLE_CATALOG,  
  20075.         TABLE_SCHEMA,   
  20076.         TABLE_NAME, 
  20077.         COLUMN_NAME,    
  20078.         COLUMN_GUID,    
  20079.         COLUMN_PROPID,  
  20080.         ORDINAL
  20081.         --PK_NAME       
  20082.     from master.dbo.SYSREMOTE_PRIMARY_KEYS (
  20083.                 @table_server,
  20084.                 @table_catalog,
  20085.                 @table_schema,
  20086.                 @table_name )
  20087.     order by 1,2,3
  20088. go
  20089.  
  20090. grant execute on sp_primary_keys_rowset to public
  20091. go
  20092.  
  20093. dump tran master with no_log
  20094. go
  20095. if (charindex('6.00', @@version) > 0)
  20096.     begin
  20097.     if (exists (select * from sysobjects
  20098.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  20099.         begin
  20100.         drop procedure sp_primary_keys_rowset
  20101.         dump tran master with no_log
  20102.         end
  20103.     end
  20104. go
  20105.  
  20106.  
  20107. print ''
  20108. print 'creating sp_provider_types_rowset'
  20109. go
  20110.  
  20111. /*  Procedure for 6.0 and 6.50 servers */
  20112. create proc sp_provider_types_rowset
  20113.     (
  20114.     @data_type  smallint = null,
  20115.     @best_match tinyint  = null
  20116.     )
  20117. as
  20118.     select
  20119.         TYPE_NAME       = case when t.usertype = 80 then t.name 
  20120.                         else d.type_name 
  20121.                         end,
  20122.         DATA_TYPE       = d.oledb_data_type,                            
  20123.         COLUMN_SIZE     = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20124.                             then @@max_precision
  20125.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  20126.         LITERAL_PREFIX      = d.literal_prefix,
  20127.         LITERAL_SUFFIX      = d.literal_suffix,
  20128.         CREATE_PARAMS       = convert(varchar(32),e.CREATE_PARAMS),
  20129.         IS_NULLABLE     = t.allownulls,
  20130.         CASE_SENSITIVE      = d.case_sensitive,
  20131.         SEARCHABLE      = d.searchable,
  20132.         UNSIGNED_ATTRIBUTE  = d.unsigned_attribute,
  20133.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  20134.         AUTO_UNIQUE_VALUE   = d.auto_unique_value, 
  20135.         LOCAL_TYPE_NAME     = case  when t.usertype = 80 then t.name 
  20136.                         else d.local_type_name
  20137.                         end,
  20138.         MINIMUM_SCALE       = convert(smallint,
  20139.                         case 
  20140.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  20141.                         else null 
  20142.                         end),
  20143.         MAXIMUM_SCALE       = convert(smallint,
  20144.                         case 
  20145.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20146.                         then @@max_precision
  20147.                         else null 
  20148.                         end),
  20149.         GUID            = convert(binary(16),null),
  20150.         TYPELIB         = convert(varchar(1),null),
  20151.         VERSION         = convert(varchar(1),null),
  20152.         IS_LONG         = d.is_long,
  20153.         BEST_MATCH      = case when t.usertype = 80
  20154.                         then convert(bit,0)
  20155.                         else d.best_match
  20156.                         end,
  20157.         IS_FIXEDLENGTH      = convert(bit, 
  20158.                         case when d.fixlen is null then 0 else 1 end)
  20159.     from    master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  20160.     where   d.ss_dtype = t.type 
  20161.     and t.usertype <= 100
  20162.     and t.usertype <> 18 /* sysname */
  20163.     and (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  20164.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  20165.     and     t.usertype *= e.user_type 
  20166.     and     e.AUTO_INCREMENT = 0
  20167.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  20168.     and (@data_type is null or d.oledb_data_type = @data_type)
  20169.     and (@best_match is null or d.best_match = @best_match)
  20170.     order by 2
  20171. go
  20172. dump tran master with no_log
  20173. go
  20174.  
  20175. if (charindex('7.00', @@version) = 0 and
  20176.     charindex('8.00', @@version) = 0)
  20177. begin
  20178.     print ''
  20179.     print ''
  20180.     print 'Warning:'
  20181.     print 'you are installing the stored procedures '
  20182.     print 'on a pre 8.0 SQL Server.'
  20183.     print 'Ignore the following errors.'
  20184. end
  20185. else
  20186.     drop proc sp_provider_types_rowset
  20187. go
  20188.  
  20189. /*  Procedure for 8.0 server */
  20190. create proc sp_provider_types_rowset
  20191.     (
  20192.     @data_type  smallint = null,
  20193.     @best_match tinyint  = null
  20194.     )
  20195. as
  20196.     select
  20197.         TYPE_NAME       = case  when t.usertype = 80 then t.name 
  20198.                         else d.type_name 
  20199.                         end,
  20200.         DATA_TYPE       = d.oledb_data_type,                            
  20201.  
  20202.  
  20203.         COLUMN_SIZE     = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20204.                             then @@max_precision
  20205.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  20206.         LITERAL_PREFIX      = d.literal_prefix,
  20207.         LITERAL_SUFFIX      = d.literal_suffix,
  20208.         CREATE_PARAMS       = convert(nvarchar(32),e.CREATE_PARAMS),
  20209.         IS_NULLABLE     = t.allownulls,
  20210.         CASE_SENSITIVE      = d.case_sensitive,
  20211.         SEARCHABLE      = d.searchable,
  20212.         UNSIGNED_ATTRIBUTE  = d.unsigned_attribute,
  20213.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  20214.         AUTO_UNIQUE_VALUE   = d.auto_unique_value, 
  20215.         LOCAL_TYPE_NAME     = case  when t.usertype = 80 then t.name 
  20216.                         else d.local_type_name
  20217.                         end,
  20218.         MINIMUM_SCALE       = convert(smallint,
  20219.                         case 
  20220.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  20221.                         else null 
  20222.                         end),
  20223.         MAXIMUM_SCALE       = convert(smallint,
  20224.                         case 
  20225.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  20226.                         then @@max_precision
  20227.                         else null 
  20228.                         end),
  20229.         GUID            = convert(uniqueidentifier,null),
  20230.         TYPELIB         = convert(nvarchar(1),null),
  20231.         VERSION         = convert(nvarchar(1),null),
  20232.         IS_LONG         = d.is_long,
  20233.         BEST_MATCH      = case  when t.usertype = 80
  20234.                         then convert(bit,0)
  20235.                         else d.best_match
  20236.                         end,
  20237.         IS_FIXEDLENGTH      = convert(bit, 
  20238.                         case when d.fixlen is null then 0 else 1 end)
  20239.  
  20240.     from master.dbo.spt_provider_types d
  20241.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  20242.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  20243.             t.xusertype = e.user_type
  20244.             and e.AUTO_INCREMENT = 0
  20245.     where   (@data_type is null or d.oledb_data_type = @data_type)
  20246.     and (@best_match is null or d.best_match = @best_match) 
  20247.     and t.usertype <= 255
  20248.     and t.usertype <> 18 /* sysname */
  20249.     order by 2
  20250. go
  20251.  
  20252. grant execute on sp_provider_types_rowset to public
  20253. go
  20254.  
  20255. dump tran master with no_log
  20256. go
  20257. if (charindex('6.00', @@version) > 0)
  20258.     begin
  20259.     if (exists (select * from sysobjects
  20260.         where name = 'sp_provider_types_rowset' and type = 'P '))
  20261.         begin
  20262.         drop procedure sp_provider_types_rowset
  20263.         dump tran master with no_log
  20264.         end
  20265.     end
  20266. go
  20267.  
  20268.  
  20269. print ''
  20270. print 'creating sp_procedure_params_rowset'
  20271. go
  20272.  
  20273. /*  Procedure for 6.0 and 6.50 servers */
  20274. create procedure sp_procedure_params_rowset
  20275.     (
  20276.     @procedure_name     varchar(255) = null,
  20277.     @group_number       int = null,
  20278.     @procedure_schema   varchar(255) = null,
  20279.     @parameter_name     varchar(255) = null
  20280.     )
  20281. as
  20282. IF @procedure_name is not null
  20283.     BEGIN
  20284.     select
  20285.         PROCEDURE_CATALOG   = db_name(),
  20286.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20287.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20288.         PARAMETER_NAME      = c.name,
  20289.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20290.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20291.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20292.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20293.         IS_NULLABLE     = convert(bit,
  20294.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20295.                         then 0 else 1
  20296.                         end),
  20297.         DATA_TYPE       = d.oledb_data_type,
  20298.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20299.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20300.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20301.                         then coalesce(d.column_size,c.length)
  20302.                         else null 
  20303.                         end),
  20304.         CHARACTER_OCTET_LENGTH  = convert(int,
  20305.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20306.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20307.                         then coalesce(d.column_size,c.length)
  20308.                         else null 
  20309.                         end),
  20310.         NUMERIC_PRECISION   = convert(smallint,
  20311.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  20312.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20313.                             then d.data_precision else null end),
  20314.         NUMERIC_SCALE       = convert(smallint, 
  20315.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20316.         DESCRIPTION     = convert(varchar(1),null),
  20317.         TYPE_NAME       = d.type_name,
  20318.         LOCAL_TYPE_NAME     = d.local_type_name
  20319.     
  20320.     from
  20321.         syscolumns c,
  20322.         sysobjects o,
  20323.         master.dbo.spt_provider_types d,
  20324.         systypes t
  20325.     where
  20326.         o.name = @procedure_name
  20327.     and o.type = 'P'                            /* Just Procedures */
  20328.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20329.     and o.id = c.id
  20330.     and c.number = @group_number
  20331.     and c.type = d.ss_dtype
  20332.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20333.     and c.usertype = t.usertype
  20334.     and (t.usertype != 80 or d.type_name='timestamp')
  20335.     and (@parameter_name is null or @parameter_name = c.name)
  20336.     UNION ALL
  20337.     SELECT         /* return value row*/
  20338.         PROCEDURE_CATALOG   = db_name(),
  20339.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20340.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20341.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20342.         ORDINAL_POSITION    = convert(smallint,0),
  20343.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20344.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20345.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20346.         IS_NULLABLE     = convert(bit,0),
  20347.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20348.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20349.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20350.         NUMERIC_PRECISION   = convert(smallint,10),
  20351.         NUMERIC_SCALE       = convert(smallint,null),
  20352.         DESCRIPTION     = convert(varchar(1),null),
  20353.         TYPE_NAME       = convert(sysname,'int'),
  20354.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20355.     from
  20356.         syscomments c,
  20357.         sysobjects o
  20358.     where
  20359.         o.name = @procedure_name
  20360.     and o.type = 'P'                        /* Just Procedures */
  20361.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20362.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20363.     and c.id = o.id
  20364.     and c.number = @group_number
  20365.     and c.colid = 1
  20366.     order by 2, 3, 5
  20367.     END
  20368. ELSE
  20369.     BEGIN
  20370.     select
  20371.         PROCEDURE_CATALOG   = db_name(),
  20372.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20373.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20374.         PARAMETER_NAME      = c.name,
  20375.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20376.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20377.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20378.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20379.         IS_NULLABLE     = convert(bit,
  20380.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20381.                         then 0 else 1
  20382.                         end),
  20383.         DATA_TYPE       = d.oledb_data_type,
  20384.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20385.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20386.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20387.                         then coalesce(d.column_size,c.length)
  20388.                         else null 
  20389.                         end),
  20390.         CHARACTER_OCTET_LENGTH  = convert(int,
  20391.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20392.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20393.                         then coalesce(d.column_size,c.length)
  20394.                         else null 
  20395.                         end),
  20396.         NUMERIC_PRECISION   = convert(smallint,
  20397.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20398.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20399.                             then d.data_precision else null end),
  20400.         NUMERIC_SCALE       = convert(smallint, 
  20401.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20402.         DESCRIPTION     = convert(varchar(1),null),
  20403.         TYPE_NAME       = d.type_name,
  20404.         LOCAL_TYPE_NAME     = d.local_type_name
  20405.     
  20406.     from
  20407.         syscolumns c,
  20408.         sysobjects o,
  20409.         master.dbo.spt_provider_types d,
  20410.         systypes t
  20411.     where
  20412.         o.type = 'P'                            /* Just Procedures */
  20413.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20414.     and o.id = c.id
  20415.     and c.type = d.ss_dtype
  20416.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20417.     and c.usertype = t.usertype
  20418.     and (t.usertype != 80 or d.type_name='timestamp')
  20419.     and (@parameter_name is null or @parameter_name = c.name)
  20420.     UNION ALL
  20421.     SELECT         /* return value row*/
  20422.         PROCEDURE_CATALOG   = db_name(),
  20423.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20424.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20425.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20426.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20427.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20428.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20429.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20430.         IS_NULLABLE     = convert(bit,0),
  20431.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20432.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20433.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20434.         NUMERIC_PRECISION   = convert(smallint,10),
  20435.         NUMERIC_SCALE       = convert(smallint,null),
  20436.         DESCRIPTION     = convert(varchar(1),null),
  20437.         TYPE_NAME       = convert(sysname,'int'),
  20438.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20439.     from
  20440.         syscomments c,
  20441.         sysobjects o
  20442.     where
  20443.         o.type = 'P'                        /* Just Procedures */
  20444.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20445.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20446.     and c.id = o.id
  20447.     and     c.colid = 1
  20448.     order by 2, 3, 5
  20449.     END
  20450. go
  20451. dump tran master with no_log
  20452. go
  20453. create procedure sp_procedure_params_rowset;2
  20454.     (
  20455.     @handle         int output,
  20456.     @scrollopt      int output,
  20457.     @ccopt          int output,
  20458.     @rows           int output,
  20459.     @procedure_name     varchar(255) = null,
  20460.     @group_number       int = null,
  20461.     @procedure_schema   varchar(255) = null,
  20462.     @parameter_name     varchar(255) = null
  20463.     )
  20464. as
  20465.     declare @ret int
  20466.  
  20467.  
  20468. SET NOCOUNT ON
  20469.  
  20470.     create table #spprocparamrowset1
  20471.         (
  20472.         PROCEDURE_CATALOG   sysname not null,
  20473.         PROCEDURE_SCHEMA    sysname not null,
  20474.         PROCEDURE_NAME      varchar(35) not null,
  20475.         PARAMETER_NAME      sysname not null,
  20476.         ORDINAL_POSITION    smallint not null,
  20477.         PARAMETER_TYPE      smallint null,
  20478.         PARAMETER_HASDEFAULT    tinyint null,
  20479.         PARAMETER_DEFAULT   varchar(255) null,
  20480.         IS_NULLABLE     bit not null,
  20481.         DATA_TYPE       smallint null,
  20482.         CHARACTER_MAXIMUM_LENGTH int null,
  20483.         CHARACTER_OCTET_LENGTH  int null,
  20484.         NUMERIC_PRECISION   smallint null,
  20485.         NUMERIC_SCALE       smallint null,
  20486.         DESCRIPTION     varchar(1) null,
  20487.         TYPE_NAME       sysname null,
  20488.         LOCAL_TYPE_NAME     sysname null,
  20489.         )
  20490.  
  20491. IF @procedure_name is not null
  20492.     BEGIN
  20493.     insert into #spprocparamrowset1
  20494.     select
  20495.         PROCEDURE_CATALOG   = db_name(),
  20496.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20497.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20498.         PARAMETER_NAME      = c.name,
  20499.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20500.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20501.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20502.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20503.         IS_NULLABLE     = convert(bit,
  20504.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20505.                         then 0 else 1
  20506.                         end),
  20507.         DATA_TYPE       = d.oledb_data_type,
  20508.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20509.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20510.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20511.                         then coalesce(d.column_size,c.length)
  20512.                         else null 
  20513.                         end),
  20514.         CHARACTER_OCTET_LENGTH  = convert(int,
  20515.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20516.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20517.                         then coalesce(d.column_size,c.length)
  20518.                         else null 
  20519.                         end),
  20520.         NUMERIC_PRECISION   = convert(smallint,
  20521.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20522.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20523.                             then d.data_precision else null end),
  20524.         NUMERIC_SCALE       = convert(smallint, 
  20525.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20526.         DESCRIPTION     = convert(varchar(1),null),
  20527.         TYPE_NAME       = d.type_name,
  20528.         LOCAL_TYPE_NAME     = d.local_type_name
  20529.     
  20530.     from
  20531.         syscolumns c,
  20532.         sysobjects o,
  20533.         master.dbo.spt_provider_types d,
  20534.         systypes t
  20535.     where
  20536.         o.name = @procedure_name
  20537.     and o.type = 'P'                            /* Just Procedures */
  20538.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20539.     and o.id = c.id
  20540.     and c.number = @group_number
  20541.     and c.type = d.ss_dtype
  20542.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20543.     and c.usertype = t.usertype
  20544.     and (t.usertype != 80 or d.type_name='timestamp')
  20545.     and (@parameter_name is null or @parameter_name = c.name)
  20546.     UNION ALL
  20547.     SELECT         /* return value row*/
  20548.         PROCEDURE_CATALOG   = db_name(),
  20549.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20550.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20551.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20552.         ORDINAL_POSITION    = convert(smallint,0),
  20553.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20554.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20555.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20556.         IS_NULLABLE     = convert(bit,0),
  20557.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20558.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20559.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20560.         NUMERIC_PRECISION   = convert(smallint,10),
  20561.         NUMERIC_SCALE       = convert(smallint,null),
  20562.         DESCRIPTION     = convert(varchar(1),null),
  20563.         TYPE_NAME       = convert(sysname,'int'),
  20564.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20565.     from
  20566.         syscomments c,
  20567.         sysobjects o
  20568.     where
  20569.         o.name = @procedure_name
  20570.     and o.type = 'P'                        /* Just Procedures */
  20571.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20572.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20573.     and c.id = o.id
  20574.     and c.number = @group_number
  20575.     and c.colid = 1
  20576.     order by 2, 3, 5
  20577.     END
  20578. ELSE
  20579.     BEGIN
  20580.     insert into #spprocparamrowset1
  20581.     select
  20582.         PROCEDURE_CATALOG   = db_name(),
  20583.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20584.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20585.         PARAMETER_NAME      = c.name,
  20586.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20587.         PARAMETER_TYPE      = convert(smallint, 1+((c.status/64)&1)),
  20588.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20589.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20590.         IS_NULLABLE     = convert(bit,
  20591.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20592.                         then 0 else 1
  20593.                         end),
  20594.         DATA_TYPE       = d.oledb_data_type,
  20595.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20596.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20597.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20598.                         then coalesce(d.column_size,c.length)
  20599.                         else null 
  20600.                         end),
  20601.         CHARACTER_OCTET_LENGTH  = convert(int,
  20602.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20603.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20604.                         then coalesce(d.column_size,c.length)
  20605.                         else null 
  20606.                         end),
  20607.         NUMERIC_PRECISION   = convert(smallint,
  20608.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20609.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20610.                             then d.data_precision else null end),
  20611.         NUMERIC_SCALE       = convert(smallint, 
  20612.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20613.         DESCRIPTION     = convert(varchar(1),null),
  20614.         TYPE_NAME       = d.type_name,
  20615.         LOCAL_TYPE_NAME     = d.local_type_name
  20616.     
  20617.     from
  20618.         syscolumns c,
  20619.         sysobjects o,
  20620.         master.dbo.spt_provider_types d,
  20621.         systypes t
  20622.     where
  20623.         o.type = 'P'                            /* Just Procedures */
  20624.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20625.     and o.id = c.id
  20626.     and c.type = d.ss_dtype
  20627.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20628.     and c.usertype = t.usertype
  20629.     and (t.usertype != 80 or d.type_name='timestamp')
  20630.     and (@parameter_name is null or @parameter_name = c.name)
  20631.     UNION ALL
  20632.     SELECT         /* return value row*/
  20633.         PROCEDURE_CATALOG   = db_name(),
  20634.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20635.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20636.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20637.         ORDINAL_POSITION    = convert(smallint,0),
  20638.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20639.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20640.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20641.         IS_NULLABLE     = convert(bit,0),
  20642.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20643.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20644.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20645.         NUMERIC_PRECISION   = convert(smallint,10),
  20646.         NUMERIC_SCALE       = convert(smallint,null),
  20647.         DESCRIPTION     = convert(varchar(1),null),
  20648.         TYPE_NAME       = convert(sysname,'int'),
  20649.         LOCAL_TYPE_NAME     = convert(sysname,'int')
  20650.     from
  20651.         syscomments c,
  20652.         sysobjects o
  20653.     where
  20654.         o.type = 'P'                        /* Just Procedures */
  20655.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20656.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20657.     and c.id = o.id
  20658.     and     c.colid = 1
  20659.     order by 2, 3, 5
  20660.     END
  20661.  
  20662.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  20663.         @scrollopt output, @ccopt output, @rows output
  20664.  
  20665.     drop table #spprocparamrowset1
  20666.     return isnull(@ret,0)
  20667. go
  20668. dump tran master with no_log
  20669. go
  20670. create procedure sp_procedure_params_rowset;3
  20671. as
  20672.     select
  20673.         PROCEDURE_CATALOG   = convert(sysname, ' '),
  20674.         PROCEDURE_SCHEMA    = convert(sysname, ' '),
  20675.         PROCEDURE_NAME      = convert(varchar(35), ' '),
  20676.         PARAMETER_NAME      = convert(sysname, ' '),
  20677.         ORDINAL_POSITION    = convert(smallint, 0),
  20678.         PARAMETER_TYPE      = convert(smallint, 0),
  20679.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20680.         PARAMETER_DEFAULT   = convert(varchar(255),null),
  20681.         IS_NULLABLE     = convert(bit, 0),
  20682.         DATA_TYPE       = convert(smallint, 0),
  20683.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  20684.         CHARACTER_OCTET_LENGTH  = convert(int, 0),
  20685.         NUMERIC_PRECISION   = convert(smallint, 0),
  20686.         NUMERIC_SCALE       = convert(smallint, 0),
  20687.         DESCRIPTION     = convert(varchar(1),null),
  20688.         TYPE_NAME       = convert(sysname,null),
  20689.         LOCAL_TYPE_NAME     = convert(sysname,null)
  20690.     where   1=0
  20691. go
  20692. dump tran master with no_log
  20693. go
  20694.  
  20695.  
  20696. if (charindex('7.00', @@version) > 0 or
  20697.     charindex('8.00', @@version) > 0)
  20698.     drop procedure sp_procedure_params_rowset
  20699. else
  20700. begin
  20701.     print ''
  20702.     print ''
  20703.     print 'Warning:'
  20704.     print 'you are installing the stored procedures '
  20705.     print 'on a pre 7.0 SQL Server.'
  20706.     print 'Ignore the following errors.'
  20707. end
  20708. go
  20709.  
  20710. /*  Procedure for 7.0 servers */
  20711. create procedure sp_procedure_params_rowset
  20712.     (
  20713.     @procedure_name     sysname,
  20714.     @group_number       int = 1,
  20715.     @procedure_schema   sysname = null,
  20716.     @parameter_name     sysname = null
  20717.     )
  20718. as
  20719.     select
  20720.         PROCEDURE_CATALOG   = db_name(),
  20721.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20722.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20723.         PARAMETER_NAME      = c.name,
  20724.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20725.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  20726.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20727.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20728.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20729.         DATA_TYPE       = d.oledb_data_type,
  20730.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20731.                         case 
  20732.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20733.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20734.                         then coalesce(d.column_size,c.length)
  20735.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20736.                         then coalesce(d.column_size,c.length/2)
  20737.                         else null 
  20738.                         end),
  20739.         CHARACTER_OCTET_LENGTH  = convert(int,
  20740.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20741.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20742.                         then coalesce(d.column_size,c.length)
  20743.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20744.                         then coalesce(d.column_size*2,c.length)
  20745.                         else null 
  20746.                         end),
  20747.         NUMERIC_PRECISION   = convert(smallint,
  20748.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20749.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20750.                             then d.data_precision else null end),
  20751.         NUMERIC_SCALE       = convert(smallint, 
  20752.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20753.         DESCRIPTION     = convert(nvarchar(1),null),
  20754.         TYPE_NAME       = d.type_name,
  20755.         LOCAL_TYPE_NAME     = d.local_type_name
  20756.     
  20757.     from
  20758.         sysobjects o,
  20759.         syscolumns c,
  20760.         master.dbo.spt_provider_types d,
  20761.         systypes t
  20762.     where
  20763.         o.name = @procedure_name
  20764.     and o.type = 'P'                            /* Just Procedures */
  20765.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20766.     and o.id = c.id
  20767.     and c.number = @group_number
  20768.     and c.xtype = d.ss_dtype
  20769.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20770.     and c.xusertype = t.xusertype
  20771.     and (@parameter_name is null or @parameter_name = c.name)
  20772.     UNION ALL
  20773.     SELECT         /* return value row*/
  20774.         PROCEDURE_CATALOG   = db_name(),
  20775.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20776.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20777.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20778.         ORDINAL_POSITION    = convert(smallint,0),
  20779.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20780.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20781.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20782.         IS_NULLABLE     = convert(bit,0),
  20783.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20784.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20785.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20786.         NUMERIC_PRECISION   = convert(smallint,10),
  20787.         NUMERIC_SCALE       = convert(smallint,null),
  20788.         DESCRIPTION     = convert(nvarchar(1),null),
  20789.         TYPE_NAME       = convert(sysname,N'int'),
  20790.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  20791.     from
  20792.         sysobjects o,
  20793.         syscomments c
  20794.     where
  20795.         o.name = @procedure_name
  20796.     and o.id = c.id 
  20797.     and c.number = @group_number
  20798.     and c.colid = 1
  20799.     and o.type = 'P'                        /* Just Procedures */
  20800.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20801.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20802.     order by 2, 3, 5
  20803. go
  20804. dump tran master with no_log
  20805. go
  20806. create procedure sp_procedure_params_rowset;2
  20807.     (
  20808.     @procedure_schema   sysname = null,
  20809.     @parameter_name     sysname = null
  20810.     )
  20811. as
  20812.     select
  20813.         PROCEDURE_CATALOG   = db_name(),
  20814.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20815.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20816.         PARAMETER_NAME      = c.name,
  20817.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20818.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  20819.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20820.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20821.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20822.         DATA_TYPE       = d.oledb_data_type,
  20823.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20824.                         case 
  20825.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20826.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20827.                         then coalesce(d.column_size,c.length)
  20828.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20829.                         then coalesce(d.column_size,c.length/2)
  20830.                         else null 
  20831.                         end),
  20832.         CHARACTER_OCTET_LENGTH  = convert(int,
  20833.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20834.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20835.                         then coalesce(d.column_size,c.length)
  20836.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20837.                         then coalesce(d.column_size*2,c.length)
  20838.                         else null 
  20839.                         end),
  20840.         NUMERIC_PRECISION   = convert(smallint,
  20841.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20842.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20843.                             then d.data_precision else null end),
  20844.         NUMERIC_SCALE       = convert(smallint, 
  20845.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20846.         DESCRIPTION     = convert(nvarchar(1),null),
  20847.         TYPE_NAME       = d.type_name,
  20848.         LOCAL_TYPE_NAME     = d.local_type_name
  20849.     
  20850.     from
  20851.         sysobjects o,
  20852.         syscolumns c,
  20853.         master.dbo.spt_provider_types d,
  20854.         systypes t
  20855.     where
  20856.         o.type = 'P'                            /* Just Procedures */
  20857.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20858.     and o.id = c.id
  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 (@parameter_name is null or @parameter_name = c.name)
  20863.     UNION ALL
  20864.     SELECT         /* return value row*/
  20865.         PROCEDURE_CATALOG   = db_name(),
  20866.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20867.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20868.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20869.         ORDINAL_POSITION    = convert(smallint,0),
  20870.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20871.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20872.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20873.         IS_NULLABLE     = convert(bit,0),
  20874.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20875.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20876.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20877.         NUMERIC_PRECISION   = convert(smallint,10),
  20878.         NUMERIC_SCALE       = convert(smallint,null),
  20879.         DESCRIPTION     = convert(nvarchar(1),null),
  20880.         TYPE_NAME       = convert(sysname,N'int'),
  20881.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  20882.     from
  20883.         sysobjects o,
  20884.         syscomments c
  20885.     where
  20886.         o.type = 'P'        /* Just Procedures */
  20887.     and o.id = c.id 
  20888.     and c.colid = 1
  20889.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20890.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20891.     order by 2, 3, 5
  20892. go
  20893.  
  20894. if (charindex('8.00', @@version) > 0)
  20895.     drop procedure sp_procedure_params_rowset
  20896. else
  20897. begin
  20898.     print ''
  20899.     print ''
  20900.     print 'Warning:'
  20901.     print 'you are installing the stored procedures '
  20902.     print 'on a pre 8.0 SQL Server.'
  20903.     print 'Ignore the following errors.'
  20904. end
  20905. go
  20906.  
  20907. /*  Procedure for 8.0 servers */
  20908. create procedure sp_procedure_params_rowset
  20909.     (
  20910.     @procedure_name     sysname,
  20911.     @group_number       int = 1,
  20912.     @procedure_schema   sysname = null,
  20913.     @parameter_name     sysname = null
  20914.     )
  20915. as
  20916.     select
  20917.         PROCEDURE_CATALOG   = db_name(),
  20918.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20919.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20920.         PARAMETER_NAME      = c.name,
  20921.         ORDINAL_POSITION    = convert(smallint, c.colid),
  20922.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  20923.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20924.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20925.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20926.         DATA_TYPE       = d.oledb_data_type,
  20927.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20928.                         case 
  20929.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20930.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20931.                         then coalesce(d.column_size,c.length)
  20932.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20933.                         then coalesce(d.column_size,c.length/2)
  20934.                         else null 
  20935.                         end),
  20936.         CHARACTER_OCTET_LENGTH  = convert(int,
  20937.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20938.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20939.                         then coalesce(d.column_size,c.length)
  20940.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20941.                         then coalesce(d.column_size*2,c.length)
  20942.                         else null 
  20943.                         end),
  20944.         NUMERIC_PRECISION   = convert(smallint,
  20945.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20946.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20947.                             then d.data_precision else null end),
  20948.         NUMERIC_SCALE       = convert(smallint, 
  20949.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20950.         DESCRIPTION     = convert(nvarchar(1),null),
  20951.         TYPE_NAME       = d.type_name,
  20952.         LOCAL_TYPE_NAME     = d.local_type_name
  20953.     
  20954.     from
  20955.         sysobjects o,
  20956.         syscolumns c,
  20957.         master.dbo.spt_provider_types d,
  20958.         systypes t
  20959.     where
  20960.         o.name = @procedure_name
  20961.     and     (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20962.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20963.     and o.id = c.id
  20964.     and ((c.number = @group_number and o.type = 'P') 
  20965.             or (c.number = 0 and o.type = 'FN')
  20966.             or (c.number = 1 and o.type in ('TF', 'IF')))
  20967.     and c.xtype = d.ss_dtype
  20968.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20969.     and c.xusertype = t.xusertype
  20970.     and (@parameter_name is null or @parameter_name = c.name)
  20971.     UNION ALL
  20972.     SELECT         /* return value row*/
  20973.         PROCEDURE_CATALOG   = db_name(),
  20974.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20975.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20976.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  20977.         ORDINAL_POSITION    = convert(smallint,0),
  20978.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20979.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20980.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  20981.         IS_NULLABLE     = convert(bit,0),
  20982.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  20983.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20984.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  20985.         NUMERIC_PRECISION   = convert(smallint,10),
  20986.         NUMERIC_SCALE       = convert(smallint,null),
  20987.         DESCRIPTION     = convert(nvarchar(1),null),
  20988.         TYPE_NAME       = convert(sysname,N'int'),
  20989.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  20990.     from
  20991.         sysobjects o,
  20992.         syscomments c
  20993.     where
  20994.         o.name = @procedure_name
  20995.     and o.id = c.id 
  20996.     and c.number = @group_number
  20997.     and c.colid = 1
  20998.     and o.type = 'P'            /* Just Procedures */
  20999.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21000.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21001.     UNION ALL
  21002.     SELECT         /* UDF return value row*/
  21003.         PROCEDURE_CATALOG        = db_name(),
  21004.         PROCEDURE_SCHEMA         = user_name(o.uid),
  21005.         PROCEDURE_NAME           = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21006.         PARAMETER_NAME           = convert(sysname,'@RETURN_VALUE'),
  21007.         ORDINAL_POSITION         = convert(smallint, 0),
  21008.         PARAMETER_TYPE           = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21009.         PARAMETER_HASDEFAULT     = convert(tinyint, 0),
  21010.         PARAMETER_DEFAULT        = convert(nvarchar(255),null),
  21011.         IS_NULLABLE              = convert(bit, c.isnullable),
  21012.         DATA_TYPE                = d.oledb_data_type,
  21013.         CHARACTER_MAXIMUM_LENGTH = convert(int,
  21014.                         case 
  21015.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21016.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21017.                         then coalesce(d.column_size,c.length)
  21018.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21019.                         then coalesce(d.column_size,c.length/2)
  21020.                         else null 
  21021.                         end),
  21022.         CHARACTER_OCTET_LENGTH   = convert(int,
  21023.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21024.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21025.                         then coalesce(d.column_size,c.length)
  21026.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21027.                         then coalesce(d.column_size*2,c.length)
  21028.                         else null 
  21029.                         end),
  21030.         NUMERIC_PRECISION        = convert(smallint,
  21031.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21032.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21033.                             then d.data_precision else null end),
  21034.         NUMERIC_SCALE            = convert(smallint, 
  21035.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21036.         DESCRIPTION              = convert(nvarchar(1),null),
  21037.         TYPE_NAME                = d.type_name,
  21038.         LOCAL_TYPE_NAME          = d.local_type_name
  21039.     FROM
  21040.         sysobjects o,
  21041.         syscolumns c,
  21042.         master.dbo.spt_provider_types d,
  21043.         systypes t
  21044.     WHERE
  21045.         o.name = @procedure_name
  21046.     and o.id = c.id 
  21047.     and c.number = 0
  21048.     and c.colid = 0
  21049.     and o.type = 'FN'           /* UDF scalar functions */
  21050.     and c.xtype = d.ss_dtype
  21051.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21052.     and c.xusertype = t.xusertype
  21053.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21054.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21055.     UNION ALL
  21056.     SELECT         /* UDF table value row*/
  21057.         PROCEDURE_CATALOG   = db_name(),
  21058.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21059.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21060.         PARAMETER_NAME      = convert(sysname,'@TABLE_RETURN_VALUE'),
  21061.         ORDINAL_POSITION    = convert(smallint, 0),
  21062.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21063.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21064.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21065.         IS_NULLABLE     = convert(bit,0),
  21066.         DATA_TYPE       = convert(smallint, 0),             /*DBTYPE_EMPTY*/
  21067.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21068.                         case 
  21069.                         when d.oledb_data_type = 129        /*DBTYPE_STR*/ 
  21070.                             or d.oledb_data_type = 128  /*DBTYPE_BYTES*/
  21071.                         then coalesce(d.column_size,c.length)
  21072.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  21073.                         then coalesce(d.column_size,c.length/2)
  21074.                         else null 
  21075.                         end),
  21076.         CHARACTER_OCTET_LENGTH  = convert(int,
  21077.                         case when d.oledb_data_type = 129   /*DBTYPE_STR*/ 
  21078.                             or d.oledb_data_type = 128  /*DBTYPE_BYTES*/
  21079.                         then coalesce(d.column_size,c.length)
  21080.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  21081.                         then coalesce(d.column_size*2,c.length)
  21082.                         else null 
  21083.                         end),
  21084.         NUMERIC_PRECISION   = convert(smallint,
  21085.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21086.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21087.                             then d.data_precision else null end),
  21088.         NUMERIC_SCALE       = convert(smallint, 
  21089.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21090.         DESCRIPTION     = convert(nvarchar(50), N'Result table returned by table valued function'),
  21091.         TYPE_NAME       = N'table',
  21092.         LOCAL_TYPE_NAME     = N'table'
  21093.     
  21094.     from
  21095.         sysobjects o,
  21096.         syscolumns c,
  21097.         master.dbo.spt_provider_types d
  21098.     where
  21099.         o.name = @procedure_name
  21100.     and o.id = c.id 
  21101.     and c.number = 0
  21102.     and c.colid = 1
  21103.     and o.type in ('TF', 'IF')          /* UDF table functions */
  21104.     and c.xtype = d.ss_dtype
  21105.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21106.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21107.     and (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  21108.     order by 2, 3, 5
  21109. go
  21110. dump tran master with no_log
  21111. go
  21112. create procedure sp_procedure_params_rowset;2
  21113.     (
  21114.     @procedure_schema   sysname = null,
  21115.     @parameter_name     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(c.number,5))),
  21122.         PARAMETER_NAME      = c.name,
  21123.         ORDINAL_POSITION    = convert(smallint, c.colid),
  21124.         PARAMETER_TYPE      = convert(smallint, 1+c.isoutparam),
  21125.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21126.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21127.         IS_NULLABLE     = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  21128.         DATA_TYPE       = d.oledb_data_type,
  21129.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21130.                         case 
  21131.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21132.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21133.                         then coalesce(d.column_size,c.length)
  21134.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21135.                         then coalesce(d.column_size,c.length/2)
  21136.                         else null 
  21137.                         end),
  21138.         CHARACTER_OCTET_LENGTH  = convert(int,
  21139.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21140.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21141.                         then coalesce(d.column_size,c.length)
  21142.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21143.                         then coalesce(d.column_size*2,c.length)
  21144.                         else null 
  21145.                         end),
  21146.         NUMERIC_PRECISION   = convert(smallint,
  21147.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21148.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21149.                             then d.data_precision else null end),
  21150.         NUMERIC_SCALE       = convert(smallint, 
  21151.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21152.         DESCRIPTION     = convert(nvarchar(1),null),
  21153.         TYPE_NAME       = d.type_name,
  21154.         LOCAL_TYPE_NAME     = d.local_type_name
  21155.     
  21156.     from
  21157.         sysobjects o,
  21158.         syscolumns c,
  21159.         master.dbo.spt_provider_types d,
  21160.         systypes t
  21161.     where
  21162.         (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  21163.     and (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21164.     and o.id = c.id
  21165.     and (o.type = 'P' or (c.number = 0 and o.type = 'FN') or (c.number = 1 and o.type in ('TF', 'IF')))
  21166.     and c.xtype = d.ss_dtype
  21167.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21168.     and c.xusertype = t.xusertype
  21169.     and (@parameter_name is null or @parameter_name = c.name)
  21170.     UNION ALL
  21171.     SELECT         /* return value row*/
  21172.         PROCEDURE_CATALOG   = db_name(),
  21173.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21174.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21175.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  21176.         ORDINAL_POSITION    = convert(smallint,0),
  21177.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21178.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21179.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21180.         IS_NULLABLE     = convert(bit,0),
  21181.         DATA_TYPE       = convert(smallint, 3 /*DBTYPE_I4*/),
  21182.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  21183.         CHARACTER_OCTET_LENGTH  = convert(int,null),
  21184.         NUMERIC_PRECISION   = convert(smallint,10),
  21185.         NUMERIC_SCALE       = convert(smallint,null),
  21186.         DESCRIPTION     = convert(nvarchar(1),null),
  21187.         TYPE_NAME       = convert(sysname,N'int'),
  21188.         LOCAL_TYPE_NAME     = convert(sysname,N'int')
  21189.     from
  21190.         sysobjects o,
  21191.         syscomments c
  21192.     where
  21193.         o.type = 'P'            /* Just Procedures */
  21194.     and o.id = c.id 
  21195.     and c.colid = 1
  21196.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21197.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21198.     UNION ALL
  21199.     SELECT         /* UDF return value row*/
  21200.         PROCEDURE_CATALOG   = db_name(),
  21201.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21202.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21203.         PARAMETER_NAME      = convert(sysname,'@RETURN_VALUE'),
  21204.         ORDINAL_POSITION    = convert(smallint, 0),
  21205.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21206.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21207.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21208.         IS_NULLABLE     = convert(bit, c.isnullable),
  21209.         DATA_TYPE       = d.oledb_data_type,
  21210.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21211.                         case 
  21212.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21213.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21214.                         then coalesce(d.column_size,c.length)
  21215.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21216.                         then coalesce(d.column_size,c.length/2)
  21217.                         else null 
  21218.                         end),
  21219.         CHARACTER_OCTET_LENGTH  = convert(int,
  21220.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21221.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21222.                         then coalesce(d.column_size,c.length)
  21223.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21224.                         then coalesce(d.column_size*2,c.length)
  21225.                         else null 
  21226.                         end),
  21227.         NUMERIC_PRECISION   = convert(smallint,
  21228.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21229.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21230.                             then d.data_precision else null end),
  21231.         NUMERIC_SCALE       = convert(smallint, 
  21232.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21233.         DESCRIPTION     = convert(nvarchar(1),null),
  21234.         TYPE_NAME       = d.type_name,
  21235.         LOCAL_TYPE_NAME     = d.local_type_name
  21236.     
  21237.     from
  21238.         sysobjects o,
  21239.         syscolumns c,
  21240.         master.dbo.spt_provider_types d,
  21241.         systypes t
  21242.     where
  21243.         o.id = c.id 
  21244.     and c.number = 0
  21245.     and c.colid = 0
  21246.     and o.type = 'FN'           /* UDF scalar functions */
  21247.     and c.xtype = d.ss_dtype
  21248.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21249.     and c.xusertype = t.xusertype
  21250.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21251.     and (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21252.     UNION ALL
  21253.     SELECT         /* UDF table value row*/
  21254.         PROCEDURE_CATALOG   = db_name(),
  21255.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21256.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21257.         PARAMETER_NAME      = convert(sysname,'@TABLE_RETURN_VALUE'),
  21258.         ORDINAL_POSITION    = convert(smallint, 0),
  21259.         PARAMETER_TYPE      = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21260.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21261.         PARAMETER_DEFAULT   = convert(nvarchar(255),null),
  21262.         IS_NULLABLE     = convert(bit,0),
  21263.         DATA_TYPE       = convert(smallint, 0),         /*DBTYPE_EMPTY*/
  21264.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21265.                         case 
  21266.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21267.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21268.                         then coalesce(d.column_size,c.length)
  21269.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21270.                         then coalesce(d.column_size,c.length/2)
  21271.                         else null 
  21272.                         end),
  21273.         CHARACTER_OCTET_LENGTH  = convert(int,
  21274.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21275.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21276.                         then coalesce(d.column_size,c.length)
  21277.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21278.                         then coalesce(d.column_size*2,c.length)
  21279.                         else null 
  21280.                         end),
  21281.         NUMERIC_PRECISION   = convert(smallint,
  21282.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21283.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21284.                             then d.data_precision else null end),
  21285.         NUMERIC_SCALE       = convert(smallint, 
  21286.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21287.         DESCRIPTION     = convert(nvarchar(1),null),
  21288.         TYPE_NAME       = N'table',
  21289.         LOCAL_TYPE_NAME     = N'table'
  21290.     
  21291.     from
  21292.         sysobjects o,
  21293.         syscolumns c,
  21294.         master.dbo.spt_provider_types d
  21295.     where
  21296.         o.id = c.id 
  21297.     and c.number = 0
  21298.     and c.colid = 1
  21299.     and o.type in ('TF', 'IF')          /* UDF table functions */
  21300.     and c.xtype = d.ss_dtype
  21301.     and c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21302.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21303.     and (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  21304.     order by 2, 3, 5
  21305. go
  21306.  
  21307. grant execute on sp_procedure_params_rowset to public
  21308. go
  21309.  
  21310. dump tran master with no_log
  21311. go
  21312. if (charindex('6.00', @@version) > 0)
  21313.     begin
  21314.     if (exists (select * from sysobjects
  21315.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  21316.         begin
  21317.         drop procedure sp_procedure_params_rowset
  21318.         dump tran master with no_log
  21319.         end
  21320.     end
  21321. go
  21322.  
  21323.  
  21324. print ''
  21325. print 'creating sp_procedures_rowset'
  21326. go
  21327.  
  21328.  
  21329. /* pre 7.0 version */
  21330. create procedure sp_procedures_rowset
  21331.     (
  21332.     @procedure_name     varchar(255), 
  21333.     @group_number       int = 1,
  21334.     @procedure_schema   varchar(255) = null
  21335.     )       
  21336. as
  21337.     select
  21338.         PROCEDURE_CATALOG   = db_name(),
  21339.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21340.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  21341.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21342.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  21343.         DESCRIPTION     = convert(varchar(1),null),
  21344.         DATE_CREATED        = o.crdate,
  21345.         DATE_MODIFIED       = convert(datetime,null)
  21346.     from    
  21347.         sysobjects o, 
  21348.         syscomments p,
  21349.         sysusers u
  21350.     where
  21351.             o.name = @procedure_name
  21352.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21353.     and     o.type = 'P'        /* Object type of Procedure */
  21354.     and     p.colid = 1
  21355.     and     p.id = o.id
  21356.     and p.number = @group_number
  21357.     and     u.uid = user_id()   /* constrain sysusers uid for use in subquery */
  21358.     and     (   suser_id() = 1  /* User is the System Administrator */
  21359.         or  o.uid = user_id()   /* User created the object */
  21360.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21361.         or  ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21362.              from sysprotects p
  21363.              where p.id = o.id
  21364.                  /*  get rows for public,current user,user's group */
  21365.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21366.                  /* check for SELECT,EXECUTE privilege */
  21367.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21368.             ) = 1    /* final magic...compare Grants    */
  21369.         )
  21370.     order by 2, 3
  21371. go
  21372. dump tran master with no_log
  21373. go
  21374. create procedure sp_procedures_rowset;2
  21375.     (
  21376.     @procedure_schema   varchar(255) = null
  21377.     )       
  21378. as
  21379.     select
  21380.         PROCEDURE_CATALOG   = db_name(),
  21381.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21382.         PROCEDURE_NAME      = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  21383.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21384.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  21385.         DESCRIPTION     = convert(varchar(1),null),
  21386.         DATE_CREATED        = o.crdate,
  21387.         DATE_MODIFIED       = convert(datetime,null)
  21388.     from    
  21389.         sysobjects o, 
  21390.         syscomments p,
  21391.         sysusers u
  21392.     where
  21393.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21394.     and     o.type = 'P'        /* Object type of Procedure */
  21395.     and     p.colid = 1
  21396.     and     p.id = o.id
  21397.     and     u.uid = user_id()   /* constrain sysusers uid for use in subquery */
  21398.     and     (   suser_id() = 1  /* User is the System Administrator */
  21399.         or  o.uid = user_id()   /* User created the object */
  21400.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21401.         or  ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21402.              from sysprotects p
  21403.              where p.id = o.id
  21404.                  /*  get rows for public,current user,user's group */
  21405.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21406.                  /* check for SELECT,EXECUTE privilege */
  21407.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21408.             ) = 1    /* final magic...compare Grants    */
  21409.         )
  21410.     order by 2, 3
  21411. go
  21412. dump tran master with no_log
  21413. go
  21414.  
  21415. if (charindex('7.00', @@version) > 0 or
  21416.     charindex('8.00', @@version) > 0)
  21417.     drop procedure sp_procedures_rowset
  21418. else
  21419. begin
  21420.     print ''
  21421.     print ''
  21422.     print 'Warning:'
  21423.     print 'you are installing the stored procedures '
  21424.     print 'on a pre 7.0 SQL Server.'
  21425.     print 'Ignore the following errors.'
  21426. end
  21427. go
  21428.  
  21429. /* 7.0 version */
  21430. create procedure sp_procedures_rowset
  21431.     (
  21432.     @procedure_name     sysname, 
  21433.     @group_number       int = 1,
  21434.     @procedure_schema   sysname = null
  21435.     )       
  21436. as
  21437.     select
  21438.         PROCEDURE_CATALOG   = db_name(),
  21439.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21440.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21441.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21442.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21443.         DESCRIPTION     = convert(nvarchar(1),null),
  21444.         DATE_CREATED        = o.crdate,
  21445.         DATE_MODIFIED       = convert(datetime,null)
  21446.     from    
  21447.         sysobjects o, 
  21448.         syscomments p
  21449.     where
  21450.         permissions(o.id) <> 0
  21451.     and o.name = @procedure_name
  21452.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21453.     and     o.type = 'P'        /* Object type of Procedure */
  21454.     and     p.colid = 1
  21455.     and     p.id = o.id
  21456.     and p.number = @group_number
  21457.     order by 2, 3
  21458. go
  21459. dump tran master with no_log
  21460. go
  21461. create procedure sp_procedures_rowset;2
  21462.     (
  21463.     @procedure_schema   sysname = null
  21464.     )       
  21465. as
  21466.     select
  21467.         PROCEDURE_CATALOG   = db_name(),
  21468.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21469.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21470.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21471.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21472.         DESCRIPTION     = convert(nvarchar(1),null),
  21473.         DATE_CREATED        = o.crdate,
  21474.         DATE_MODIFIED       = convert(datetime,null)
  21475.     from    sysobjects o, syscomments p
  21476.     where
  21477.         permissions(o.id) <> 0
  21478.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21479.     and     o.type = 'P'        /* Object type of Procedure */
  21480.     and p.colid = 1
  21481.     and p.id = o.id
  21482.     order by 2, 3
  21483. go
  21484.  
  21485. if (charindex('8.00', @@version) > 0)
  21486.     drop procedure sp_procedures_rowset
  21487. else
  21488. begin
  21489.     print ''
  21490.     print ''
  21491.     print 'Warning:'
  21492.     print 'you are installing the stored procedures '
  21493.     print 'on a pre 8.0 SQL Server.'
  21494.     print 'Ignore the following errors.'
  21495. end
  21496. go
  21497.  
  21498. /* 8.0 version */
  21499. create procedure sp_procedures_rowset
  21500.     (
  21501.     @procedure_name     sysname, 
  21502.     @group_number       int = 1,
  21503.     @procedure_schema   sysname = null
  21504.     )       
  21505. as
  21506.     select
  21507.         PROCEDURE_CATALOG   = db_name(),
  21508.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21509.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21510.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21511.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21512.         DESCRIPTION     = convert(nvarchar(1),null),
  21513.         DATE_CREATED        = o.crdate,
  21514.         DATE_MODIFIED       = convert(datetime,null)
  21515.     from    
  21516.         sysobjects o, 
  21517.         syscomments p
  21518.     where
  21519.         permissions(o.id) <> 0
  21520.     and o.name = @procedure_name
  21521.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21522.     and     o.type in ('P', 'FN', 'TF', 'IF')   /* Object type of Procedure */
  21523.     and     p.colid = 1
  21524.     and     p.id = o.id
  21525.     and p.number = @group_number
  21526.     order by 2, 3
  21527. go
  21528. dump tran master with no_log
  21529. go
  21530. create procedure sp_procedures_rowset;2
  21531.     (
  21532.     @procedure_schema   sysname = null
  21533.     )       
  21534. as
  21535.     select
  21536.         PROCEDURE_CATALOG   = db_name(),
  21537.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21538.         PROCEDURE_NAME      = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21539.         PROCEDURE_TYPE      = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21540.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21541.         DESCRIPTION     = convert(nvarchar(1),null),
  21542.         DATE_CREATED        = o.crdate,
  21543.         DATE_MODIFIED       = convert(datetime,null)
  21544.     from    sysobjects o, syscomments p
  21545.     where
  21546.         permissions(o.id) <> 0
  21547.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21548.     and     o.type in ('P', 'FN', 'TF', 'IF')   /* Object type of Procedure */
  21549.     and p.colid = 1
  21550.     and p.id = o.id
  21551.     order by 2, 3
  21552. go
  21553.  
  21554. grant execute on sp_procedures_rowset to public
  21555. go
  21556.  
  21557. dump tran master with no_log
  21558. go
  21559.  
  21560. if (charindex('6.00', @@version) > 0)
  21561.     begin
  21562.     if (exists (select * from sysobjects
  21563.             where name = 'sp_procedures_rowset' and type = 'P '))
  21564.         begin
  21565.         drop procedure sp_procedures_rowset
  21566.         dump tran master with no_log
  21567.         end
  21568.     end
  21569. go
  21570.  
  21571.  
  21572. print ''
  21573. print 'creating sp_schemata_rowset'
  21574. go
  21575.  
  21576. /*  6.0 and 6.5 and 7.0 version */
  21577. create procedure sp_schemata_rowset
  21578.     (
  21579.     @schema_name    varchar(90) = null, 
  21580.     @schema_owner   varchar(90) = null
  21581.     )       
  21582. as
  21583.     select  distinct
  21584.         CATALOG_NAME            = db_name(),
  21585.         SCHEMA_NAME         = user_name(o.uid), 
  21586.         SCHEMA_OWNER            = user_name(o.uid), 
  21587.         DEFAULT_CHARACTER_SET_CATALOG   = convert(sysname,'master'),
  21588.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  21589.         DEFAULT_CHARACTER_SET_NAME  = convert(sysname,a_cha.name)
  21590.     from    
  21591.         sysobjects o,
  21592.         master.dbo.sysconfigures    cfg,
  21593.         master.dbo.syscharsets      a_cha, /* charset/1001, not sortorder */
  21594.         master.dbo.syscharsets      b_cha  /* sortorder/2001, not charset */
  21595.     where   
  21596.         (@schema_name is null or @schema_name = user_name(o.uid))
  21597.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21598.     and a_cha.type = 1001 /* type is charset */
  21599.     and     b_cha.type = 2001 /* type is sortorder */
  21600.     and     a_cha.id = b_cha.csid
  21601.     and     b_cha.id = cfg.value
  21602.     order by 2
  21603. go
  21604. dump tran master with no_log
  21605. go
  21606.  
  21607. if (charindex('8.00', @@version) > 0)
  21608.     drop procedure sp_schemata_rowset
  21609. else
  21610. begin
  21611.     print ''
  21612.     print ''
  21613.     print 'Warning:'
  21614.     print 'you are installing the stored procedures '
  21615.     print 'on a pre 8.0 SQL Server.'
  21616.     print 'Ignore the following errors.'
  21617. end
  21618. go
  21619.  
  21620. /* 8.0 version */
  21621. create procedure sp_schemata_rowset
  21622.     (
  21623.     @schema_name    sysname = null, 
  21624.     @schema_owner   sysname = null
  21625.     )       
  21626. as
  21627.     select  distinct
  21628.         CATALOG_NAME            = db_name(),
  21629.         SCHEMA_NAME         = user_name(o.uid), 
  21630.         SCHEMA_OWNER            = user_name(o.uid), 
  21631.         DEFAULT_CHARACTER_SET_CATALOG   = convert(sysname,N'master'),
  21632.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  21633.         DEFAULT_CHARACTER_SET_NAME  = convert(sysname,a_cha.name)
  21634.     from    
  21635.         sysobjects o,
  21636.         master.dbo.syscharsets      a_cha /* charset/1001, not sortorder */
  21637.     where   
  21638.         (@schema_name is null or @schema_name = user_name(o.uid))
  21639.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21640.     and a_cha.type = 1001 /* type is charset */
  21641.     and     a_cha.id = convert(tinyint, DatabasePropertyEx(db_name(), 'sqlcharset')) /* what is charset of a table? */
  21642.     order by 2
  21643. go
  21644.  
  21645. dump tran master with no_log
  21646. go
  21647.  
  21648. /* The following stored procedure is used for Sphinx and Hydra */
  21649. create procedure sp_schemata_rowset;3
  21650. as
  21651.     select
  21652.         CATALOG_NAME            = convert(sysname,' '),
  21653.         SCHEMA_NAME         = convert(sysname,' '), 
  21654.         SCHEMA_OWNER            = convert(sysname,' '), 
  21655.         DEFAULT_CHARACTER_SET_CATALOG   = convert(sysname,' '),
  21656.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  21657.         DEFAULT_CHARACTER_SET_NAME  = convert(sysname,' ')
  21658.     where   1=0
  21659. go
  21660. grant execute on sp_schemata_rowset to public
  21661. go
  21662.  
  21663. dump tran master with no_log
  21664. go
  21665. if (charindex('6.00', @@version) > 0)
  21666.     begin
  21667.     if (exists (select * from sysobjects
  21668.             where name = 'sp_schemata_rowset' and type = 'P '))
  21669.         begin
  21670.         drop procedure sp_schemata_rowset
  21671.         dump tran master with no_log
  21672.         end
  21673.     end
  21674. go
  21675.  
  21676.  
  21677. print ''
  21678. print 'creating sp_statistics_rowset'
  21679. go
  21680.  
  21681.  
  21682. /*  6.0 and 6.5 version */
  21683. create procedure sp_statistics_rowset
  21684.     (
  21685.     @table_name varchar(255),
  21686.     @table_schema   varchar(255) = null  
  21687.     )
  21688. as
  21689.     select  db_name()               as TABLE_CATALOG,       
  21690.         user_name(o.uid)            as TABLE_SCHEMA,
  21691.         o.name                  as TABLE_NAME,
  21692.         x.rows                  as CARDINALITY
  21693.     from    sysobjects o, sysindexes x, sysusers u
  21694.     where   o.type in ('U')
  21695.     and     o.name = @table_name
  21696.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21697.     and     x.id = o.id
  21698.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21699.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21700.     and     (
  21701.         suser_id() = 1   /* User is the System Administrator */
  21702.         or o.uid = user_id()     /* User created the object */
  21703.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21704.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21705.             from sysprotects p
  21706.             where p.id = o.id
  21707.             /* get rows for public,current user,user's group */
  21708.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21709.             /* check for SELECT,EXECUTE privilege */
  21710.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21711.             ) = 1   /* final magic...compare Grants   */
  21712.            )
  21713.     order by 2, 3
  21714. go
  21715. dump tran master with no_log
  21716. go
  21717. create procedure sp_statistics_rowset;2
  21718.     (
  21719.     @table_schema   varchar(255) = null  
  21720.     )
  21721. as
  21722.     select  db_name()               as TABLE_CATALOG,       
  21723.         user_name(o.uid)            as TABLE_SCHEMA,
  21724.         o.name                  as TABLE_NAME,
  21725.         x.rows                  as CARDINALITY
  21726.     from    sysobjects o, sysindexes x, sysusers u
  21727.     where   o.type in ('U')
  21728.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21729.     and     x.id = o.id
  21730.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21731.     and     (
  21732.         suser_id() = 1   /* User is the System Administrator */
  21733.         or o.uid = user_id()     /* User created the object */
  21734.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21735.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21736.             from sysprotects p
  21737.             where p.id = o.id
  21738.             /* get rows for public,current user,user's group */
  21739.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21740.             /* check for SELECT,EXECUTE privilege */
  21741.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21742.             ) = 1   /* final magic...compare Grants   */
  21743.            )        
  21744.     order by 2, 3
  21745. go
  21746. dump tran master with no_log
  21747. go
  21748.  
  21749. if (charindex('7.00', @@version) > 0 or
  21750.     charindex('8.00', @@version) > 0)
  21751.     drop procedure sp_statistics_rowset
  21752. else
  21753. begin
  21754.     print ''
  21755.     print ''
  21756.     print 'Warning:'
  21757.     print 'you are installing the stored procedures '
  21758.     print 'on a pre 8.0 SQL Server.'
  21759.     print 'Ignore the following errors.'
  21760. end
  21761. go
  21762.  
  21763. /*  8.0 version */
  21764. create procedure sp_statistics_rowset
  21765.     (
  21766.     @table_name sysname,
  21767.     @table_schema   sysname = null   
  21768.     )
  21769. as
  21770.     select  db_name()               as TABLE_CATALOG,       
  21771.         user_name(o.uid)            as TABLE_SCHEMA,
  21772.         o.name                  as TABLE_NAME,
  21773.         x.rows                  as CARDINALITY
  21774.     from    sysobjects o, sysindexes x
  21775.     where   o.type in ('U')
  21776.     and     o.name = @table_name
  21777.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21778.     and     x.id = o.id
  21779.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21780.     and     permissions(o.id) <> 0
  21781.     order by 2, 3
  21782. go
  21783. dump tran master with no_log
  21784. go
  21785. create procedure sp_statistics_rowset;2
  21786.     (
  21787.     @table_schema   sysname = null   
  21788.     )
  21789. as
  21790.     select  db_name()               as TABLE_CATALOG,       
  21791.         user_name(o.uid)            as TABLE_SCHEMA,
  21792.         o.name                  as TABLE_NAME,
  21793.         x.rows                  as CARDINALITY
  21794.     from    sysobjects o, sysindexes x
  21795.     where   o.type in ('U')
  21796.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21797.     and     x.id = o.id
  21798.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21799.     and     permissions(o.id) <> 0
  21800.     order by 2, 3
  21801. go
  21802.  
  21803. grant execute on sp_statistics_rowset to public
  21804. go
  21805.  
  21806. dump tran master with no_log
  21807. go
  21808. if (charindex('6.00', @@version) > 0)
  21809.     begin
  21810.     if (exists (select * from sysobjects
  21811.             where name = 'sp_statistics_rowset' and type = 'P '))
  21812.         begin
  21813.         drop procedure sp_statistics_rowset
  21814.         dump tran master with no_log
  21815.         end
  21816.     end
  21817. go
  21818.  
  21819.  
  21820. print ''
  21821. print 'creating sp_tables_rowset'
  21822. go
  21823.  
  21824.  
  21825. /*  Procedure for 6.50 and earlier servers */
  21826. create procedure sp_tables_rowset
  21827.     (
  21828.     @table_name varchar(255), 
  21829.     @table_schema   varchar(255) = null,    
  21830.     @table_type varchar(255) = null 
  21831.     )
  21832. as
  21833.     select  TABLE_CATALOG   = db_name(),
  21834.         TABLE_SCHEMA    = user_name(o.uid),
  21835.         TABLE_NAME  = o.name,
  21836.         TABLE_TYPE  = convert(varchar(30),
  21837.                     case o.type 
  21838.                     when 'U' then 'TABLE'
  21839.                     when 'V' then 'VIEW'
  21840.                     when 'S' then 'SYSTEM TABLE'
  21841.                     end),
  21842.         TABLE_GUID  = convert(binary(16), null),
  21843.         DESCRIPTION = convert(varchar(1), null),
  21844.         TABLE_PROPID    = convert(int,null),
  21845.         DATE_CREATED    = o.crdate,
  21846.         DATE_MODIFIED   = convert(datetime,null)
  21847.     from    sysusers u, sysobjects o
  21848.     where   o.type in ('U','V','S')
  21849.     and     o.name = @table_name
  21850.     and     (   @table_schema is null
  21851.         or  @table_schema = user_name(o.uid)
  21852.         )
  21853.     and     (
  21854.             @table_type is null
  21855.         or  @table_type = case o.type 
  21856.                     when 'U' then 'TABLE'
  21857.                     when 'V' then 'VIEW'
  21858.                     when 'S' then 'SYSTEM TABLE'
  21859.                     end
  21860.         )
  21861.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21862.     and     (
  21863.         suser_id() = 1   /* User is the System Administrator */
  21864.         or o.uid = user_id()     /* User created the object */
  21865.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21866.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21867.             from sysprotects p
  21868.             /* join to correlate with all rows in sysobjects */
  21869.             where p.id = o.id
  21870.             /* get rows for public,current user,user's group */
  21871.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21872.             /* check for SELECT,EXECUTE privilege */
  21873.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21874.             ) = 1   /* final magic...compare Grants   */
  21875.         )       
  21876.     order by 4, 2, 3
  21877. go
  21878. dump tran master with no_log
  21879. go
  21880. create procedure sp_tables_rowset;2
  21881.     (
  21882.     @table_schema   varchar(255) = null,    
  21883.     @table_type varchar(255) = null 
  21884.     )
  21885. as
  21886.     select  TABLE_CATALOG   = db_name(),
  21887.         TABLE_SCHEMA    = user_name(o.uid),
  21888.         TABLE_NAME  = o.name,
  21889.         TABLE_TYPE  = convert(varchar(30),
  21890.                     case o.type 
  21891.                     when 'U' then 'TABLE'
  21892.                     when 'V' then 'VIEW'
  21893.                     when 'S' then 'SYSTEM TABLE'
  21894.                     end),
  21895.         TABLE_GUID  = convert(binary(16), null),
  21896.         DESCRIPTION = convert(varchar(1), null),
  21897.         TABLE_PROPID    = convert(int,null),
  21898.         DATE_CREATED    = o.crdate,
  21899.         DATE_MODIFIED   = convert(datetime,null)
  21900.     from    sysusers u, sysobjects o
  21901.     where   o.type in ('U','V','S')
  21902.     and     (   @table_schema is null
  21903.         or  @table_schema = user_name(o.uid)
  21904.         )
  21905.     and     (
  21906.             @table_type is null
  21907.         or  @table_type = case o.type 
  21908.                     when 'U' then 'TABLE'
  21909.                     when 'V' then 'VIEW'
  21910.                     when 'S' then 'SYSTEM TABLE'
  21911.                     end
  21912.         )
  21913.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21914.     and     (
  21915.         suser_id() = 1   /* User is the System Administrator */
  21916.         or o.uid = user_id()     /* User created the object */
  21917.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21918.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21919.             from sysprotects p
  21920.             /* join to correlate with all rows in sysobjects */
  21921.             where p.id = o.id
  21922.             /* get rows for public,current user,user's group */
  21923.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21924.             /* check for SELECT,EXECUTE privilege */
  21925.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21926.             ) = 1   /* final magic...compare Grants   */
  21927.         )       
  21928.     order by 4, 2, 3
  21929. go
  21930. dump tran master with no_log
  21931. go
  21932.  
  21933. if (charindex('7.00', @@version) > 0 or
  21934.     charindex('8.00', @@version) > 0)
  21935.     drop procedure sp_tables_rowset
  21936. else
  21937. begin
  21938.     print ''
  21939.     print ''
  21940.     print 'Warning:'
  21941.     print 'you are installing the stored procedures '
  21942.     print 'on a pre 8.0 SQL Server.'
  21943.     print 'Ignore the following errors.'
  21944. end
  21945. go
  21946.  
  21947. /*  Procedure for 8.0 server */
  21948. create procedure sp_tables_rowset
  21949.     (
  21950.     @table_name sysname, 
  21951.     @table_schema   sysname = null, 
  21952.     @table_type nvarchar(255) = null 
  21953.     )
  21954. as
  21955.     select  *
  21956.     from    (select TABLE_CATALOG   = db_name(),
  21957.             TABLE_SCHEMA    = user_name(uid),
  21958.             TABLE_NAME  = name,
  21959.             TABLE_TYPE  = convert(nvarchar(30),
  21960.                         case type 
  21961.                         when 'U' then 
  21962.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21963.                             then N'TABLE' else N'SYSTEM TABLE' end
  21964.                         when 'S' then N'SYSTEM TABLE'
  21965.                         when 'V' then 
  21966.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21967.                             then N'VIEW' else N'SYSTEM VIEW' end
  21968.                         end),
  21969.             TABLE_GUID  = convert(uniqueidentifier, null),
  21970.             DESCRIPTION = convert(nvarchar(1), null),
  21971.             TABLE_PROPID    = convert(int,null),
  21972.             DATE_CREATED    = crdate,
  21973.             DATE_MODIFIED   = convert(datetime,null)
  21974.         from    sysobjects
  21975.         where   name = @table_name
  21976.         and type in ('U','V','S')
  21977.         and permissions(id) <> 0
  21978.         ) as o
  21979.  
  21980.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21981.     and     (@table_type is null or @table_type = TABLE_TYPE)   
  21982.     order by 4, 2, 3
  21983. go
  21984. dump tran master with no_log
  21985. go
  21986. create procedure sp_tables_rowset;2
  21987.     (
  21988.     @table_schema   sysname = null, 
  21989.     @table_type nvarchar(255) = null 
  21990.     )
  21991. as
  21992.     select  *
  21993.     from    (select TABLE_CATALOG   = db_name(),
  21994.             TABLE_SCHEMA    = user_name(uid),
  21995.             TABLE_NAME  = name,
  21996.             TABLE_TYPE  = convert(nvarchar(30),
  21997.                         case type 
  21998.                         when 'U' then 
  21999.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  22000.                             then N'TABLE' else N'SYSTEM TABLE' end
  22001.                         when 'S' then N'SYSTEM TABLE'
  22002.                         when 'V' then 
  22003.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  22004.                             then N'VIEW' else N'SYSTEM VIEW' end
  22005.                         end),
  22006.             TABLE_GUID  = convert(uniqueidentifier, null),
  22007.             DESCRIPTION = convert(nvarchar(1), null),
  22008.             TABLE_PROPID    = convert(int,null),
  22009.             DATE_CREATED    = crdate,
  22010.             DATE_MODIFIED   = convert(datetime,null)
  22011.         from    sysobjects
  22012.         where   type in ('U','V','S')
  22013.         and permissions(id) <> 0
  22014.         ) as o
  22015.  
  22016.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22017.     and     (@table_type is null or @table_type = TABLE_TYPE)   
  22018.     order by 4, 2, 3
  22019. go
  22020. dump tran master with no_log
  22021. go
  22022. create procedure sp_tables_rowset;5
  22023.     (
  22024.     @table_server       sysname,
  22025.     @table_catalog      sysname = null,
  22026.     @table_name     sysname = null,
  22027.     @table_schema       sysname = null,
  22028.     @table_type     sysname = null
  22029.     )
  22030. as
  22031.     select
  22032.         TABLE_CATALOG,
  22033.         TABLE_SCHEMA,
  22034.         TABLE_NAME,
  22035.         TABLE_TYPE,
  22036.         TABLE_GUID,
  22037.         DESCRIPTION
  22038.     --  TABLE_PROPID,
  22039.     --  DATE_CREATED,
  22040.     --  DATE_MODIFIED
  22041.     from master.dbo.SYSREMOTE_TABLES (
  22042.                 @table_server,
  22043.                 @table_catalog,
  22044.                 @table_schema,
  22045.                 @table_name,
  22046.                 @table_type )
  22047.     order by 4,1,2,3
  22048. go
  22049.  
  22050. grant execute on sp_tables_rowset to public
  22051. go
  22052.  
  22053. dump tran master with no_log
  22054. go
  22055. if (charindex('6.00', @@version) > 0)
  22056.     begin
  22057.     if (exists (select * from sysobjects
  22058.             where name = 'sp_tables_rowset' and type = 'P '))
  22059.         begin
  22060.         drop procedure sp_tables_rowset
  22061.         dump tran master with no_log
  22062.         end
  22063.     end
  22064. go
  22065.  
  22066.  
  22067.  
  22068. print ''
  22069. print 'creating sp_tables_info_rowset'
  22070. go
  22071.  
  22072.  
  22073. /*  Procedure for 6.50 and earlier servers */
  22074. create procedure sp_tables_info_rowset
  22075.     (
  22076.     @table_name varchar(255), 
  22077.     @table_schema   varchar(255) = null,    
  22078.     @table_type varchar(255) = null 
  22079.     )
  22080. as
  22081.     select  TABLE_CATALOG       = db_name(),
  22082.         TABLE_SCHEMA        = user_name(o.uid),
  22083.         TABLE_NAME      = o.name,
  22084.         TABLE_TYPE      = convert(varchar(30),
  22085.                         case o.type 
  22086.                         when 'U' then 'TABLE'
  22087.                         when 'V' then 'VIEW'
  22088.                         when 'S' then 'SYSTEM TABLE'
  22089.                         end),
  22090.         TABLE_GUID      = convert(binary(16), null),
  22091.         BOOKMARKS       = convert(bit, 1),
  22092.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22093.         BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22094.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22095.         BOOKMARK_INFORMATION    = convert(int, 0),
  22096.         TABLE_VERSION       = convert(int, o.schema_ver),
  22097.         CARDINALITY     = x.rows,
  22098.         DESCRIPTION     = convert(varchar(1), null),
  22099.         TABLE_PROPID        = convert(int, null)
  22100.  
  22101.     from    sysusers u, 
  22102.         sysobjects o,
  22103.         sysindexes x
  22104.  
  22105.     where   o.type in ('U','V','S')
  22106.     and     o.name = @table_name
  22107.     and     (   @table_schema is null
  22108.         or  @table_schema = user_name(o.uid)
  22109.         )
  22110.     and     (
  22111.             @table_type is null
  22112.         or  @table_type = case o.type 
  22113.                     when 'U' then 'TABLE'
  22114.                     when 'V' then 'VIEW'
  22115.                     when 'S' then 'SYSTEM TABLE'
  22116.                     end
  22117.         )
  22118.     and o.id *= x.id
  22119.     and x.indid in (0,1)
  22120.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22121.     and     (
  22122.         suser_id() = 1   /* User is the System Administrator */
  22123.         or o.uid = user_id()     /* User created the object */
  22124.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22125.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22126.             from sysprotects p
  22127.             /* join to correlate with all rows in sysobjects */
  22128.             where p.id = o.id
  22129.             /* get rows for public,current user,user's group */
  22130.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22131.             /* check for SELECT,EXECUTE privilege */
  22132.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22133.             ) = 1   /* final magic...compare Grants   */
  22134.         )       
  22135.     order by 4, 2, 3
  22136. go
  22137. dump tran master with no_log
  22138. go
  22139. create procedure sp_tables_info_rowset;2
  22140.     (
  22141.     @table_schema   varchar(255) = null,    
  22142.     @table_type varchar(255) = null 
  22143.     )
  22144. as
  22145.     select  TABLE_CATALOG       = db_name(),
  22146.         TABLE_SCHEMA        = user_name(o.uid),
  22147.         TABLE_NAME      = o.name,
  22148.         TABLE_TYPE      = convert(varchar(30),
  22149.                         case o.type 
  22150.                         when 'U' then 'TABLE'
  22151.                         when 'V' then 'VIEW'
  22152.                         when 'S' then 'SYSTEM TABLE'
  22153.                         end),
  22154.         TABLE_GUID      = convert(binary(16), null),
  22155.         BOOKMARKS       = convert(bit, 1),
  22156.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22157.         BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22158.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22159.         BOOKMARK_INFORMATION    = convert(int, 0),
  22160.         TABLE_VERSION       = convert(int, o.schema_ver),
  22161.         CARDINALITY     = x.rows,
  22162.         DESCRIPTION     = convert(varchar(1), null),
  22163.         TABLE_PROPID        = convert(int, null)
  22164.  
  22165.     from    sysusers u, 
  22166.         sysobjects o,
  22167.         sysindexes x
  22168.  
  22169.     where   o.type in ('U','V','S')
  22170.     and     (   @table_schema is null
  22171.         or  @table_schema = user_name(o.uid)
  22172.         )
  22173.     and     (
  22174.             @table_type is null
  22175.         or  @table_type = case o.type 
  22176.                     when 'U' then 'TABLE'
  22177.                     when 'V' then 'VIEW'
  22178.                     when 'S' then 'SYSTEM TABLE'
  22179.                     end
  22180.         )
  22181.     and o.id *= x.id
  22182.     and x.indid in (0,1)
  22183.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22184.     and     (
  22185.         suser_id() = 1   /* User is the System Administrator */
  22186.         or o.uid = user_id()     /* User created the object */
  22187.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22188.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22189.             from sysprotects p
  22190.             /* join to correlate with all rows in sysobjects */
  22191.             where p.id = o.id
  22192.             /* get rows for public,current user,user's group */
  22193.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22194.             /* check for SELECT,EXECUTE privilege */
  22195.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22196.             ) = 1   /* final magic...compare Grants   */
  22197.         )       
  22198.     order by 4, 2, 3
  22199. go
  22200. dump tran master with no_log
  22201. go
  22202.  
  22203. if (charindex('7.00', @@version) > 0 or
  22204.     charindex('8.00', @@version) > 0)
  22205.     drop procedure sp_tables_info_rowset
  22206. else
  22207. begin
  22208.     print ''
  22209.     print ''
  22210.     print 'Warning:'
  22211.     print 'you are installing the stored procedures '
  22212.     print 'on a pre 7.0 SQL Server.'
  22213.     print 'Ignore the following errors.'
  22214. end
  22215. go
  22216.  
  22217. /*  Procedure for 7.0 server */
  22218. create procedure sp_tables_info_rowset
  22219.     (
  22220.     @table_name sysname, 
  22221.     @table_schema   sysname = null, 
  22222.     @table_type nvarchar(255) = null 
  22223.     )
  22224. as
  22225.     select  *
  22226.     from    (select TABLE_CATALOG       = db_name(),
  22227.             TABLE_SCHEMA        = user_name(o.uid),
  22228.             TABLE_NAME      = o.name,
  22229.             TABLE_TYPE      = convert(nvarchar(30),
  22230.                             case o.type 
  22231.                             when 'U' then 
  22232.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22233.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22234.                             when 'S' then N'SYSTEM TABLE'
  22235.                             when 'V' then 
  22236.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22237.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22238.                             end),
  22239.             TABLE_GUID      = convert(uniqueidentifier, null),
  22240.             BOOKMARKS       = convert(bit, 1),
  22241.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22242.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22243.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22244.             BOOKMARK_INFORMATION    = convert(int, 0),
  22245.             TABLE_VERSION       = convert(int, o.schema_ver),
  22246.             CARDINALITY     = x.rows,
  22247.             DESCRIPTION     = convert(nvarchar(1), null),
  22248.             TABLE_PROPID        = convert(int, null) 
  22249.     
  22250.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22251.         where   o.name = @table_name
  22252.         and     o.type in ('U','V','S')
  22253.         and permissions(o.id) <> 0) as t
  22254.  
  22255.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22256.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22257.     order by 4, 2, 3
  22258. go
  22259. dump tran master with no_log
  22260. go
  22261. create procedure sp_tables_info_rowset;2
  22262.     (
  22263.     @table_schema   sysname = null, 
  22264.     @table_type nvarchar(255) = null 
  22265.     )
  22266. as
  22267.     select  *
  22268.     from    (select TABLE_CATALOG       = db_name(),
  22269.             TABLE_SCHEMA        = user_name(o.uid),
  22270.             TABLE_NAME      = o.name,
  22271.             TABLE_TYPE      = convert(nvarchar(30),
  22272.                             case o.type 
  22273.                             when 'U' then 
  22274.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22275.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22276.                             when 'S' then N'SYSTEM TABLE'
  22277.                             when 'V' then 
  22278.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22279.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22280.                             end),
  22281.             TABLE_GUID      = convert(uniqueidentifier, null),
  22282.             BOOKMARKS       = convert(bit, 1),
  22283.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22284.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22285.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22286.             BOOKMARK_INFORMATION    = convert(int, 0),
  22287.             TABLE_VERSION       = convert(int, o.schema_ver),
  22288.             CARDINALITY     = x.rows,
  22289.             DESCRIPTION     = convert(nvarchar(1), null),
  22290.             TABLE_PROPID        = convert(int, null) 
  22291.     
  22292.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22293.         where   o.type in ('U','V','S')
  22294.         and permissions(o.id) <> 0) as t
  22295.  
  22296.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22297.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22298.     order by 4, 2, 3
  22299. go
  22300.  
  22301. dump tran master with no_log
  22302. go
  22303.  
  22304. if (charindex('8.00', @@version) > 0)
  22305.     drop procedure sp_tables_info_rowset
  22306. else
  22307. begin
  22308.     print ''
  22309.     print ''
  22310.     print 'Warning:'
  22311.     print 'you are installing the stored procedures '
  22312.     print 'on a pre 8.0 SQL Server.'
  22313.     print 'Ignore the following errors.'
  22314. end
  22315. go
  22316.  
  22317. /*  Procedure for 8.0 server */
  22318. create procedure sp_tables_info_rowset
  22319.     (
  22320.     @table_name sysname, 
  22321.     @table_schema   sysname = null, 
  22322.     @table_type nvarchar(255) = null 
  22323.     )
  22324. as
  22325.     select  *
  22326.     from    (select TABLE_CATALOG       = db_name(),
  22327.             TABLE_SCHEMA        = user_name(o.uid),
  22328.             TABLE_NAME      = o.name,
  22329.             TABLE_TYPE      = convert(nvarchar(30),
  22330.                             case o.type 
  22331.                             when 'U' then 
  22332.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22333.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22334.                             when 'S' then N'SYSTEM TABLE'
  22335.                             when 'V' then 
  22336.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22337.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22338.                             end),
  22339.             TABLE_GUID      = convert(uniqueidentifier, null),
  22340.             BOOKMARKS       = convert(bit, 1),
  22341.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22342.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22343.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22344.             BOOKMARK_INFORMATION    = convert(int, 0),
  22345.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22346.             CARDINALITY     = x.rows,
  22347.             DESCRIPTION     = convert(nvarchar(1), null),
  22348.             TABLE_PROPID        = convert(int, null) 
  22349.     
  22350.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22351.         where   o.name = @table_name
  22352.         and     o.type in ('U','V','S')
  22353.         and permissions(o.id) <> 0) as t
  22354.  
  22355.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22356.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22357.     order by 4, 2, 3
  22358. go
  22359. dump tran master with no_log
  22360. go
  22361. create procedure sp_tables_info_rowset;2
  22362.     (
  22363.     @table_schema   sysname = null, 
  22364.     @table_type nvarchar(255) = null 
  22365.     )
  22366. as
  22367.     select  *
  22368.     from    (select TABLE_CATALOG       = db_name(),
  22369.             TABLE_SCHEMA        = user_name(o.uid),
  22370.             TABLE_NAME      = o.name,
  22371.             TABLE_TYPE      = convert(nvarchar(30),
  22372.                             case o.type 
  22373.                             when 'U' then 
  22374.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22375.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22376.                             when 'S' then N'SYSTEM TABLE'
  22377.                             when 'V' then 
  22378.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22379.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22380.                             end),
  22381.             TABLE_GUID      = convert(uniqueidentifier, null),
  22382.             BOOKMARKS       = convert(bit, 1),
  22383.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22384.             BOOKMARK_DATATYPE   = convert(smallint, 19 /*DBTYPE_UI4 */),
  22385.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22386.             BOOKMARK_INFORMATION    = convert(int, 0),
  22387.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22388.             CARDINALITY     = x.rows,
  22389.             DESCRIPTION     = convert(nvarchar(1), null),
  22390.             TABLE_PROPID        = convert(int, null) 
  22391.     
  22392.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22393.         where   o.type in ('U','V','S')
  22394.         and permissions(o.id) <> 0) as t
  22395.  
  22396.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22397.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22398.     order by 4, 2, 3
  22399. go
  22400.  
  22401. grant execute on sp_tables_info_rowset to public
  22402. go
  22403.  
  22404. dump tran master with no_log
  22405. go
  22406.  
  22407. if (charindex('6.00', @@version) > 0)
  22408.     begin
  22409.     if (exists (select * from sysobjects
  22410.         where name = 'sp_tables_info_rowset' and type = 'P '))
  22411.         begin
  22412.         drop procedure sp_tables_info_rowset
  22413.         dump tran master with no_log
  22414.         end
  22415.     end
  22416. go
  22417.  
  22418. print ''
  22419. print 'creating sp_tables_info_rowset_64'
  22420. go
  22421.  
  22422.  
  22423. /*  Procedure for 6.50 and earlier servers */
  22424. create procedure sp_tables_info_rowset_64
  22425.     (
  22426.     @table_name varchar(255), 
  22427.     @table_schema   varchar(255) = null,    
  22428.     @table_type varchar(255) = null 
  22429.     )
  22430. as
  22431.     select  TABLE_CATALOG       = db_name(),
  22432.         TABLE_SCHEMA        = user_name(o.uid),
  22433.         TABLE_NAME      = o.name,
  22434.         TABLE_TYPE      = convert(varchar(30),
  22435.                         case o.type 
  22436.                         when 'U' then 'TABLE'
  22437.                         when 'V' then 'VIEW'
  22438.                         when 'S' then 'SYSTEM TABLE'
  22439.                         end),
  22440.         TABLE_GUID      = convert(binary(16), null),
  22441.         BOOKMARKS       = convert(bit, 1),
  22442.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22443.         BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22444.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22445.         BOOKMARK_INFORMATION    = convert(int, 0),
  22446.         TABLE_VERSION       = convert(int, o.schema_ver),
  22447.         CARDINALITY     = x.rows,
  22448.         DESCRIPTION     = convert(varchar(1), null),
  22449.         TABLE_PROPID        = convert(int, null)
  22450.  
  22451.     from    sysusers u, 
  22452.         sysobjects o,
  22453.         sysindexes x
  22454.  
  22455.     where   o.type in ('U','V','S')
  22456.     and     o.name = @table_name
  22457.     and     (   @table_schema is null
  22458.         or  @table_schema = user_name(o.uid)
  22459.         )
  22460.     and     (
  22461.             @table_type is null
  22462.         or  @table_type = case o.type 
  22463.                     when 'U' then 'TABLE'
  22464.                     when 'V' then 'VIEW'
  22465.                     when 'S' then 'SYSTEM TABLE'
  22466.                     end
  22467.         )
  22468.     and o.id *= x.id
  22469.     and x.indid in (0,1)
  22470.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22471.     and     (
  22472.         suser_id() = 1   /* User is the System Administrator */
  22473.         or o.uid = user_id()     /* User created the object */
  22474.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22475.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22476.             from sysprotects p
  22477.             /* join to correlate with all rows in sysobjects */
  22478.             where p.id = o.id
  22479.             /* get rows for public,current user,user's group */
  22480.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22481.             /* check for SELECT,EXECUTE privilege */
  22482.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22483.             ) = 1   /* final magic...compare Grants   */
  22484.         )       
  22485.     order by 4, 2, 3
  22486. go
  22487. dump tran master with no_log
  22488. go
  22489. create procedure sp_tables_info_rowset_64;2
  22490.     (
  22491.     @table_schema   varchar(255) = null,    
  22492.     @table_type varchar(255) = null 
  22493.     )
  22494. as
  22495.     select  TABLE_CATALOG       = db_name(),
  22496.         TABLE_SCHEMA        = user_name(o.uid),
  22497.         TABLE_NAME      = o.name,
  22498.         TABLE_TYPE      = convert(varchar(30),
  22499.                         case o.type 
  22500.                         when 'U' then 'TABLE'
  22501.                         when 'V' then 'VIEW'
  22502.                         when 'S' then 'SYSTEM TABLE'
  22503.                         end),
  22504.         TABLE_GUID      = convert(binary(16), null),
  22505.         BOOKMARKS       = convert(bit, 1),
  22506.         BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22507.         BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22508.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22509.         BOOKMARK_INFORMATION    = convert(int, 0),
  22510.         TABLE_VERSION       = convert(int, o.schema_ver),
  22511.         CARDINALITY     = x.rows,
  22512.         DESCRIPTION     = convert(varchar(1), null),
  22513.         TABLE_PROPID        = convert(int, null)
  22514.  
  22515.     from    sysusers u, 
  22516.         sysobjects o,
  22517.         sysindexes x
  22518.  
  22519.     where   o.type in ('U','V','S')
  22520.     and     (   @table_schema is null
  22521.         or  @table_schema = user_name(o.uid)
  22522.         )
  22523.     and     (
  22524.             @table_type is null
  22525.         or  @table_type = case o.type 
  22526.                     when 'U' then 'TABLE'
  22527.                     when 'V' then 'VIEW'
  22528.                     when 'S' then 'SYSTEM TABLE'
  22529.                     end
  22530.         )
  22531.     and o.id *= x.id
  22532.     and x.indid in (0,1)
  22533.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22534.     and     (
  22535.         suser_id() = 1   /* User is the System Administrator */
  22536.         or o.uid = user_id()     /* User created the object */
  22537.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22538.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22539.             from sysprotects p
  22540.             /* join to correlate with all rows in sysobjects */
  22541.             where p.id = o.id
  22542.             /* get rows for public,current user,user's group */
  22543.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22544.             /* check for SELECT,EXECUTE privilege */
  22545.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22546.             ) = 1   /* final magic...compare Grants   */
  22547.         )       
  22548.     order by 4, 2, 3
  22549. go
  22550. dump tran master with no_log
  22551. go
  22552.  
  22553. if (charindex('7.00', @@version) > 0 or
  22554.     charindex('8.00', @@version) > 0)
  22555.     drop procedure sp_tables_info_rowset_64
  22556. else
  22557. begin
  22558.     print ''
  22559.     print ''
  22560.     print 'Warning:'
  22561.     print 'you are installing the stored procedures '
  22562.     print 'on a pre 7.0 SQL Server.'
  22563.     print 'Ignore the following errors.'
  22564. end
  22565. go
  22566.  
  22567. /*  Procedure for 7.0 server */
  22568. create procedure sp_tables_info_rowset_64
  22569.     (
  22570.     @table_name sysname, 
  22571.     @table_schema   sysname = null, 
  22572.     @table_type nvarchar(255) = null 
  22573.     )
  22574. as
  22575.     select  *
  22576.     from    (select TABLE_CATALOG       = db_name(),
  22577.             TABLE_SCHEMA        = user_name(o.uid),
  22578.             TABLE_NAME      = o.name,
  22579.             TABLE_TYPE      = convert(nvarchar(30),
  22580.                             case o.type 
  22581.                             when 'U' then 
  22582.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22583.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22584.                             when 'S' then N'SYSTEM TABLE'
  22585.                             when 'V' then 
  22586.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22587.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22588.                             end),
  22589.             TABLE_GUID      = convert(uniqueidentifier, null),
  22590.             BOOKMARKS       = convert(bit, 1),
  22591.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22592.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22593.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22594.             BOOKMARK_INFORMATION    = convert(int, 0),
  22595.             TABLE_VERSION       = convert(int, o.schema_ver),
  22596.             CARDINALITY     = x.rows,
  22597.             DESCRIPTION     = convert(nvarchar(1), null),
  22598.             TABLE_PROPID        = convert(int, null) 
  22599.     
  22600.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22601.         where   o.name = @table_name
  22602.         and     o.type in ('U','V','S')
  22603.         and permissions(o.id) <> 0) as t
  22604.  
  22605.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22606.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22607.     order by 4, 2, 3
  22608. go
  22609. dump tran master with no_log
  22610. go
  22611. create procedure sp_tables_info_rowset_64;2
  22612.     (
  22613.     @table_schema   sysname = null, 
  22614.     @table_type nvarchar(255) = null 
  22615.     )
  22616. as
  22617.     select  *
  22618.     from    (select TABLE_CATALOG       = db_name(),
  22619.             TABLE_SCHEMA        = user_name(o.uid),
  22620.             TABLE_NAME      = o.name,
  22621.             TABLE_TYPE      = convert(nvarchar(30),
  22622.                             case o.type 
  22623.                             when 'U' then 
  22624.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22625.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22626.                             when 'S' then N'SYSTEM TABLE'
  22627.                             when 'V' then 
  22628.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22629.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22630.                             end),
  22631.             TABLE_GUID      = convert(uniqueidentifier, null),
  22632.             BOOKMARKS       = convert(bit, 1),
  22633.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22634.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22635.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22636.             BOOKMARK_INFORMATION    = convert(int, 0),
  22637.             TABLE_VERSION       = convert(int, o.schema_ver),
  22638.             CARDINALITY     = x.rows,
  22639.             DESCRIPTION     = convert(nvarchar(1), null),
  22640.             TABLE_PROPID        = convert(int, null) 
  22641.     
  22642.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22643.         where   o.type in ('U','V','S')
  22644.         and permissions(o.id) <> 0) as t
  22645.  
  22646.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22647.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22648.     order by 4, 2, 3
  22649. go
  22650.  
  22651. dump tran master with no_log
  22652. go
  22653.  
  22654. if (charindex('8.00', @@version) > 0)
  22655.     drop procedure sp_tables_info_rowset_64
  22656. else
  22657. begin
  22658.     print ''
  22659.     print ''
  22660.     print 'Warning:'
  22661.     print 'you are installing the stored procedures '
  22662.     print 'on a pre 8.0 SQL Server.'
  22663.     print 'Ignore the following errors.'
  22664. end
  22665. go
  22666.  
  22667. /*  Procedure for 8.0 server */
  22668. create procedure sp_tables_info_rowset_64
  22669.     (
  22670.     @table_name sysname, 
  22671.     @table_schema   sysname = null, 
  22672.     @table_type nvarchar(255) = null 
  22673.     )
  22674. as
  22675.     select  *
  22676.     from    (select TABLE_CATALOG       = db_name(),
  22677.             TABLE_SCHEMA        = user_name(o.uid),
  22678.             TABLE_NAME      = o.name,
  22679.             TABLE_TYPE      = convert(nvarchar(30),
  22680.                             case o.type 
  22681.                             when 'U' then 
  22682.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22683.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22684.                             when 'S' then N'SYSTEM TABLE'
  22685.                             when 'V' then 
  22686.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22687.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22688.                             end),
  22689.             TABLE_GUID      = convert(uniqueidentifier, null),
  22690.             BOOKMARKS       = convert(bit, 1),
  22691.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22692.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22693.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22694.             BOOKMARK_INFORMATION    = convert(int, 0),
  22695.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22696.             CARDINALITY     = x.rows,
  22697.             DESCRIPTION     = convert(nvarchar(1), null),
  22698.             TABLE_PROPID        = convert(int, null) 
  22699.     
  22700.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22701.         where   o.name = @table_name
  22702.         and     o.type in ('U','V','S')
  22703.         and permissions(o.id) <> 0) as t
  22704.  
  22705.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22706.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22707.     order by 4, 2, 3
  22708. go
  22709. dump tran master with no_log
  22710. go
  22711. create procedure sp_tables_info_rowset_64;2
  22712.     (
  22713.     @table_schema   sysname = null, 
  22714.     @table_type nvarchar(255) = null 
  22715.     )
  22716. as
  22717.     select  *
  22718.     from    (select TABLE_CATALOG       = db_name(),
  22719.             TABLE_SCHEMA        = user_name(o.uid),
  22720.             TABLE_NAME      = o.name,
  22721.             TABLE_TYPE      = convert(nvarchar(30),
  22722.                             case o.type 
  22723.                             when 'U' then 
  22724.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22725.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22726.                             when 'S' then N'SYSTEM TABLE'
  22727.                             when 'V' then 
  22728.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22729.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22730.                             end),
  22731.             TABLE_GUID      = convert(uniqueidentifier, null),
  22732.             BOOKMARKS       = convert(bit, 1),
  22733.             BOOKMARK_TYPE       = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22734.             BOOKMARK_DATATYPE   = convert(smallint, 21 /*DBTYPE_UI8 */),
  22735.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22736.             BOOKMARK_INFORMATION    = convert(int, 0),
  22737.             TABLE_VERSION       = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22738.             CARDINALITY     = x.rows,
  22739.             DESCRIPTION     = convert(nvarchar(1), null),
  22740.             TABLE_PROPID        = convert(int, null) 
  22741.     
  22742.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22743.         where   o.type in ('U','V','S')
  22744.         and permissions(o.id) <> 0) as t
  22745.  
  22746.     where   (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22747.     and     (@table_type is null or @table_type = TABLE_TYPE)
  22748.     order by 4, 2, 3
  22749. go
  22750.  
  22751. grant execute on sp_tables_info_rowset_64 to public
  22752. go
  22753.  
  22754. dump tran master with no_log
  22755. go
  22756.  
  22757. if (charindex('6.00', @@version) > 0)
  22758.     begin
  22759.     if (exists (select * from sysobjects
  22760.         where name = 'sp_tables_info_rowset_64' and type = 'P '))
  22761.         begin
  22762.         drop procedure sp_tables_info_rowset_64
  22763.         dump tran master with no_log
  22764.         end
  22765.     end
  22766. go
  22767.  
  22768. print ''
  22769. print 'creating sp_table_constraints_rowset'
  22770. go
  22771.  
  22772.  
  22773. /*  Procedure for 6.50 and earlier servers */
  22774. create procedure sp_table_constraints_rowset
  22775.     (
  22776.     @table_name         varchar(255), 
  22777.     @table_schema       varchar(255) = null,
  22778.     @table_catalog      varchar(255) = null,
  22779.     @constraint_name    varchar(255) = null,
  22780.     @constraint_schema  varchar(255) = null,
  22781.     @constraint_catalog varchar(255) = null,
  22782.     @constraint_type    varchar(255) = null 
  22783.     )
  22784. as
  22785.     select
  22786.         CONSTRAINT_CATALOG  = db_name(),
  22787.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22788.         CONSTRAINT_NAME     = c_obj.name,
  22789.         TABLE_CATALOG       = db_name(),
  22790.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22791.         TABLE_NAME          = t_obj.name,
  22792.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22793.                                 when 1 then 'PRIMARY KEY'
  22794.                                 when 2 then 'UNIQUE'
  22795.                                 when 3 then 'FOREIGN KEY'
  22796.                                 when 4 then 'CHECK'
  22797.                                 end, 
  22798.         IS_DEFERRABLE       = convert(tinyint, 0),
  22799.         INITIALLY_DEFERRED  = convert(tinyint, 0),
  22800.         DESCRIPTION         = convert(varchar(1), null)
  22801.  
  22802.     from 
  22803.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22804.     where
  22805.             t_obj.name  = @table_name
  22806.         and t_obj.type in ('U','S')
  22807.         and (@table_catalog is null or @table_catalog = db_name())
  22808.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22809.         and c.id = t_obj.id
  22810.         and (c.status & 0xf) between 1 and 4
  22811.         and c_obj.id    = c.constid
  22812.         and c_obj.uid   = user_id()
  22813.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22814.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22815.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22816.         and (@constraint_type is null
  22817.             or (c.status & 0xf)
  22818.                 = case @constraint_type
  22819.                     when 'PRIMARY KEY' then 1
  22820.                     when 'UNIQUE' then 2
  22821.                     when 'FOREIGN KEY' then 3
  22822.                     when 'CHECK' then 4
  22823.                     end)
  22824. order by 2,3,5,6,7 
  22825. go
  22826. dump tran master with no_log
  22827. go
  22828. create procedure sp_table_constraints_rowset;2
  22829.     (
  22830.     @table_schema       varchar(255) = null,
  22831.     @table_catalog      varchar(255) = null,
  22832.     @constraint_name    varchar(255) = null,
  22833.     @constraint_schema  varchar(255) = null,
  22834.     @constraint_catalog varchar(255) = null,
  22835.     @constraint_type    varchar(255) = null 
  22836.     )
  22837. as
  22838.     select
  22839.         CONSTRAINT_CATALOG  = db_name(),
  22840.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22841.         CONSTRAINT_NAME     = c_obj.name,
  22842.         TABLE_CATALOG       = db_name(),
  22843.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22844.         TABLE_NAME          = t_obj.name,
  22845.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22846.                                 when 1 then 'PRIMARY KEY'
  22847.                                 when 2 then 'UNIQUE'
  22848.                                 when 3 then 'FOREIGN KEY'
  22849.                                 when 4 then 'CHECK'
  22850.                                 end, 
  22851.         IS_DEFERRABLE       = convert(tinyint, 0),
  22852.         INITIALLY_DEFERRED  = convert(tinyint, 0),
  22853.         DESCRIPTION         = convert(varchar(1), null)
  22854.  
  22855.     from 
  22856.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22857.     where
  22858.             t_obj.type in ('U','S')
  22859.         and (@table_catalog is null or @table_catalog = db_name())
  22860.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22861.         and c.id = t_obj.id
  22862.         and (c.status & 0xf) between 1 and 4
  22863.         and c_obj.id    = c.constid
  22864.         and c_obj.uid   = user_id()
  22865.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22866.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22867.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22868.         and (@constraint_type is null
  22869.             or (c.status & 0xf)
  22870.                 = case @constraint_type
  22871.                     when 'PRIMARY KEY' then 1
  22872.                     when 'UNIQUE' then 2
  22873.                     when 'FOREIGN KEY' then 3
  22874.                     when 'CHECK' then 4
  22875.                     end)
  22876. order by 2,3,5,6,7 
  22877. go
  22878. dump tran master with no_log
  22879. go
  22880.  
  22881.  
  22882. if (charindex('7.00', @@version) > 0 or
  22883.     charindex('8.00', @@version) > 0)
  22884.     drop procedure sp_table_constraints_rowset
  22885. else
  22886. begin
  22887.     print ''
  22888.     print ''
  22889.     print 'Warning:'
  22890.     print 'you are installing the stored procedures '
  22891.     print 'on a pre 8.0 SQL Server.'
  22892.     print 'Ignore the following errors.'
  22893. end
  22894. go
  22895.  
  22896. /*  Procedure for 8.0 server */
  22897. create procedure sp_table_constraints_rowset
  22898.     (
  22899.     @table_name         sysname, 
  22900.     @table_schema       sysname = null,
  22901.     @table_catalog      sysname = null,
  22902.     @constraint_name    sysname = null,
  22903.     @constraint_schema  sysname = null,
  22904.     @constraint_catalog sysname = null,
  22905.     @constraint_type    nvarchar(255) = null 
  22906.     )
  22907. as
  22908.     select
  22909.         CONSTRAINT_CATALOG  = db_name(),
  22910.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22911.         CONSTRAINT_NAME     = c_obj.name,
  22912.         TABLE_CATALOG       = db_name(),
  22913.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22914.         TABLE_NAME          = t_obj.name,
  22915.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22916.                                 when 1 then N'PRIMARY KEY'
  22917.                                 when 2 then N'UNIQUE'
  22918.                                 when 3 then N'FOREIGN KEY'
  22919.                                 when 4 then N'CHECK'
  22920.                                 end, 
  22921.         IS_DEFERRABLE       = convert(bit, 0),
  22922.         INITIALLY_DEFERRED  = convert(bit, 0),
  22923.         DESCRIPTION         = convert(nvarchar(1), null)
  22924.  
  22925.     from 
  22926.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22927.     where
  22928.             t_obj.name  = @table_name
  22929.         and t_obj.type in ('U','S')
  22930.         and (@table_catalog is null or @table_catalog = db_name())
  22931.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22932.         and c.id = t_obj.id
  22933.         and (c.status & 0xf) between 1 and 4
  22934.         and c_obj.id    = c.constid
  22935.         and c_obj.uid   = user_id()
  22936.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22937.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22938.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22939.         and (@constraint_type is null
  22940.             or (c.status & 0xf)
  22941.                 = case @constraint_type
  22942.                     when N'PRIMARY KEY' then 1
  22943.                     when N'UNIQUE' then 2
  22944.                     when N'FOREIGN KEY' then 3
  22945.                     when N'CHECK' then 4
  22946.                     end)
  22947. order by 2,3,5,6,7 
  22948. go
  22949. dump tran master with no_log
  22950. go
  22951. create procedure sp_table_constraints_rowset;2
  22952.     (
  22953.     @table_schema       sysname = null,
  22954.     @table_catalog      sysname = null,
  22955.     @constraint_name    sysname = null,
  22956.     @constraint_schema  sysname = null,
  22957.     @constraint_catalog sysname = null,
  22958.     @constraint_type    nvarchar(255) = null 
  22959.     )
  22960. as
  22961.     select
  22962.         CONSTRAINT_CATALOG  = db_name(),
  22963.         CONSTRAINT_SCHEMA   = user_name(c_obj.uid),
  22964.         CONSTRAINT_NAME     = c_obj.name,
  22965.         TABLE_CATALOG       = db_name(),
  22966.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22967.         TABLE_NAME          = t_obj.name,
  22968.         CONSTRAINT_TYPE     = case (c.status & 0xf)
  22969.                                 when 1 then N'PRIMARY KEY'
  22970.                                 when 2 then N'UNIQUE'
  22971.                                 when 3 then N'FOREIGN KEY'
  22972.                                 when 4 then N'CHECK'
  22973.                                 end, 
  22974.         IS_DEFERRABLE       = convert(bit, 0),
  22975.         INITIALLY_DEFERRED  = convert(bit, 0),
  22976.         DESCRIPTION         = convert(nvarchar(1), null)
  22977.  
  22978.     from 
  22979.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22980.     where
  22981.             t_obj.type in ('U','S')
  22982.         and (@table_catalog is null or @table_catalog = db_name())
  22983.         and (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22984.         and c.id = t_obj.id
  22985.         and (c.status & 0xf) between 1 and 4
  22986.         and c_obj.id    = c.constid
  22987.         and c_obj.uid   = user_id()
  22988.         and (@constraint_name is null or c_obj.name = @constraint_name)
  22989.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22990.         and (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22991.         and (@constraint_type is null
  22992.             or (c.status & 0xf)
  22993.                 = case @constraint_type
  22994.                     when N'PRIMARY KEY' then 1
  22995.                     when N'UNIQUE' then 2
  22996.                     when N'FOREIGN KEY' then 3
  22997.                     when N'CHECK' then 4
  22998.                     end)
  22999. order by 2,3,5,6,7 
  23000. go
  23001. dump tran master with no_log
  23002.  
  23003.  
  23004. grant execute on sp_table_constraints_rowset to public
  23005. go
  23006.  
  23007. dump tran master with no_log
  23008. go
  23009. if (charindex('6.00', @@version) > 0)
  23010.     begin
  23011.     if (exists (select * from sysobjects
  23012.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  23013.         begin
  23014.         drop procedure sp_table_constraints_rowset
  23015.         dump tran master with no_log
  23016.         end
  23017.     end
  23018. go
  23019.  
  23020. print ''
  23021. print 'creating sp_table_privileges_rowset'
  23022. go
  23023.  
  23024. /*  Procedure for 6.0 and 6.5 server */
  23025. CREATE PROCEDURE sp_table_privileges_rowset
  23026.     (
  23027.     @table_name varchar(255) = null,
  23028.     @table_schema   varchar(255) = null,
  23029.     @grantor    varchar(255) = null,
  23030.     @grantee    varchar(255) = null
  23031.     )
  23032. as
  23033. IF @table_name is not null
  23034.     BEGIN
  23035.     select
  23036.         GRANTOR     = user_name(p.grantor),
  23037.         GRANTEE     = user_name(u.uid),
  23038.         TABLE_CATALOG   = db_name(),
  23039.         TABLE_SCHEMA    = user_name(o.uid),
  23040.         TABLE_NAME  = o.name,
  23041.         PRIVILEGE_TYPE  = convert(varchar(30),
  23042.                     case p.action
  23043.                     when 193 then 'SELECT'
  23044.                     when 195 then 'INSERT'
  23045.                     when 196 then 'DELETE'
  23046.                     when 197 then 'UPDATE'
  23047.                     else 'REFERENCES'
  23048.                     end),
  23049.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23050.     from 
  23051.         sysprotects p, sysobjects o, sysusers u
  23052.     where
  23053.         o.name = @table_name
  23054.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23055.     and (@grantee is null or @grantee = user_name(u.uid))
  23056.     and     o.type in ('U','V','S')
  23057.     and p.id = o.id
  23058.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23059.             /* expand groups */
  23060.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23061.     and     p.protecttype <> 206    /* only grant rows */
  23062.     and     p.action in (26,193,195,196,197)
  23063.     and     o.uid <> u.uid          /* no rows for owner */
  23064.     and     not exists (            /* exclude revoke'd privileges */
  23065.             select  *
  23066.             from    sysprotects p1
  23067.             where   p1.protecttype = 206
  23068.             and     p1.action = p.action
  23069.             and     p1.id = p.id
  23070.             and     p1.uid = u.uid)
  23071.     union
  23072.     select  /*  Add rows for table owner */
  23073.         GRANTOR     = user_name(u.uid),
  23074.         GRANTEE     = user_name(o.uid),
  23075.         TABLE_CATALOG   = db_name(),
  23076.         TABLE_SCHEMA    = user_name(o.uid),
  23077.         TABLE_NAME  = o.name,
  23078.         PRIVILEGE_TYPE  = convert(varchar(30),
  23079.                     case v.number
  23080.                     when 193 then 'SELECT'
  23081.                     when 195 then 'INSERT'
  23082.                     when 196 then 'DELETE'
  23083.                     when 197 then 'UPDATE'
  23084.                     else 'REFERENCES'
  23085.                     end),
  23086.         IS_GRANTABLE    = convert(bit,1)    
  23087.     from 
  23088.         sysobjects o, master.dbo.spt_values v, sysusers u
  23089.     where
  23090.         o.name = @table_name
  23091.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23092.     and (@grantee is null or @grantee = user_name(o.uid))
  23093.     and     o.type in ('U','V','S')
  23094.     and     u.uid = 1       /* grantor is dbo of database */
  23095.     and (@grantor is null or @grantor = user_name(u.uid))
  23096.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23097.     and     v.number in (26,193,195,196,197)
  23098.     and     not exists (    /* exclude revoke'd privileges */
  23099.             select  *
  23100.             from    sysprotects p1
  23101.             where   p1.protecttype = 206
  23102.             and     p1.action = v.number
  23103.             and     p1.id = o.id
  23104.             and     p1.uid = o.uid)
  23105.     order by 4,5,6,1,2
  23106.     END
  23107. ELSE
  23108.     BEGIN
  23109.     select
  23110.         GRANTOR     = user_name(p.grantor),
  23111.         GRANTEE     = user_name(u.uid),
  23112.         TABLE_CATALOG   = db_name(),
  23113.         TABLE_SCHEMA    = user_name(o.uid),
  23114.         TABLE_NAME  = o.name,
  23115.         PRIVILEGE_TYPE  = convert(varchar(30),
  23116.                     case p.action
  23117.                     when 193 then 'SELECT'
  23118.                     when 195 then 'INSERT'
  23119.                     when 196 then 'DELETE'
  23120.                     when 197 then 'UPDATE'
  23121.                     else 'REFERENCES'
  23122.                     end),
  23123.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23124.     from 
  23125.         sysprotects p, sysobjects o, sysusers u
  23126.     where
  23127.         o.type in ('U','V','S')
  23128.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23129.     and (@grantee is null or @grantee = user_name(u.uid))
  23130.     and p.id = o.id
  23131.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23132.             /* expand groups */
  23133.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23134.     and     p.protecttype <> 206    /* only grant rows */
  23135.     and     p.action in (26,193,195,196,197)
  23136.     and     o.uid <> u.uid          /* no rows for owner */
  23137.     and     not exists (            /* exclude revoke'd privileges */
  23138.             select  *
  23139.             from    sysprotects p1
  23140.             where   p1.protecttype = 206
  23141.             and     p1.action = p.action
  23142.             and     p1.id = p.id
  23143.             and     p1.uid = u.uid)
  23144.     union
  23145.     select  /*  Add rows for table owner */
  23146.         GRANTOR     = user_name(u.uid),
  23147.         GRANTEE     = user_name(o.uid),
  23148.         TABLE_CATALOG   = db_name(),
  23149.         TABLE_SCHEMA    = user_name(o.uid),
  23150.         TABLE_NAME  = o.name,
  23151.         PRIVILEGE_TYPE  = convert(varchar(30),
  23152.                     case v.number
  23153.                     when 193 then 'SELECT'
  23154.                     when 195 then 'INSERT'
  23155.                     when 196 then 'DELETE'
  23156.                     when 197 then 'UPDATE'
  23157.                     else 'REFERENCES'
  23158.                     end),
  23159.         IS_GRANTABLE    = convert(bit,1)    
  23160.     from 
  23161.         sysobjects o, master.dbo.spt_values v, sysusers u
  23162.     where
  23163.         o.type in ('U','V','S')
  23164.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23165.     and (@grantee is null or @grantee = user_name(o.uid))
  23166.     and     u.uid = 1       /* grantor is dbo of database */
  23167.     and (@grantor is null or @grantor = user_name(u.uid))
  23168.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23169.     and     v.number in (26,193,195,196,197)
  23170.     and     not exists (    /* exclude revoke'd privileges */
  23171.             select  *
  23172.             from    sysprotects p1
  23173.             where   p1.protecttype = 206
  23174.             and     p1.action = v.number
  23175.             and     p1.id = o.id
  23176.             and     p1.uid = o.uid)
  23177.     order by 4,5,6,1,2
  23178.     END
  23179. go
  23180. dump tran master with no_log
  23181. go
  23182. CREATE PROCEDURE sp_table_privileges_rowset;2
  23183.     (
  23184.     @handle     int output,
  23185.     @scrollopt  int output,
  23186.     @ccopt      int output,
  23187.     @rows       int output,
  23188.     @table_name     varchar(255) = null,
  23189.     @table_schema   varchar(255) = null,
  23190.     @grantor    varchar(255) = null,
  23191.     @grantee    varchar(255) = null
  23192.     )
  23193. as
  23194. declare @ret int
  23195. SET NOCOUNT ON
  23196. create table #sptprivsrowset1
  23197.     (
  23198.     GRANTOR     sysname not null,
  23199.     GRANTEE     sysname not null,
  23200.     TABLE_CATALOG   sysname not null,
  23201.     TABLE_SCHEMA    sysname not null,
  23202.     TABLE_NAME  sysname not null,
  23203.     PRIVILEGE_TYPE  sysname not null,
  23204.     IS_GRANTABLE    bit not null
  23205.     )
  23206.     
  23207. IF @table_name is not null
  23208.     BEGIN
  23209.     insert into #sptprivsrowset1
  23210.     select
  23211.         GRANTOR     = user_name(p.grantor),
  23212.         GRANTEE     = user_name(u.uid),
  23213.         TABLE_CATALOG   = db_name(),
  23214.         TABLE_SCHEMA    = user_name(o.uid),
  23215.         TABLE_NAME  = o.name,
  23216.         PRIVILEGE_TYPE  = convert(varchar(30),
  23217.                     case p.action
  23218.                     when 193 then 'SELECT'
  23219.                     when 195 then 'INSERT'
  23220.                     when 196 then 'DELETE'
  23221.                     when 197 then 'UPDATE'
  23222.                     else 'REFERENCES'
  23223.                     end),
  23224.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23225.     from 
  23226.         sysprotects p, sysobjects o, sysusers u
  23227.     where
  23228.         o.name = @table_name
  23229.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23230.     and (@grantee is null or @grantee = user_name(u.uid))
  23231.     and     o.type in ('U','V','S')
  23232.     and p.id = o.id
  23233.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23234.             /* expand groups */
  23235.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23236.     and     p.protecttype <> 206    /* only grant rows */
  23237.     and     p.action in (26,193,195,196,197)
  23238.     and     o.uid <> u.uid          /* no rows for owner */
  23239.     and     not exists (            /* exclude revoke'd privileges */
  23240.             select  *
  23241.             from    sysprotects p1
  23242.             where   p1.protecttype = 206
  23243.             and     p1.action = p.action
  23244.             and     p1.id = p.id
  23245.             and     p1.uid = u.uid)
  23246.     union
  23247.     select  /*  Add rows for table owner */
  23248.         GRANTOR     = user_name(u.uid),
  23249.         GRANTEE     = user_name(o.uid),
  23250.         TABLE_CATALOG   = db_name(),
  23251.         TABLE_SCHEMA    = user_name(o.uid),
  23252.         TABLE_NAME  = o.name,
  23253.         PRIVILEGE_TYPE  = convert(varchar(30),
  23254.                     case v.number
  23255.                     when 193 then 'SELECT'
  23256.                     when 195 then 'INSERT'
  23257.                     when 196 then 'DELETE'
  23258.                     when 197 then 'UPDATE'
  23259.                     else 'REFERENCES'
  23260.                     end),
  23261.         IS_GRANTABLE    = convert(bit,1)    
  23262.     from 
  23263.         sysobjects o, master.dbo.spt_values v, sysusers u
  23264.     where
  23265.         o.name = @table_name
  23266.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23267.     and (@grantee is null or @grantee = user_name(o.uid))
  23268.     and     o.type in ('U','V','S')
  23269.     and     u.uid = 1       /* grantor is dbo of database */
  23270.     and (@grantor is null or @grantor = user_name(u.uid))
  23271.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23272.     and     v.number in (26,193,195,196,197)
  23273.     and     not exists (    /* exclude revoke'd privileges */
  23274.             select  *
  23275.             from    sysprotects p1
  23276.             where   p1.protecttype = 206
  23277.             and     p1.action = v.number
  23278.             and     p1.id = o.id
  23279.             and     p1.uid = o.uid)
  23280.     order by 4,5,6,1,2
  23281.     END
  23282. ELSE
  23283.     BEGIN
  23284.     insert into #sptprivsrowset1
  23285.     select
  23286.         GRANTOR     = user_name(p.grantor),
  23287.         GRANTEE     = user_name(u.uid),
  23288.         TABLE_CATALOG   = db_name(),
  23289.         TABLE_SCHEMA    = user_name(o.uid),
  23290.         TABLE_NAME  = o.name,
  23291.         PRIVILEGE_TYPE  = convert(varchar(30),
  23292.                     case p.action
  23293.                     when 193 then 'SELECT'
  23294.                     when 195 then 'INSERT'
  23295.                     when 196 then 'DELETE'
  23296.                     when 197 then 'UPDATE'
  23297.                     else 'REFERENCES'
  23298.                     end),
  23299.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23300.     from 
  23301.         sysprotects p, sysobjects o, sysusers u
  23302.     where
  23303.         o.type in ('U','V','S')
  23304.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23305.     and (@grantee is null or @grantee = user_name(u.uid))
  23306.     and p.id = o.id
  23307.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23308.             /* expand groups */
  23309.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23310.     and     p.protecttype <> 206    /* only grant rows */
  23311.     and     p.action in (26,193,195,196,197)
  23312.     and     o.uid <> u.uid          /* no rows for owner */
  23313.     and     not exists (            /* exclude revoke'd privileges */
  23314.             select  *
  23315.             from    sysprotects p1
  23316.             where   p1.protecttype = 206
  23317.             and     p1.action = p.action
  23318.             and     p1.id = p.id
  23319.             and     p1.uid = u.uid)
  23320.     union
  23321.     select  /*  Add rows for table owner */
  23322.         GRANTOR     = user_name(u.uid),
  23323.         GRANTEE     = user_name(o.uid),
  23324.         TABLE_CATALOG   = db_name(),
  23325.         TABLE_SCHEMA    = user_name(o.uid),
  23326.         TABLE_NAME  = o.name,
  23327.         PRIVILEGE_TYPE  = convert(varchar(30),
  23328.                     case v.number
  23329.                     when 193 then 'SELECT'
  23330.                     when 195 then 'INSERT'
  23331.                     when 196 then 'DELETE'
  23332.                     when 197 then 'UPDATE'
  23333.                     else 'REFERENCES'
  23334.                     end),
  23335.         IS_GRANTABLE    = convert(bit,1)    
  23336.     from 
  23337.         sysobjects o, master.dbo.spt_values v, sysusers u
  23338.     where
  23339.         o.type in ('U','V','S')
  23340.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23341.     and (@grantee is null or @grantee = user_name(o.uid))
  23342.     and     u.uid = 1       /* grantor is dbo of database */
  23343.     and (@grantor is null or @grantor = user_name(u.uid))
  23344.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23345.     and     v.number in (26,193,195,196,197)
  23346.     and     not exists (    /* exclude revoke'd privileges */
  23347.             select  *
  23348.             from    sysprotects p1
  23349.             where   p1.protecttype = 206
  23350.             and     p1.action = v.number
  23351.             and     p1.id = o.id
  23352.             and     p1.uid = o.uid)
  23353.     order by 4,5,6,1,2
  23354.     END
  23355.  
  23356. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  23357.     @scrollopt output, @ccopt output, @rows output
  23358.  
  23359. drop table #sptprivsrowset1
  23360. return isnull(@ret,0)
  23361. go
  23362. dump tran master with no_log
  23363. go
  23364. CREATE PROCEDURE sp_table_privileges_rowset;3
  23365. as
  23366.     select
  23367.         GRANTOR     = convert(sysname, ' '),
  23368.         GRANTEE     = convert(sysname, ' '),
  23369.         TABLE_CATALOG   = convert(sysname, ' '),
  23370.         TABLE_SCHEMA    = convert(sysname, ' '),
  23371.         TABLE_NAME  = convert(sysname, ' '),
  23372.         PRIVILEGE_TYPE  = convert(varchar(30), ' '),
  23373.         IS_GRANTABLE    = convert(bit, 0)
  23374.     where   1=0
  23375. go
  23376.  
  23377. if (charindex('7.00', @@version) = 0 and
  23378.     charindex('8.00', @@version) = 0)
  23379. begin
  23380.     print ''
  23381.     print ''
  23382.     print 'Warning:'
  23383.     print 'you are installing the stored procedures '
  23384.     print 'on a pre 8.0 SQL Server.'
  23385.     print 'Ignore the following errors.'
  23386. end
  23387. else
  23388.     drop proc sp_table_privileges_rowset
  23389. go
  23390.  
  23391.  
  23392. /*  Procedure for 8.0 server */
  23393. CREATE PROCEDURE sp_table_privileges_rowset
  23394.     (
  23395.     @table_name         sysname,
  23396.     @table_schema       sysname = null,
  23397.     @grantor        sysname = null,
  23398.     @grantee        sysname = null
  23399.     )
  23400. as
  23401.     select
  23402.         GRANTOR     = user_name(p.grantor),
  23403.         GRANTEE     = user_name(u.uid),
  23404.         TABLE_CATALOG   = db_name(),
  23405.         TABLE_SCHEMA    = user_name(o.uid),
  23406.         TABLE_NAME  = o.name,
  23407.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23408.                     case p.action
  23409.                     when 193 then N'SELECT'
  23410.                     when 195 then N'INSERT'
  23411.                     when 196 then N'DELETE'
  23412.                     when 197 then N'UPDATE'
  23413.                     else N'REFERENCES'
  23414.                     end),
  23415.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23416.     from 
  23417.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  23418.     where
  23419.         o.name = @table_name
  23420.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23421.     and (@grantee is null or @grantee = user_name(u.uid))
  23422.     and     o.type in ('U','V','S')
  23423.     and p.id = o.id
  23424.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23425.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  23426.     and     (u.uid > 0 and u.uid < 16384)
  23427.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  23428.     and     p.protecttype <> 206    /* only grant rows */
  23429.     and     p.action in (26,193,195,196,197)
  23430.     and     o.uid <> u.uid          /* no rows for owner */
  23431.     and     not exists (            /* exclude revoke'd privileges */
  23432.             select  *
  23433.             from    sysprotects p1
  23434.             where   p1.protecttype = 206
  23435.             and     p1.action = p.action
  23436.             and     p1.id = p.id
  23437.             and     p1.uid = u.uid)
  23438.     union
  23439.     select  /*  Add rows for table owner */
  23440.         GRANTOR     = user_name(u.uid),
  23441.         GRANTEE     = user_name(o.uid),
  23442.         TABLE_CATALOG   = db_name(),
  23443.         TABLE_SCHEMA    = user_name(o.uid),
  23444.         TABLE_NAME  = o.name,
  23445.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23446.                     case v.number
  23447.                     when 193 then N'SELECT'
  23448.                     when 195 then N'INSERT'
  23449.                     when 196 then N'DELETE'
  23450.                     when 197 then N'UPDATE'
  23451.                     else N'REFERENCES'
  23452.                     end),
  23453.         IS_GRANTABLE    = convert(bit,1)    
  23454.     from 
  23455.         sysobjects o, master.dbo.spt_values v, sysusers u
  23456.     where
  23457.         o.name = @table_name
  23458.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23459.     and (@grantee is null or @grantee = user_name(o.uid))
  23460.     and     o.type in ('U','V','S')
  23461.     and     u.uid = 1       /* grantor is dbo of database */
  23462.     and (@grantor is null or @grantor = user_name(u.uid))
  23463.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23464.     and     v.number in (26,193,195,196,197)
  23465.     and     not exists (    /* exclude revoke'd privileges */
  23466.             select  *
  23467.             from    sysprotects p1
  23468.             where   p1.protecttype = 206
  23469.             and     p1.action = v.number
  23470.             and     p1.id = o.id
  23471.             and     p1.uid = o.uid)
  23472.     order by 4,5,6,1,2
  23473. go
  23474. dump tran master with no_log
  23475. go
  23476. CREATE PROCEDURE sp_table_privileges_rowset;2
  23477.     (
  23478.     @table_schema       sysname = null,
  23479.     @grantor        sysname = null,
  23480.     @grantee        sysname = null
  23481.     )
  23482. as
  23483.     select
  23484.         GRANTOR     = user_name(p.grantor),
  23485.         GRANTEE     = user_name(u.uid),
  23486.         TABLE_CATALOG   = db_name(),
  23487.         TABLE_SCHEMA    = user_name(o.uid),
  23488.         TABLE_NAME  = o.name,
  23489.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23490.                     case p.action
  23491.                     when 193 then N'SELECT'
  23492.                     when 195 then N'INSERT'
  23493.                     when 196 then N'DELETE'
  23494.                     when 197 then N'UPDATE'
  23495.                     else N'REFERENCES'
  23496.                     end),
  23497.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23498.     from 
  23499.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  23500.     where
  23501.         o.type in ('U','V','S')
  23502.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23503.     and (@grantee is null or @grantee = user_name(u.uid))
  23504.     and p.id = o.id
  23505.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23506.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  23507.     and     (u.uid > 0 and u.uid < 16384)
  23508.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  23509.     and     p.protecttype <> 206    /* only grant rows */
  23510.     and     p.action in (26,193,195,196,197)
  23511.     and     o.uid <> u.uid          /* no rows for owner */
  23512.     and     not exists (            /* exclude revoke'd privileges */
  23513.             select  *
  23514.             from    sysprotects p1
  23515.             where   p1.protecttype = 206
  23516.             and     p1.action = p.action
  23517.             and     p1.id = p.id
  23518.             and     p1.uid = u.uid)
  23519.     union
  23520.     select  /*  Add rows for table owner */
  23521.         GRANTOR     = user_name(u.uid),
  23522.         GRANTEE     = user_name(o.uid),
  23523.         TABLE_CATALOG   = db_name(),
  23524.         TABLE_SCHEMA    = user_name(o.uid),
  23525.         TABLE_NAME  = o.name,
  23526.         PRIVILEGE_TYPE  = convert(nvarchar(30),
  23527.                     case v.number
  23528.                     when 193 then N'SELECT'
  23529.                     when 195 then N'INSERT'
  23530.                     when 196 then N'DELETE'
  23531.                     when 197 then N'UPDATE'
  23532.                     else N'REFERENCES'
  23533.                     end),
  23534.         IS_GRANTABLE    = convert(bit,1)    
  23535.     from 
  23536.         sysobjects o, master.dbo.spt_values v, sysusers u
  23537.     where
  23538.         o.type in ('U','V','S')
  23539.     and (@table_schema is null or @table_schema = user_name(o.uid))
  23540.     and (@grantee is null or @grantee = user_name(o.uid))
  23541.     and     u.uid = 1       /* grantor is dbo of database */
  23542.     and (@grantor is null or @grantor = user_name(u.uid))
  23543.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23544.     and     v.number in (26,193,195,196,197)
  23545.     and     not exists (    /* exclude revoke'd privileges */
  23546.             select  *
  23547.             from    sysprotects p1
  23548.             where   p1.protecttype = 206
  23549.             and     p1.action = v.number
  23550.             and     p1.id = o.id
  23551.             and     p1.uid = o.uid)
  23552.     order by 4,5,6,1,2
  23553. go
  23554. dump tran master with no_log
  23555. go
  23556. create procedure sp_table_privileges_rowset;5
  23557.     (
  23558.     @table_server       sysname,
  23559.     @table_catalog      sysname = null,
  23560.     @table_name     sysname = null,
  23561.     @table_schema       sysname = null,
  23562.     @grantor        sysname = null,
  23563.     @grantee        sysname = null
  23564.     )
  23565. as
  23566.     select
  23567.         GRANTOR,        
  23568.         GRANTEE,        
  23569.         TABLE_CATALOG,  
  23570.         TABLE_SCHEMA,   
  23571.         TABLE_NAME, 
  23572.         PRIVILEGE_TYPE, 
  23573.         IS_GRANTABLE    
  23574.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES (
  23575.                 @table_server,
  23576.                 @table_catalog,
  23577.                 @table_schema,
  23578.                 @table_name,
  23579.                 @grantor,
  23580.                 @grantee )
  23581.     order by 3,4,5,6,1,2
  23582. go
  23583.  
  23584. grant execute on sp_table_privileges_rowset to public
  23585. go
  23586. dump tran master with no_log
  23587. go
  23588. if (charindex('6.00', @@version) > 0)
  23589.     begin
  23590.     if (exists (select * from sysobjects
  23591.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  23592.         begin
  23593.         drop proc sp_table_privileges_rowset
  23594.         dump tran master with no_log
  23595.         end
  23596.     end
  23597. go
  23598.  
  23599.  
  23600. print ''
  23601. print 'creating sp_linkedservers_rowset'
  23602. go
  23603. if (charindex('7.00', @@version) = 0 and
  23604.     charindex('8.00', @@version) = 0)
  23605. begin
  23606.     print ''
  23607.     print ''
  23608.     print 'Warning:'
  23609.     print 'you are installing the stored procedures '
  23610.     print 'on a pre 8.0 SQL Server.'
  23611.     print 'Ignore the following errors.'
  23612. end
  23613. go
  23614. /*  Procedure for 8.0 server */
  23615. create proc sp_linkedservers_rowset
  23616.     (
  23617.     @srvname    sysname
  23618.     )
  23619. as
  23620.     select
  23621.         SVR_NAME             = srvname,
  23622.         SVR_PRODUCT            = srvproduct,
  23623.         SVR_PROVIDERNAME    = providername,
  23624.         SVR_DATASOURCE        = datasource,
  23625.         SVR_PROVIDERSTRING  = case when is_srvrolemember('sysadmin') = 1 then providerstring else NULL end,
  23626.         SVR_LOCATION        = location,
  23627.         SVR_CATALOG            = catalog
  23628.     from master.dbo.sysservers
  23629.     where srvname = @srvname and (srvstatus & 128) = 128
  23630.     order by 1
  23631. go
  23632. dump tran master with no_log
  23633. go
  23634. create proc sp_linkedservers_rowset;2
  23635. as
  23636.     select
  23637.         SVR_NAME             = srvname,
  23638.         SVR_PRODUCT            = srvproduct,
  23639.         SVR_PROVIDERNAME    = providername,
  23640.         SVR_DATASOURCE        = datasource,
  23641.         SVR_PROVIDERSTRING  = case when is_srvrolemember('sysadmin') = 1 then providerstring else NULL end,
  23642.         SVR_LOCATION        = location,
  23643.         SVR_CATALOG            = catalog
  23644.     from master.dbo.sysservers
  23645.     where (srvstatus & 128) = 128
  23646.     order by 1
  23647. go
  23648.  
  23649. grant execute on sp_linkedservers_rowset to public
  23650. go
  23651.  
  23652. dump tran master with no_log
  23653. go
  23654.  
  23655. print ''
  23656. print 'creating sp_table_statistics_rowset'
  23657. go
  23658. if (charindex('8.00', @@version) = 0)
  23659. begin
  23660.     print ''
  23661.     print ''
  23662.     print 'Warning:'
  23663.     print 'you are installing the stored procedures '
  23664.     print 'on a pre 8.00 SQL Server.'
  23665.     print 'Ignore the following errors.'
  23666. end
  23667. go
  23668. /*  Procedure for 8.00 server */
  23669. create proc sp_table_statistics_rowset
  23670. as
  23671.     select
  23672.         TABLE_CATALOG = convert(sysname, null),
  23673.         TABLE_SCHEMA = convert(sysname, null),
  23674.         TABLE_NAME = convert(sysname, null),
  23675.         STATISTICS_CATALOG = convert(sysname, null),
  23676.         STATISTICS_SCHEMA = convert(sysname, null),
  23677.         STATISTICS_NAME = convert(sysname, null),
  23678.         STATISTICS_TYPE = convert(smallint,0),
  23679.         COLUMN_NAME = convert(sysname, null),
  23680.         COLUMN_GUID = convert(uniqueidentifier, null),
  23681.         COLUMN_PROPID = convert(int, null),
  23682.         ORDINAL_POSITION = convert(int, null),
  23683.         SAMPLE_PCT = convert(smallint, null),
  23684.         LAST_UPDATE_TIME = convert(datetime, null),
  23685.         NO_OF_RANGES = convert(int, null),
  23686.         COLUMN_CARDINALITY = convert(bigint, null),
  23687.         TUPLE_CARDINALITY = convert(bigint, null),
  23688.         TABLE_CARDINALITY = convert(bigint, null),
  23689.         AVG_COLUMN_LENGTH = convert(int, null)
  23690.     where 1=0
  23691. go
  23692. dump tran master with no_log
  23693. go
  23694.  
  23695. create proc sp_table_statistics_rowset;2
  23696.                 (@table_catalog     sysname = null,
  23697.                 @table_schema       sysname = null,
  23698.                 @table_name     sysname = null,
  23699.                 @stat_catalog       sysname = null,
  23700.                 @stat_schema        sysname = null,
  23701.                 @stat_name      sysname = null)
  23702. as 
  23703. begin
  23704.     
  23705.     set nocount on
  23706.  
  23707.     -- check in parameters
  23708.     if ((@table_catalog is not null) and (db_name() <> @table_catalog)
  23709.         or (@stat_catalog is not null) and (db_name() <> @stat_catalog))
  23710.         begin   /* If qualifier doesn't match current database */
  23711.             raiserror 20001 '~~Rush_5~~'
  23712.             return
  23713.         end
  23714.  
  23715.     --- create temp table
  23716.     create table #spstattab(
  23717.         tblcatalog  sysname collate database_default not null,
  23718.         tblschema   sysname collate database_default not null,
  23719.         tblname     sysname collate database_default not null,
  23720.         statname    sysname collate database_default not null
  23721.         )
  23722.  
  23723.     insert into #spstattab
  23724.     select 
  23725.         db_name(),
  23726.         user_name(o.uid),
  23727.         o.name,
  23728.         x.name
  23729.     from sysobjects o, sysindexes x
  23730.     where o.type in ('U')
  23731.         and (@table_name is null or o.name = @table_name)
  23732.         and (@table_schema is null or @table_schema = user_name(o.uid))
  23733.         and (@stat_name is null or @stat_name = x.name)
  23734.         and (@stat_schema is null or @stat_schema = user_name(o.uid))
  23735.         and x.id = o.id
  23736.         and permissions(o.id) <> 0
  23737.         and x.statblob is not null
  23738.     order by 1,2,3,4
  23739.  
  23740.     -- fast forward CURSOR OVER THE temp TABLE
  23741.     declare ff_csr  cursor fast_forward for
  23742.     select tblcatalog, tblschema, tblname, statname from #spstattab
  23743.  
  23744.     declare @tblcatalog sysname,
  23745.         @tblschema  sysname,
  23746.         @tblname    sysname,
  23747.         @statname   sysname,
  23748.         @qtbl       nvarchar(4000),
  23749.         @rowsetcount    int
  23750.  
  23751.     set @rowsetcount = 0
  23752.  
  23753.     open ff_csr
  23754.     fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23755.     while @@fetch_status >= 0
  23756.     begin
  23757.         set @rowsetcount = @rowsetcount + 1
  23758.         set @qtbl = @tblcatalog + '.' + @tblschema + '.' + @tblname
  23759.         dbcc show_statistics(@qtbl, @statname) with stat_header join density_vector
  23760.         fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23761.     end
  23762.     close ff_csr
  23763.     deallocate ff_csr
  23764.     
  23765.     --- drop temp table
  23766.     drop table #spstattab
  23767.  
  23768.     if @rowsetcount = 0
  23769.     begin
  23770.         select  
  23771.             TABLE_CATALOG = convert(sysname, null),
  23772.             TABLE_SCHEMA = convert(sysname, null),
  23773.             TABLE_NAME = convert(sysname, null),
  23774.             STATISTICS_CATALOG = convert(sysname, null),
  23775.             STATISTICS_SCHEMA = convert(sysname, null),
  23776.             STATISTICS_NAME = convert(sysname, null),
  23777.             STATISTICS_TYPE = convert(smallint,0),
  23778.             COLUMN_NAME = convert(sysname, null),
  23779.             COLUMN_GUID = convert(uniqueidentifier, null),
  23780.             COLUMN_PROPID = convert(int, null),
  23781.             ORDINAL_POSITION = convert(int, null),
  23782.             SAMPLE_PCT = convert(smallint, null),
  23783.             LAST_UPDATE_TIME = convert(datetime, null),
  23784.             NO_OF_RANGES = convert(int, null),
  23785.             COLUMN_CARDINALITY = convert(bigint, null),
  23786.             TUPLE_CARDINALITY = convert(bigint, null),
  23787.             TABLE_CARDINALITY = convert(bigint, null),
  23788.             AVG_COLUMN_LENGTH = convert(int, null)
  23789.         where 1=0
  23790.     end
  23791.  
  23792.     set nocount off
  23793. end
  23794. go
  23795.  
  23796. grant execute on sp_table_statistics_rowset to public
  23797. go
  23798.  
  23799. dump tran master with no_log
  23800. go
  23801.  
  23802. print ''
  23803. print 'creating sp_oledb_column_constraints'
  23804. go
  23805.  
  23806. /* Procedure for 6.5 server */
  23807. create procedure sp_oledb_column_constraints
  23808.     (@tblname varchar(225)      -- the table to check for constraints
  23809.     ,@colname varchar(225)      -- the column to check for constraints
  23810.     ,@dropcnst smallint)
  23811. as
  23812. begin
  23813.     -- PRELIM
  23814.     set nocount on
  23815.  
  23816.     declare @tblid      int           -- the object id of the table
  23817.         ,@cnstname      varchar(225)   -- name of const. currently under consideration
  23818.         ,@cnstid        int
  23819.         ,@cnstatus      int
  23820.         ,@dbname        varchar(30)
  23821.  
  23822.     -- Create temp table
  23823.     create table #spcnsttab
  23824.     (cnst_name      varchar(225)        NOT NULL)
  23825.  
  23826.     -- Check to see that the object names are local to the current database.
  23827.     if @tblname like '%.%.%'
  23828.        and  substring(@tblname, 1, charindex('.', @tblname) - 1) <> db_name()
  23829.     begin
  23830.         raiserror(15250,-1,-1)
  23831.         return (1)
  23832.     end
  23833.     -- Check to see if the table exists and initialize @objid.
  23834.     select @tblid = object_id(@tblname)
  23835.     if @tblid is NULL
  23836.     begin
  23837.         select @dbname=db_name()
  23838.         raiserror(15009,-1,-1,@tblname,@dbname)
  23839.         return (1)
  23840.     end
  23841.  
  23842.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23843.     declare cnst_csr insensitive cursor for
  23844.         select c.constid, c.status, o.name 
  23845.         from sysconstraints c, sysobjects o 
  23846.         where c.id = @tblid and o.id = c.constid
  23847.             and ((c.status & 0xf)=1 and (@dropcnst & 16)<> 0
  23848.             or (c.status & 0xf)=2 and (@dropcnst & 32)<> 0 
  23849.             or (c.status & 0xf)=5 and (@dropcnst & 2)<> 0)  
  23850.         -- ONLY 6.5 sysconstraints objects
  23851.         for read only
  23852.  
  23853.     -- Now check out each constraint, figure out its type and keys and
  23854.     -- save the info in a temporary table that we'll print out at the end.
  23855.     open cnst_csr
  23856.     fetch cnst_csr into @cnstid, @cnstatus, @cnstname
  23857.     while @@fetch_status >= 0
  23858.     begin
  23859.         if ((@cnstatus & 0xf) in (1, 2)) ---- primary key, unique
  23860.             begin
  23861.                 -- get indid
  23862.                 declare @indid smallint
  23863.                 select  @indid = indid
  23864.                 from    sysindexes
  23865.                 where   name = object_name(@cnstid) and id = @tblid
  23866.  
  23867.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23868.                     and (index_col(@tblname, @indid, 2) is null)
  23869.                     begin
  23870.                         -- ADD TO TABLE
  23871.                         insert into #spcnsttab
  23872.                         values (@cnstname)
  23873.                     end
  23874.             end
  23875.         else
  23876.         if (@cnstatus & 0xf) = 5
  23877.             begin
  23878.                 if (select col_name(id, colid) from sysconstraints where colid > 0 and constid=@cnstid) = @colname
  23879.                     begin
  23880.                         insert into #spcnsttab
  23881.                         values (@cnstname)
  23882.                     end
  23883.             end
  23884.  
  23885.         fetch cnst_csr into @cnstid ,@cnstatus ,@cnstname
  23886.     end     --of major loop
  23887.     close cnst_csr
  23888.     deallocate cnst_csr
  23889.  
  23890.     -- Now print out the contents of the temporary index table.
  23891.     select 'constraint_name' = cnst_name
  23892.     from #spcnsttab 
  23893.     
  23894.     drop table #spcnsttab
  23895.  
  23896.     set nocount off
  23897.     return (0)
  23898. end 
  23899. go
  23900. dump tran master with no_log
  23901. go
  23902.  
  23903. if (charindex('7.00', @@version) = 0 and
  23904.     charindex('8.00', @@version) = 0)
  23905. begin
  23906.     print ''
  23907.     print ''
  23908.     print 'Warning:'
  23909.     print 'you are installing the stored procedures '
  23910.     print 'on a pre 7.0 SQL Server.'
  23911.     print 'Ignore the following errors.'
  23912. end
  23913. else
  23914.     drop proc sp_oledb_column_constraints
  23915. go
  23916.  
  23917. /* Procedure for 7.0 server */
  23918. create procedure sp_oledb_column_constraints
  23919.     (@tblname nvarchar(776)     -- the table to check for constraints
  23920.     ,@colname nvarchar(776)     -- the column to check for constraints
  23921.     ,@dropcnst smallint)
  23922. as
  23923. begin
  23924.     -- PRELIM
  23925.     set nocount on
  23926.  
  23927.     declare @tblid      int           -- the object id of the table
  23928.         ,@cnstname      sysname       -- name of const. currently under consideration
  23929.         ,@cnstid        int
  23930.         ,@cnsttype      character(2)
  23931.         ,@dbname        sysname
  23932.  
  23933.     -- Create temp table
  23934.     create table #spcnsttab
  23935.     (cnst_name      sysname     NOT NULL)
  23936.  
  23937.     -- Check to see that the object names are local to the current database.
  23938.     select @dbname = parsename(@tblname,3) 
  23939.     if @dbname is not null and @dbname <> db_name()
  23940.     begin
  23941.         raiserror(15250,-1,-1)
  23942.         return (1)
  23943.     end
  23944.  
  23945.     -- Check to see if the table exists and initialize @objid.
  23946.     select @tblid = object_id(@tblname)
  23947.     if @tblid is NULL
  23948.     begin
  23949.         select @dbname=db_name()
  23950.         raiserror(15009,-1,-1,@tblname,@dbname)
  23951.         return (1)
  23952.     end
  23953.  
  23954.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23955.     declare cnst_csr cursor fast_forward for
  23956.         select id, xtype, name 
  23957.         from sysobjects 
  23958.         where parent_obj = @tblid
  23959.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23960.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23961.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))  
  23962.         -- ONLY 6.5 sysconstraints objects
  23963.         for read only
  23964.  
  23965.     -- Now check out each constraint, figure out its type and keys and
  23966.     -- save the info in a temporary table that we'll print out at the end.
  23967.     open cnst_csr
  23968.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23969.     while @@fetch_status >= 0
  23970.     begin
  23971.         if @cnsttype in ('PK','UQ')
  23972.             begin
  23973.                 -- get indid
  23974.                 declare @indid smallint
  23975.                 select  @indid = indid
  23976.                 from    sysindexes
  23977.                 where   name = object_name(@cnstid) and id = @tblid
  23978.  
  23979.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23980.                     and (index_col(@tblname, @indid, 2) is null)
  23981.                     begin
  23982.                         -- ADD TO TABLE
  23983.                         insert into #spcnsttab
  23984.                         values (@cnstname)
  23985.                     end
  23986.             end
  23987.         else
  23988.         if (@cnsttype = 'D ')
  23989.             begin
  23990.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23991.                     begin
  23992.                         insert into #spcnsttab
  23993.                         values (@cnstname)
  23994.                     end
  23995.             end
  23996.  
  23997.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23998.     end     --of major loop
  23999.     close cnst_csr
  24000.     deallocate cnst_csr
  24001.  
  24002.     -- Now print out the contents of the temporary index table.
  24003.     select 'constraint_name' = cnst_name
  24004.     from #spcnsttab 
  24005.  
  24006.     drop table #spcnsttab
  24007.     
  24008.     set nocount off
  24009.     return (0)
  24010. end
  24011. go
  24012.  
  24013. if (charindex('8.00', @@version) = 0)
  24014. begin
  24015.     print ''
  24016.     print ''
  24017.     print 'Warning:'
  24018.     print 'you are installing the stored procedures '
  24019.     print 'on a pre 8.0 SQL Server.'
  24020.     print 'Ignore the following errors.'
  24021. end
  24022. else
  24023.     drop proc sp_oledb_column_constraints
  24024. go
  24025.  
  24026. /* Procedure for 8.0 server */
  24027. create procedure sp_oledb_column_constraints
  24028.     (@tblname nvarchar(776)     -- the table to check for constraints
  24029.     ,@colname nvarchar(776)     -- the column to check for constraints
  24030.     ,@dropcnst smallint)
  24031. as
  24032. begin
  24033.     -- PRELIM
  24034.     set nocount on
  24035.  
  24036.     declare @tblid      int           -- the object id of the table
  24037.         ,@cnstname      sysname       -- name of const. currently under consideration
  24038.         ,@cnstid        int
  24039.         ,@cnsttype      character(2)
  24040.         ,@dbname        sysname
  24041.  
  24042.     -- Create temp table
  24043.     create table #spcnsttab
  24044.     (cnst_name      sysname     collate database_default NOT NULL)
  24045.  
  24046.     -- Check to see that the object names are local to the current database.
  24047.     select @dbname = parsename(@tblname,3) 
  24048.     if @dbname is not null and @dbname <> db_name()
  24049.     begin
  24050.         raiserror(15250,-1,-1)
  24051.         return (1)
  24052.     end
  24053.  
  24054.     -- Check to see if the table exists and initialize @objid.
  24055.     select @tblid = object_id(@tblname)
  24056.     if @tblid is NULL
  24057.     begin
  24058.         select @dbname=db_name()
  24059.         raiserror(15009,-1,-1,@tblname,@dbname)
  24060.         return (1)
  24061.     end
  24062.  
  24063.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  24064.     declare cnst_csr cursor fast_forward for
  24065.         select id, xtype, name 
  24066.         from sysobjects 
  24067.         where parent_obj = @tblid
  24068.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  24069.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  24070.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))  
  24071.         -- ONLY 6.5 sysconstraints objects
  24072.         for read only
  24073.  
  24074.     -- Now check out each constraint, figure out its type and keys and
  24075.     -- save the info in a temporary table that we'll print out at the end.
  24076.     open cnst_csr
  24077.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  24078.     while @@fetch_status >= 0
  24079.     begin
  24080.         if @cnsttype in ('PK','UQ')
  24081.             begin
  24082.                 -- get indid
  24083.                 declare @indid smallint
  24084.                 select  @indid = indid
  24085.                 from    sysindexes
  24086.                 where   name = object_name(@cnstid) and id = @tblid
  24087.  
  24088.                 if (@colname = index_col(@tblname, @indid, 1)) 
  24089.                     and (index_col(@tblname, @indid, 2) is null)
  24090.                     begin
  24091.                         -- ADD TO TABLE
  24092.                         insert into #spcnsttab
  24093.                         values (@cnstname)
  24094.                     end
  24095.             end
  24096.         else
  24097.         if (@cnsttype = 'D ')
  24098.             begin
  24099.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  24100.                     begin
  24101.                         insert into #spcnsttab
  24102.                         values (@cnstname)
  24103.                     end
  24104.             end
  24105.  
  24106.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  24107.     end     --of major loop
  24108.     close cnst_csr
  24109.     deallocate cnst_csr
  24110.  
  24111.     -- Now print out the contents of the temporary index table.
  24112.     select 'constraint_name' = cnst_name
  24113.     from #spcnsttab 
  24114.  
  24115.     drop table #spcnsttab
  24116.     
  24117.     set nocount off
  24118.     return (0)
  24119. end
  24120. go
  24121.  
  24122. grant execute on sp_oledb_column_constraints to public
  24123. go
  24124.  
  24125. dump tran master with no_log
  24126. go
  24127.  
  24128. print ''
  24129. print 'creating sp_oledb_indexinfo'
  24130. go
  24131.  
  24132. /* Procedure for 6.5 server */
  24133. create procedure sp_oledb_indexinfo
  24134.         @objname varchar(225)       -- the table to check for indexes
  24135.         ,@indname varchar(225)
  24136. as
  24137. begin
  24138.     -- PRELIM
  24139.     set nocount on
  24140.  
  24141.     declare @objid int,         -- the object id of the table
  24142.         @indid smallint,    -- the index id of an index
  24143.         @status int,
  24144.         @dbname varchar(30),
  24145.         @OrigFillFactor tinyint,
  24146.         @i int, 
  24147.         @thiskey varchar(32),
  24148.         @tptr     varbinary(16) -- pointer for building text strings.
  24149.     ---- Check to see that the object names are local to the current database.
  24150.     if @objname like '%.%.%'
  24151.        and  substring(@objname, 1, charindex('.', @objname) - 1) <> db_name()
  24152.     begin
  24153.         raiserror(15250,-1,-1)
  24154.         return (1)
  24155.     end
  24156.     ---- Check to see if the table exists and initialize @objid.
  24157.     select @objid = object_id(@objname)
  24158.     ---- Table does not exist so return.
  24159.     if @objid is NULL
  24160.     begin
  24161.         select @dbname=db_name()
  24162.         raiserror(15009,-1,-1,@objname,@dbname)
  24163.         return (1)
  24164.     end
  24165.  
  24166.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  24167.     from sysindexes
  24168.     where id = @objid and name = @indname 
  24169.  
  24170.     -- IF NO INDEX, QUIT
  24171.     if @indid is NULL
  24172.     begin
  24173.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  24174.         return (1)
  24175.     end
  24176.     -- create temp table
  24177.     create table #spindtab
  24178.     (
  24179.         status              int,
  24180.         OrigFillFactor          tinyint,
  24181.         index_keys          text    NOT NULL,
  24182.     )
  24183.     -- Now check out each index, figure out its type and keys and
  24184.     -- First we'll figure out what the keys are.
  24185.     select @i = 1
  24186.     while (@i <= 16)
  24187.         begin
  24188.             select @thiskey = index_col(@objname, @indid, @i)
  24189.             if @thiskey is NULL
  24190.                     goto keysdone
  24191.             if @i=1
  24192.                 begin
  24193.                     insert into #spindtab
  24194.                     values (@status, @OrigFillFactor, @thiskey)
  24195.  
  24196.             select @tptr = textptr(index_keys) from #spindtab
  24197.                 end
  24198.             else
  24199.                 begin
  24200.                     select @thiskey = ', ' + @thiskey
  24201.  
  24202.             if @tptr is not null
  24203.                     updatetext #spindtab.index_keys @tptr null null @thiskey
  24204.                 end
  24205.  
  24206.         select @i = @i + 1
  24207.          end --loop 16
  24208.          ---- When we get here we now have all the keys.
  24209.  
  24210. keysdone:
  24211.     select * from #spindtab
  24212.     
  24213.     drop table #spindtab
  24214.     set nocount off
  24215.     return (0)
  24216. end
  24217. go
  24218. dump tran master with no_log
  24219. go
  24220.  
  24221. if (charindex('7.00', @@version) = 0 and
  24222.     charindex('8.00', @@version) = 0)
  24223. begin
  24224.     print ''
  24225.     print ''
  24226.     print 'Warning:'
  24227.     print 'you are installing the stored procedures '
  24228.     print 'on a pre 8.0 SQL Server.'
  24229.     print 'Ignore the following errors.'
  24230. end
  24231. else
  24232.     drop proc sp_oledb_indexinfo
  24233. go
  24234.  
  24235. /* Procedure for 8.0 server */
  24236. create proc sp_oledb_indexinfo
  24237.         @objname nvarchar(776)      -- the table to check for indexes
  24238.         ,@indname nvarchar(776)
  24239. as
  24240. begin
  24241.     -- PRELIM
  24242.     set nocount on
  24243.  
  24244.     declare @objid int,         -- the object id of the table
  24245.         @indid smallint,    -- the index id of an index
  24246.         @status int,
  24247.         @keys nvarchar(2078),-- string build index key list, length = (16*max_id_length)+(15*2)
  24248.         @dbname sysname,
  24249.         @OrigFillFactor tinyint
  24250.  
  24251.     -- Check to see that the object names are local to the current database.
  24252.     select @dbname = parsename(@objname,3) 
  24253.  
  24254.     if @dbname is not null and @dbname <> db_name()
  24255.     begin
  24256.             raiserror(15250,-1,-1)
  24257.             return (1)
  24258.     end
  24259.  
  24260.     -- Check to see the the table exists and initialize @objid.
  24261.     select @objid = object_id(@objname)
  24262.     if @objid is NULL
  24263.     begin
  24264.         select @dbname=db_name()
  24265.         raiserror(15009,-1,-1,@objname,@dbname)
  24266.         return (1)
  24267.     end
  24268.  
  24269.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  24270.     from sysindexes
  24271.     where id = @objid and name = @indname 
  24272.  
  24273.     -- IF NO INDEX, QUIT
  24274.     if @indid is NULL
  24275.     begin
  24276.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  24277.         return (1)
  24278.     end
  24279.  
  24280.     -- First we'll figure out what the keys are.
  24281.     declare @i int, @thiskey sysname
  24282.     select @keys = index_col(@objname, @indid, 1),
  24283.         @i = 2, @thiskey = index_col(@objname, @indid, 2)
  24284.     while (@thiskey is not null )
  24285.     begin
  24286.         select @keys = @keys + ', ' + @thiskey, @i = @i + 1
  24287.         select @thiskey = index_col(@objname, @indid, @i)
  24288.     end
  24289.     -- DISPLAY THE RESULTS
  24290.     select 'Status'=@status, 'OrigFillFactor' =@OrigFillFactor, 'Index keys'=@keys
  24291.     set nocount off
  24292.     return (0) 
  24293. end
  24294. go
  24295.  
  24296. grant execute on sp_oledb_indexinfo to public
  24297. go
  24298.  
  24299. dump tran master with no_log
  24300. go
  24301.  
  24302. print ''
  24303. print 'creating sp_oledb_ro_usrname'
  24304. go
  24305.  
  24306. create procedure sp_oledb_ro_usrname
  24307. as
  24308. begin
  24309.     select substring('NY',status/1024&1+1,1),user_name() 
  24310.     from master..sysdatabases 
  24311.     where name=DB_NAME()
  24312. end
  24313. go
  24314.  
  24315. grant execute on sp_oledb_ro_usrname to public
  24316. go
  24317.  
  24318. dump tran master with no_log
  24319. go
  24320.  
  24321. if (charindex('6.00', @@version) > 0 or
  24322.     charindex('6.50', @@version) > 0 or
  24323.     charindex('7.00', @@version) > 0)
  24324.     begin
  24325.     if (exists (select * from sysobjects
  24326.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  24327.         begin
  24328.             drop proc sp_oledb_ro_usrname
  24329.             dump tran master with no_log
  24330.         end
  24331.     end
  24332. go
  24333.  
  24334. print ''
  24335. print 'creating sp_oledb_deflang'
  24336. go
  24337.  
  24338. if (charindex('8.00', @@version) = 0)
  24339. begin
  24340.     print ''
  24341.     print ''
  24342.     print 'Warning:'
  24343.     print 'you are installing the stored procedures '
  24344.     print 'on a pre 8.0 SQL Server.'
  24345.     print 'Ignore the following errors.'
  24346. end
  24347. go
  24348.  
  24349. create procedure sp_oledb_deflang
  24350. as
  24351.     begin
  24352.         select ISNULL(language,'us_english') 
  24353.         from master..syslogins 
  24354.         where sid=SUSER_SID()
  24355.     end
  24356. go
  24357.  
  24358. grant execute on sp_oledb_deflang to public
  24359. go
  24360.  
  24361. dump tran master with no_log
  24362. go
  24363.  
  24364. if (charindex('6.00', @@version) > 0 or
  24365.     charindex('6.50', @@version) > 0 or
  24366.     charindex('7.00', @@version) > 0)
  24367.     begin
  24368.     if (exists (select * from sysobjects
  24369.         where name = 'sp_oledb_deflang' and type = 'P '))
  24370.         begin
  24371.             drop proc sp_oledb_deflang
  24372.             dump tran master with no_log
  24373.         end
  24374.     end
  24375. go
  24376.  
  24377. print ''
  24378. print 'creating sp_oledb_defdb'
  24379. go
  24380.  
  24381. if (charindex('8.00', @@version) = 0)
  24382. begin
  24383.     print ''
  24384.     print ''
  24385.     print 'Warning:'
  24386.     print 'you are installing the stored procedures '
  24387.     print 'on a pre 8.0 SQL Server.'
  24388.     print 'Ignore the following errors.'
  24389. end
  24390. go
  24391.  
  24392. create procedure sp_oledb_defdb
  24393. as
  24394.     begin
  24395.         select dbname from master..syslogins where sid=SUSER_SID()
  24396.     end
  24397. go
  24398.  
  24399. grant execute on sp_oledb_defdb to public
  24400. go
  24401.  
  24402. dump tran master with no_log
  24403. go
  24404.  
  24405. if (charindex('6.00', @@version) > 0 or
  24406.     charindex('6.50', @@version) > 0 or
  24407.     charindex('7.00', @@version) > 0)
  24408.     begin
  24409.     if (exists (select * from sysobjects
  24410.         where name = 'sp_oledb_defdb' and type = 'P '))
  24411.         begin
  24412.             drop proc sp_oledb_defdb
  24413.             dump tran master with no_log
  24414.         end
  24415.     end
  24416. go
  24417.  
  24418. print ''
  24419. print 'creating sp_oledb_database'
  24420. go
  24421.  
  24422. create procedure sp_oledb_database
  24423. as
  24424.     begin
  24425.         select name from master..sysdatabases
  24426.     end
  24427. go
  24428.  
  24429. grant execute on sp_oledb_database to public
  24430. go
  24431.  
  24432. dump tran master with no_log
  24433. go
  24434.  
  24435. if (charindex('6.00', @@version) > 0 or
  24436.     charindex('6.50', @@version) > 0 or
  24437.     charindex('7.00', @@version) > 0)
  24438.     begin
  24439.     if (exists (select * from sysobjects
  24440.         where name = 'sp_oledb_database' and type = 'P '))
  24441.         begin
  24442.             drop proc sp_oledb_database
  24443.             dump tran master with no_log
  24444.         end
  24445.     end
  24446. go
  24447.  
  24448. print ''
  24449. print 'creating sp_oledb_language'
  24450. go
  24451.  
  24452. create procedure sp_oledb_language
  24453. as
  24454. begin
  24455.     select 'English','us_english' 
  24456.     union 
  24457.     select alias,name from master..syslanguages
  24458. end
  24459. go
  24460.  
  24461. grant execute on sp_oledb_language to public
  24462. go
  24463.  
  24464. dump tran master with no_log
  24465. go
  24466. if (charindex('6.00', @@version) > 0 or
  24467.     charindex('6.50', @@version) > 0 or
  24468.     charindex('7.00', @@version) > 0)
  24469.     begin
  24470.     if (exists (select * from sysobjects
  24471.         where name = 'sp_oledb_language' and type = 'P '))
  24472.         begin
  24473.             drop proc sp_oledb_language
  24474.             dump tran master with no_log
  24475.         end
  24476.     end
  24477. go
  24478.  
  24479. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  24480.  
  24481.  
  24482. /*---------------------------- BEGIN BCP CATALOG PROCS ------------------------*/
  24483.  
  24484. print 'creating sp_tablecollations'
  24485. go
  24486.  
  24487. /* Used by BCP to gather all the collation names for a table */
  24488. if (charindex('8.00', @@version) = 0)
  24489. begin
  24490.     print ''
  24491.     print ''
  24492.     print 'Warning:'
  24493.     print 'you are installing the stored procedures '
  24494.     print 'on a pre 8.0 SQL Server.'
  24495.     print 'Ignore the following errors.'
  24496. end
  24497. go
  24498.  
  24499. /* Procedure for 8.0 server */
  24500. create procedure sp_tablecollations (@object nvarchar(4000))
  24501. as
  24502. begin
  24503.     select c.colid,
  24504.            c.name, 
  24505.            tds_collation = c.tdscollation,
  24506.            collation_name = BCPCollationName(c.tdscollation, c.xtype)
  24507.     from syscolumns c inner join sysobjects t on c.id = t.id
  24508.     where t.id = object_id(@object, 'local')
  24509.     order by c.colid
  24510. end
  24511. go
  24512.  
  24513. grant execute on sp_tablecollations to public
  24514. go
  24515.  
  24516. print 'creating sp_bcp_dbcmptlevel'
  24517. go
  24518.  
  24519. /* Used by BCP to get current db compatibility level */
  24520. if (charindex('8.00', @@version) = 0)
  24521. begin
  24522.     print ''
  24523.     print ''
  24524.     print 'Warning:'
  24525.     print 'you are installing the stored procedures '
  24526.     print 'on a pre 8.0 SQL Server.'
  24527.     print 'Ignore the following errors.'
  24528. end
  24529. go
  24530.  
  24531. /* Procedure for 8.0 server */
  24532. create procedure sp_bcp_dbcmptlevel (@dbname sysname)
  24533. as
  24534. begin
  24535.     select cmptlevel
  24536.     from master.dbo.sysdatabases
  24537.     where name = @dbname
  24538. end
  24539. go
  24540.  
  24541. grant execute on sp_bcp_dbcmptlevel to public
  24542. go
  24543.  
  24544. /*---------------------------- END BCP CATALOG PROCS ------------------------*/
  24545.  
  24546. dump tran master with no_log
  24547. go
  24548.  
  24549.  
  24550.  
  24551.  
  24552. if (exists (select * from sysobjects
  24553.         where name = 'sp_configure' and type = 'P '))
  24554.     begin
  24555.         exec sp_configure 'allow updates',0
  24556.         reconfigure with override
  24557.     end
  24558. go
  24559.  
  24560. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  24561.  
  24562. go
  24563.  
  24564. if exists (select * from sysobjects where name = 'sp_check_objects'
  24565.             and type = 'P ')
  24566.     begin
  24567.         /* Only supported on 6.0 servers */
  24568.         print ''
  24569.         print 'Checking objects created by instcat.sql.'
  24570.  
  24571.         exec sp_check_objects 'catalog'
  24572.     end
  24573. go
  24574.  
  24575. print ''
  24576. print 'instcat.sql completed successfully.'
  24577. go
  24578.  
  24579. set quoted_identifier off
  24580. go
  24581.  
  24582. dump tran master with no_log
  24583. go
  24584. checkpoint
  24585. go
  24586. /**/
  24587.