home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 July / PCWorld_2001-07_cd.bin / Software / Topware / w2ksp2en / w2ksp2.exe / i386 / instcat.sq_ / instcat.sql
Encoding:
Text File  |  2001-05-04  |  604.4 KB  |  20,681 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. **    Copyright 1992-1999, Microsoft Corp.    All rights reserved.
  6. */
  7.  
  8. /*
  9. NOTE:  you MUST change the last row inserted into spt_server_info
  10. to be version number of this file.    the convention is j.nn.bbb, where
  11. j is the major version number ('7' now), nn is the minor version number
  12. ('00' now), and bbb is the build number.
  13. insert into spt_server_info
  14.     values (500, 'SYS_SPROC_VERSION', '7.00.bbb')
  15. */
  16.  
  17. /****************************************************************************/
  18. /* This portion sets up the ability to perform all the functions in this    */
  19. /* script                                                                    */
  20. /****************************************************************************/
  21. go
  22. use master
  23. go
  24. dump tran master with no_log
  25. go
  26.  
  27. set quoted_identifier on
  28. go
  29.  
  30. if (exists (select * from sysobjects
  31.         where name = 'sp_configure' and type = 'P '))
  32. begin
  33.     execute sp_configure 'update',1
  34. end
  35. reconfigure with override
  36. go
  37.  
  38. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  39.  
  40. go
  41.  
  42. /*
  43. ** If old versions of tables exist, drop them.
  44. */
  45. if (exists (select * from sysobjects
  46.         where name = 'MSdatatype_info' and type = 'U '))
  47.     drop table MSdatatype_info
  48. go
  49. if (exists (select * from sysobjects
  50.         where name = 'MSdatatype_info_ext' and type = 'U '))
  51.     drop table MSdatatype_info_ext
  52. go
  53. if (exists (select * from sysobjects
  54.         where name = 'MStable_types' and type = 'U '))
  55.     drop table MStable_types
  56. go
  57. if (exists (select * from sysobjects
  58.         where name = 'MSserver_info' and type = 'U '))
  59.     drop table MSserver_info
  60. go
  61. if (exists (select * from sysobjects
  62.         where name = 'spt_table_types' and type = 'U '))
  63.     drop table spt_table_types
  64. go
  65. /*
  66. ** If tables or procs already exist, drop them.
  67. */
  68.  
  69. if (exists (select * from sysobjects
  70.         where name = 'spt_datatype_info' and type = 'U '))
  71.     drop table spt_datatype_info
  72. go
  73. if (exists (select * from sysobjects
  74.         where name = 'spt_datatype_info_ext' and type = 'U '))
  75.     drop table spt_datatype_info_ext
  76. go
  77. if (exists (select * from sysobjects
  78.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  79.     drop proc sp_add_server_sortinfo
  80. go
  81. if (exists (select * from sysobjects
  82.         where name = 'spt_server_info' and type = 'U '))
  83.     drop table spt_server_info
  84. go
  85. if (exists (select * from sysobjects
  86.         where name = 'sp_tables' and type = 'P '))
  87.     drop proc sp_tables
  88. go
  89. if (exists (select * from sysobjects
  90.         where name = 'sp_statistics' and type = 'P '))
  91.     drop proc sp_statistics
  92. go
  93. if (exists (select * from sysobjects
  94.         where name = 'sp_columns' and type = 'P '))
  95.     drop proc sp_columns
  96. go
  97. if (exists (select * from sysobjects
  98.         where name = 'sp_fkeys' and type = 'P '))
  99.     drop proc sp_fkeys
  100. go
  101. if (exists (select * from sysobjects
  102.         where name = 'sp_pkeys' and type = 'P '))
  103.     drop proc sp_pkeys
  104. dump tran master with no_log
  105. go
  106.  
  107. go
  108. if (exists (select * from sysobjects
  109.         where name = 'sp_stored_procedures' and type = 'P '))
  110.     drop proc sp_stored_procedures
  111. go
  112. if (exists (select * from sysobjects
  113.         where name = 'sp_sproc_columns' and type = 'P '))
  114.     drop proc sp_sproc_columns
  115. go
  116. if (exists (select * from sysobjects
  117.         where name = 'sp_table_privileges' and type = 'P '))
  118.     drop proc sp_table_privileges
  119. go
  120. if (exists (select * from sysobjects
  121.         where name = 'sp_column_privileges' and type = 'P '))
  122.     drop proc sp_column_privileges
  123. go
  124. dump tran master with no_log
  125. go
  126. if (exists (select * from sysobjects
  127.         where name = 'sp_server_info' and type = 'P '))
  128.     drop proc sp_server_info
  129. go
  130. if (exists (select * from sysobjects
  131.         where name = 'sp_datatype_info' and type = 'P '))
  132.     drop proc sp_datatype_info
  133. go
  134. if (exists (select * from sysobjects
  135.         where name = 'sp_special_columns' and type = 'P '))
  136.     drop proc sp_special_columns
  137. go
  138. if (exists (select * from sysobjects
  139.         where name = 'sp_databases' and type = 'P '))
  140.     drop proc sp_databases
  141. go
  142. dump tran master with no_log
  143. go
  144. if (exists (select * from sysobjects
  145.         where name = 'sp_ddopen' and type = 'P '))
  146.     drop proc sp_ddopen
  147. go
  148. if (exists (select * from sysobjects
  149.         where name = 'sp_tableswc' and type = 'P '))
  150.     drop proc sp_tableswc
  151. go
  152.  
  153. dump tran master with no_log
  154. go
  155.  
  156.  
  157. if (exists (select * from sysobjects
  158.         where name = 'spt_provider_types' and type = 'U '))
  159.     begin
  160.     drop table spt_provider_types
  161.     dump tran master with no_log
  162.     end
  163. go
  164. if (exists (select * from sysobjects
  165.         where name = 'sp_catalogs_rowset' and type = 'P '))
  166.     begin
  167.     drop procedure sp_catalogs_rowset
  168.     dump tran master with no_log
  169.     end
  170. go
  171. if (exists (select * from sysobjects
  172.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  173.     begin
  174.     drop procedure sp_column_privileges_rowset
  175.     dump tran master with no_log
  176.     end
  177. go
  178. if (exists (select * from sysobjects
  179.         where name = 'sp_columns_rowset' and type = 'P '))
  180.     begin
  181.     drop procedure sp_columns_rowset
  182.     dump tran master with no_log
  183.     end
  184. go
  185. if (exists (select * from sysobjects
  186.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  187.     begin
  188.     drop procedure sp_foreign_keys_rowset
  189.     dump tran master with no_log
  190.     end
  191. go
  192. if (exists (select * from sysobjects
  193.         where name = 'sp_indexes_rowset' and type = 'P '))
  194.     begin
  195.     drop procedure sp_indexes_rowset
  196.     dump tran master with no_log
  197.     end
  198. go
  199. if (exists (select * from sysobjects
  200.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  201.     begin
  202.     drop procedure sp_primary_keys_rowset
  203.     dump tran master with no_log
  204.     end
  205. go
  206. if (exists (select * from sysobjects
  207.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  208.     begin
  209.     drop procedure sp_procedure_params_rowset
  210.     dump tran master with no_log
  211.     end
  212. go
  213. if (exists (select * from sysobjects
  214.         where name = 'sp_procedures_rowset' and type = 'P '))
  215.     begin
  216.     drop procedure sp_procedures_rowset
  217.     dump tran master with no_log
  218.     end
  219. go
  220. if (exists (select * from sysobjects
  221.         where name = 'sp_provider_types_rowset' and type = 'P '))
  222.     begin
  223.     drop procedure sp_provider_types_rowset
  224.     dump tran master with no_log
  225.     end
  226. go
  227. if (exists (select * from sysobjects
  228.         where name = 'sp_schemata_rowset' and type = 'P '))
  229.     begin
  230.     drop procedure sp_schemata_rowset
  231.     dump tran master with no_log
  232.     end
  233. go
  234. if (exists (select * from sysobjects
  235.         where name = 'sp_statistics_rowset' and type = 'P '))
  236.     begin
  237.     drop procedure sp_statistics_rowset
  238.     dump tran master with no_log
  239.     end
  240. go
  241. if (exists (select * from sysobjects
  242.         where name = 'sp_tables_rowset' and type = 'P '))
  243.     begin
  244.     drop procedure sp_tables_rowset
  245.     dump tran master with no_log
  246.     end
  247. go
  248. if (exists (select * from sysobjects
  249.         where name = 'sp_tables_info_rowset' and type = 'P '))
  250.     begin
  251.     drop procedure sp_tables_info_rowset
  252.     dump tran master with no_log
  253.     end
  254. go
  255. if (exists (select * from sysobjects
  256.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  257.     begin
  258.     drop proc sp_table_constraints_rowset
  259.     dump tran master with no_log
  260.     end
  261. go
  262. if (exists (select * from sysobjects
  263.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  264.     begin
  265.     drop proc sp_table_privileges_rowset
  266.     dump tran master with no_log
  267.     end
  268. go
  269. if (exists (select * from sysobjects
  270.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  271.     begin
  272.     drop proc sp_linkedservers_rowset
  273.     dump tran master with no_log
  274.     end
  275. go
  276.  
  277.  
  278. print 'creating table spt_datatype_info_ext'
  279. go
  280.  
  281. if (charindex('6.00', @@version) = 0 and
  282.     charindex('6.50', @@version) = 0 and
  283.     charindex('7.00', @@version) = 0)
  284. begin    /*    Pre 6.0 server */
  285.     print ''
  286.     print ''
  287.     print 'Warning:'
  288.     print 'you are installing the stored procedures '
  289.     print 'on a pre 6.0 SQL Server.'
  290.     print 'Ignore the following error.'
  291.     create table spt_datatype_info_ext (
  292.                 user_type        smallint    not null,
  293.                 CREATE_PARAMS    varchar(32) null,
  294.                 AUTO_INCREMENT    smallint null,
  295.                 typename        varchar(32))
  296. end
  297. go
  298. if (charindex('6.00', @@version) > 0 or
  299.     charindex('6.50', @@version) > 0 or
  300.     charindex('7.00', @@version) > 0)
  301. begin    /* 6.0 or later server */
  302.     create table spt_datatype_info_ext (
  303.                 user_type        smallint    not null,
  304.                 CREATE_PARAMS    varchar(32) null,
  305.                 AUTO_INCREMENT    smallint null,
  306.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  307. end
  308. go
  309.  
  310. grant select on spt_datatype_info_ext to public
  311. go
  312.  
  313.  
  314. insert into spt_datatype_info_ext
  315.     /* CHAR      user_type, create_params, auto_increment */
  316.     values             (1,    'length' ,0, 'char')
  317.  
  318. insert into spt_datatype_info_ext
  319.     /* VARCHAR     user_type, create_params, auto_increment */
  320.     values             (2,    'max length' ,0, 'varchar')
  321.  
  322. insert into spt_datatype_info_ext
  323.     /* BINARY     user_type, create_params, auto_increment */
  324.     values             (3,    'length' ,0, 'binary')
  325.  
  326. insert into spt_datatype_info_ext
  327.     /* VARBINARY user_type, create_params, auto_increment */
  328.     values             (4,    'max length' ,0, 'varbinary')
  329.  
  330. if    (charindex('6.00', @@version) > 0 or
  331.      charindex('6.50', @@version) > 0 or
  332.      charindex('7.00', @@version) > 0)
  333. begin    /*    Add 6.0 data types */
  334.     insert into spt_datatype_info_ext
  335.         /* DECIMAL user_type, create_params, auto_increment */
  336.         values             (26,    'precision,scale' ,0, 'decimal')
  337.  
  338.     insert into spt_datatype_info_ext
  339.         /* NUMERIC user_type, create_params, auto_increment */
  340.         values             (25,    'precision,scale' ,0, 'numeric')
  341.  
  342.     insert into spt_datatype_info_ext
  343.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  344.         values             (26,    'precision' ,1, 'decimal')
  345.  
  346.     insert into spt_datatype_info_ext
  347.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  348.         values             (25,    'precision' ,1, 'numeric')
  349.  
  350. end
  351. else    /*    Pre 6.0 server, add SYSNAME create param */
  352.     begin
  353.         insert into spt_datatype_info_ext
  354.             /* SYSNAME     user_type, create_param, auto_increments */
  355.             values             (18,    'max length' ,0, 'sysname')
  356.  
  357.     end
  358. go
  359.  
  360. if (charindex('7.00', @@version) = 0)
  361. begin
  362.     print ''
  363.     print ''
  364.     print 'Warning:'
  365.     print 'you are installing the stored procedures '
  366.     print 'on a pre 7.0 SQL Server.'
  367.     print 'Ignore the following errors.'
  368. end
  369. go
  370. if (charindex('7.00', @@version) > 0)
  371. begin    /*    Update usertypes for 7.00 server */
  372.     begin tran
  373.     insert into spt_datatype_info_ext
  374.         /* NCHAR     user_type, create_params, auto_increment */
  375.         values             (0,    'length' ,0, 'nchar')
  376.  
  377.     insert into spt_datatype_info_ext
  378.         /* NVARCHAR     user_type, create_params, auto_increment */
  379.         values             (0,    'max length' ,0, 'nvarchar')
  380.  
  381.     /* SET user_type TO SPHINX VALUES */
  382.     update spt_datatype_info_ext set user_type = xusertype
  383.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  384.  
  385.     commit tran
  386. end
  387. go
  388.  
  389. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  390. go
  391.  
  392. print 'creating table spt_datatype_info'
  393. go
  394. if (charindex('6.00', @@version) = 0 and
  395.     charindex('6.50', @@version) = 0 and
  396.     charindex('7.00', @@version) = 0)
  397. begin    /*    Pre 6.0 server */
  398.     print ''
  399.     print ''
  400.     print 'Warning:'
  401.     print 'you are installing the stored procedures '
  402.     print 'on a pre 6.0 SQL Server.'
  403.     print 'Ignore the following error.'
  404.     create table spt_datatype_info (
  405.         ss_dtype            tinyint     not null,
  406.         fixlen                int         null,        /* datatype len for variable, else null */
  407.         ODBCVer             tinyint     null,        /* version if needed, else null */
  408.         TYPE_NAME            varchar(32)    not null,
  409.         DATA_TYPE            smallint    not null,
  410.         data_precision        int         null,
  411.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  412.         RADIX                smallint    null,
  413.         length                int         null,
  414.         LITERAL_PREFIX        varchar(32) null,
  415.         LITERAL_SUFFIX        varchar(32) null,
  416.         CREATE_PARAMS        varchar(32) null,
  417.         NULLABLE            smallint    not null,
  418.         CASE_SENSITIVE        smallint    not null,
  419.         SEARCHABLE            smallint    not null,
  420.         UNSIGNED_ATTRIBUTE    smallint    null,
  421.         MONEY                smallint    not null,
  422.         AUTO_INCREMENT        smallint    null,
  423.         LOCAL_TYPE_NAME     varchar(32)    null,
  424.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  425.         SQL_DATA_TYPE        smallint    not null,
  426.         SQL_DATETIME_SUB    smallint    null)
  427. end
  428. go
  429. if (charindex('6.00', @@version) > 0 or
  430.     charindex('6.50', @@version) > 0 or
  431.     charindex('7.00', @@version) > 0)
  432. begin    /* 6.0 or later server */
  433.     create table spt_datatype_info (
  434.         ss_dtype            tinyint     not null,
  435.         fixlen                int         null,        /* datatype len for variable, else null */
  436.         ODBCVer             tinyint     null,        /* version if needed, else null */
  437.         TYPE_NAME            sysname     not null,
  438.         DATA_TYPE            smallint    not null,
  439.         data_precision        int         null,
  440.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  441.         RADIX                smallint    null,
  442.         length                int         null,
  443.         LITERAL_PREFIX        varchar(32) null,
  444.         LITERAL_SUFFIX        varchar(32) null,
  445.         CREATE_PARAMS        varchar(32) null,
  446.         NULLABLE            smallint    not null,
  447.         CASE_SENSITIVE        smallint    not null,
  448.         SEARCHABLE            smallint    not null,
  449.         UNSIGNED_ATTRIBUTE    smallint    null,
  450.         MONEY                smallint    not null,
  451.         AUTO_INCREMENT        smallint    null,
  452.         LOCAL_TYPE_NAME     sysname     null,
  453.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  454.         SQL_DATA_TYPE        smallint    not null,
  455.         SQL_DATETIME_SUB    smallint    null)
  456. end
  457. go
  458.  
  459. grant select on spt_datatype_info to public
  460. go
  461.  
  462. /* Get case sensitivity */
  463. if 'A' = 'A' /* create dummy begin block */
  464. begin
  465.     declare @case smallint
  466.  
  467.     begin tran
  468.     select @case = 0
  469.     select @case = 1 where 'a' <> 'A'
  470.  
  471.     /* Local Binary */
  472.     insert into spt_datatype_info values
  473.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  474.  
  475.     /* Local Bit */
  476.     insert into spt_datatype_info values
  477.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  478.  
  479.     /* Local Char */
  480.     insert into spt_datatype_info values
  481.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  482.  
  483.     /* Local Datetime */
  484.     insert into spt_datatype_info values
  485.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  486.     insert into spt_datatype_info values
  487.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  488.  
  489.     /* Local Smalldatetime */
  490.     insert into spt_datatype_info values
  491.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  492.     insert into spt_datatype_info values
  493.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  494.  
  495.     /* Local Float */
  496.     insert into spt_datatype_info values
  497.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  498.     insert into spt_datatype_info values
  499.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  500.  
  501.     /* Local Real */
  502.     insert into spt_datatype_info values
  503.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  504.     insert into spt_datatype_info values
  505.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  506.  
  507.     /* Local Smallmoney */
  508.     insert into spt_datatype_info values
  509.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  510.  
  511.     /* Local Money */
  512.     insert into spt_datatype_info values
  513.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  514.  
  515.     /* Local Int */
  516.     insert into spt_datatype_info values
  517.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  518.  
  519.     commit tran
  520. end
  521. go
  522.  
  523. if 'A' = 'A' /* create dummy begin block */
  524. begin
  525.     declare @case smallint
  526.  
  527.     begin tran
  528.     select @case = 0
  529.     select @case = 1 where 'a' <> 'A'
  530.  
  531.  
  532.     /* Local Smallint */
  533.     insert into spt_datatype_info values
  534.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  535.     insert into spt_datatype_info values
  536.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  537.  
  538.     /* Local Tinyint */
  539.     insert into spt_datatype_info values
  540.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  541.  
  542.     /* Local Text */
  543.     insert into spt_datatype_info values
  544.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  545.  
  546.     /* Local Varbinary */
  547.     insert into spt_datatype_info values
  548.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  549.  
  550.     /* Local Varchar */
  551.     insert into spt_datatype_info values
  552.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  553.  
  554.     /* Local Image */
  555.     insert into spt_datatype_info values
  556.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  557.  
  558.     if (charindex('6.00', @@version) > 0 or
  559.         charindex('6.50', @@version) > 0 or
  560.         charindex('7.00', @@version) > 0)
  561.     begin    /*    Add 6.0 data types */
  562.         /* Local Decimal */
  563.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  564.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  565.  
  566.         /* Local Numeric */
  567.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  568.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  569.  
  570.         /* Identity attribute data types */
  571.  
  572.         /* Identity Int */
  573.         insert into spt_datatype_info values
  574.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  575.  
  576.         /* Identity Smallint */
  577.         insert into spt_datatype_info values
  578.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  579.  
  580.         /* Identity Tinyint */
  581.         insert into spt_datatype_info values
  582.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  583.  
  584.         /* Identity Numeric */
  585.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  586.         (55,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  587.  
  588.         /* Identity Numeric */
  589.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  590.         (63,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  591.  
  592.     end
  593.  
  594.     if (charindex('7.00', @@version) = 0)
  595.     begin    /*    Add nullable type for non-Sphinx server */
  596.         /* Local Datetimn */
  597.         insert into spt_datatype_info values
  598.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  599.         insert into spt_datatype_info values
  600.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  601.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  602.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  603.         insert into spt_datatype_info values
  604.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  605.  
  606.         /* Local Floatn */
  607.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  608.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  609.         insert into spt_datatype_info values
  610.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  611.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  612.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  613.         insert into spt_datatype_info values
  614.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  615.  
  616.         /* Local Moneyn */
  617.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  618.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  619.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  620.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  621.  
  622.         /* Local Intn */
  623.         insert into spt_datatype_info values /* sql server type is 'intn' */
  624.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  625.         insert into spt_datatype_info values /* sql server type is 'intn' */
  626.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  627.         insert into spt_datatype_info values
  628.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  629.  
  630.         if (charindex('6.00', @@version) > 0 or
  631.             charindex('6.50', @@version) > 0 or
  632.             charindex('7.00', @@version) > 0)
  633.         begin    /*    Add 6.0 data types */
  634.             /* Local Decimaln */
  635.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  636.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  637.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  638.             (106,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  639.  
  640.             /* Local Numericn */
  641.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  642.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  643.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  644.             (108,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  645.         end
  646.     end
  647.  
  648.     commit tran
  649. end
  650. go
  651.  
  652. if (charindex('7.00', @@version) = 0)
  653. begin
  654.     print ''
  655.     print ''
  656.     print 'Warning:'
  657.     print 'you are installing the stored procedures '
  658.     print 'on a pre 7.0 SQL Server.'
  659.     print 'Ignore the following errors.'
  660. end
  661. go
  662. if (charindex('7.00', @@version) > 0)
  663.     begin
  664.         declare @ncase smallint
  665.  
  666.         select @ncase = 0
  667.         select @ncase = 1 where N'a' <> N'A'
  668.  
  669.         /* Local Timestamp */
  670.         insert into spt_datatype_info values
  671.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  672.  
  673.         /* Local GUID */
  674.         insert into spt_datatype_info values
  675.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  676.  
  677.         /* Local NChar */
  678.         insert into spt_datatype_info values
  679.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  680.  
  681.         /* Local NVarchar */
  682.         insert into spt_datatype_info values
  683.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  684.  
  685.         /* Local NText */
  686.         insert into spt_datatype_info values
  687.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  688.  
  689.         update spt_datatype_info set NULLABLE = 1
  690.             where TYPE_NAME = 'bit'
  691.  
  692.         update spt_datatype_info set ss_dtype = (select xtype from systypes
  693.             where TYPE_NAME like name+'%')
  694.     end
  695. go
  696.  
  697. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  698. go
  699.  
  700. dump tran master with no_log
  701. go
  702.  
  703. print 'creating table spt_server_info'
  704. go
  705. create table spt_server_info (
  706.               attribute_id        int NOT NULL,
  707.               attribute_name    varchar(60) NOT NULL,
  708.               attribute_value    varchar(255) NOT NULL)
  709. go
  710.  
  711. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  712. go
  713.  
  714. if (charindex('7.00', @@version) = 0)
  715.     begin
  716.         drop procedure sp_add_server_sortinfo    /* not used by older servers */
  717.         dump tran master with no_log
  718.     end
  719. go
  720.  
  721.  
  722. insert into spt_server_info
  723.     values (1,'DBMS_NAME','Microsoft SQL Server')
  724. insert into spt_server_info
  725.     values (2,'DBMS_VER',@@version)
  726. insert into spt_server_info
  727.     values (10,'OWNER_TERM','owner')
  728. insert into spt_server_info
  729.     values (11,'TABLE_TERM','table')
  730. insert into spt_server_info
  731.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  732. insert into spt_server_info
  733.     values (13,'TABLE_LENGTH','30')
  734. insert into spt_server_info
  735.     values (14,'MAX_QUAL_LENGTH','30')
  736. insert into spt_server_info
  737.     values (15,'COLUMN_LENGTH','30')
  738. if 'A' = 'a' /* If not case sensitive server */
  739. begin
  740.     insert into spt_server_info
  741.         values (16,'IDENTIFIER_CASE','MIXED')
  742. end
  743. else
  744. begin
  745.     insert into spt_server_info
  746.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  747. end
  748. insert into spt_server_info
  749.     values (17,'TX_ISOLATION','2')
  750. if (charindex('6.00', @@version) > 0 or
  751.     charindex('6.50', @@version) > 0 or
  752.     charindex('7.00', @@version) > 0)
  753. begin    /*    Add 6.0 collation sequence */
  754.     insert into spt_server_info
  755.         select 18,'COLLATION_SEQ',
  756.             'charset='+t2.name+' sort_order='+t1.name
  757.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  758.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  759.         from syscharsets t1, syscharsets t2, sysconfigures t3
  760.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  761. end
  762. else
  763. begin    /*    Add 4.2x collation sequence */
  764.     insert into spt_server_info
  765.         select 18,'COLLATION_SEQ',
  766.             'charset='+t2.name+' sort_order='+t1.name
  767.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  768.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  769.         from syscharsets t1, syscharsets t2, sysconfigures t3
  770.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  771. end
  772. insert into spt_server_info
  773.     values (19,'SAVEPOINT_SUPPORT','Y')
  774. insert into spt_server_info
  775.     values (20,'MULTI_RESULT_SETS','Y')
  776. insert into spt_server_info
  777.     values (22,'ACCESSIBLE_TABLES','Y')
  778. go
  779.  
  780. insert into spt_server_info
  781.     values (100,'USERID_LENGTH','30')
  782. insert into spt_server_info
  783.     values (101,'QUALIFIER_TERM','database')
  784. insert into spt_server_info
  785.     values (102,'NAMED_TRANSACTIONS','Y')
  786. insert into spt_server_info
  787.     values (103,'SPROC_AS_LANGUAGE','Y')
  788. insert into spt_server_info
  789.     values (104,'ACCESSIBLE_SPROC','Y')
  790. insert into spt_server_info
  791.     values (105,'MAX_INDEX_COLS','16')
  792. insert into spt_server_info
  793.     values (106,'RENAME_TABLE','Y')
  794. insert into spt_server_info
  795.     values (107,'RENAME_COLUMN','Y')
  796. insert into spt_server_info
  797.     values (108,'DROP_COLUMN','N')
  798. insert into spt_server_info
  799.     values (109,'INCREASE_COLUMN_LENGTH','N')
  800. if (charindex('6.50', @@version) = 0 and
  801.     charindex('7.00', @@version) = 0)
  802. begin
  803.     insert into spt_server_info
  804.         values (110,'DDL_IN_TRANSACTION','N')
  805. end
  806. else
  807. begin
  808.     insert into spt_server_info
  809.         values (110,'DDL_IN_TRANSACTION','Y')
  810. end
  811. insert into spt_server_info
  812.     values (111,'DESCENDING_INDEXES','N')
  813. insert into spt_server_info
  814.     values (112,'SP_RENAME','Y')
  815. insert into spt_server_info
  816.     values (113,'REMOTE_SPROC','Y')
  817. insert into spt_server_info
  818.     values (500,'SYS_SPROC_VERSION','7.01.823')
  819. go
  820.  
  821. if (charindex('7.00', @@version) > 0)
  822. begin    /*    Update values for 7.00 server */
  823.     update spt_server_info set attribute_value = '128'
  824.         where attribute_id in (12,13,14,15,100)
  825. end
  826. go
  827.  
  828. grant select on spt_server_info to public
  829. go
  830.  
  831. print 'creating sp_column_privileges'
  832. go
  833.  
  834. /*    Procedure for pre 6.50 server */
  835. CREATE PROCEDURE sp_column_privileges (
  836.             @table_name         varchar(32),
  837.             @table_owner        varchar(32) = null,
  838.             @table_qualifier    varchar(32) = null,
  839.             @column_name        varchar(96) = null)    /* 3*32 */
  840. as
  841.  
  842.     set nocount on
  843.     declare @table_id     int
  844.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  845.     declare @low smallint                     /* range of userids to check */
  846.     declare @high smallint
  847.     declare @owner_uid smallint
  848.  
  849.     select @low = 0, @high = 32767
  850.  
  851.     if @column_name is null /*    If column name not supplied, match all */
  852.         select @column_name = '%'
  853.  
  854.     if @table_qualifier is not null
  855.     begin
  856.         if db_name() <> @table_qualifier
  857.         begin    /* If qualifier doesn't match current database */
  858.             raiserror 20001 '~~Rush_5~~'
  859.             return
  860.         end
  861.     end
  862.     if @table_owner is null
  863.     begin    /* If unqualified table name */
  864.         SELECT @full_table_name = @table_name
  865.     end
  866.     else
  867.     begin    /* Qualified table name */
  868.         SELECT @full_table_name = @table_owner + '.' + @table_name
  869.     end
  870.     /*    Get Object ID */
  871.     select @table_id = object_id(@full_table_name)
  872.  
  873.     if (@@trancount <> 0)
  874.     begin    /* If inside a transaction */
  875.         raiserror 20003 '~~Rush_6~~'
  876.         return
  877.     end
  878.  
  879.     /*
  880.     ** We need to create a table which will contain a row for every row to
  881.     ** be returned to the client.
  882.     */
  883.  
  884.     create table #column_priv1(
  885.         COLUMN_NAME             varchar(32) NOT NULL,
  886.         grantor                 smallint NOT NULL,
  887.         grantee                 smallint NOT NULL,
  888.         select_privilege        bit,
  889.         select_grantable        bit,
  890.         insert_privilege        bit,
  891.         insert_grantable        bit,
  892.         update_privilege        bit,
  893.         update_grantable        bit,
  894.         references_privilege    bit,
  895.         references_grantable    bit,
  896.         uid                     smallint NOT NULL,
  897.         gid                     smallint NOT NULL)
  898.  
  899. /*
  900. ** insert a row for the table owner (who has all permissions)
  901. */
  902.     select @owner_uid = (
  903.         select uid
  904.         from sysobjects
  905.         where id = @table_id)
  906.  
  907.     if (charindex('6.00', @@version) > 0)
  908.     begin
  909.         insert into #column_priv1
  910.             select
  911.                 c.name,
  912.                 u.uid,
  913.                 @owner_uid,
  914.                 0,
  915.                 1,
  916.                 0,
  917.                 1,
  918.                 0,
  919.                 1,
  920.                 0,
  921.                 1,
  922.                 @owner_uid,
  923.                 0
  924.             from syscolumns c, sysusers u
  925.             where id = @table_id
  926.                 and c.number = 0
  927.                 and u.suid = 1        /* grantor is dbo of database */
  928.     end
  929.     else
  930.     begin
  931.         insert into #column_priv1
  932.             select
  933.                 c.name,
  934.                 u.uid,
  935.                 @owner_uid,
  936.                 0,
  937.                 1,
  938.                 0,
  939.                 1,
  940.                 0,
  941.                 1,
  942.                 0,
  943.                 0,
  944.                 @owner_uid,
  945.                 0
  946.             from syscolumns c, sysusers u
  947.             where id = @table_id
  948.                 and c.number = 0
  949.                 and u.suid = 1        /* grantor is dbo of database */
  950.     end
  951. /*
  952. ** now stick in a row for every column for every user in the database
  953. ** we will need to weed out those who have no permissions later
  954. ** (and yes this is a cartesion product: the uid field in sysprotects
  955. ** can also have a group id, in which case we need to extend those
  956. ** privileges to all group members).
  957. */
  958.  
  959.     insert into #column_priv1
  960.         select distinct
  961.             c.name,
  962.             o.uid,
  963.             u.uid,
  964.             0,
  965.             0,
  966.             0,
  967.             0,
  968.             0,
  969.             0,
  970.             0,
  971.             0,
  972.             u.uid,
  973.             u.gid
  974.         from sysusers u, syscolumns c, sysobjects o
  975.         where o.id = @table_id
  976.             and c.id = o.id
  977.             and c.number = 0
  978.             and u.gid <> u.uid
  979.             and u.uid <> @owner_uid
  980.  
  981.     /*
  982.     ** we need to create another temporary table to contain all the various
  983.     ** protection information for the table in question
  984.     */
  985.     create table #protects (
  986.                 uid         smallint NOT NULL,
  987.                 grantor        smallint NOT NULL,
  988.                 action        tinyint NOT NULL,
  989.                 protecttype tinyint NOT NULL,
  990.                 name        varchar(32) NOT NULL)
  991.  
  992.     insert into #protects
  993.         select
  994.             p.uid,
  995.             p.uid,
  996.             p.action,
  997.             p.protecttype,
  998.             isnull(col_name(id, c.number), '~All')
  999.             from
  1000.                 sysprotects p,
  1001.                 master.dbo.spt_values c,
  1002.                 master.dbo.spt_values a,
  1003.                 master.dbo.spt_values b
  1004.             where
  1005.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1006.                     & c.high <> 0
  1007.                     and c.number <= (
  1008.                         select count(*)
  1009.                         from syscolumns
  1010.                         where id = @table_id)
  1011.                 and c.type = 'P'
  1012.                 and a.type = 'T'
  1013.                 and a.number = p.action
  1014.                 and p.action in (193,195,197,26)
  1015.                 and b.type = 'T'
  1016.                 and b.number = p.protecttype
  1017.                 and p.id = @table_id
  1018.                 and p.uid between @low and @high
  1019.  
  1020.  
  1021.     update #column_priv1
  1022.     set select_privilege = 1
  1023.         from #protects p
  1024.     where
  1025.         p.protecttype = 205
  1026.         and p.action = 193
  1027.         and (p.name = #column_priv1.COLUMN_NAME
  1028.             or name = '~All')
  1029.         and (p.uid = 0
  1030.             or p.uid = #column_priv1.gid
  1031.             or p.uid = #column_priv1.uid)
  1032.         and not exists (
  1033.             select * from #protects
  1034.             where
  1035.                 protecttype = 206
  1036.                 and action = 193
  1037.                 and (name = #column_priv1.COLUMN_NAME
  1038.                     or name = '~All')
  1039.                 and ( uid = 0
  1040.                     or uid = #column_priv1.gid
  1041.                     or uid = #column_priv1.uid))
  1042.  
  1043.     update #column_priv1
  1044.     set insert_privilege = 1
  1045.         from #protects p
  1046.     where
  1047.         p.protecttype = 205
  1048.         and p.action = 195
  1049.         and (p.name = #column_priv1.COLUMN_NAME
  1050.             or name = '~All')
  1051.         and (p.uid = 0
  1052.             or p.uid = #column_priv1.gid
  1053.             or p.uid = #column_priv1.uid)
  1054.         and not exists (
  1055.             select * from #protects
  1056.             where
  1057.                 protecttype = 206
  1058.                 and action = 195
  1059.                 and (name = #column_priv1.COLUMN_NAME
  1060.                        or name = '~All')
  1061.                 and (uid = 0
  1062.                     or uid = #column_priv1.gid
  1063.                     or uid = #column_priv1.uid))
  1064.  
  1065.     update #column_priv1
  1066.     set update_privilege = 1
  1067.         from #protects p
  1068.     where
  1069.         p.protecttype = 205
  1070.         and p.action = 197
  1071.         and (p.name = #column_priv1.COLUMN_NAME
  1072.             or name = '~All')
  1073.         and (p.uid = 0
  1074.             or p.uid = #column_priv1.gid
  1075.             or p.uid = #column_priv1.uid)
  1076.         and not exists (
  1077.             select * from #protects
  1078.                 where protecttype = 206
  1079.                 and action = 197
  1080.                 and (name = #column_priv1.COLUMN_NAME
  1081.                     or name = '~All')
  1082.                 and (uid = 0
  1083.                     or uid = #column_priv1.gid
  1084.                     or uid = #column_priv1.uid))
  1085.  
  1086.     update #column_priv1
  1087.     set references_privilege = 1
  1088.         from #protects p
  1089.     where
  1090.         p.protecttype = 205
  1091.         and p.action = 26
  1092.         and (p.name = #column_priv1.COLUMN_NAME
  1093.             or name = '~All')
  1094.         and (p.uid = 0
  1095.             or p.uid = #column_priv1.gid
  1096.             or p.uid = #column_priv1.uid)
  1097.         and not exists (
  1098.             select * from #protects
  1099.                 where protecttype = 206
  1100.                 and action = 26
  1101.                 and (name = #column_priv1.COLUMN_NAME
  1102.                     or name = '~All')
  1103.                 and (uid = 0
  1104.                     or uid = #column_priv1.gid
  1105.                     or uid = #column_priv1.uid))
  1106.  
  1107.     update #column_priv1
  1108.     set select_grantable = 1
  1109.         from #protects p
  1110.     where
  1111.         p.protecttype = 204
  1112.         and p.action = 193
  1113.         and (p.name = #column_priv1.COLUMN_NAME
  1114.             or name = '~All')
  1115.         and (p.uid = 0
  1116.             or p.uid = #column_priv1.gid
  1117.             or p.uid = #column_priv1.uid)
  1118.         and not exists (
  1119.             select * from #protects
  1120.             where
  1121.                 protecttype = 206
  1122.                 and action = 193
  1123.                 and (name = #column_priv1.COLUMN_NAME
  1124.                     or name = '~All')
  1125.                 and ( uid = 0
  1126.                     or uid = #column_priv1.gid
  1127.                     or uid = #column_priv1.uid))
  1128.  
  1129.     update #column_priv1
  1130.     set insert_grantable = 1
  1131.         from #protects p
  1132.     where
  1133.         p.protecttype = 204
  1134.         and p.action = 195
  1135.         and (p.name = #column_priv1.COLUMN_NAME
  1136.             or name = '~All')
  1137.         and (p.uid = 0
  1138.             or p.uid = #column_priv1.gid
  1139.             or p.uid = #column_priv1.uid)
  1140.         and not exists (
  1141.             select * from #protects
  1142.             where
  1143.                 protecttype = 206
  1144.                 and action = 195
  1145.                 and (name = #column_priv1.COLUMN_NAME
  1146.                     or name = '~All')
  1147.                 and ( uid = 0
  1148.                     or uid = #column_priv1.gid
  1149.                     or uid = #column_priv1.uid))
  1150.  
  1151.     update #column_priv1
  1152.     set update_grantable = 1
  1153.         from #protects p
  1154.     where
  1155.         p.protecttype = 204
  1156.         and p.action = 197
  1157.         and (p.name = #column_priv1.COLUMN_NAME
  1158.             or name = '~All')
  1159.         and (p.uid = 0
  1160.             or p.uid = #column_priv1.gid
  1161.             or p.uid = #column_priv1.uid)
  1162.         and not exists (
  1163.             select * from #protects
  1164.             where
  1165.                 protecttype = 206
  1166.                 and action = 197
  1167.                 and (name = #column_priv1.COLUMN_NAME
  1168.                     or name = '~All')
  1169.                 and ( uid = 0
  1170.                     or uid = #column_priv1.gid
  1171.                     or uid = #column_priv1.uid))
  1172.  
  1173.     update #column_priv1
  1174.     set references_grantable = 1
  1175.         from #protects p
  1176.     where
  1177.         p.protecttype = 204
  1178.         and p.action = 26
  1179.         and (p.name = #column_priv1.COLUMN_NAME
  1180.             or name = '~All')
  1181.         and (p.uid = 0
  1182.             or p.uid = #column_priv1.gid
  1183.             or p.uid = #column_priv1.uid)
  1184.         and not exists (
  1185.             select * from #protects
  1186.             where
  1187.                 protecttype = 206
  1188.                 and action = 26
  1189.                 and (name = #column_priv1.COLUMN_NAME
  1190.                     or name = '~All')
  1191.                 and ( uid = 0
  1192.                     or uid = #column_priv1.gid
  1193.                     or uid = #column_priv1.uid))
  1194.  
  1195.     create table #column_priv2(
  1196.         COLUMN_NAME     varchar(32) NOT NULL,
  1197.         grantor         smallint NULL,
  1198.         grantee         smallint NOT NULL,
  1199.         PRIVILEGE        varchar(32) NOT NULL,
  1200.         IS_GRANTABLE    varchar(3) NULL)
  1201.  
  1202.     insert into #column_priv2
  1203.         select
  1204.             COLUMN_NAME,
  1205.             grantor,
  1206.             grantee,
  1207.             'SELECT',
  1208.             'NO'
  1209.         from #column_priv1
  1210.         where select_privilege = 1 and select_grantable    = 0
  1211.  
  1212.     insert into #column_priv2
  1213.         select
  1214.             COLUMN_NAME,
  1215.             grantor,
  1216.             grantee,
  1217.             'INSERT',
  1218.             'NO'
  1219.         from #column_priv1
  1220.         where insert_privilege = 1 and insert_grantable = 0
  1221.  
  1222.     insert into #column_priv2
  1223.         select
  1224.             COLUMN_NAME,
  1225.             grantor,
  1226.             grantee,
  1227.             'UPDATE',
  1228.             'NO'
  1229.         from #column_priv1
  1230.         where update_privilege = 1 and update_grantable = 0
  1231.  
  1232.     insert into #column_priv2
  1233.         select
  1234.             COLUMN_NAME,
  1235.             grantor,
  1236.             grantee,
  1237.             'REFERENCES',
  1238.             'NO'
  1239.         from #column_priv1
  1240.         where references_privilege = 1 and references_grantable = 0
  1241.  
  1242.     insert into #column_priv2
  1243.         select
  1244.             COLUMN_NAME,
  1245.             grantor,
  1246.             grantee,
  1247.             'SELECT',
  1248.             'YES'
  1249.         from #column_priv1
  1250.         where select_grantable = 1
  1251.  
  1252.     insert into #column_priv2
  1253.         select
  1254.             COLUMN_NAME,
  1255.             grantor,
  1256.             grantee,
  1257.             'INSERT',
  1258.             'YES'
  1259.         from #column_priv1
  1260.         where insert_grantable = 1
  1261.  
  1262.     insert into #column_priv2
  1263.         select
  1264.             COLUMN_NAME,
  1265.             grantor,
  1266.             grantee,
  1267.             'UPDATE',
  1268.             'YES'
  1269.         from #column_priv1
  1270.         where update_grantable = 1
  1271.  
  1272.     insert into #column_priv2
  1273.         select
  1274.             COLUMN_NAME,
  1275.             grantor,
  1276.             grantee,
  1277.             'REFERENCES',
  1278.             'YES'
  1279.         from #column_priv1
  1280.         where references_grantable = 1
  1281.  
  1282.     select
  1283.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1284.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1285.         @table_name TABLE_NAME,
  1286.         COLUMN_NAME,
  1287.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1288.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1289.         PRIVILEGE,
  1290.         IS_GRANTABLE
  1291.     from #column_priv2
  1292.     where COLUMN_NAME like @column_name
  1293.     order by 4, 7
  1294. go
  1295.  
  1296. if (charindex('6.50', @@version) = 0 and
  1297.     charindex('7.00', @@version) = 0)
  1298. begin
  1299.     print ''
  1300.     print ''
  1301.     print 'Warning:'
  1302.     print 'you are installing the stored procedures '
  1303.     print 'on a pre 6.50 SQL Server.'
  1304.     print 'Ignore the following errors.'
  1305. end
  1306. else
  1307.     drop proc sp_column_privileges
  1308. go
  1309.  
  1310. /*    Procedure for 6.50 server */
  1311. CREATE PROCEDURE sp_column_privileges (
  1312.             @table_name         varchar(32),
  1313.             @table_owner        varchar(32) = null,
  1314.             @table_qualifier    varchar(32) = null,
  1315.             @column_name        varchar(96) = null)    /* 3*32 */
  1316. as
  1317.  
  1318.     declare @table_id     int
  1319.  
  1320.     if @column_name is null /*    If column name not supplied, match all */
  1321.         select @column_name = '%'
  1322.  
  1323.     if @table_qualifier is not null
  1324.     begin
  1325.         if db_name() <> @table_qualifier
  1326.         begin    /* If qualifier doesn't match current database */
  1327.             raiserror (15250, -1,-1)
  1328.             return
  1329.         end
  1330.     end
  1331.     if @table_owner is null
  1332.     begin    /* If unqualified table name */
  1333.         select @table_id = object_id(@table_name)
  1334.     end
  1335.     else
  1336.     begin    /* Qualified table name */
  1337.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1338.     end
  1339.  
  1340.     select
  1341.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1342.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1343.         @table_name TABLE_NAME,
  1344.         convert(varchar(32),c.name) COLUMN_NAME,
  1345.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1346.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1347.         convert (varchar(32),case p.action
  1348.              when 193 then 'SELECT'
  1349.              when 195 then 'INSERT'
  1350.              when 197 then 'UPDATE'
  1351.              else 'REFERENCES'
  1352.         end) PRIVILEGE,
  1353.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1354.             else 'YES'
  1355.         end) IS_GRANTABLE
  1356.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1357.     where
  1358.         c.id = @table_id
  1359.         and c.name like @column_name
  1360.         and c.id = p.id
  1361.         and c.id = o.id
  1362.         and case substring(p.columns, 1, 1) & 1
  1363.                 when NULL then 255    /* all columns have permission */
  1364.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1365.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1366.             end
  1367.             & v.high <> 0            /* permission applies to this column */
  1368.         and v.number <= (select count(*) from syscolumns
  1369.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1370.         and v.type = 'P'
  1371.         and v.number = c.colid
  1372.             /* expand groups */
  1373.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1374.              (p.uid = u.gid and u.uid <> u.gid))
  1375.         and p.protecttype <> 206    /* only grant rows */
  1376.         and p.action in (26,193,195,197)
  1377.         and o.uid <> u.uid            /* no rows for owner */
  1378.         and not exists (            /* exclude revoke'd privileges */
  1379.             select *
  1380.             from sysprotects p1
  1381.             where
  1382.                 p1.protecttype = 206
  1383.                 and p1.action = p.action
  1384.                 and p1.id = p.id
  1385.                 and p1.uid = u.uid
  1386.                 and case substring(p1.columns, 1, 1) & 1
  1387.                         when NULL then 255    /* all columns have permission */
  1388.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1389.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1390.                     end
  1391.                     & v.high <> 0)            /* permission applies to this column */
  1392.     union all
  1393.     select    /*    Add rows for table owner */
  1394.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1395.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1396.         @table_name TABLE_NAME,
  1397.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1398.         convert(varchar(32),user_name(u.uid)) grantor,
  1399.         convert(varchar(32),user_name(o.uid)) grantee,
  1400.         convert (varchar(32),case v.number
  1401.             when 193 then 'SELECT'
  1402.             when 195 then 'INSERT'
  1403.             when 197 then 'UPDATE'
  1404.             else 'REFERENCES'
  1405.         end) PRIVILEGE,
  1406.         convert(varchar(3),'YES') IS_GRANTABLE
  1407.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1408.     where
  1409.         c.id = @table_id
  1410.         and c.name like @column_name
  1411.         and c.id = o.id
  1412.         and u.suid = 1        /* grantor is dbo of database */
  1413.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1414.         and v.number in (26,193,195,197)
  1415.         and not exists (    /* exclude revoke'd privileges */
  1416.             select *
  1417.             from sysprotects p1
  1418.             where
  1419.                 p1.protecttype = 206
  1420.                 and p1.action = v.number
  1421.                 and p1.id = o.id
  1422.                 and p1.uid = o.uid)
  1423.     order by 4, 7
  1424. go
  1425.  
  1426.  
  1427.  
  1428. if (charindex('7.00', @@version) = 0)
  1429. begin
  1430.     print ''
  1431.     print ''
  1432.     print 'Warning:'
  1433.     print 'you are installing the stored procedures '
  1434.     print 'on a pre 7.0 SQL Server.'
  1435.     print 'Ignore the following errors.'
  1436. end
  1437. else
  1438.     drop proc sp_column_privileges
  1439. go
  1440.  
  1441. /*    Procedure for 7.00 server */
  1442. CREATE PROCEDURE sp_column_privileges (
  1443.             @table_name         sysname,
  1444.             @table_owner        sysname = null,
  1445.             @table_qualifier    sysname = null,
  1446.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1447. as
  1448.  
  1449.     declare @table_id     int
  1450.  
  1451.     if @column_name is null /*    If column name not supplied, match all */
  1452.         select @column_name = '%'
  1453.  
  1454.     if @table_qualifier is not null
  1455.     begin
  1456.         if db_name() <> @table_qualifier
  1457.         begin    /* If qualifier doesn't match current database */
  1458.             raiserror (15250, -1,-1)
  1459.             return
  1460.         end
  1461.     end
  1462.     if @table_owner is null
  1463.     begin    /* If unqualified table name */
  1464.         select @table_id = object_id(quotename(@table_name))
  1465.     end
  1466.     else
  1467.     begin    /* Qualified table name */
  1468.         if @table_owner = N''
  1469.         begin    /* If empty owner name */
  1470.             select @table_id = 0
  1471.         end
  1472.         else
  1473.         begin
  1474.             select @table_id = object_id(quotename(@table_owner) +
  1475.                 '.' + quotename(@table_name))
  1476.         end
  1477.     end
  1478.  
  1479.     select
  1480.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1481.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1482.         @table_name TABLE_NAME,
  1483.         convert(sysname,c.name) COLUMN_NAME,
  1484.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1485.         convert(sysname,user_name(u.uid)) GRANTEE,
  1486.         convert (varchar(32),case p.action
  1487.              when 193 then 'SELECT'
  1488.              when 195 then 'INSERT'
  1489.              when 197 then 'UPDATE'
  1490.              else 'REFERENCES'
  1491.         end) PRIVILEGE,
  1492.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1493.             else 'YES'
  1494.         end) IS_GRANTABLE
  1495.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1496.     where
  1497.         c.id = @table_id
  1498.         and c.name like @column_name
  1499.         and c.id = p.id
  1500.         and c.id = o.id
  1501.         and case substring(p.columns, 1, 1) & 1
  1502.                 when NULL then 255    /* all columns have permission */
  1503.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1504.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1505.             end
  1506.             & v.high <> 0            /* permission applies to this column */
  1507.         and v.number <= (select count(*) from syscolumns
  1508.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1509.         and v.type = 'P'
  1510.         and v.number = c.colid
  1511.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1512.         and (u.uid > 0 and u.uid < 16384)
  1513.         and ((p.uid = u.uid) or 
  1514.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1515.         and p.protecttype <> 206    /* only grant rows */
  1516.         and p.action in (26,193,195,197)
  1517.         and o.uid <> u.uid            /* no rows for owner */
  1518.         and not exists (            /* exclude revoke'd privileges */
  1519.             select *
  1520.             from sysprotects p1
  1521.             where
  1522.                 p1.protecttype = 206
  1523.                 and p1.action = p.action
  1524.                 and p1.id = p.id
  1525.                 and p1.uid = u.uid
  1526.                 and case substring(p1.columns, 1, 1) & 1
  1527.                         when NULL then 255    /* all columns have permission */
  1528.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1529.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1530.                     end
  1531.                     & v.high <> 0)            /* permission applies to this column */
  1532.     union all
  1533.     select    /*    Add rows for table owner */
  1534.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1535.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1536.         @table_name TABLE_NAME,
  1537.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1538.         convert(sysname,user_name(u.uid)) grantor,
  1539.         convert(sysname,user_name(o.uid)) grantee,
  1540.         convert (varchar(32),case v.number
  1541.             when 193 then 'SELECT'
  1542.             when 195 then 'INSERT'
  1543.             when 197 then 'UPDATE'
  1544.             else 'REFERENCES'
  1545.         end) PRIVILEGE,
  1546.         convert(varchar(3),'YES') IS_GRANTABLE
  1547.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1548.     where
  1549.         c.id = @table_id
  1550.         and c.name like @column_name
  1551.         and c.id = o.id
  1552.         and u.uid = 1        /* grantor is 'dbo' of database */
  1553.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1554.         and v.number in (26,193,195,197)
  1555.         and not exists (    /* exclude revoke'd privileges */
  1556.             select *
  1557.             from sysprotects p1
  1558.             where
  1559.                 p1.protecttype = 206
  1560.                 and p1.action = v.number
  1561.                 and p1.id = o.id
  1562.                 and p1.uid = o.uid)
  1563.     order by 4, 7
  1564. go
  1565.  
  1566.  
  1567. grant execute on sp_column_privileges to public
  1568. go
  1569.  
  1570. dump tran master with no_log
  1571. go
  1572.  
  1573. print 'creating sp_columns'
  1574. go
  1575.  
  1576. /*    Procedure for pre-6.0 server */
  1577. CREATE PROCEDURE sp_columns (
  1578.                  @table_name        varchar(96),
  1579.                  @table_owner        varchar(96) = null,
  1580.                  @table_qualifier    varchar(32) = null,
  1581.                  @column_name        varchar(96) = null,
  1582.                  @ODBCVer            int = 2)
  1583. AS
  1584.     DECLARE @full_table_name varchar(193)
  1585.     DECLARE @table_id        int
  1586.  
  1587.     /* Declare bindings */
  1588.     DECLARE @TABLEQUALIFIER     varchar(32),
  1589.         @TABLEOWNER     varchar(32),
  1590.         @TABLENAME     varchar(32),
  1591.         @COLUMNNAME     varchar(32),
  1592.         @ORDINALPOSITION int
  1593.  
  1594.     /* Declare tracking variables */
  1595.     DECLARE @currQualifier varchar(32),
  1596.          @currOwner     varchar(32),
  1597.         @currName      varchar(32),
  1598.         @currPos       int
  1599.  
  1600.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  1601.     CREATE TABLE #columns
  1602.     (
  1603.         TABLE_QUALIFIER        varchar(32),
  1604.         TABLE_OWNER        varchar(32),
  1605.         TABLE_NAME        varchar(32),
  1606.         COLUMN_NAME        varchar(32),
  1607.         DATA_TYPE        smallint,
  1608.         TYPE_NAME        varchar(32),            
  1609.         "PRECISION"        int,
  1610.         LENGTH            int,
  1611.         SCALE             smallint        null,
  1612.         RADIX            smallint        null,
  1613.         NULLABLE        smallint,
  1614.         REMARKS            varchar(254)    null,
  1615.         COLUMN_DEF        varchar(255)    null,
  1616.         SQL_DATA_TYPE        smallint,
  1617.         SQL_DATETIME_SUB    smallint    null,
  1618.         CHAR_OCTET_LENGTH    int            null,
  1619.         ORDINAL_POSITION    int,
  1620.         IS_NULLABLE        varchar(254),
  1621.         SS_DATA_TYPE        tinyint
  1622.     )        
  1623.  
  1624.     /* Declare cursor for fixup */
  1625.     DECLARE curColumns CURSOR
  1626.     FOR SELECT TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  1627.         FROM #columns
  1628.         ORDER BY TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, ORDINAL_POSITION
  1629.  
  1630.     if @ODBCVer <> 3
  1631.         select @ODBCVer = 2
  1632.     if @column_name is null /*    If column name not supplied, match all */
  1633.         select @column_name = '%'
  1634.     if @table_qualifier is not null
  1635.     begin
  1636.         if db_name() <> @table_qualifier
  1637.         begin    /* If qualifier doesn't match current database */
  1638.             raiserror 20001 '~~Rush_5~~'
  1639.             return
  1640.         end
  1641.     end
  1642.     if @table_name is null
  1643.     begin    /*    If table name not supplied, match all */
  1644.         select @table_name = '%'
  1645.     end
  1646.     if @table_owner is null
  1647.     begin    /* If unqualified table name */
  1648.         SELECT @full_table_name = @table_name
  1649.     end
  1650.     else
  1651.     begin    /* Qualified table name */
  1652.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1653.     end
  1654.  
  1655.     /*    Get Object ID */
  1656.     SELECT @table_id = object_id(@full_table_name)
  1657.     if ((charindex('%',@full_table_name) = 0) and
  1658.         (charindex('_',@full_table_name) = 0) and
  1659.         @table_id <> 0)
  1660.     begin
  1661.         /* this block is for the case where there is no pattern
  1662.              matching required for the table name */
  1663.  
  1664.         set nocount on
  1665.  
  1666.         /* Store results into the temp table */
  1667.         insert into #columns
  1668.             (
  1669.             TABLE_QUALIFIER,
  1670.             TABLE_OWNER,
  1671.             TABLE_NAME,
  1672.             COLUMN_NAME,
  1673.             DATA_TYPE,
  1674.             TYPE_NAME,            
  1675.             "PRECISION",
  1676.             LENGTH,
  1677.             SCALE,
  1678.             RADIX,
  1679.             NULLABLE,
  1680.             REMARKS,
  1681.             COLUMN_DEF,
  1682.             SQL_DATA_TYPE,
  1683.             SQL_DATETIME_SUB,
  1684.             CHAR_OCTET_LENGTH,
  1685.             ORDINAL_POSITION,
  1686.             IS_NULLABLE,
  1687.             SS_DATA_TYPE
  1688.             )
  1689.         SELECT
  1690.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1691.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1692.             TABLE_NAME = convert(varchar(32),o.name),
  1693.             COLUMN_NAME = convert(varchar(32),c.name),
  1694.             d.DATA_TYPE,
  1695.             TYPE_NAME = t.name,
  1696.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1697.             LENGTH = isnull(d.length, convert(int,c.length)),
  1698.             SCALE = d.numeric_scale,
  1699.             d.RADIX,
  1700.             NULLABLE =    /* set nullability from status flag */
  1701.                 convert(smallint, convert(bit, c.status&8)),
  1702.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1703.             COLUMN_DEF = text,
  1704.             d.SQL_DATA_TYPE,
  1705.             d.SQL_DATETIME_SUB,
  1706.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1707.             ORDINAL_POSITION = convert(int,c.colid),
  1708.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1709.             SS_DATA_TYPE = c.type
  1710.         FROM
  1711.             syscolumns c,
  1712.             sysobjects o,
  1713.             syscomments m,
  1714.             master.dbo.spt_datatype_info d,
  1715.             systypes t
  1716.         WHERE
  1717.             o.id = @table_id
  1718.             AND c.id = o.id
  1719.             AND t.type = d.ss_dtype
  1720.             AND c.length = isnull(d.fixlen, c.length)
  1721.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1722.             AND o.type <> 'P'
  1723.             AND c.usertype = t.usertype
  1724.             AND c.name like @column_name
  1725.             AND c.cdefault *= m.id
  1726.             AND m.colid = 1
  1727.         ORDER BY 17
  1728.     end
  1729.     else
  1730.     begin
  1731.         /* this block is for the case where there IS pattern
  1732.              matching done on the table name */
  1733.         if @table_owner is null /*    If owner not supplied, match all */
  1734.             select @table_owner = '%'
  1735.  
  1736.         set nocount on
  1737.  
  1738.         /* Store results into the temp table */
  1739.         insert into #columns
  1740.             (
  1741.             TABLE_QUALIFIER,
  1742.             TABLE_OWNER,
  1743.             TABLE_NAME,
  1744.             COLUMN_NAME,
  1745.             DATA_TYPE,
  1746.             TYPE_NAME,            
  1747.             "PRECISION",
  1748.             LENGTH,
  1749.             SCALE,
  1750.             RADIX,
  1751.             NULLABLE,
  1752.             REMARKS,
  1753.             COLUMN_DEF,
  1754.             SQL_DATA_TYPE,
  1755.             SQL_DATETIME_SUB,
  1756.             CHAR_OCTET_LENGTH,
  1757.             ORDINAL_POSITION,
  1758.             IS_NULLABLE,
  1759.             SS_DATA_TYPE
  1760.             )
  1761.         SELECT
  1762.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1763.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1764.             TABLE_NAME = convert(varchar(32),o.name),
  1765.             COLUMN_NAME = convert(varchar(32),c.name),
  1766.             d.DATA_TYPE,
  1767.             TYPE_NAME = t.name,
  1768.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1769.             LENGTH = isnull(d.length, convert(int,c.length)),
  1770.             SCALE = d.numeric_scale,
  1771.             d.RADIX,
  1772.             NULLABLE =    /* set nullability from status flag */
  1773.                 convert(smallint, convert(bit, c.status&8)),
  1774.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1775.             COLUMN_DEF = text,
  1776.             d.SQL_DATA_TYPE,
  1777.             d.SQL_DATETIME_SUB,
  1778.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1779.             ORDINAL_POSITION = convert(int,c.colid),
  1780.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1781.             SS_DATA_TYPE = c.type
  1782.         FROM
  1783.             syscolumns c,
  1784.             sysobjects o,
  1785.             syscomments m,
  1786.             master.dbo.spt_datatype_info d,
  1787.             systypes t
  1788.         WHERE
  1789.             o.name like @table_name
  1790.             AND user_name(o.uid) like @table_owner
  1791.             AND o.id = c.id
  1792.             AND t.type = d.ss_dtype
  1793.             AND c.length = isnull(d.fixlen, c.length)
  1794.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1795.             AND o.type <> 'P'
  1796.             AND c.usertype = t.usertype
  1797.             AND c.name like @column_name
  1798.             AND c.cdefault *= m.id
  1799.             AND m.colid = 1
  1800.         ORDER BY 2, 3, 17
  1801.     end
  1802.  
  1803.     /* Fix ordinal positions */
  1804.     OPEN curColumns
  1805.     FETCH NEXT FROM curColumns
  1806.     INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  1807.     
  1808.     /* Initialize tracking variables */
  1809.     SELECT @currQualifier = NULL, @currOwner = NULL, @currName = NULL
  1810.     WHILE @@FETCH_STATUS = 0
  1811.     BEGIN
  1812.         /* Check to see if the catalog/schema/table */
  1813.         /* has changed since last row               */
  1814.         IF (@currQualifier  + '.' + @currOwner  + '.' + @currName =
  1815.             @TABLEQUALIFIER + '.' + @TABLEOWNER + '.' + @TABLENAME)
  1816.         BEGIN
  1817.             /* Increment ordinal count */
  1818.             SELECT @currPos = @currPos + 1
  1819.         END
  1820.         ELSE
  1821.         BEGIN
  1822.             /* Set the catalog/schema/table to new values */
  1823.             SELECT @currQualifier = @TABLEQUALIFIER,
  1824.                    @currOwner     = @TABLEOWNER,
  1825.                    @currName      = @TABLENAME,
  1826.                    @currPos       = 1
  1827.         END
  1828.  
  1829.         /* Check for holes in the ordinal position order */
  1830.         IF (@currPos < @ORDINALPOSITION)
  1831.         BEGIN        
  1832.             /* Update the ordinal position */
  1833.             UPDATE #columns
  1834.             SET    ORDINAL_POSITION = @currPos
  1835.             WHERE  TABLE_QUALIFIER  = @TABLEQUALIFIER
  1836.               AND  TABLE_OWNER      = @TABLEOWNER
  1837.               AND  TABLE_NAME       = @TABLENAME
  1838.               AND  COLUMN_NAME      = @COLUMNNAME
  1839.         END
  1840.  
  1841.         /* Fetch next result */
  1842.         FETCH NEXT FROM curColumns
  1843.         INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  1844.     END
  1845.  
  1846.     /* Cleanup cursor */
  1847.     CLOSE curColumns
  1848.     DEALLOCATE curColumns
  1849.  
  1850.     set nocount off
  1851.  
  1852.     /* Output the result set */
  1853.     select *
  1854.     from #columns
  1855.     order by 2, 3, 17
  1856. go
  1857.  
  1858. if (charindex('6.00', @@version) = 0 and
  1859.     charindex('6.50', @@version) = 0 and
  1860.     charindex('7.00', @@version) = 0)
  1861. begin
  1862.     print ''
  1863.     print ''
  1864.     print 'Warning:'
  1865.     print 'you are installing the stored procedures '
  1866.     print 'on a pre 6.0 SQL Server.'
  1867.     print 'Ignore the following error.'
  1868. end
  1869. else
  1870.     drop proc sp_columns
  1871. go
  1872.  
  1873. /*    Procedure for 6.0 and 6.50 server */
  1874. CREATE PROCEDURE sp_columns (
  1875.                  @table_name        varchar(96),
  1876.                  @table_owner        varchar(96) = null,
  1877.                  @table_qualifier    varchar(32) = null,
  1878.                  @column_name        varchar(96) = null,
  1879.                  @ODBCVer            int = 2)
  1880. AS
  1881.     DECLARE @full_table_name    varchar(193)
  1882.     DECLARE @table_id int
  1883.  
  1884.     /* Declare bindings */
  1885.     DECLARE @TABLEQUALIFIER     varchar(32),
  1886.         @TABLEOWNER     varchar(32),
  1887.         @TABLENAME     varchar(32),
  1888.         @COLUMNNAME     varchar(32),
  1889.         @ORDINALPOSITION int
  1890.  
  1891.     /* Declare tracking variables */
  1892.     DECLARE @currQualifier varchar(32),
  1893.          @currOwner     varchar(32),
  1894.         @currName      varchar(32),
  1895.         @currPos       int
  1896.  
  1897.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  1898.     CREATE TABLE #columns
  1899.     (
  1900.         TABLE_QUALIFIER        varchar(32),
  1901.         TABLE_OWNER        varchar(32),
  1902.         TABLE_NAME        varchar(32),
  1903.         COLUMN_NAME        varchar(32),
  1904.         DATA_TYPE        smallint,
  1905.         TYPE_NAME        varchar(32),            
  1906.         "PRECISION"        int,
  1907.         LENGTH            int,
  1908.         SCALE             smallint        null,
  1909.         RADIX            smallint        null,
  1910.         NULLABLE        smallint,
  1911.         REMARKS            varchar(254)    null,
  1912.         COLUMN_DEF        varchar(255)    null,
  1913.         SQL_DATA_TYPE        smallint,
  1914.         SQL_DATETIME_SUB    smallint    null,
  1915.         CHAR_OCTET_LENGTH    int            null,
  1916.         ORDINAL_POSITION    int,
  1917.         IS_NULLABLE        varchar(254),
  1918.         SS_DATA_TYPE        tinyint
  1919.     )        
  1920.  
  1921.     /* Declare cursor for fixup */
  1922.     DECLARE curColumns CURSOR
  1923.     FOR SELECT TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  1924.         FROM #columns
  1925.         ORDER BY TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, ORDINAL_POSITION
  1926.  
  1927.     if @ODBCVer <> 3
  1928.         select @ODBCVer = 2
  1929.     if @column_name is null /*    If column name not supplied, match all */
  1930.         select @column_name = '%'
  1931.     if @table_qualifier is not null
  1932.     begin
  1933.         if db_name() <> @table_qualifier
  1934.         begin    /* If qualifier doesn't match current database */
  1935.             raiserror (15250, -1,-1)
  1936.             return
  1937.         end
  1938.     end
  1939.     if @table_name is null
  1940.     begin
  1941.         /* If table name not supplied, match all */
  1942.         select @table_name = '%'
  1943.     end
  1944.     if @table_owner is null
  1945.     begin
  1946.         /* If unqualified table name */
  1947.         SELECT @full_table_name = @table_name
  1948.     end
  1949.     else
  1950.     begin
  1951.         /* Qualified table name */
  1952.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1953.     end
  1954.  
  1955.     /* Get Object ID */
  1956.     SELECT @table_id = object_id(@full_table_name)
  1957.     if ((charindex('%',@full_table_name) = 0) and
  1958.         (charindex('[',@full_table_name) = 0) and
  1959.         (charindex('_',@full_table_name) = 0) and
  1960.         @table_id <> 0)
  1961.     begin
  1962.         /* this block is for the case where there is no pattern
  1963.             matching required for the table name */
  1964.  
  1965.         set nocount on
  1966.  
  1967.         /* Store results into the temp table */
  1968.         insert into #columns
  1969.             (
  1970.             TABLE_QUALIFIER,
  1971.             TABLE_OWNER,
  1972.             TABLE_NAME,
  1973.             COLUMN_NAME,
  1974.             DATA_TYPE,
  1975.             TYPE_NAME,            
  1976.             "PRECISION",
  1977.             LENGTH,
  1978.             SCALE,
  1979.             RADIX,
  1980.             NULLABLE,
  1981.             REMARKS,
  1982.             COLUMN_DEF,
  1983.             SQL_DATA_TYPE,
  1984.             SQL_DATETIME_SUB,
  1985.             CHAR_OCTET_LENGTH,
  1986.             ORDINAL_POSITION,
  1987.             IS_NULLABLE,
  1988.             SS_DATA_TYPE
  1989.             )
  1990.         SELECT
  1991.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1992.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1993.             TABLE_NAME = convert(varchar(32),o.name),
  1994.             COLUMN_NAME = convert(varchar(32),c.name),
  1995.             DATA_TYPE = d.DATA_TYPE,
  1996.             TYPE_NAME = convert(varchar(32),
  1997.                     case
  1998.                         when t.usertype > 100 or t.usertype in (18,80) then t.name
  1999.                         else d.TYPE_NAME
  2000.                     end),
  2001.             "PRECISION" = convert(int,
  2002.                     case
  2003.                         when d.DATA_TYPE in (6,7) then d.data_precision    /* FLOAT/REAL */
  2004.                         else isnull(convert(int,c.prec), 2147483647)
  2005.                     end),
  2006.             LENGTH = convert(int,
  2007.                     case
  2008.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2009.                         c.prec+2
  2010.                         else
  2011.                             isnull(d.length, c.length)
  2012.                     end),
  2013.             SCALE = convert(smallint, c.scale),
  2014.             RADIX = d.RADIX,
  2015.             NULLABLE =    /* set nullability from status flag */
  2016.                 convert(smallint, convert(bit, c.status&8)),
  2017.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2018.             COLUMN_DEF = text,
  2019.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2020.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2021.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  2022.             ORDINAL_POSITION = convert(int,c.colid),
  2023.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2024.             SS_DATA_TYPE = c.type
  2025.         FROM
  2026.             syscolumns c,
  2027.             sysobjects o,
  2028.             syscomments m,
  2029.             master.dbo.spt_datatype_info d,
  2030.             systypes t
  2031.         WHERE
  2032.             o.id = @table_id
  2033.             AND c.id = o.id
  2034.             AND t.type = d.ss_dtype
  2035.             AND c.length = isnull(d.fixlen, c.length)
  2036.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2037.             AND o.type <> 'P'
  2038.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2039.             AND c.usertype = t.usertype
  2040.             AND c.name like @column_name
  2041.             AND c.cdefault *= m.id
  2042.             AND m.colid = 1
  2043.         ORDER BY 17
  2044.     end
  2045.     else
  2046.     begin
  2047.         /* this block is for the case where there IS pattern
  2048.             matching done on the table name */
  2049.  
  2050.         set nocount on
  2051.  
  2052.         if @table_owner is null /*    If owner not supplied, match all */
  2053.             select @table_owner = '%'
  2054.  
  2055.         /* Store results into the temp table */
  2056.         insert into #columns
  2057.             (
  2058.             TABLE_QUALIFIER,
  2059.             TABLE_OWNER,
  2060.             TABLE_NAME,
  2061.             COLUMN_NAME,
  2062.             DATA_TYPE,
  2063.             TYPE_NAME,            
  2064.             "PRECISION",
  2065.             LENGTH,
  2066.             SCALE,
  2067.             RADIX,
  2068.             NULLABLE,
  2069.             REMARKS,
  2070.             COLUMN_DEF,
  2071.             SQL_DATA_TYPE,
  2072.             SQL_DATETIME_SUB,
  2073.             CHAR_OCTET_LENGTH,
  2074.             ORDINAL_POSITION,
  2075.             IS_NULLABLE,
  2076.             SS_DATA_TYPE
  2077.             )
  2078.         SELECT
  2079.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  2080.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  2081.             TABLE_NAME = convert(varchar(32),o.name),
  2082.             COLUMN_NAME = convert(varchar(32),c.name),
  2083.             DATA_TYPE = d.DATA_TYPE,
  2084.             TYPE_NAME = convert(varchar(32), 
  2085.                 case 
  2086.                     when t.usertype > 100 or t.usertype in (18,80) then t.name
  2087.                     else d.TYPE_NAME
  2088.                 end),
  2089.             "PRECISION" = convert(int,
  2090.                     case
  2091.                         when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2092.                         else isnull(convert(int,c.prec), 2147483647)
  2093.                     end),
  2094.             LENGTH = convert(int,
  2095.                     case
  2096.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2097.                             c.prec+2
  2098.                         else
  2099.                             isnull(d.length, c.length)
  2100.                     end),
  2101.             SCALE = convert(smallint, c.scale),
  2102.             RADIX = d.RADIX,
  2103.             NULLABLE =    /* set nullability from status flag */
  2104.                 convert(smallint, convert(bit, c.status&8)),
  2105.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2106.             COLUMN_DEF = text,
  2107.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2108.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2109.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647) + d.charbin,
  2110.             ORDINAL_POSITION = convert(int,c.colid),
  2111.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2112.             SS_DATA_TYPE = c.type
  2113.         FROM
  2114.             syscolumns c,
  2115.             sysobjects o,
  2116.             syscomments m,
  2117.             master.dbo.spt_datatype_info d,
  2118.             systypes t
  2119.         WHERE
  2120.             o.name like @table_name
  2121.             AND user_name(o.uid) like @table_owner
  2122.             AND o.id = c.id
  2123.             AND t.type = d.ss_dtype
  2124.             AND c.length = isnull(d.fixlen, c.length)
  2125.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2126.             AND o.type <> 'P'
  2127.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2128.             AND c.usertype = t.usertype
  2129.             AND c.name like @column_name
  2130.             AND c.cdefault *= m.id
  2131.             AND m.colid = 1
  2132.         ORDER BY 2, 3, 17
  2133.  
  2134.     end
  2135.  
  2136.     /* Fix ordinal positions */
  2137.     OPEN curColumns
  2138.     FETCH NEXT FROM curColumns
  2139.     INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2140.     
  2141.     /* Initialize tracking variables */
  2142.     SELECT @currQualifier = NULL, @currOwner = NULL, @currName = NULL
  2143.     WHILE @@FETCH_STATUS = 0
  2144.     BEGIN
  2145.         /* Check to see if the catalog/schema/table */
  2146.         /* has changed since last row               */
  2147.         IF (@currQualifier  + '.' + @currOwner  + '.' + @currName =
  2148.             @TABLEQUALIFIER + '.' + @TABLEOWNER + '.' + @TABLENAME)
  2149.         BEGIN
  2150.             /* Increment ordinal count */
  2151.             SELECT @currPos = @currPos + 1
  2152.         END
  2153.         ELSE
  2154.         BEGIN
  2155.             /* Set the catalog/schema/table to new values */
  2156.             SELECT @currQualifier = @TABLEQUALIFIER,
  2157.                    @currOwner     = @TABLEOWNER,
  2158.                    @currName      = @TABLENAME,
  2159.                    @currPos       = 1
  2160.         END
  2161.  
  2162.         /* Check for holes in the ordinal position order */
  2163.         IF (@currPos < @ORDINALPOSITION)
  2164.         BEGIN        
  2165.             /* Update the ordinal position */
  2166.             UPDATE #columns
  2167.             SET    ORDINAL_POSITION = @currPos
  2168.             WHERE  TABLE_QUALIFIER  = @TABLEQUALIFIER
  2169.               AND  TABLE_OWNER      = @TABLEOWNER
  2170.               AND  TABLE_NAME       = @TABLENAME
  2171.               AND  COLUMN_NAME      = @COLUMNNAME
  2172.         END
  2173.  
  2174.         /* Fetch next result */
  2175.         FETCH NEXT FROM curColumns
  2176.         INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2177.     END
  2178.  
  2179.     /* Cleanup cursor */
  2180.     CLOSE curColumns
  2181.     DEALLOCATE curColumns
  2182.  
  2183.     set nocount off
  2184.  
  2185.     /* Output the result set */
  2186.     select *
  2187.     from #columns
  2188.     order by 2, 3, 17
  2189. go
  2190.  
  2191. if (charindex('7.00', @@version) = 0)
  2192. begin
  2193.     print ''
  2194.     print ''
  2195.     print 'Warning:'
  2196.     print 'you are installing the stored procedures '
  2197.     print 'on a pre 7.0 SQL Server.'
  2198.     print 'Ignore the following errors.'
  2199. end
  2200. else
  2201.     drop proc sp_columns
  2202. go
  2203.  
  2204. /*    Procedure for 7.0 server */
  2205. CREATE PROCEDURE sp_columns (
  2206.                  @table_name        nvarchar(384),
  2207.                  @table_owner        nvarchar(384) = null,
  2208.                  @table_qualifier    sysname = null,
  2209.                  @column_name        nvarchar(384) = null,
  2210.                  @ODBCVer            int = 2)
  2211. AS
  2212.     DECLARE @full_table_name    nvarchar(769)
  2213.     DECLARE @table_id int
  2214.  
  2215.     /* Declare bindings */
  2216.     DECLARE @TABLEQUALIFIER     sysname,
  2217.         @TABLEOWNER     sysname,
  2218.         @TABLENAME     sysname,
  2219.         @COLUMNNAME     sysname,
  2220.         @ORDINALPOSITION int
  2221.  
  2222.     /* Declare tracking variables */
  2223.     DECLARE @currQualifier varchar(32),
  2224.          @currOwner     varchar(32),
  2225.         @currName      varchar(32),
  2226.         @currPos       int
  2227.  
  2228.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  2229.     CREATE TABLE #columns
  2230.     (
  2231.         TABLE_QUALIFIER        sysname,
  2232.         TABLE_OWNER        sysname,
  2233.         TABLE_NAME        sysname,
  2234.         COLUMN_NAME        sysname,
  2235.         DATA_TYPE        smallint,
  2236.         TYPE_NAME        sysname,
  2237.         "PRECISION"        int,
  2238.         LENGTH            int,
  2239.         SCALE             smallint        null,
  2240.         RADIX            smallint        null,
  2241.         NULLABLE        smallint,
  2242.         REMARKS            varchar(254)    null,
  2243.         COLUMN_DEF        ntext            null,
  2244.         SQL_DATA_TYPE        smallint,
  2245.         SQL_DATETIME_SUB    smallint    null,
  2246.         CHAR_OCTET_LENGTH    int            null,
  2247.         ORDINAL_POSITION    int,
  2248.         IS_NULLABLE        varchar(254),
  2249.         SS_DATA_TYPE        tinyint
  2250.     )        
  2251.  
  2252.     /* Declare cursor for fixup */
  2253.     DECLARE curColumns CURSOR
  2254.     FOR SELECT TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  2255.         FROM #columns
  2256.         ORDER BY TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, ORDINAL_POSITION
  2257.     if @ODBCVer <> 3
  2258.         select @ODBCVer = 2
  2259.     if @column_name is null /*    If column name not supplied, match all */
  2260.         select @column_name = '%'
  2261.     if @table_qualifier is not null
  2262.     begin
  2263.         if db_name() <> @table_qualifier
  2264.         begin    /* If qualifier doesn't match current database */
  2265.             raiserror (15250, -1,-1)
  2266.             return
  2267.         end
  2268.     end
  2269.     if @table_name is null
  2270.     begin    /*    If table name not supplied, match all */
  2271.         select @table_name = '%'
  2272.     end
  2273.     if @table_owner is null
  2274.     begin    /* If unqualified table name */
  2275.         SELECT @full_table_name = quotename(@table_name)
  2276.     end
  2277.     else
  2278.     begin    /* Qualified table name */
  2279.         if @table_owner = ''
  2280.         begin    /* If empty owner name */
  2281.             SELECT @full_table_name = quotename(@table_owner)
  2282.         end
  2283.         else
  2284.         begin
  2285.             SELECT @full_table_name = quotename(@table_owner) +
  2286.                 '.' + quotename(@table_name)
  2287.         end
  2288.     end
  2289.  
  2290.     /*    Get Object ID */
  2291.     SELECT @table_id = object_id(@full_table_name)
  2292.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2293.         (isnull(charindex('[', @table_name),0) = 0) and
  2294.         (isnull(charindex('[', @table_owner),0) = 0) and
  2295.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2296.         @table_id <> 0)
  2297.     begin
  2298.         /* this block is for the case where there is no pattern
  2299.             matching required for the table name */
  2300.         
  2301.         set nocount on
  2302.  
  2303.         /* Store results into the temp table */
  2304.         insert into #columns
  2305.             (
  2306.             TABLE_QUALIFIER,
  2307.             TABLE_OWNER,
  2308.             TABLE_NAME,
  2309.             COLUMN_NAME,
  2310.             DATA_TYPE,
  2311.             TYPE_NAME,            
  2312.             "PRECISION",
  2313.             LENGTH,
  2314.             SCALE,
  2315.             RADIX,
  2316.             NULLABLE,
  2317.             REMARKS,
  2318.             COLUMN_DEF,
  2319.             SQL_DATA_TYPE,
  2320.             SQL_DATETIME_SUB,
  2321.             CHAR_OCTET_LENGTH,
  2322.             ORDINAL_POSITION,
  2323.             IS_NULLABLE,
  2324.             SS_DATA_TYPE
  2325.             )
  2326.         SELECT
  2327.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2328.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2329.             TABLE_NAME = convert(sysname,o.name),
  2330.             COLUMN_NAME = convert(sysname,c.name),
  2331.             d.DATA_TYPE,
  2332.             convert (sysname,case
  2333.                 when t.xusertype > 255 then t.name
  2334.                 else d.TYPE_NAME
  2335.             end) TYPE_NAME,
  2336.             convert(int,case
  2337.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2338.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2339.             end) "PRECISION",
  2340.             convert(int,case
  2341.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2342.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2343.                 else
  2344.                     isnull(d.length, c.length)
  2345.             end) LENGTH,
  2346.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2347.             d.RADIX,
  2348.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2349.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2350.             COLUMN_DEF = text,
  2351.             d.SQL_DATA_TYPE,
  2352.             d.SQL_DATETIME_SUB,
  2353.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2354.             ORDINAL_POSITION = convert(int,c.colid),
  2355.             IS_NULLABLE = convert(varchar(254),
  2356.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2357.             SS_DATA_TYPE = c.type
  2358.         FROM
  2359.             sysobjects o,
  2360.             master.dbo.spt_datatype_info d,
  2361.             systypes t,
  2362.             syscolumns c
  2363.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2364.                 AND m.colid = 1
  2365.         WHERE
  2366.             o.id = @table_id
  2367.             AND c.id = o.id
  2368.             AND t.xtype = d.ss_dtype
  2369.             AND c.length = isnull(d.fixlen, c.length)
  2370.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2371.             AND o.type <> 'P'
  2372.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2373.             AND c.xusertype = t.xusertype
  2374.             AND c.name like @column_name
  2375.         ORDER BY 17
  2376.     end
  2377.     else
  2378.     begin
  2379.         /* this block is for the case where there IS pattern
  2380.             matching done on the table name */
  2381.  
  2382.         if @table_owner is null /*    If owner not supplied, match all */
  2383.             select @table_owner = '%'
  2384.  
  2385.         set nocount on
  2386.  
  2387.         /* Store results into the temp table */
  2388.         insert into #columns
  2389.             (
  2390.             TABLE_QUALIFIER,
  2391.             TABLE_OWNER,
  2392.             TABLE_NAME,
  2393.             COLUMN_NAME,
  2394.             DATA_TYPE,
  2395.             TYPE_NAME,            
  2396.             "PRECISION",
  2397.             LENGTH,
  2398.             SCALE,
  2399.             RADIX,
  2400.             NULLABLE,
  2401.             REMARKS,
  2402.             COLUMN_DEF,
  2403.             SQL_DATA_TYPE,
  2404.             SQL_DATETIME_SUB,
  2405.             CHAR_OCTET_LENGTH,
  2406.             ORDINAL_POSITION,
  2407.             IS_NULLABLE,
  2408.             SS_DATA_TYPE
  2409.             )
  2410.         SELECT
  2411.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2412.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2413.             TABLE_NAME = convert(sysname,o.name),
  2414.             COLUMN_NAME = convert(sysname,c.name),
  2415.             d.DATA_TYPE,
  2416.             convert (sysname,case
  2417.                 when t.xusertype > 255 then t.name
  2418.                 else d.TYPE_NAME
  2419.             end) TYPE_NAME,
  2420.             convert(int,case
  2421.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2422.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2423.             end) "PRECISION",
  2424.             convert(int,case
  2425.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2426.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2427.                 else
  2428.                     isnull(d.length, c.length)
  2429.             end) LENGTH,
  2430.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2431.             d.RADIX,
  2432.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2433.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2434.             COLUMN_DEF = text,
  2435.             d.SQL_DATA_TYPE,
  2436.             d.SQL_DATETIME_SUB,
  2437.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2438.             ORDINAL_POSITION = convert(int,c.colid),
  2439.             IS_NULLABLE = convert(varchar(254),
  2440.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2441.             SS_DATA_TYPE = c.type
  2442.         FROM
  2443.             sysobjects o,
  2444.             master.dbo.spt_datatype_info d,
  2445.             systypes t,
  2446.             syscolumns c
  2447.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2448.                 AND m.colid = 1
  2449.         WHERE
  2450.             o.name like @table_name
  2451.             AND user_name(o.uid) like @table_owner
  2452.             AND o.id = c.id
  2453.             AND t.xtype = d.ss_dtype
  2454.             AND c.length = isnull(d.fixlen, c.length)
  2455.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2456.             AND o.type <> 'P'
  2457.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2458.             AND c.xusertype = t.xusertype
  2459.             AND c.name like @column_name
  2460.         ORDER BY 2, 3, 17
  2461.     end
  2462.     /* Fix ordinal positions */
  2463.     OPEN curColumns
  2464.     FETCH NEXT FROM curColumns
  2465.     INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2466.     
  2467.     /* Initialize tracking variables */
  2468.     SELECT @currQualifier = NULL, @currOwner = NULL, @currName = NULL
  2469.     WHILE @@FETCH_STATUS = 0
  2470.     BEGIN
  2471.         /* Check to see if the catalog/schema/table */
  2472.         /* has changed since last row               */
  2473.         IF (@currQualifier  + '.' + @currOwner  + '.' + @currName =
  2474.             @TABLEQUALIFIER + '.' + @TABLEOWNER + '.' + @TABLENAME)
  2475.         BEGIN
  2476.             /* Increment ordinal count */
  2477.             SELECT @currPos = @currPos + 1
  2478.         END
  2479.         ELSE
  2480.         BEGIN
  2481.             /* Set the catalog/schema/table to new values */
  2482.             SELECT @currQualifier = @TABLEQUALIFIER,
  2483.                    @currOwner     = @TABLEOWNER,
  2484.                    @currName      = @TABLENAME,
  2485.                    @currPos       = 1
  2486.         END
  2487.  
  2488.         /* Check for holes in the ordinal position order */
  2489.         IF (@currPos < @ORDINALPOSITION)
  2490.         BEGIN        
  2491.             /* Update the ordinal position */
  2492.             UPDATE #columns
  2493.             SET    ORDINAL_POSITION = @currPos
  2494.             WHERE  TABLE_QUALIFIER  = @TABLEQUALIFIER
  2495.               AND  TABLE_OWNER      = @TABLEOWNER
  2496.               AND  TABLE_NAME       = @TABLENAME
  2497.               AND  COLUMN_NAME      = @COLUMNNAME
  2498.         END
  2499.  
  2500.         /* Fetch next result */
  2501.         FETCH NEXT FROM curColumns
  2502.         INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2503.     END
  2504.  
  2505.     /* Cleanup cursor */
  2506.     CLOSE curColumns
  2507.     DEALLOCATE curColumns
  2508.  
  2509.     set nocount off
  2510.  
  2511.     /* Output the result set */
  2512.     select     TABLE_QUALIFIER,
  2513.         TABLE_OWNER,
  2514.         TABLE_NAME,
  2515.         COLUMN_NAME,
  2516.         DATA_TYPE,
  2517.         TYPE_NAME,
  2518.         "PRECISION",
  2519.         LENGTH,
  2520.         SCALE,
  2521.         RADIX,
  2522.         NULLABLE,
  2523.         REMARKS,
  2524.         COLUMN_DEF = convert(nvarchar(4000), COLUMN_DEF),
  2525.         SQL_DATA_TYPE,
  2526.         SQL_DATETIME_SUB,
  2527.         CHAR_OCTET_LENGTH,
  2528.         ORDINAL_POSITION,
  2529.         IS_NULLABLE,
  2530.         SS_DATA_TYPE
  2531.     from #columns
  2532.     order by 2, 3, 17
  2533. go
  2534.  
  2535. grant execute on sp_columns to public
  2536. go
  2537.  
  2538. dump tran master with no_log
  2539. go
  2540.  
  2541. print 'creating sp_databases'
  2542. go
  2543.  
  2544. /*    Procedure for pre-7.0 server */
  2545. create proc sp_databases
  2546. as
  2547.     set nocount on
  2548.     /* Use temporary table to sum up database size w/o using group by */
  2549.     create table #databases (
  2550.                   DATABASE_NAME varchar(32) NOT NULL,
  2551.                   size int NOT NULL)
  2552.  
  2553.     /* Insert row for each database */
  2554.     insert into #databases
  2555.         select
  2556.             name,
  2557.             (select sum(size) from master.dbo.sysusages
  2558.                 where dbid = d.dbid)
  2559.         from master.dbo.sysdatabases d
  2560.  
  2561.     select
  2562.          DATABASE_NAME,
  2563.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2564.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2565.     from #databases
  2566.     order by 1
  2567. go
  2568.  
  2569. if (charindex('7.00', @@version) = 0)
  2570. begin
  2571.     print ''
  2572.     print ''
  2573.     print 'Warning:'
  2574.     print 'you are installing the stored procedures '
  2575.     print 'on a pre 7.0 SQL Server.'
  2576.     print 'Ignore the following errors.'
  2577. end
  2578. else
  2579.     drop proc sp_databases
  2580. go
  2581.  
  2582. /*    Procedure for 7.0 server */
  2583. create proc sp_databases
  2584. as
  2585.     set nocount on
  2586.     declare @name sysname
  2587.     declare @SQL  nvarchar(600)
  2588.  
  2589.     /* Use temporary table to sum up database size w/o using group by */
  2590.     create table #databases (
  2591.                   DATABASE_NAME sysname NOT NULL,
  2592.                   size int NOT NULL)
  2593.  
  2594.     declare c1 cursor for 
  2595.         select name from master.dbo.sysdatabases
  2596.  
  2597.     open c1
  2598.     fetch c1 into @name
  2599.  
  2600.     while @@fetch_status >= 0
  2601.     begin
  2602.         select @SQL = 'insert into #databases
  2603.                 select N'''+ @name + ''', sum(size) from '
  2604.                 + QuoteName(@name) + '.dbo.sysfiles'
  2605.         /* Insert row for each database */
  2606.         execute (@SQL)
  2607.         fetch c1 into @name
  2608.     end
  2609.     deallocate c1
  2610.  
  2611.     select    
  2612.         DATABASE_NAME,
  2613.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2614.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2615.     from #databases
  2616.     order by 1
  2617. go
  2618.  
  2619. grant execute on sp_databases to public
  2620. go
  2621.  
  2622. dump tran master with no_log
  2623. go
  2624.  
  2625. print 'creating sp_datatype_info'
  2626. go
  2627.  
  2628. /*    Procedure for pre-6.0 server */
  2629. create proc sp_datatype_info
  2630.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2631. as
  2632.     if @ODBCVer <> 3
  2633.         select @ODBCVer = 2
  2634.     if @data_type = 0
  2635.         select
  2636.             TYPE_NAME = t.name,
  2637.             d.DATA_TYPE,
  2638.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2639.             d.LITERAL_PREFIX,
  2640.             d.LITERAL_SUFFIX,
  2641.             e.CREATE_PARAMS,
  2642.             d.NULLABLE,
  2643.             d.CASE_SENSITIVE,
  2644.             d.SEARCHABLE,
  2645.             d.UNSIGNED_ATTRIBUTE,
  2646.             d.MONEY,
  2647.             d.AUTO_INCREMENT,
  2648.             LOCAL_TYPE_NAME = t.name,
  2649.             MINIMUM_SCALE = d.numeric_scale,
  2650.             MAXIMUM_SCALE = d.numeric_scale,
  2651.             d.SQL_DATA_TYPE,
  2652.             d.SQL_DATETIME_SUB,
  2653.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2654.             INTERVAL_PRECISION = convert(smallint,NULL),
  2655.             USERTYPE = t.usertype
  2656.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2657.         where
  2658.             d.ss_dtype = t.type
  2659.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2660.             and t.usertype *= e.user_type
  2661.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2662.                 t.usertype > 100)
  2663.         order by 2, 12, 11, t.usertype
  2664.  
  2665.     else
  2666.         select
  2667.             TYPE_NAME = t.name,
  2668.             d.DATA_TYPE,
  2669.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2670.             d.LITERAL_PREFIX,
  2671.             d.LITERAL_SUFFIX,
  2672.             e.CREATE_PARAMS,
  2673.             d.NULLABLE,
  2674.             d.CASE_SENSITIVE,
  2675.             d.SEARCHABLE,
  2676.             d.UNSIGNED_ATTRIBUTE,
  2677.             d.MONEY,
  2678.             d.AUTO_INCREMENT,
  2679.             LOCAL_TYPE_NAME = t.name,
  2680.             MINIMUM_SCALE = d.numeric_scale,
  2681.             MAXIMUM_SCALE = d.numeric_scale,
  2682.             d.SQL_DATA_TYPE,
  2683.             d.SQL_DATETIME_SUB,
  2684.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2685.             INTERVAL_PRECISION = convert(smallint,NULL),
  2686.             USERTYPE = t.usertype
  2687.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2688.         where
  2689.             DATA_TYPE = @data_type
  2690.             and d.ss_dtype = t.type
  2691.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2692.             and t.usertype *= e.user_type
  2693.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2694.                 t.usertype > 100)
  2695.         order by 12, 11, t.usertype
  2696.  
  2697. go
  2698.  
  2699. if (charindex('6.00', @@version) = 0 and
  2700.     charindex('6.50', @@version) = 0 and
  2701.     charindex('7.00', @@version) = 0)
  2702. begin
  2703.     print ''
  2704.     print ''
  2705.     print 'Warning:'
  2706.     print 'you are installing the stored procedures '
  2707.     print 'on a pre 6.0 SQL Server.'
  2708.     print 'Ignore the following errors.'
  2709. end
  2710. else
  2711.     drop proc sp_datatype_info
  2712. go
  2713.  
  2714. /*    Procedure for 6.0 and 6.50 servers */
  2715. create proc sp_datatype_info
  2716.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2717. as
  2718.     if @ODBCVer <> 3
  2719.         select @ODBCVer = 2
  2720.     if @data_type = 0
  2721.         select
  2722.             convert(varchar(32),case
  2723.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2724.                 else d.TYPE_NAME
  2725.             end) TYPE_NAME,
  2726.             d.DATA_TYPE,
  2727.             convert(int,case
  2728.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2729.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2730.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2731.                 else t.prec
  2732.             end) "PRECISION",
  2733.             d.LITERAL_PREFIX,
  2734.             d.LITERAL_SUFFIX,
  2735.             e.CREATE_PARAMS,
  2736.             convert(smallint,case
  2737.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2738.                 else t.allownulls
  2739.             end) NULLABLE,
  2740.             d.CASE_SENSITIVE,
  2741.             d.SEARCHABLE,
  2742.             d.UNSIGNED_ATTRIBUTE,
  2743.             d.MONEY,
  2744.             d.AUTO_INCREMENT,
  2745.             convert(varchar(32),case
  2746.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2747.                 else d.TYPE_NAME
  2748.             end) LOCAL_TYPE_NAME,
  2749.             convert(smallint,case
  2750.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2751.                 else d.numeric_scale
  2752.             end) MINIMUM_SCALE,
  2753.             convert(smallint,case
  2754.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2755.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2756.                 else t.scale
  2757.             end) MAXIMUM_SCALE,
  2758.             d.SQL_DATA_TYPE,
  2759.             d.SQL_DATETIME_SUB,
  2760.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2761.             INTERVAL_PRECISION = convert(smallint,NULL),
  2762.             USERTYPE = t.usertype
  2763.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2764.         where
  2765.             d.ss_dtype = t.type
  2766.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2767.             and t.usertype *= e.user_type
  2768.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2769.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2770.                 t.usertype > 100)
  2771.             and (t.usertype <= 100 or
  2772.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2773.         order by 2, 12, 11, t.usertype
  2774.  
  2775.     else
  2776.         select
  2777.             convert(varchar(32),case
  2778.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2779.                 else d.TYPE_NAME
  2780.             end) TYPE_NAME,
  2781.             d.DATA_TYPE,
  2782.             convert(int,case
  2783.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2784.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2785.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2786.                 else t.prec
  2787.             end) "PRECISION",
  2788.             d.LITERAL_PREFIX,
  2789.             d.LITERAL_SUFFIX,
  2790.             e.CREATE_PARAMS,
  2791.             convert(smallint,case
  2792.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2793.                 else t.allownulls
  2794.             end) NULLABLE,
  2795.             d.CASE_SENSITIVE,
  2796.             d.SEARCHABLE,
  2797.             d.UNSIGNED_ATTRIBUTE,
  2798.             d.MONEY,
  2799.             d.AUTO_INCREMENT,
  2800.             convert(varchar(32),case
  2801.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2802.                 else d.TYPE_NAME
  2803.             end) LOCAL_TYPE_NAME,
  2804.             convert(smallint,case
  2805.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2806.                 else d.numeric_scale
  2807.             end) MINIMUM_SCALE,
  2808.             convert(smallint,case
  2809.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2810.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2811.                 else t.scale
  2812.             end) MAXIMUM_SCALE,
  2813.             d.SQL_DATA_TYPE,
  2814.             d.SQL_DATETIME_SUB,
  2815.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2816.             INTERVAL_PRECISION = convert(smallint,NULL),
  2817.             USERTYPE = t.usertype
  2818.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2819.         where
  2820.             d.DATA_TYPE = @data_type
  2821.             and d.ss_dtype = t.type
  2822.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2823.             and t.usertype *= e.user_type
  2824.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2825.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2826.                 t.usertype > 100)
  2827.             and (t.usertype <= 100 or
  2828.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2829.         order by 12, 11, t.usertype
  2830. go
  2831.  
  2832. if (charindex('7.00', @@version) = 0)
  2833. begin
  2834.     print ''
  2835.     print ''
  2836.     print 'Warning:'
  2837.     print 'you are installing the stored procedures '
  2838.     print 'on a pre 7.0 SQL Server.'
  2839.     print 'Ignore the following errors.'
  2840. end
  2841. else
  2842.     drop proc sp_datatype_info
  2843. go
  2844.  
  2845. /*    Procedure for 7.0 server */
  2846. create proc sp_datatype_info
  2847.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2848. as
  2849.     declare @mintype int
  2850.     declare @maxtype int
  2851.  
  2852.     if @ODBCVer <> 3
  2853.         select @ODBCVer = 2
  2854.     if @data_type = 0
  2855.     begin
  2856.         select @mintype = -32768
  2857.         select @maxtype = 32767
  2858.     end
  2859.     else
  2860.     begin
  2861.         select @mintype = @data_type
  2862.         select @maxtype = @data_type
  2863.     end
  2864.  
  2865.     select
  2866.         convert(sysname,case
  2867.             when t.xusertype > 255 then t.name
  2868.             else d.TYPE_NAME
  2869.         end) TYPE_NAME,
  2870.         d.DATA_TYPE,
  2871.         convert(int,case
  2872.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2873.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2874.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2875.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2876.         end) "PRECISION",
  2877.         d.LITERAL_PREFIX,
  2878.         d.LITERAL_SUFFIX,
  2879.         e.CREATE_PARAMS,
  2880.         convert(smallint,case
  2881.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2882.             else TypeProperty (t.name, 'AllowsNull')
  2883.         end) NULLABLE,
  2884.         d.CASE_SENSITIVE,
  2885.         d.SEARCHABLE,
  2886.         d.UNSIGNED_ATTRIBUTE,
  2887.         d.MONEY,
  2888.         d.AUTO_INCREMENT,
  2889.         convert(sysname,case
  2890.             when t.xusertype > 255 then t.name
  2891.             else d.LOCAL_TYPE_NAME
  2892.         end) LOCAL_TYPE_NAME,
  2893.         convert(smallint,case
  2894.             when type_name(d.ss_dtype) IN ('numeric','decimal') and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2895.             else d.numeric_scale
  2896.         end) MINIMUM_SCALE,
  2897.         convert(smallint,case
  2898.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2899.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2900.             else TypeProperty (t.name, 'Scale')
  2901.         end) MAXIMUM_SCALE,
  2902.         d.SQL_DATA_TYPE,
  2903.         d.SQL_DATETIME_SUB,
  2904.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2905.         INTERVAL_PRECISION = convert(smallint,NULL),
  2906.         USERTYPE = t.usertype
  2907.     from master.dbo.spt_datatype_info d
  2908.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2909.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2910.             t.xusertype = e.user_type
  2911.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2912.     where
  2913.         d.DATA_TYPE between @mintype and @maxtype
  2914.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2915.         and (t.xusertype <= 255 or
  2916.             isnull(d.AUTO_INCREMENT,0) = 0)
  2917.     order by 2, 12, 11,
  2918.     case
  2919.         when t.usertype=18 then 255
  2920.         else t.usertype
  2921.     end
  2922.  
  2923. go
  2924.  
  2925. grant execute on sp_datatype_info to public
  2926. go
  2927.  
  2928. dump tran master with no_log
  2929. go
  2930.  
  2931. print 'creating sp_fkeys'
  2932. go
  2933.  
  2934. /*    Procedure for pre-6.0 server */
  2935. CREATE PROCEDURE sp_fkeys(
  2936.                @pktable_name        varchar(32) = null,
  2937.                @pktable_owner        varchar(32) = null,
  2938.                @pktable_qualifier    varchar(32) = null,
  2939.                @fktable_name        varchar(32) = null,
  2940.                @fktable_owner        varchar(32) = null,
  2941.                @fktable_qualifier    varchar(32) = null )
  2942. as
  2943.     set nocount on
  2944.     declare    @order_by_pk int
  2945.  
  2946.     select  @order_by_pk = 0
  2947.  
  2948.     if (@pktable_name is null) and (@fktable_name is null)
  2949.     begin    /* If neither primary key nor foreign key table names given */
  2950.         raiserror 20004 '~~Rush_25~~'
  2951.         return
  2952.     end
  2953.     if @fktable_qualifier is not null
  2954.     begin
  2955.         if db_name() <> @fktable_qualifier
  2956.         begin    /* If qualifier doesn't match current database */
  2957.             raiserror 20001 '~~Rush_26~~'
  2958.             return
  2959.         end
  2960.     end
  2961.     if @pktable_qualifier is not null
  2962.     begin
  2963.         if db_name() <> @pktable_qualifier
  2964.         begin    /* If qualifier doesn't match current database */
  2965.             raiserror 20001 '~~Rush_27~~'
  2966.             return
  2967.         end
  2968.     end
  2969.  
  2970.     if @pktable_name is null
  2971.     begin /*  If table name not supplied, match all */
  2972.         select @pktable_name = '%'
  2973.         select @order_by_pk = 1
  2974.     end
  2975.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  2976.         select @pktable_owner = '%'
  2977.     if @fktable_name is null    /*    If table name not supplied, match all */
  2978.         select @fktable_name = '%'
  2979.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  2980.         select @fktable_owner = '%'
  2981.  
  2982.     if @@trancount <> 0
  2983.     begin    /* If inside a transaction */
  2984.         raiserror 20003 '~~Rush_28~~'
  2985.         return
  2986.     end
  2987.     create table #fkeys(
  2988.              PKTABLE_QUALIFIER    varchar(32) NULL,
  2989.              PKTABLE_OWNER        varchar(32) NULL,
  2990.              PKTABLE_NAME        varchar(32) NOT NULL,
  2991.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  2992.              FKTABLE_QUALIFIER    varchar(32) NULL,
  2993.              FKTABLE_OWNER        varchar(32) NULL,
  2994.              FKTABLE_NAME        varchar(32) NOT NULL,
  2995.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  2996.              KEY_SEQ            smallint NOT NULL)
  2997.  
  2998.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2999.     /*    Process syskeys for each relationship */
  3000.     /*    The inserts below adds a row to the temp table for each of the
  3001.         8 possible relationships */
  3002.     insert into #fkeys
  3003.         select
  3004.             db_name(),
  3005.             user_name(o1.uid),
  3006.             object_name(k.depid),
  3007.             c2.name,
  3008.             db_name(),
  3009.             user_name(o2.uid),
  3010.             object_name(k.id),
  3011.             c1.name,
  3012.             1
  3013.         from
  3014.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3015.         where
  3016.             c1.id = k.id
  3017.             and k.type = 2    /* Foreign type key */
  3018.             and c1.colid = k.key1
  3019.             and c2.id = k.depid
  3020.             and c2.colid = k.depkey1
  3021.             and o1.id = k.depid
  3022.             and o2.id = k.id
  3023.     union all
  3024.         select
  3025.             db_name(),
  3026.             user_name(o1.uid),
  3027.             object_name(k.depid),
  3028.             c2.name,
  3029.             db_name(),
  3030.             user_name(o2.uid),
  3031.             object_name(k.id),
  3032.             c1.name,
  3033.             2
  3034.         from
  3035.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3036.         where
  3037.             c1.id = k.id
  3038.             and k.type = 2    /* Foreign type key */
  3039.             and c1.colid = k.key2
  3040.             and c2.id = k.depid
  3041.             and c2.colid = k.depkey2
  3042.             and o1.id = k.depid
  3043.             and o2.id = k.id
  3044.     union all
  3045.         select
  3046.             db_name(),
  3047.             user_name(o1.uid),
  3048.             object_name(k.depid),
  3049.             c2.name,
  3050.             db_name(),
  3051.             user_name(o2.uid),
  3052.             object_name(k.id),
  3053.             c1.name,
  3054.             3
  3055.         from
  3056.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3057.         where
  3058.             c1.id = k.id
  3059.             and k.type = 2    /* Foreign type key */
  3060.             and c1.colid = k.key3
  3061.             and c2.id = k.depid
  3062.             and c2.colid = k.depkey3
  3063.             and o1.id = k.depid
  3064.             and o2.id = k.id
  3065.     union all
  3066.         select
  3067.             db_name(),
  3068.             user_name(o1.uid),
  3069.             object_name(k.depid),
  3070.             c2.name,
  3071.             db_name(),
  3072.             user_name(o2.uid),
  3073.             object_name(k.id),
  3074.             c1.name,
  3075.             4
  3076.         from
  3077.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3078.         where
  3079.             c1.id = k.id
  3080.             and k.type = 2    /* Foreign type key */
  3081.             and c1.colid = k.key4
  3082.             and c2.id = k.depid
  3083.             and c2.colid = k.depkey4
  3084.             and o1.id = k.depid
  3085.             and o2.id = k.id
  3086.     union all
  3087.         select
  3088.             db_name(),
  3089.             user_name(o1.uid),
  3090.             object_name(k.depid),
  3091.             c2.name,
  3092.             db_name(),
  3093.             user_name(o2.uid),
  3094.             object_name(k.id),
  3095.             c1.name,
  3096.             5
  3097.         from
  3098.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3099.         where
  3100.             c1.id = k.id
  3101.             and k.type = 2    /* Foreign type key */
  3102.             and c1.colid = k.key5
  3103.             and c2.id = k.depid
  3104.             and c2.colid = k.depkey5
  3105.             and o1.id = k.depid
  3106.             and o2.id = k.id
  3107.     union all
  3108.         select
  3109.             db_name(),
  3110.             user_name(o1.uid),
  3111.             object_name(k.depid),
  3112.             c2.name,
  3113.             db_name(),
  3114.             user_name(o2.uid),
  3115.             object_name(k.id),
  3116.             c1.name,
  3117.             6
  3118.         from
  3119.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3120.         where
  3121.             c1.id = k.id
  3122.             and k.type = 2    /* Foreign type key */
  3123.             and c1.colid = k.key6
  3124.             and c2.id = k.depid
  3125.             and c2.colid = k.depkey6
  3126.             and o1.id = k.depid
  3127.             and o2.id = k.id
  3128.     union all
  3129.         select
  3130.             db_name(),
  3131.             user_name(o1.uid),
  3132.             object_name(k.depid),
  3133.             c2.name,
  3134.             db_name(),
  3135.             user_name(o2.uid),
  3136.             object_name(k.id),
  3137.             c1.name,
  3138.             7
  3139.         from
  3140.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3141.         where
  3142.             c1.id = k.id
  3143.             and k.type = 2    /* Foreign type key */
  3144.             and c1.colid = k.key7
  3145.             and c2.id = k.depid
  3146.             and c2.colid = k.depkey7
  3147.             and o1.id = k.depid
  3148.             and o2.id = k.id
  3149.     union all
  3150.         select
  3151.             db_name(),
  3152.             user_name(o1.uid),
  3153.             object_name(k.depid),
  3154.             c2.name,
  3155.             db_name(),
  3156.             user_name(o2.uid),
  3157.             object_name(k.id),
  3158.             c1.name,
  3159.             8
  3160.         from
  3161.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3162.         where
  3163.             c1.id = k.id
  3164.             and k.type = 2    /* Foreign type key */
  3165.             and c1.colid = k.key8
  3166.             and c2.id = k.depid
  3167.             and c2.colid = k.depkey8
  3168.             and o1.id = k.depid
  3169.             and o2.id = k.id
  3170.  
  3171.     if @order_by_pk = 1 /*    If order by PK fields */
  3172.         select
  3173.             PKTABLE_QUALIFIER,
  3174.             PKTABLE_OWNER,
  3175.             PKTABLE_NAME,
  3176.             PKCOLUMN_NAME,
  3177.             FKTABLE_QUALIFIER,
  3178.             FKTABLE_OWNER,
  3179.             FKTABLE_NAME,
  3180.             FKCOLUMN_NAME,
  3181.             KEY_SEQ,
  3182.             UPDATE_RULE = convert(smallint, null),
  3183.             DELETE_RULE = convert(smallint,null),
  3184.             FK_NAME = convert(varchar(32),null),
  3185.             PK_NAME = convert(varchar(32),null),
  3186.             DEFERRABILITY = convert(smallint,7)
  3187.         from #fkeys
  3188.         where FKTABLE_NAME like @fktable_name
  3189.             and FKTABLE_OWNER like @fktable_owner
  3190.             and PKTABLE_NAME  like @pktable_name
  3191.             and PKTABLE_OWNER like @pktable_owner
  3192.         order by 1, 2, 3, 9
  3193.     else        /*    Order by FK fields */
  3194.         select
  3195.             PKTABLE_QUALIFIER,
  3196.             PKTABLE_OWNER,
  3197.             PKTABLE_NAME,
  3198.             PKCOLUMN_NAME,
  3199.             FKTABLE_QUALIFIER,
  3200.             FKTABLE_OWNER,
  3201.             FKTABLE_NAME,
  3202.             FKCOLUMN_NAME,
  3203.             KEY_SEQ,
  3204.             UPDATE_RULE = convert(smallint,null),
  3205.             DELETE_RULE = convert(smallint,null),
  3206.             FK_NAME = convert(varchar(32),null),
  3207.             PK_NAME = convert(varchar(32),null),
  3208.             DEFERRABILITY = convert(smallint,7)
  3209.         from #fkeys
  3210.         where FKTABLE_NAME like @fktable_name
  3211.             and FKTABLE_OWNER like @fktable_owner
  3212.             and PKTABLE_NAME  like @pktable_name
  3213.             and PKTABLE_OWNER like @pktable_owner
  3214.         order by 5, 6, 7, 9
  3215. go
  3216.  
  3217. if (charindex('6.00', @@version) = 0 and
  3218.     charindex('6.50', @@version) = 0 and
  3219.     charindex('7.00', @@version) = 0)
  3220. begin
  3221.     print ''
  3222.     print ''
  3223.     print 'Warning:'
  3224.     print 'you are installing the stored procedures '
  3225.     print 'on a pre 6.0 SQL Server.'
  3226.     print 'Ignore the following errors.'
  3227. end
  3228. else
  3229.     drop proc sp_fkeys
  3230. go
  3231.  
  3232. /*    Procedure for 6.0 and 6.50 servers */
  3233. CREATE PROCEDURE sp_fkeys(
  3234.                @pktable_name        varchar(32) = null,
  3235.                @pktable_owner        varchar(32) = null,
  3236.                @pktable_qualifier    varchar(32) = null,
  3237.                @fktable_name        varchar(32) = null,
  3238.                @fktable_owner        varchar(32) = null,
  3239.                @fktable_qualifier    varchar(32) = null )
  3240. as
  3241.     set nocount on
  3242.     DECLARE @pktable_id            int
  3243.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  3244.     DECLARE @fktable_id            int
  3245.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  3246.     declare    @order_by_pk        int
  3247.  
  3248.     select  @order_by_pk = 0
  3249.  
  3250.     if (@pktable_name is null) and (@fktable_name is null)
  3251.     begin    /* If neither primary key nor foreign key table names given */
  3252.         raiserror (15252,-1,-1)
  3253.         return
  3254.     end
  3255.     if @fktable_qualifier is not null
  3256.     begin
  3257.         if db_name() <> @fktable_qualifier
  3258.         begin    /* If qualifier doesn't match current database */
  3259.             raiserror (15250, -1,-1)
  3260.             return
  3261.         end
  3262.     end
  3263.     if @pktable_qualifier is not null
  3264.     begin
  3265.         if db_name() <> @pktable_qualifier
  3266.         begin    /* If qualifier doesn't match current database */
  3267.             raiserror (15250, -1,-1)
  3268.             return
  3269.         end
  3270.     end
  3271.  
  3272.     if @pktable_owner is null
  3273.     begin    /* If unqualified primary key table name */
  3274.         SELECT @pkfull_table_name = @pktable_name
  3275.     end
  3276.     else
  3277.     begin    /* Qualified primary key table name */
  3278.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3279.     end
  3280.     /*    Get Object ID */
  3281.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3282.  
  3283.     if @fktable_owner is null
  3284.     begin    /* If unqualified foreign key table name */
  3285.         SELECT @fkfull_table_name = @fktable_name
  3286.     end
  3287.     else
  3288.     begin    /* Qualified foreign key table name */
  3289.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3290.     end
  3291.     /*    Get Object ID */
  3292.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3293.  
  3294.     if @fktable_name is not null
  3295.     begin
  3296.         if @fktable_id is null
  3297.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3298.     end
  3299.  
  3300.     if @pktable_name is null
  3301.     begin /*  If table name not supplied, match all */
  3302.         select @order_by_pk = 1
  3303.     end
  3304.     else
  3305.     begin
  3306.         if @pktable_id is null
  3307.         begin
  3308.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3309.         end
  3310.     end
  3311.  
  3312.     if (@@trancount <> 0 and
  3313.         charindex('6.50', @@version) = 0)
  3314.     begin    /* If inside a transaction */
  3315.         raiserror (15002,-1,-1,'sp_fkeys')
  3316.         return
  3317.     end
  3318.  
  3319.     create table #fkeys(
  3320.              pkdb_id        int NOT NULL,
  3321.              pktable_id     int NOT NULL,
  3322.              pkcolid        int NOT NULL,
  3323.              fkdb_id        int NOT NULL,
  3324.              fktable_id        int NOT NULL,
  3325.              fkcolid        int NOT NULL,
  3326.              KEY_SEQ        smallint NOT NULL,
  3327.              fk_id            int NOT NULL,
  3328.              pk_id            int NOT NULL)
  3329.  
  3330.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3331.     /*    Process syskeys for each relationship */
  3332.     /*    The inserts below adds a row to the temp table for each of the
  3333.         16 possible relationships */
  3334.     insert into #fkeys
  3335.         select
  3336.             r.rkeydbid,
  3337.             r.rkeyid,
  3338.             r.rkey1,
  3339.             r.fkeydbid,
  3340.             r.fkeyid,
  3341.             r.fkey1,
  3342.             1,
  3343.             r.constid,
  3344.             s.constid
  3345.         from
  3346.             sysreferences r, sysconstraints s
  3347.         where    r.rkeyid = s.id
  3348.             AND (s.status & 0xf) = 1
  3349.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3350.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3351.       union all
  3352.         select
  3353.             r.rkeydbid,
  3354.             r.rkeyid,
  3355.             r.rkey2,
  3356.             r.fkeydbid,
  3357.             r.fkeyid,
  3358.             r.fkey2,
  3359.             2,
  3360.             r.constid,
  3361.             s.constid
  3362.         from
  3363.             sysreferences r, sysconstraints s
  3364.         where    r.rkeyid = s.id
  3365.             AND (s.status & 0xf) = 1
  3366.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3367.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3368.       union all
  3369.         select
  3370.             r.rkeydbid,
  3371.             r.rkeyid,
  3372.             r.rkey3,
  3373.             r.fkeydbid,
  3374.             r.fkeyid,
  3375.             r.fkey3,
  3376.             3,
  3377.             r.constid,
  3378.             s.constid
  3379.         from
  3380.             sysreferences r, sysconstraints s
  3381.         where    r.rkeyid = s.id
  3382.             AND (s.status & 0xf) = 1
  3383.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3384.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3385.       union all
  3386.         select
  3387.             r.rkeydbid,
  3388.             r.rkeyid,
  3389.             r.rkey4,
  3390.             r.fkeydbid,
  3391.             r.fkeyid,
  3392.             r.fkey4,
  3393.             4,
  3394.             r.constid,
  3395.             s.constid
  3396.         from
  3397.             sysreferences r, sysconstraints s
  3398.         where    r.rkeyid = s.id
  3399.             AND (s.status & 0xf) = 1
  3400.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3401.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3402.       union all
  3403.         select
  3404.             r.rkeydbid,
  3405.             r.rkeyid,
  3406.             r.rkey5,
  3407.             r.fkeydbid,
  3408.             r.fkeyid,
  3409.             r.fkey5,
  3410.             5,
  3411.             r.constid,
  3412.             s.constid
  3413.         from
  3414.             sysreferences r, sysconstraints s
  3415.         where    r.rkeyid = s.id
  3416.             AND (s.status & 0xf) = 1
  3417.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3418.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3419.       union all
  3420.         select
  3421.             r.rkeydbid,
  3422.             r.rkeyid,
  3423.             r.rkey6,
  3424.             r.fkeydbid,
  3425.             r.fkeyid,
  3426.             r.fkey6,
  3427.             6,
  3428.             r.constid,
  3429.             s.constid
  3430.         from
  3431.             sysreferences r, sysconstraints s
  3432.         where    r.rkeyid = s.id
  3433.             AND (s.status & 0xf) = 1
  3434.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3435.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3436.       union all
  3437.         select
  3438.             r.rkeydbid,
  3439.             r.rkeyid,
  3440.             r.rkey7,
  3441.             r.fkeydbid,
  3442.             r.fkeyid,
  3443.             r.fkey7,
  3444.             7,
  3445.             r.constid,
  3446.             s.constid
  3447.         from
  3448.             sysreferences r, sysconstraints s
  3449.         where    r.rkeyid = s.id
  3450.             AND (s.status & 0xf) = 1
  3451.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3452.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3453.       union all
  3454.         select
  3455.             r.rkeydbid,
  3456.             r.rkeyid,
  3457.             r.rkey8,
  3458.             r.fkeydbid,
  3459.             r.fkeyid,
  3460.             r.fkey8,
  3461.             8,
  3462.             r.constid,
  3463.             s.constid
  3464.         from
  3465.             sysreferences r, sysconstraints s
  3466.         where    r.rkeyid = s.id
  3467.             AND (s.status & 0xf) = 1
  3468.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3469.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3470.       union all
  3471.         select
  3472.             r.rkeydbid,
  3473.             r.rkeyid,
  3474.             r.rkey9,
  3475.             r.fkeydbid,
  3476.             r.fkeyid,
  3477.             r.fkey9,
  3478.             9,
  3479.             r.constid,
  3480.             s.constid
  3481.         from
  3482.             sysreferences r, sysconstraints s
  3483.         where    r.rkeyid = s.id
  3484.             AND (s.status & 0xf) = 1
  3485.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3486.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3487.       union all
  3488.         select
  3489.             r.rkeydbid,
  3490.             r.rkeyid,
  3491.             r.rkey10,
  3492.             r.fkeydbid,
  3493.             r.fkeyid,
  3494.             r.fkey10,
  3495.             10,
  3496.             r.constid,
  3497.             s.constid
  3498.         from
  3499.             sysreferences r, sysconstraints s
  3500.         where    r.rkeyid = s.id
  3501.             AND (s.status & 0xf) = 1
  3502.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3503.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3504.       union all
  3505.         select
  3506.             r.rkeydbid,
  3507.             r.rkeyid,
  3508.             r.rkey11,
  3509.             r.fkeydbid,
  3510.             r.fkeyid,
  3511.             r.fkey11,
  3512.             11,
  3513.             r.constid,
  3514.             s.constid
  3515.         from
  3516.             sysreferences r, sysconstraints s
  3517.         where    r.rkeyid = s.id
  3518.             AND (s.status & 0xf) = 1
  3519.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3520.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3521.       union all
  3522.         select
  3523.             r.rkeydbid,
  3524.             r.rkeyid,
  3525.             r.rkey12,
  3526.             r.fkeydbid,
  3527.             r.fkeyid,
  3528.             r.fkey12,
  3529.             12,
  3530.             r.constid,
  3531.             s.constid
  3532.         from
  3533.             sysreferences r, sysconstraints s
  3534.         where    r.rkeyid = s.id
  3535.             AND (s.status & 0xf) = 1
  3536.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3537.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3538.       union all
  3539.         select
  3540.             r.rkeydbid,
  3541.             r.rkeyid,
  3542.             r.rkey13,
  3543.             r.fkeydbid,
  3544.             r.fkeyid,
  3545.             r.fkey13,
  3546.             13,
  3547.             r.constid,
  3548.             s.constid
  3549.         from
  3550.             sysreferences r, sysconstraints s
  3551.         where    r.rkeyid = s.id
  3552.             AND (s.status & 0xf) = 1
  3553.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3554.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3555.       union all
  3556.         select
  3557.             r.rkeydbid,
  3558.             r.rkeyid,
  3559.             r.rkey14,
  3560.             r.fkeydbid,
  3561.             r.fkeyid,
  3562.             r.fkey14,
  3563.             14,
  3564.             r.constid,
  3565.             s.constid
  3566.         from
  3567.             sysreferences r, sysconstraints s
  3568.         where    r.rkeyid = s.id
  3569.             AND (s.status & 0xf) = 1
  3570.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3571.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3572.       union all
  3573.         select
  3574.             r.rkeydbid,
  3575.             r.rkeyid,
  3576.             r.rkey15,
  3577.             r.fkeydbid,
  3578.             r.fkeyid,
  3579.             r.fkey15,
  3580.             15,
  3581.             r.constid,
  3582.             s.constid
  3583.         from
  3584.             sysreferences r, sysconstraints s
  3585.         where    r.rkeyid = s.id
  3586.             AND (s.status & 0xf) = 1
  3587.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3588.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3589.       union all
  3590.         select
  3591.             r.rkeydbid,
  3592.             r.rkeyid,
  3593.             r.rkey16,
  3594.             r.fkeydbid,
  3595.             r.fkeyid,
  3596.             r.fkey16,
  3597.             16,
  3598.             r.constid,
  3599.             s.constid
  3600.         from
  3601.             sysreferences r, sysconstraints s
  3602.         where    r.rkeyid = s.id
  3603.             AND (s.status & 0xf) = 1
  3604.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3605.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3606.  
  3607.     if @order_by_pk = 1 /*    If order by PK fields */
  3608.         select
  3609.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3610.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3611.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3612.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3613.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3614.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3615.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3616.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3617.             KEY_SEQ,
  3618.             UPDATE_RULE = convert(smallint,1),
  3619.             DELETE_RULE = convert(smallint,1),
  3620.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3621.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3622.             DEFERRABILITY = convert(smallint,7)
  3623.         from #fkeys f,
  3624.             sysobjects o1, sysobjects o2,
  3625.             syscolumns c1, syscolumns c2
  3626.         where    o1.id = f.pktable_id
  3627.             AND o2.id = f.fktable_id
  3628.             AND c1.id = f.pktable_id
  3629.             AND c2.id = f.fktable_id
  3630.             AND c1.colid = f.pkcolid
  3631.             AND c2.colid = f.fkcolid
  3632.         order by 1,2,3,9
  3633.     else        /*    Order by FK fields */
  3634.         select
  3635.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3636.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3637.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3638.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3639.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3640.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3641.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3642.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3643.             KEY_SEQ,
  3644.             UPDATE_RULE = convert(smallint,1),
  3645.             DELETE_RULE = convert(smallint,1),
  3646.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3647.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3648.             DEFERRABILITY = convert(smallint,7)
  3649.         from #fkeys f,
  3650.             sysobjects o1, sysobjects o2,
  3651.             syscolumns c1, syscolumns c2
  3652.         where    o1.id = f.pktable_id
  3653.             AND o2.id = f.fktable_id
  3654.             AND c1.id = f.pktable_id
  3655.             AND c2.id = f.fktable_id
  3656.             AND c1.colid = f.pkcolid
  3657.             AND c2.colid = f.fkcolid
  3658.         order by 5,6,7,9
  3659. go
  3660.  
  3661. if (charindex('7.00', @@version) = 0)
  3662. begin
  3663.     print ''
  3664.     print ''
  3665.     print 'Warning:'
  3666.     print 'you are installing the stored procedures '
  3667.     print 'on a pre 7.0 SQL Server.'
  3668.     print 'Ignore the following errors.'
  3669. end
  3670. else
  3671.     drop proc sp_fkeys
  3672. go
  3673.  
  3674. /*    Procedure for 7.0 and later servers */
  3675. CREATE PROCEDURE sp_fkeys(
  3676.                @pktable_name        sysname = null,
  3677.                @pktable_owner        sysname = null,
  3678.                @pktable_qualifier    sysname = null,
  3679.                @fktable_name        sysname = null,
  3680.                @fktable_owner        sysname = null,
  3681.                @fktable_qualifier    sysname = null )
  3682. as
  3683.     set nocount on
  3684.     DECLARE @pktable_id            int
  3685.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3686.     DECLARE @fktable_id            int
  3687.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3688.     declare    @order_by_pk        int
  3689.  
  3690.     /* select 'XXX starting table creation' */
  3691.  
  3692.     create table #fkeysall(
  3693.             rkeyid int NOT NULL,
  3694.             rkey1 int NOT NULL,
  3695.                 rkey2 int NOT NULL,
  3696.                 rkey3 int NOT NULL,
  3697.                 rkey4 int NOT NULL,
  3698.                 rkey5 int NOT NULL,
  3699.                 rkey6 int NOT NULL,
  3700.                 rkey7 int NOT NULL,
  3701.                 rkey8 int NOT NULL,
  3702.                 rkey9 int NOT NULL,
  3703.                 rkey10 int NOT NULL,
  3704.                 rkey11 int NOT NULL,
  3705.                 rkey12 int NOT NULL,
  3706.                 rkey13 int NOT NULL,
  3707.                 rkey14 int NOT NULL,
  3708.                 rkey15 int NOT NULL,
  3709.                 rkey16 int NOT NULL,
  3710.             fkeyid int NOT NULL,
  3711.             fkey1 int NOT NULL,
  3712.                 fkey2 int NOT NULL,
  3713.                 fkey3 int NOT NULL,
  3714.                 fkey4 int NOT NULL,
  3715.                 fkey5 int NOT NULL,
  3716.                 fkey6 int NOT NULL,
  3717.                 fkey7 int NOT NULL,
  3718.                 fkey8 int NOT NULL,
  3719.                 fkey9 int NOT NULL,
  3720.                 fkey10 int NOT NULL,
  3721.                 fkey11 int NOT NULL,
  3722.                 fkey12 int NOT NULL,
  3723.                 fkey13 int NOT NULL,
  3724.                 fkey14 int NOT NULL,
  3725.                 fkey15 int NOT NULL,
  3726.                 fkey16 int NOT NULL,
  3727.             constid int NOT NULL,
  3728.             name sysname NOT NULL)
  3729.  
  3730.     create table #fkeys(
  3731.             pktable_id        int NOT NULL,
  3732.             pkcolid         int NOT NULL,
  3733.             fktable_id        int NOT NULL,
  3734.             fkcolid         int NOT NULL,
  3735.             KEY_SEQ         smallint NOT NULL,
  3736.             fk_id            int NOT NULL,
  3737.             PK_NAME            sysname NOT NULL)
  3738.  
  3739.     create table #fkeysout(
  3740.             PKTABLE_QUALIFIER sysname NULL,
  3741.             PKTABLE_OWNER sysname NULL,
  3742.             PKTABLE_NAME sysname NOT NULL,
  3743.             PKCOLUMN_NAME sysname NOT NULL,
  3744.             FKTABLE_QUALIFIER sysname NULL,
  3745.             FKTABLE_OWNER sysname NULL,
  3746.             FKTABLE_NAME sysname NOT NULL,
  3747.             FKCOLUMN_NAME sysname NOT NULL,
  3748.             KEY_SEQ smallint NOT NULL,
  3749.             UPDATE_RULE smallint NULL,
  3750.             DELETE_RULE smallint NULL,
  3751.             FK_NAME sysname NULL,
  3752.             PK_NAME sysname NULL,
  3753.             DEFERRABILITY smallint null)
  3754.  
  3755.     /* select 'XXX starting parameter analysis' */
  3756.  
  3757.     select  @order_by_pk = 0
  3758.  
  3759.     if (@pktable_name is null) and (@fktable_name is null)
  3760.     begin    /* If neither primary key nor foreign key table names given */
  3761.         raiserror (15252,-1,-1)
  3762.         return
  3763.     end
  3764.     if @fktable_qualifier is not null
  3765.     begin
  3766.         if db_name() <> @fktable_qualifier
  3767.         begin    /* If qualifier doesn't match current database */
  3768.             raiserror (15250, -1,-1)
  3769.             return
  3770.         end
  3771.     end
  3772.     if @pktable_qualifier is not null
  3773.     begin
  3774.         if db_name() <> @pktable_qualifier
  3775.         begin    /* If qualifier doesn't match current database */
  3776.             raiserror (15250, -1,-1)
  3777.             return
  3778.         end
  3779.     end
  3780.  
  3781.     if @pktable_owner is null
  3782.     begin    /* If unqualified primary key table name */
  3783.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3784.     end
  3785.     else
  3786.     begin    /* Qualified primary key table name */
  3787.         if @pktable_owner = ''
  3788.         begin    /* If empty owner name */
  3789.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3790.         end
  3791.         else
  3792.         begin
  3793.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3794.                 '.' + quotename(@pktable_name)
  3795.         end
  3796.     end
  3797.     /*    Get Object ID */
  3798.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3799.  
  3800.     if @fktable_owner is null
  3801.     begin    /* If unqualified foreign key table name */
  3802.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3803.     end
  3804.     else
  3805.     begin    /* Qualified foreign key table name */
  3806.         if @fktable_owner = ''
  3807.         begin    /* If empty owner name */
  3808.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3809.         end
  3810.         else
  3811.         begin
  3812.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3813.                 '.' + quotename(@fktable_name)
  3814.         end
  3815.     end
  3816.     /*    Get Object ID */
  3817.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3818.  
  3819.     if @fktable_name is not null
  3820.     begin
  3821.         if @fktable_id is null
  3822.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3823.     end
  3824.  
  3825.     if @pktable_name is null
  3826.     begin /*  If table name not supplied, match all */
  3827.         select @order_by_pk = 1
  3828.     end
  3829.     else
  3830.     begin
  3831.         if @pktable_id is null
  3832.         begin
  3833.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3834.         end
  3835.     end
  3836.  
  3837.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3838.     /*    Process syskeys for each relationship */
  3839.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3840.         them out with a 16-way "insert select ... union select ..." */
  3841.  
  3842.     /* select 'XXX starting data analysis' */
  3843.  
  3844.     insert into #fkeysall
  3845.         select
  3846.             r.rkeyid,
  3847.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3848.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3849.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3850.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3851.             r.fkeyid,
  3852.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3853.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3854.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3855.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3856.             r.constid,
  3857.             i.name
  3858.         from
  3859.             sysreferences r, sysobjects o, sysindexes i
  3860.         where    r.constid = o.id
  3861.             AND o.xtype = 'F'
  3862.             AND r.rkeyindid = i.indid
  3863.             AND r.rkeyid = i.id
  3864.             AND r.rkeyid between isnull(@pktable_id, 0)
  3865.                             and isnull(@pktable_id, 0x7fffffff)
  3866.             AND r.fkeyid between isnull(@fktable_id, 0)
  3867.                             and isnull(@fktable_id, 0x7fffffff)
  3868.  
  3869.     /* select count (*) as 'XXX countall' from #fkeysall */
  3870.  
  3871.     insert into #fkeys
  3872.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3873.             from #fkeysall
  3874.         union all
  3875.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3876.             from #fkeysall
  3877.         union all
  3878.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3879.             from #fkeysall
  3880.         union all
  3881.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3882.             from #fkeysall
  3883.         union all
  3884.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3885.             from #fkeysall
  3886.         union all
  3887.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3888.             from #fkeysall
  3889.         union all
  3890.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3891.             from #fkeysall
  3892.         union all
  3893.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3894.             from #fkeysall
  3895.         union all
  3896.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3897.             from #fkeysall
  3898.         union all
  3899.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3900.             from #fkeysall
  3901.         union all
  3902.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3903.             from #fkeysall
  3904.         union all
  3905.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3906.             from #fkeysall
  3907.         union all
  3908.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3909.             from #fkeysall
  3910.         union all
  3911.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3912.             from #fkeysall
  3913.         union all
  3914.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3915.             from #fkeysall
  3916.         union all
  3917.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3918.             from #fkeysall
  3919.  
  3920.     /* select count (*) as 'XXX count' from #fkeys */
  3921.  
  3922.     insert into #fkeysout
  3923.         select
  3924.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3925.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3926.             PKTABLE_NAME = convert(sysname,o1.name),
  3927.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3928.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3929.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3930.             FKTABLE_NAME = convert(sysname,o2.name),
  3931.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3932.             KEY_SEQ,
  3933.             UPDATE_RULE = convert(smallint,1),
  3934.             DELETE_RULE = convert(smallint,1),
  3935.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3936.             PK_NAME,
  3937.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  3938.         from #fkeys f,
  3939.             sysobjects o1, sysobjects o2,
  3940.             syscolumns c1, syscolumns c2
  3941.         where    o1.id = f.pktable_id
  3942.             AND o2.id = f.fktable_id
  3943.             AND c1.id = f.pktable_id
  3944.             AND c2.id = f.fktable_id
  3945.             AND c1.colid = f.pkcolid
  3946.             AND c2.colid = f.fkcolid
  3947.  
  3948.     /* select count (*) as 'XXX countout' from #fkeysout */
  3949.  
  3950.     if @order_by_pk = 1 /*    If order by PK fields */
  3951.         select
  3952.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3953.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3954.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3955.         from #fkeysout
  3956.         order by 1,2,3,9
  3957.     else        /*    Order by FK fields */
  3958.         select
  3959.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3960.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3961.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3962.         from #fkeysout
  3963.         order by 5,6,7,9
  3964. go
  3965.  
  3966. grant execute on sp_fkeys to public
  3967. go
  3968.  
  3969. dump tran master with no_log
  3970. go
  3971.  
  3972. print 'creating sp_pkeys'
  3973. go
  3974.  
  3975. /*    Procedure for pre-6.0 server */
  3976. CREATE PROCEDURE sp_pkeys(
  3977.                @table_name        varchar(32),
  3978.                @table_owner     varchar(32) = null,
  3979.                @table_qualifier varchar(32) = null )
  3980. as
  3981.     set nocount on
  3982.     if @table_qualifier is not null
  3983.     begin
  3984.         if db_name() <> @table_qualifier
  3985.         begin    /* If qualifier doesn't match current database */
  3986.             raiserror 20001 '~~Rush_5~~'
  3987.             return
  3988.         end
  3989.     end
  3990.     if @table_owner is null /*    If owner not supplied, match all */
  3991.         select @table_owner = '%'
  3992.     if @@trancount <> 0
  3993.     begin    /* If inside a transaction */
  3994.         raiserror 20003 '~~Rush_35~~'
  3995.         return
  3996.     end
  3997.  
  3998.     create table #pkeys(
  3999.              TABLE_QUALIFIER varchar(32) NULL,
  4000.              TABLE_OWNER     varchar(32) NULL,
  4001.              TABLE_NAME      varchar(32) NOT NULL,
  4002.              COLUMN_NAME     varchar(32) NOT NULL,
  4003.              KEY_SEQ         smallint NOT NULL)
  4004.  
  4005.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  4006.     /*    Process syskeys for each relationship */
  4007.     /*    The inserts below adds a row to the temp table for each of the
  4008.         8 possible relationships */
  4009.     insert into #pkeys
  4010.         select
  4011.             db_name(),
  4012.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4013.             object_name(k.id),
  4014.             c.name,
  4015.             1
  4016.         from
  4017.             syskeys k, syscolumns c
  4018.         where
  4019.             c.id = k.id
  4020.             and k.type = 1    /* Primary type key */
  4021.             and c.colid = k.key1
  4022.     if (@@rowcount = 0)
  4023.         goto done
  4024.  
  4025.     insert into #pkeys
  4026.         select
  4027.             db_name(),
  4028.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4029.             object_name(k.id),
  4030.             c.name,
  4031.             2
  4032.         from
  4033.             syskeys k, syscolumns c
  4034.         where
  4035.             c.id = k.id
  4036.             and k.type = 1    /* Primary type key */
  4037.             and c.colid = key2
  4038.     if (@@rowcount = 0)
  4039.         goto done
  4040.  
  4041.     insert into #pkeys
  4042.         select
  4043.             db_name(),
  4044.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4045.             object_name(k.id),
  4046.             c.name,
  4047.             3
  4048.         from
  4049.             syskeys k, syscolumns c
  4050.         where
  4051.             c.id = k.id
  4052.             and k.type = 1    /* Primary type key */
  4053.             and c.colid = key3
  4054.     if (@@rowcount = 0)
  4055.         goto done
  4056.  
  4057.     insert into #pkeys
  4058.         select
  4059.             db_name(),
  4060.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4061.             object_name(k.id),
  4062.             c.name,
  4063.             4
  4064.         from
  4065.             syskeys k, syscolumns c
  4066.         where
  4067.             c.id = k.id
  4068.             and k.type = 1    /* Primary type key */
  4069.             and c.colid = key4
  4070.     if (@@rowcount = 0)
  4071.         goto done
  4072.  
  4073.     insert into #pkeys
  4074.         select
  4075.             db_name(),
  4076.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4077.             object_name(k.id),
  4078.             c.name,
  4079.             5
  4080.         from
  4081.             syskeys k, syscolumns c
  4082.         where
  4083.             c.id = k.id
  4084.             and k.type = 1    /* Primary type key */
  4085.             and c.colid = key5
  4086.     if (@@rowcount = 0)
  4087.         goto done
  4088.  
  4089.     insert into #pkeys
  4090.         select
  4091.             db_name(),
  4092.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4093.             object_name(k.id),
  4094.             c.name,
  4095.             6
  4096.         from
  4097.             syskeys k, syscolumns c
  4098.         where
  4099.             c.id = k.id
  4100.             and k.type = 1    /* Primary type key */
  4101.             and c.colid = key6
  4102.     if (@@rowcount = 0)
  4103.         goto done
  4104.  
  4105.     insert into #pkeys
  4106.         select
  4107.             db_name(),
  4108.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4109.             object_name(k.id),
  4110.             c.name,
  4111.             7
  4112.         from
  4113.             syskeys k, syscolumns c
  4114.         where
  4115.             c.id = k.id
  4116.             and k.type = 1    /* Primary type key */
  4117.             and c.colid = key7
  4118.     if (@@rowcount = 0)
  4119.         goto done
  4120.  
  4121.     insert into #pkeys
  4122.          select
  4123.              db_name(),
  4124.              (select user_name(uid) from sysobjects o where o.id = k.id),
  4125.              object_name(k.id),
  4126.              c.name,
  4127.              8
  4128.          from
  4129.              syskeys k, syscolumns c
  4130.          where
  4131.              c.id = k.id
  4132.              and k.type = 1 /* Primary type key */
  4133.              and c.colid = key8
  4134.  
  4135.     done:
  4136.     select
  4137.         TABLE_QUALIFIER,
  4138.         TABLE_OWNER,
  4139.         TABLE_NAME,
  4140.         COLUMN_NAME,
  4141.         KEY_SEQ,
  4142.         PK_NAME = convert(varchar(32),null)
  4143.     from #pkeys
  4144.     where TABLE_NAME = @table_name
  4145.         and TABLE_OWNER like @table_owner
  4146.     order by 1, 2, 3, 5
  4147. go
  4148.  
  4149. if (charindex('6.00', @@version) = 0 and
  4150.     charindex('6.50', @@version) = 0 and
  4151.     charindex('7.00', @@version) = 0)
  4152. begin
  4153.     print ''
  4154.     print ''
  4155.     print 'Warning:'
  4156.     print 'you are installing the stored procedures '
  4157.     print 'on a pre 6.0 SQL Server.'
  4158.     print 'Ignore the following error.'
  4159. end
  4160. else
  4161.     drop proc sp_pkeys
  4162. go
  4163.  
  4164. /*    Procedure for 6.0 and 6.50 servers */
  4165. CREATE PROCEDURE sp_pkeys(
  4166.                @table_name        sysname,
  4167.                @table_owner     sysname = null,
  4168.                @table_qualifier sysname = null )
  4169. as
  4170.     DECLARE @table_id            int
  4171.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4172.  
  4173.     if @table_qualifier is not null
  4174.     begin
  4175.         if db_name() <> @table_qualifier
  4176.         begin    /* If qualifier doesn't match current database */
  4177.             raiserror (15250, -1,-1)
  4178.             return
  4179.         end
  4180.     end
  4181.     if @table_owner is null
  4182.     begin    /* If unqualified table name */
  4183.         SELECT @full_table_name = @table_name
  4184.     end
  4185.     else
  4186.     begin    /* Qualified table name */
  4187.         if @table_owner = ''
  4188.         begin    /* If empty owner name */
  4189.             SELECT @full_table_name = @table_owner
  4190.         end
  4191.         else
  4192.         begin
  4193.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4194.         end
  4195.     end
  4196.     /*    Get Object ID */
  4197.     SELECT @table_id = object_id(@full_table_name)
  4198.  
  4199.     select
  4200.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4201.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4202.         TABLE_NAME = convert(sysname,o.name),
  4203.         COLUMN_NAME = convert(sysname,c.name),
  4204.         KEY_SEQ = convert(smallint,c1.colid),
  4205.         PK_NAME = convert(sysname,i.name)
  4206.     from
  4207.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  4208.     where
  4209.         o.id = @table_id
  4210.         and o.id = c.id
  4211.         and o.id = i.id
  4212.         and (i.status & 0x800) = 0x800
  4213.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4214.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4215.         and c1.id = @table_id
  4216.     order by 1, 2, 3, 5
  4217. go
  4218.  
  4219. if (charindex('7.00', @@version) = 0)
  4220. begin
  4221.     print ''
  4222.     print ''
  4223.     print 'Warning:'
  4224.     print 'you are installing the stored procedures '
  4225.     print 'on a pre 7.0 SQL Server.'
  4226.     print 'Ignore the following errors.'
  4227. end
  4228. else
  4229.     drop proc sp_pkeys
  4230. go
  4231.  
  4232. /*    Procedure for 7.0 and later servers */
  4233. CREATE PROCEDURE sp_pkeys(
  4234.                @table_name        sysname,
  4235.                @table_owner     sysname = null,
  4236.                @table_qualifier sysname = null )
  4237. as
  4238.     DECLARE @table_id            int
  4239.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4240.  
  4241.     if @table_qualifier is not null
  4242.     begin
  4243.         if db_name() <> @table_qualifier
  4244.         begin    /* If qualifier doesn't match current database */
  4245.             raiserror (15250, -1,-1)
  4246.             return
  4247.         end
  4248.     end
  4249.     if @table_owner is null
  4250.     begin    /* If unqualified table name */
  4251.         SELECT @full_table_name = quotename(@table_name)
  4252.     end
  4253.     else
  4254.     begin    /* Qualified table name */
  4255.         if @table_owner = ''
  4256.         begin    /* If empty owner name */
  4257.             SELECT @full_table_name = quotename(@table_owner)
  4258.         end
  4259.         else
  4260.         begin
  4261.             SELECT @full_table_name = quotename(@table_owner) +
  4262.                 '.' + quotename(@table_name)
  4263.         end
  4264.     end
  4265.     /*    Get Object ID */
  4266.     SELECT @table_id = object_id(@full_table_name)
  4267.  
  4268.     select
  4269.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4270.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4271.         TABLE_NAME = convert(sysname,o.name),
  4272.         COLUMN_NAME = convert(sysname,c.name),
  4273.         --KEY_SEQ = convert(smallint,c.colid),
  4274.         KEY_SEQ = 
  4275.             case
  4276.                 when c.name = index_col(@full_table_name, i.indid,  1) then 1
  4277.                 when c.name = index_col(@full_table_name, i.indid,  2) then 2
  4278.                 when c.name = index_col(@full_table_name, i.indid,  3) then 3
  4279.                 when c.name = index_col(@full_table_name, i.indid,  4) then 4
  4280.                 when c.name = index_col(@full_table_name, i.indid,  5) then 5
  4281.                 when c.name = index_col(@full_table_name, i.indid,  6) then 6
  4282.                 when c.name = index_col(@full_table_name, i.indid,  7) then 7
  4283.                 when c.name = index_col(@full_table_name, i.indid,  8) then 8
  4284.                 when c.name = index_col(@full_table_name, i.indid,  9) then 9
  4285.                 when c.name = index_col(@full_table_name, i.indid, 10) then 10
  4286.                 when c.name = index_col(@full_table_name, i.indid, 11) then 11
  4287.                 when c.name = index_col(@full_table_name, i.indid, 12) then 12
  4288.                 when c.name = index_col(@full_table_name, i.indid, 13) then 13
  4289.                 when c.name = index_col(@full_table_name, i.indid, 14) then 14
  4290.                 when c.name = index_col(@full_table_name, i.indid, 15) then 15
  4291.                 when c.name = index_col(@full_table_name, i.indid, 16) then 16
  4292.             end,
  4293.         PK_NAME = convert(sysname,i.name)
  4294.     from
  4295.         sysindexes i, syscolumns c, sysobjects o --, syscolumns c1
  4296.     where
  4297.         o.id = @table_id
  4298.         and o.id = c.id
  4299.         and o.id = i.id
  4300.         and (i.status & 0x800) = 0x800
  4301.         -- and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4302.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4303.              c.name = index_col (@full_table_name, i.indid,  2) or
  4304.              c.name = index_col (@full_table_name, i.indid,  3) or
  4305.              c.name = index_col (@full_table_name, i.indid,  4) or
  4306.              c.name = index_col (@full_table_name, i.indid,  5) or
  4307.              c.name = index_col (@full_table_name, i.indid,  6) or
  4308.              c.name = index_col (@full_table_name, i.indid,  7) or
  4309.              c.name = index_col (@full_table_name, i.indid,  8) or
  4310.              c.name = index_col (@full_table_name, i.indid,  9) or
  4311.              c.name = index_col (@full_table_name, i.indid, 10) or
  4312.              c.name = index_col (@full_table_name, i.indid, 11) or
  4313.              c.name = index_col (@full_table_name, i.indid, 12) or
  4314.              c.name = index_col (@full_table_name, i.indid, 13) or
  4315.              c.name = index_col (@full_table_name, i.indid, 14) or
  4316.              c.name = index_col (@full_table_name, i.indid, 15) or
  4317.              c.name = index_col (@full_table_name, i.indid, 16)
  4318.             )
  4319.         -- and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4320.         -- and c1.id = @table_id
  4321.     order by 1, 2, 3, 5
  4322. go
  4323.  
  4324. grant execute on sp_pkeys to public
  4325. go
  4326.  
  4327. dump tran master with no_log
  4328. go
  4329.  
  4330. print 'creating sp_server_info'
  4331. go
  4332.  
  4333. create proc sp_server_info (
  4334.             @attribute_id  int = null)
  4335. as
  4336.     if @attribute_id is not null
  4337.         select *
  4338.         from master.dbo.spt_server_info
  4339.         where attribute_id = @attribute_id
  4340.     else
  4341.         select *
  4342.         from master.dbo.spt_server_info
  4343.         order by attribute_id
  4344. go
  4345.  
  4346. grant execute on sp_server_info to public
  4347. go
  4348.  
  4349. dump tran master with no_log
  4350. go
  4351.  
  4352. print 'creating sp_special_columns'
  4353. go
  4354.  
  4355. /*    Procedure for pre-6.0 server */
  4356. CREATE PROCEDURE sp_special_columns (
  4357.                  @table_name        varchar(32),
  4358.                  @table_owner        varchar(32) = null,
  4359.                  @table_qualifier    varchar(32) = null,
  4360.                  @col_type            char(1) = 'R',
  4361.                  @scope                char(1) = 'T',
  4362.                  @nullable            char(1) = 'U',
  4363.                  @ODBCVer            int = 2)
  4364. AS
  4365.     DECLARE @indid                int
  4366.     DECLARE @table_id            int
  4367.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4368.     DECLARE @scopeout            smallint
  4369.  
  4370.     if @col_type not in ('R','V')
  4371.     begin
  4372.         raiserror 20002 '~~Rush_42~~'
  4373.         return
  4374.     end
  4375.  
  4376.     if @scope = 'C'
  4377.         select @scopeout = 0
  4378.     else if @scope = 'T'
  4379.         select @scopeout = 1
  4380.     else
  4381.     begin
  4382.         raiserror 20002 '~~Rush_43~~'
  4383.         return
  4384.     end
  4385.  
  4386.     if @nullable not in ('U','O')
  4387.     begin
  4388.         raiserror 20002 '~~Rush_44~~'
  4389.         return
  4390.     end
  4391.  
  4392.     if @table_qualifier is not null
  4393.     begin
  4394.         if db_name() <> @table_qualifier
  4395.         begin /* If qualifier doesn't match current database */
  4396.             raiserror 20001 '~~Rush_5~~'
  4397.             return
  4398.         end
  4399.     end
  4400.     if @table_owner is null
  4401.     begin     /* If unqualified table name */
  4402.         SELECT @full_table_name = @table_name
  4403.     end
  4404.     else
  4405.     begin    /* Qualified table name */
  4406.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4407.     end
  4408.     /*    Get Object ID */
  4409.     SELECT @table_id = object_id(@full_table_name)
  4410.  
  4411.     if @col_type = 'V'
  4412.     BEGIN /* if ROWVER, just run that query */
  4413.         SELECT
  4414.             SCOPE = convert(smallint,NULL),
  4415.             COLUMN_NAME = convert(varchar(32),c.name),
  4416.             DATA_TYPE = convert(smallint, -3),
  4417.             TYPE_NAME = t.name,
  4418.             "PRECISION" = convert(int,8),
  4419.             LENGTH = convert(int,8),
  4420.             SCALE = convert(smallint, NULL),
  4421.             PSEUDO_COLUMN = convert(smallint,1)
  4422.         FROM
  4423.             systypes t, syscolumns c
  4424.         WHERE
  4425.             c.id = @table_id
  4426.             AND c.usertype = 80 /*    TIMESTAMP */
  4427.             AND t.usertype = 80 /*    TIMESTAMP */
  4428.         RETURN
  4429.     END
  4430.  
  4431.     /* ROWID, now find the id of the 'best' index for this table */
  4432.  
  4433.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4434.                            nullable columns. */
  4435.  
  4436.             SELECT @indid = MIN(indid)
  4437.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4438.                 WHERE
  4439.                     i.status&2 = 2        /*    If Unique Index */
  4440.                      AND c.id = i.id
  4441.                      AND c2.id = c.id
  4442.                      AND c2.colid < i.keycnt + (i.status&16)/16
  4443.                     AND i.id = @table_id
  4444.                     AND indid > 0        /*    Eliminate Table Row */
  4445.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4446.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4447.  
  4448.     ELSE    /* Include indexes that are partially nullable. */
  4449.  
  4450.         SELECT @indid = MIN(indid)
  4451.             FROM sysindexes i
  4452.             WHERE
  4453.                 status&2 = 2        /*    If Unique Index */
  4454.                 AND id = @table_id
  4455.                 AND indid > 0        /*    Eliminate Table Row */
  4456.  
  4457.     SELECT
  4458.         SCOPE = @scopeout,
  4459.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4460.         d.DATA_TYPE,
  4461.         TYPE_NAME = t.name,
  4462.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4463.         LENGTH = isnull(d.length, convert(int,c.length)),
  4464.         SCALE = d.numeric_scale,
  4465.         PSEUDO_COLUMN = convert(smallint,1)
  4466.     FROM
  4467.         sysindexes x,
  4468.         syscolumns c,
  4469.         master.dbo.spt_datatype_info d,
  4470.         systypes t,
  4471.         syscolumns c2    /* Self-join to generate list of index columns and */
  4472.                         /* to extract datatype names */
  4473.     WHERE
  4474.         x.id = @table_id
  4475.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4476.         AND c.id = x.id
  4477.         AND c2.id = x.id
  4478.         AND c2.colid < keycnt+(x.status&16)/16
  4479.         AND x.indid = @indid
  4480.         AND t.type = d.ss_dtype
  4481.         AND c.length = d.fixlen
  4482.         AND c.usertype = t.usertype
  4483.  
  4484. go
  4485.  
  4486. if (charindex('6.00', @@version) = 0 and
  4487.     charindex('6.50', @@version) = 0 and
  4488.     charindex('7.00', @@version) = 0)
  4489. begin
  4490.     print ''
  4491.     print ''
  4492.     print 'Warning:'
  4493.     print 'you are installing the stored procedures '
  4494.     print 'on a pre 6.0 SQL Server.'
  4495.     print 'Ignore the following errors.'
  4496. end
  4497. else
  4498.     drop proc sp_special_columns
  4499. go
  4500.  
  4501. /*    Procedure for 6.0 and 6.50 servers */
  4502. CREATE PROCEDURE sp_special_columns (
  4503.                  @table_name        varchar(32),
  4504.                  @table_owner        varchar(32) = null,
  4505.                  @table_qualifier    varchar(32) = null,
  4506.                  @col_type            char(1) = 'R',
  4507.                  @scope                char(1) = 'T',
  4508.                  @nullable            char(1) = 'U',
  4509.                  @ODBCVer            int = 2)
  4510. AS
  4511.     DECLARE @indid                int
  4512.     DECLARE @table_id            int
  4513.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4514.     DECLARE @scopeout            smallint
  4515.  
  4516.     if @col_type not in ('R','V')
  4517.     begin
  4518.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4519.         return
  4520.     end
  4521.  
  4522.     if @scope = 'C'
  4523.         select @scopeout = 0
  4524.     else if @scope = 'T'
  4525.         select @scopeout = 1
  4526.     else
  4527.     begin
  4528.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4529.         return
  4530.     end
  4531.  
  4532.     if @nullable not in ('U','O')
  4533.     begin
  4534.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4535.         return
  4536.     end
  4537.  
  4538.     if @table_qualifier is not null
  4539.     begin
  4540.         if db_name() <> @table_qualifier
  4541.         begin /* If qualifier doesn't match current database */
  4542.             raiserror (15250, -1,-1)
  4543.             return
  4544.         end
  4545.     end
  4546.     if @table_owner is null
  4547.     begin     /* If unqualified table name */
  4548.         SELECT @full_table_name = @table_name
  4549.     end
  4550.     else
  4551.     begin     /* Qualified table name */
  4552.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4553.     end
  4554.     /*    Get Object ID */
  4555.     SELECT @table_id = object_id(@full_table_name)
  4556.  
  4557.     if @col_type = 'V'
  4558.     BEGIN /* if ROWVER, just run that query */
  4559.         SELECT
  4560.             SCOPE = convert(smallint,NULL),
  4561.             COLUMN_NAME = convert(varchar(32),c.name),
  4562.             DATA_TYPE = convert(smallint, -2),
  4563.             TYPE_NAME = t.name,
  4564.             "PRECISION" = convert(int,8),
  4565.             LENGTH = convert(int,8),
  4566.             SCALE = convert(smallint, NULL),
  4567.             PSEUDO_COLUMN = convert(smallint,1)
  4568.         FROM
  4569.             systypes t, syscolumns c
  4570.         WHERE
  4571.             c.id = @table_id
  4572.             AND c.usertype = 80 /*    TIMESTAMP */
  4573.             AND t.usertype = 80 /*    TIMESTAMP */
  4574.         RETURN
  4575.     END
  4576.  
  4577.     /* ROWID, now find the id of the 'best' index for this table */
  4578.  
  4579.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4580.                            nullable columns. */
  4581.  
  4582.         SELECT @indid = MIN(indid)
  4583.             FROM sysindexes i,syscolumns c,syscolumns c2
  4584.             WHERE
  4585.                 i.status&2 = 2        /*    If Unique Index */
  4586.                 AND c.id = i.id
  4587.                 AND c2.id = c.id
  4588.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4589.                 AND i.id = @table_id
  4590.                 AND indid > 0        /*    Eliminate Table Row */
  4591.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4592.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4593.  
  4594.     ELSE    /* Include indexes that are partially nullable. */
  4595.  
  4596.         SELECT @indid = MIN(indid)
  4597.             FROM sysindexes i
  4598.             WHERE
  4599.                 status&2 = 2        /*    If Unique Index */
  4600.                 AND id = @table_id
  4601.                 AND indid > 0        /*    Eliminate Table Row */
  4602.  
  4603.     SELECT
  4604.         SCOPE = @scopeout,
  4605.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4606.         d.DATA_TYPE,
  4607.         convert(varchar(32),case
  4608.             when (t.usertype > 100 or t.usertype in (18,80))
  4609.                 then t.name
  4610.             else d.TYPE_NAME
  4611.         end) TYPE_NAME,
  4612.         convert(int,case
  4613.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4614.             else isnull(convert(int,c.prec), 2147483647)
  4615.         end) "PRECISION",
  4616.         convert(int,case
  4617.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4618.                 convert(int,c.prec+2)
  4619.             else
  4620.                 isnull(d.length, c.length)
  4621.         end) LENGTH,
  4622.         SCALE = convert(smallint, c.scale),
  4623.         PSEUDO_COLUMN = convert(smallint,1)
  4624.     FROM
  4625.         sysindexes x,
  4626.         syscolumns c,
  4627.         master.dbo.spt_datatype_info d,
  4628.         systypes t,
  4629.         syscolumns c2    /* Self-join to generate list of index columns and */
  4630.                         /* to extract datatype names */
  4631.     WHERE
  4632.         x.id = @table_id
  4633.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4634.         AND c.id = x.id
  4635.         AND c2.id = x.id
  4636.         AND c2.colid < x.keycnt+(x.status&16)/16
  4637.         AND x.indid = @indid
  4638.         AND t.type = d.ss_dtype
  4639.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4640.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  4641.         AND c.usertype = t.usertype
  4642. go
  4643.  
  4644. if (charindex('7.00', @@version) = 0)
  4645. begin
  4646.     print ''
  4647.     print ''
  4648.     print 'Warning:'
  4649.     print 'you are installing the stored procedures '
  4650.     print 'on a pre 7.0 SQL Server.'
  4651.     print 'Ignore the following errors.'
  4652. end
  4653. else
  4654.     drop proc sp_special_columns
  4655. go
  4656.  
  4657. /*    Procedure for 7.0 and later servers */
  4658. CREATE PROCEDURE sp_special_columns (
  4659.                  @table_name        sysname,
  4660.                  @table_owner        sysname = null,
  4661.                  @table_qualifier    sysname = null,
  4662.                  @col_type            char(1) = 'R',
  4663.                  @scope                char(1) = 'T',
  4664.                  @nullable            char(1) = 'U',
  4665.                  @ODBCVer            int = 2)
  4666. AS
  4667.     DECLARE @indid                int
  4668.     DECLARE @table_id            int
  4669.     DECLARE @full_table_name    nvarchar(257)
  4670.     DECLARE @scopeout            smallint
  4671.  
  4672.     if @col_type not in ('R','V')
  4673.     begin
  4674.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4675.         return
  4676.     end
  4677.  
  4678.     if @scope = 'C'
  4679.         select @scopeout = 0
  4680.     else if @scope = 'T'
  4681.         select @scopeout = 1
  4682.     else
  4683.     begin
  4684.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4685.         return
  4686.     end
  4687.  
  4688.     if @nullable not in ('U','O')
  4689.     begin
  4690.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4691.         return
  4692.     end
  4693.  
  4694.     if @table_qualifier is not null
  4695.     begin
  4696.         if db_name() <> @table_qualifier
  4697.         begin /* If qualifier doesn't match current database */
  4698.             raiserror (15250, -1,-1)
  4699.             return
  4700.         end
  4701.     end
  4702.     if @table_owner is null
  4703.     begin     /* If unqualified table name */
  4704.         SELECT @full_table_name = quotename(@table_name)
  4705.     end
  4706.     else
  4707.     begin    /* Qualified table name */
  4708.         if @table_owner = ''
  4709.         begin    /* If empty owner name */
  4710.             SELECT @full_table_name = quotename(@table_owner)
  4711.         end
  4712.         else
  4713.         begin
  4714.             SELECT @full_table_name = quotename(@table_owner) +
  4715.                 '.' + quotename(@table_name)
  4716.         end
  4717.     end
  4718.     /*    Get Object ID */
  4719.     SELECT @table_id = object_id(@full_table_name)
  4720.  
  4721.     if @col_type = 'V'
  4722.     BEGIN /* if ROWVER, just run that query */
  4723.         SELECT
  4724.             SCOPE = convert(smallint,NULL),
  4725.             COLUMN_NAME = convert(sysname,c.name),
  4726.             DATA_TYPE = convert(smallint, -2),
  4727.             TYPE_NAME = t.name,
  4728.             "PRECISION" = convert(int,8),
  4729.             LENGTH = convert(int,8),
  4730.             SCALE = convert(smallint, NULL),
  4731.             PSEUDO_COLUMN = convert(smallint,1)
  4732.         FROM
  4733.             systypes t, syscolumns c
  4734.         WHERE
  4735.             not (@table_id is null)
  4736.             AND c.id = @table_id
  4737.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  4738.             AND t.xtype = c.xtype        
  4739.             AND t.xusertype = c.xusertype    
  4740.         RETURN
  4741.     END
  4742.  
  4743.     /* ROWID, now find the id of the 'best' index for this table */
  4744.  
  4745.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4746.                            nullable columns. */
  4747.  
  4748.         SELECT @indid = MIN(indid)
  4749.             FROM sysindexes x, syscolumns c, syscolumns c2
  4750.             WHERE
  4751.                 not (@table_id is null)
  4752.                 AND x.status&2 = 2        /*    If Unique Index */
  4753.                 AND c.id = x.id
  4754.                 AND c2.id = c.id
  4755.                 AND c2.colid < x.keycnt + (x.status&16)/16
  4756.                 AND x.id = @table_id
  4757.                 AND indid > 0        /*    Eliminate Table Row */
  4758.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  4759.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4760.  
  4761.     ELSE    /* Include indexes that are partially nullable. */
  4762.  
  4763.         SELECT @indid = MIN(indid)
  4764.             FROM sysindexes x
  4765.             WHERE
  4766.                 not (@table_id is null)
  4767.                 AND status&2 = 2        /*    If Unique Index */
  4768.                 AND id = @table_id
  4769.                 AND indid > 0        /*    Eliminate Table Row */
  4770.  
  4771.     SELECT
  4772.         SCOPE = @scopeout,
  4773.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  4774.         d.DATA_TYPE,
  4775.         convert(sysname,case
  4776.             when t.xusertype > 255 then t.name
  4777.             else d.TYPE_NAME
  4778.         end) TYPE_NAME,
  4779.         convert(int,case
  4780.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4781.             else OdbcPrec(c.xtype,c.length,c.xprec)
  4782.         end) "PRECISION",
  4783.         convert(int,case
  4784.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4785.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  4786.             else isnull(d.length, c.length)
  4787.         end) LENGTH,
  4788.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4789.         PSEUDO_COLUMN = convert(smallint,1)
  4790.     FROM
  4791.         sysindexes x,
  4792.         syscolumns c,
  4793.         master.dbo.spt_datatype_info d,
  4794.         systypes t,
  4795.         syscolumns c2    /* Self-join to generate list of index columns and */
  4796.                         /* to extract datatype names */
  4797.     WHERE
  4798.         not (@table_id is null)
  4799.         AND x.id = @table_id
  4800.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4801.         AND c.id = x.id
  4802.         AND c2.id = x.id
  4803.         AND c2.colid < x.keycnt + (x.status&16)/16
  4804.         AND x.indid = @indid
  4805.         AND t.xtype = d.ss_dtype
  4806.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4807.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  4808.         AND c.xusertype = t.xusertype
  4809. go
  4810.  
  4811. grant execute on sp_special_columns to public
  4812. go
  4813.  
  4814. dump tran master with no_log
  4815. go
  4816.  
  4817. print 'creating sp_sproc_columns'
  4818. go
  4819.  
  4820. /*    Procedure for pre-6.0 server */
  4821. CREATE PROCEDURE sp_sproc_columns (
  4822.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  4823.                  @procedure_owner        varchar(96) = null,
  4824.                  @procedure_qualifier    varchar(32) = null,
  4825.                  @column_name            varchar(96) = null,
  4826.                  @ODBCVer                int = 2)
  4827. AS
  4828.     DECLARE @group_num_lower smallint
  4829.     DECLARE @group_num_upper smallint
  4830.     DECLARE @semi_position int
  4831.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  4832.     DECLARE @procedure_id int
  4833.  
  4834.     if @column_name is null /*    If column name not supplied, match all */
  4835.         select @column_name = '%'
  4836.     if @procedure_qualifier is not null
  4837.     begin
  4838.         if db_name() <> @procedure_qualifier
  4839.         begin
  4840.             if @procedure_qualifier = ''
  4841.             begin
  4842.                 /* in this case, we need to return an empty result set */
  4843.                 /* because the user has requested a database with an empty name */
  4844.                 select @procedure_name = ''
  4845.                 select @procedure_owner = ''
  4846.             end
  4847.             else
  4848.             begin    /* If qualifier doesn't match current database */
  4849.                 raiserror 20001 '~~Rush_51~~'
  4850.                 return
  4851.             end
  4852.         end
  4853.     end
  4854.  
  4855.     if @procedure_name is null
  4856.     begin    /*    If procedure name not supplied, match all */
  4857.         select @procedure_name = '%'
  4858.     end
  4859.  
  4860.     /* first we need to extract the procedure group number, if one exists */
  4861.     select @semi_position = charindex(';',@procedure_name)
  4862.     if (@semi_position > 0)
  4863.     begin    /* If group number separator (;) found */
  4864.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4865.         select @group_num_upper = @group_num_lower
  4866.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4867.     end
  4868.     else
  4869.     begin    /* No group separator, so default to group number of 1 */
  4870.         select @group_num_lower = 1
  4871.         select @group_num_upper = 32767            
  4872.     end
  4873.  
  4874.     if @procedure_owner is null
  4875.     begin    /* If unqualified procedure name */
  4876.         SELECT @full_procedure_name = @procedure_name
  4877.     end
  4878.     else
  4879.     begin    /* Qualified procedure name */
  4880.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4881.     end
  4882.  
  4883.     /*    Get Object ID */
  4884.     SELECT @procedure_id = object_id(@full_procedure_name)
  4885.     if ((charindex('%',@full_procedure_name) = 0) and
  4886.         (charindex('_',@full_procedure_name) = 0) and
  4887.         @procedure_id <> 0)
  4888.     begin
  4889.         /* this block is for the case where there is no pattern
  4890.             matching required for the procedure name */
  4891.         SELECT
  4892.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4893.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4894.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4895.             COLUMN_NAME = convert(varchar(32),c.name),
  4896.             COLUMN_TYPE = convert(smallint, 0),
  4897.             d.DATA_TYPE,
  4898.             TYPE_NAME = t.name,
  4899.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4900.             LENGTH = isnull(d.length, convert(int,c.length)),
  4901.             SCALE = d.numeric_scale,
  4902.             d.RADIX,
  4903.             d.NULLABLE,
  4904.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4905.             COLUMN_DEF = convert(varchar(255),null),
  4906.             d.SQL_DATA_TYPE,
  4907.             d.SQL_DATETIME_SUB,
  4908.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4909.             ORDINAL_POSITION = convert(int,c.colid),
  4910.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4911.             SS_DATA_TYPE = c.type
  4912.         FROM
  4913.             syscolumns c,
  4914.             sysobjects o,
  4915.             master.dbo.spt_datatype_info d,
  4916.             systypes t
  4917.         WHERE
  4918.             o.id = @procedure_id
  4919.             AND c.id = o.id
  4920.             AND t.type = d.ss_dtype
  4921.             AND c.length = isnull(d.fixlen, c.length)
  4922.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4923.             AND c.usertype = t.usertype
  4924.             AND c.name like @column_name
  4925.             AND c.number between @group_num_lower and @group_num_upper
  4926.         UNION ALL
  4927.         SELECT           /* return value row*/
  4928.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4929.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4930.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4931.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4932.             COLUMN_TYPE = convert(smallint, 5),
  4933.             DATA_TYPE = convert(smallint, 4),
  4934.             TYPE_NAME = convert(varchar(32),'int'),
  4935.             "PRECISION" = convert(int,10),
  4936.             LENGTH = convert(int,4),
  4937.             SCALE = convert(smallint,0),
  4938.             RADIX = convert(smallint,10),
  4939.             NULLABLE = convert(smallint,0),
  4940.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4941.             COLUMN_DEF = convert(varchar(255),NULL),
  4942.             SQL_DATA_TYPE = convert(smallint, 4),
  4943.             SQL_DATETIME_SUB = convert(smallint,null),
  4944.             CHAR_OCTET_LENGTH = convert(int,null),
  4945.             ORDINAL_POSITION = convert(int,0),
  4946.             IS_NULLABLE = convert(varchar(254),'NO'),
  4947.             SS_DATA_TYPE = convert(tinyint,56)
  4948.         FROM
  4949.             syscomments c, sysobjects o
  4950.         WHERE
  4951.             o.id = @procedure_id
  4952.             AND c.id = o.id
  4953.             AND c.colid = 1
  4954.             AND o.type = 'P'                        /* Just Procedures */
  4955.             AND 'RETURN_VALUE' like @column_name
  4956.             AND c.number between @group_num_lower and @group_num_upper        
  4957.         ORDER BY 1, 2, 3, 18
  4958.     end
  4959.     else
  4960.     begin
  4961.         /* this block is for the case where there IS pattern
  4962.             matching done on the procedure name */
  4963.         if @procedure_owner is null
  4964.             select @procedure_owner = '%'
  4965.         SELECT
  4966.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4967.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4968.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4969.             COLUMN_NAME = convert(varchar(32),c.name),
  4970.             COLUMN_TYPE = convert(smallint, 0),
  4971.             d.DATA_TYPE,
  4972.             TYPE_NAME = t.name,
  4973.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4974.             LENGTH = isnull(d.length, convert(int,c.length)),
  4975.             SCALE = d.numeric_scale,
  4976.             d.RADIX,
  4977.             d.NULLABLE,
  4978.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4979.             COLUMN_DEF = convert(varchar(255),null),
  4980.             d.SQL_DATA_TYPE,
  4981.             d.SQL_DATETIME_SUB,
  4982.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4983.             ORDINAL_POSITION = convert(int,c.colid),
  4984.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4985.             SS_DATA_TYPE = c.type
  4986.         FROM
  4987.             syscolumns c,
  4988.             sysobjects o,
  4989.             master.dbo.spt_datatype_info d,
  4990.             systypes t
  4991.         WHERE
  4992.             o.name like @procedure_name
  4993.             AND user_name(o.uid) like @procedure_owner
  4994.             AND o.id = c.id
  4995.             AND t.type = d.ss_dtype
  4996.             AND c.length = isnull(d.fixlen, c.length)
  4997.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4998.             AND c.usertype = t.usertype
  4999.             AND o.type = 'P'                            /* Just Procedures */
  5000.             AND c.name like @column_name
  5001.             AND c.number between @group_num_lower and @group_num_upper
  5002.         UNION ALL
  5003.         SELECT           /* return value row*/
  5004.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5005.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5006.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5007.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  5008.             COLUMN_TYPE = convert(smallint, 5),
  5009.             DATA_TYPE = convert(smallint, 4),
  5010.             TYPE_NAME = convert(varchar(32),'int'),
  5011.             "PRECISION" = convert(int,10),
  5012.             LENGTH = convert(int,4),
  5013.             SCALE = convert(smallint,0),
  5014.             RADIX = convert(smallint,10),
  5015.             NULLABLE = convert(smallint,0),
  5016.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5017.             COLUMN_DEF = convert(varchar(255),NULL),
  5018.             SQL_DATA_TYPE = convert(smallint, 4),
  5019.             SQL_DATETIME_SUB = convert(smallint,null),
  5020.             CHAR_OCTET_LENGTH = convert(int,null),
  5021.             ORDINAL_POSITION = convert(int,0),
  5022.             IS_NULLABLE = convert(varchar(254),'NO'),
  5023.             SS_DATA_TYPE = convert(tinyint,56)
  5024.         FROM
  5025.             syscomments c, sysobjects o
  5026.         WHERE
  5027.             o.name like @procedure_name
  5028.             AND user_name(o.uid) like @procedure_owner
  5029.             AND c.id = o.id
  5030.             AND c.colid = 1
  5031.             AND o.type = 'P'                        /* Just Procedures */
  5032.             AND 'RETURN_VALUE' like @column_name
  5033.             AND c.number between @group_num_lower and @group_num_upper
  5034.         ORDER BY 1, 2, 3, 18
  5035.     end
  5036. go
  5037.  
  5038. if (charindex('6.00', @@version) = 0 and
  5039.     charindex('6.50', @@version) = 0 and
  5040.     charindex('7.00', @@version) = 0)
  5041. begin
  5042.     print ''
  5043.     print ''
  5044.     print 'Warning:'
  5045.     print 'you are installing the stored procedures '
  5046.     print 'on a pre 6.0 SQL Server.'
  5047.     print 'Ignore the following error.'
  5048. end
  5049. else
  5050.     drop proc sp_sproc_columns
  5051. go
  5052.  
  5053. /*    Procedure for 6.0 and 6.50 servers */
  5054. CREATE PROCEDURE sp_sproc_columns (
  5055.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  5056.                  @procedure_owner        varchar(96) = null,
  5057.                  @procedure_qualifier    varchar(32) = null,
  5058.                  @column_name            varchar(96) = null,
  5059.                  @ODBCVer                int = 2)
  5060. AS
  5061.     DECLARE @group_num_lower smallint
  5062.     DECLARE @group_num_upper smallint
  5063.     DECLARE @semi_position int
  5064.     DECLARE @full_procedure_name    varchar(205)
  5065.     DECLARE @procedure_id int
  5066.  
  5067.     if @column_name is null /*    If column name not supplied, match all */
  5068.         select @column_name = '%'
  5069.     if @procedure_qualifier is not null
  5070.     begin
  5071.         if db_name() <> @procedure_qualifier
  5072.         begin
  5073.             if @procedure_qualifier = ''
  5074.             begin
  5075.                 /* in this case, we need to return an empty result set */
  5076.                 /* because the user has requested a database with an empty name */
  5077.                 select @procedure_name = ''
  5078.                 select @procedure_owner = ''
  5079.             end
  5080.             else
  5081.             begin    /* If qualifier doesn't match current database */
  5082.                 raiserror (15250, -1,-1)
  5083.                 return
  5084.             end
  5085.         end
  5086.     end
  5087.  
  5088.     if @procedure_name is null
  5089.     begin    /*    If procedure name not supplied, match all */
  5090.         select @procedure_name = '%'
  5091.     end
  5092.  
  5093.     /* first we need to extract the procedure group number, if one exists */
  5094.     select @semi_position = charindex(';',@procedure_name)
  5095.     if (@semi_position > 0)
  5096.     begin    /* If group number separator (;) found */
  5097.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5098.         select @group_num_upper = @group_num_lower
  5099.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5100.     end
  5101.     else
  5102.     begin    /* No group separator, so default to group number of 1 */
  5103.         select @group_num_lower = 1
  5104.         select @group_num_upper = 32767            
  5105.     end
  5106.  
  5107.     if @procedure_owner is null
  5108.     begin    /* If unqualified procedure name */
  5109.         SELECT @full_procedure_name = @procedure_name
  5110.     end
  5111.     else
  5112.     begin    /* Qualified procedure name */
  5113.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5114.     end
  5115.  
  5116.     /*    Get Object ID */
  5117.     SELECT @procedure_id = object_id(@full_procedure_name)
  5118.     if ((charindex('%',@full_procedure_name) = 0) and
  5119.         (charindex('[',@full_procedure_name) = 0) and
  5120.         (charindex('_',@full_procedure_name) = 0) and
  5121.         @procedure_id <> 0)
  5122.     begin
  5123.         /* this block is for the case where there is no pattern
  5124.             matching required for the procedure name */
  5125.         SELECT
  5126.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5127.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5128.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5129.             COLUMN_NAME = convert(varchar(32),c.name),
  5130.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5131.             d.DATA_TYPE,
  5132.             TYPE_NAME = t.name,
  5133.             convert(int,case
  5134.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5135.                 else isnull(convert(int,c.prec), 2147483647)
  5136.             end) "PRECISION",
  5137.             convert(int,case
  5138.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5139.                     c.prec+2
  5140.                 else
  5141.                     isnull(d.length, c.length)
  5142.             end) LENGTH,
  5143.             SCALE = convert(smallint, c.scale),
  5144.             d.RADIX,
  5145.             d.NULLABLE,
  5146.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5147.             COLUMN_DEF = convert(varchar(255),NULL),
  5148.             d.SQL_DATA_TYPE,
  5149.             d.SQL_DATETIME_SUB,
  5150.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5151.             ORDINAL_POSITION = convert(int,c.colid),
  5152.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5153.             SS_DATA_TYPE = c.type
  5154.         FROM
  5155.             syscolumns c,
  5156.             sysobjects o,
  5157.             master.dbo.spt_datatype_info d,
  5158.             systypes t
  5159.         WHERE
  5160.             o.id = @procedure_id
  5161.             AND c.id = o.id
  5162.             AND c.type = d.ss_dtype
  5163.             AND c.length = isnull(d.fixlen, c.length)
  5164.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5165.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5166.             AND c.usertype = t.usertype
  5167.             AND c.name like @column_name
  5168.             AND c.number between @group_num_lower and @group_num_upper
  5169.         UNION ALL
  5170.         SELECT           /* return value row*/
  5171.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5172.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5173.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5174.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  5175.             COLUMN_TYPE = convert(smallint, 5),
  5176.             DATA_TYPE = convert(smallint, 4),
  5177.             TYPE_NAME = convert(varchar(32),'int'),
  5178.             "PRECISION" = convert(int,10),
  5179.             LENGTH = convert(int,4),
  5180.             SCALE = convert(smallint,0),
  5181.             RADIX = convert(smallint,10),
  5182.             NULLABLE = convert(smallint,0),
  5183.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5184.             COLUMN_DEF = convert(varchar(255),NULL),
  5185.             SQL_DATA_TYPE = convert(smallint, 4),
  5186.             SQL_DATETIME_SUB = convert(smallint,null),
  5187.             CHAR_OCTET_LENGTH = convert(int,null),
  5188.             ORDINAL_POSITION = convert(int,0),
  5189.             IS_NULLABLE = convert(varchar(254),'NO'),
  5190.             SS_DATA_TYPE = convert(tinyint,56)
  5191.         FROM
  5192.             syscomments c, sysobjects o
  5193.         WHERE
  5194.             o.id = @procedure_id
  5195.             AND c.id = o.id
  5196.             AND c.colid = 1
  5197.             AND o.type = 'P'                        /* Just Procedures */
  5198.             AND 'RETURN_VALUE' like @column_name
  5199.             AND c.number between @group_num_lower and @group_num_upper
  5200.         ORDER BY 1, 2, 3, 18
  5201.     end
  5202.     else
  5203.     begin
  5204.         /* this block is for the case where there IS pattern
  5205.             matching done on the procedure name */
  5206.         if @procedure_owner is null
  5207.             select @procedure_owner = '%'
  5208.         SELECT
  5209.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5210.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5211.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5212.             COLUMN_NAME = convert(varchar(32),c.name),
  5213.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5214.             d.DATA_TYPE,
  5215.             TYPE_NAME = t.name,
  5216.             convert(int,case
  5217.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5218.                 else isnull(convert(int,c.prec), 2147483647)
  5219.             end) "PRECISION",
  5220.             convert(int,case
  5221.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5222.                     c.prec+2
  5223.                 else
  5224.                     isnull(d.length, c.length)
  5225.             end) LENGTH,
  5226.             SCALE = convert(smallint, c.scale),
  5227.             d.RADIX,
  5228.             d.NULLABLE,
  5229.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5230.             COLUMN_DEF = convert(varchar(255),NULL),
  5231.             d.SQL_DATA_TYPE,
  5232.             d.SQL_DATETIME_SUB,
  5233.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5234.             ORDINAL_POSITION = convert(int,c.colid),
  5235.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5236.             SS_DATA_TYPE = c.type
  5237.         FROM
  5238.             syscolumns c,
  5239.             sysobjects o,
  5240.             master.dbo.spt_datatype_info d,
  5241.             systypes t
  5242.         WHERE
  5243.             o.name like @procedure_name
  5244.             AND user_name(o.uid) like @procedure_owner
  5245.             AND o.id = c.id
  5246.             AND c.type = d.ss_dtype
  5247.             AND c.length = isnull(d.fixlen, c.length)
  5248.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5249.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5250.             AND c.usertype = t.usertype
  5251.             AND o.type = 'P'                            /* Just Procedures */
  5252.             AND c.name like @column_name
  5253.             AND c.number between @group_num_lower and @group_num_upper
  5254.         UNION ALL
  5255.         SELECT           /* return value row*/
  5256.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5257.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5258.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5259.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  5260.             COLUMN_TYPE = convert(smallint, 5),
  5261.             DATA_TYPE = convert(smallint, 4),
  5262.             TYPE_NAME = convert(varchar(32),'int'),
  5263.             "PRECISION" = convert(int,10),
  5264.             LENGTH = convert(int,4),
  5265.             SCALE = convert(smallint,0),
  5266.             RADIX = convert(smallint,10),
  5267.             NULLABLE = convert(smallint,0),
  5268.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5269.             COLUMN_DEF = convert(varchar(255),NULL),
  5270.             SQL_DATA_TYPE = convert(smallint, 4),
  5271.             SQL_DATETIME_SUB = convert(smallint,null),
  5272.             CHAR_OCTET_LENGTH = convert(int,null),
  5273.             ORDINAL_POSITION = convert(int,0),
  5274.             IS_NULLABLE = convert(varchar(254),'NO'),
  5275.             SS_DATA_TYPE = convert(tinyint,56)
  5276.         FROM
  5277.             syscomments c, sysobjects o
  5278.         WHERE
  5279.             o.name like @procedure_name
  5280.             AND user_name(o.uid) like @procedure_owner
  5281.             AND c.id = o.id
  5282.             AND c.colid = 1
  5283.             AND o.type = 'P'                        /* Just Procedures */
  5284.             AND 'RETURN_VALUE' like @column_name
  5285.             AND c.number between @group_num_lower and @group_num_upper
  5286.         ORDER BY 1, 2, 3, 18
  5287.     end
  5288. go
  5289.  
  5290. if (charindex('7.00', @@version) = 0)
  5291. begin
  5292.     print ''
  5293.     print ''
  5294.     print 'Warning:'
  5295.     print 'you are installing the stored procedures '
  5296.     print 'on a pre 7.0 SQL Server.'
  5297.     print 'Ignore the following errors.'
  5298. end
  5299. else
  5300.     drop proc sp_sproc_columns
  5301. go
  5302.  
  5303. /*    Procedure for 7.0 server */
  5304. CREATE PROCEDURE sp_sproc_columns (
  5305.                  @procedure_name        nvarchar(390) = '%',
  5306.                  @procedure_owner        nvarchar(384) = null,
  5307.                  @procedure_qualifier    sysname = null,
  5308.                  @column_name            nvarchar(384) = null,
  5309.                  @ODBCVer                int = 2)
  5310. AS
  5311.     DECLARE @group_num_lower smallint
  5312.     DECLARE @group_num_upper smallint
  5313.     DECLARE @semi_position int
  5314.     DECLARE @full_procedure_name    nvarchar(774)
  5315.     DECLARE @procedure_id int
  5316.  
  5317.     if @column_name is null /*    If column name not supplied, match all */
  5318.         select @column_name = '%'
  5319.     if @procedure_qualifier is not null
  5320.     begin
  5321.         if db_name() <> @procedure_qualifier
  5322.         begin
  5323.             if @procedure_qualifier = ''
  5324.             begin
  5325.                 /* in this case, we need to return an empty result set */
  5326.                 /* because the user has requested a database with an empty name */
  5327.                 select @procedure_name = ''
  5328.                 select @procedure_owner = ''
  5329.             end
  5330.             else
  5331.             begin    /* If qualifier doesn't match current database */
  5332.                 raiserror (15250, -1,-1)
  5333.                 return
  5334.             end
  5335.         end
  5336.     end
  5337.  
  5338.     if @procedure_name is null
  5339.     begin    /*    If procedure name not supplied, match all */
  5340.         select @procedure_name = '%'
  5341.     end
  5342.  
  5343.     /* first we need to extract the procedure group number, if one exists */
  5344.     select @semi_position = charindex(';',@procedure_name)
  5345.     if (@semi_position > 0)
  5346.     begin    /* If group number separator (;) found */
  5347.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5348.         select @group_num_upper = @group_num_lower
  5349.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5350.     end
  5351.     else
  5352.     begin    /* No group separator, so default to all groups */
  5353.         select @group_num_lower = 1
  5354.         select @group_num_upper = 32767            
  5355.     end
  5356.  
  5357.     if @procedure_owner is null
  5358.     begin    /* If unqualified procedure name */
  5359.         SELECT @full_procedure_name = quotename(@procedure_name)
  5360.     end
  5361.     else
  5362.     begin    /* Qualified procedure name */
  5363.         if @procedure_owner = ''
  5364.         begin    /* If empty owner name */
  5365.             SELECT @full_procedure_name = quotename(@procedure_owner)
  5366.         end
  5367.         else
  5368.         begin
  5369.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  5370.                 '.' + quotename(@procedure_name)
  5371.         end
  5372.     end
  5373.  
  5374.     /*    Get Object ID */
  5375.     SELECT @procedure_id = object_id(@full_procedure_name)
  5376.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  5377.         (isnull(charindex('[', @procedure_name),0) = 0) and
  5378.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  5379.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  5380.         not (@procedure_id is null))
  5381.     begin
  5382.         /* this block is for the case where there is no pattern
  5383.             matching required for the procedure name */
  5384.         SELECT
  5385.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5386.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5387.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5388.             COLUMN_NAME = convert(sysname,c.name),
  5389.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  5390.             d.DATA_TYPE,
  5391.             TYPE_NAME = t.name,
  5392.             convert(int,case
  5393.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5394.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5395.             end) "PRECISION",
  5396.             convert(int,case
  5397.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5398.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5399.                 else
  5400.                     isnull(d.length, c.length)
  5401.             end) LENGTH,
  5402.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5403.             d.RADIX,
  5404.             d.NULLABLE,
  5405.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5406.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5407.             d.SQL_DATA_TYPE,
  5408.             d.SQL_DATETIME_SUB,
  5409.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5410.             ORDINAL_POSITION = convert(int,c.colid),
  5411.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5412.             SS_DATA_TYPE = c.type
  5413.         FROM
  5414.             syscolumns c,
  5415.             sysobjects o,
  5416.             master.dbo.spt_datatype_info d,
  5417.             systypes t
  5418.         WHERE
  5419.             o.id = @procedure_id
  5420.             AND c.id = o.id
  5421.             AND c.xtype = d.ss_dtype
  5422.             AND c.length = isnull(d.fixlen, c.length)
  5423.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5424.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5425.             AND c.xusertype = t.xusertype
  5426.             AND c.name like @column_name
  5427.             AND c.number between @group_num_lower and @group_num_upper
  5428.         UNION ALL
  5429.         SELECT           /* return value row*/
  5430.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5431.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5432.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5433.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  5434.             COLUMN_TYPE = convert(smallint, 5),
  5435.             DATA_TYPE = convert(smallint, 4),
  5436.             TYPE_NAME = convert(sysname,'int'),
  5437.             "PRECISION" = convert(int,10),
  5438.             LENGTH = convert(int,4),
  5439.             SCALE = convert(smallint,0),
  5440.             RADIX = convert(smallint,10),
  5441.             NULLABLE = convert(smallint,0),
  5442.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5443.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5444.             SQL_DATA_TYPE = convert(smallint, 4),
  5445.             SQL_DATETIME_SUB = convert(smallint,null),
  5446.             CHAR_OCTET_LENGTH = convert(int,null),
  5447.             ORDINAL_POSITION = convert(int,0),
  5448.             IS_NULLABLE = convert(varchar(254),'NO'),
  5449.             SS_DATA_TYPE = convert(tinyint,56)
  5450.         FROM
  5451.             syscomments c, sysobjects o
  5452.         WHERE
  5453.             o.id = @procedure_id
  5454.             AND c.id = o.id
  5455.             AND c.colid = 1
  5456.             AND o.type = 'P'                        /* Just Procedures */
  5457.             AND 'RETURN_VALUE' like @column_name
  5458.             AND c.number between @group_num_lower and @group_num_upper        
  5459.         ORDER BY 1, 2, 3, 18
  5460.     end
  5461.     else
  5462.     begin
  5463.         /* this block is for the case where there IS pattern
  5464.             matching done on the procedure name */
  5465.         if @procedure_owner is null
  5466.             select @procedure_owner = '%'
  5467.         SELECT
  5468.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5469.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5470.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5471.             COLUMN_NAME = convert(sysname,c.name),
  5472.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  5473.             d.DATA_TYPE,
  5474.             TYPE_NAME = t.name,
  5475.             convert(int,case
  5476.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5477.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5478.             end) "PRECISION",
  5479.             convert(int,case
  5480.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5481.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5482.                 else
  5483.                     isnull(d.length, c.length)
  5484.             end) LENGTH,
  5485.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5486.             d.RADIX,
  5487.             d.NULLABLE,
  5488.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5489.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5490.             d.SQL_DATA_TYPE,
  5491.             d.SQL_DATETIME_SUB,
  5492.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5493.             ORDINAL_POSITION = convert(int,c.colid),
  5494.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5495.             SS_DATA_TYPE = c.type
  5496.         FROM
  5497.             syscolumns c,
  5498.             sysobjects o,
  5499.             master.dbo.spt_datatype_info d,
  5500.             systypes t
  5501.         WHERE
  5502.             o.name like @procedure_name
  5503.             AND user_name(o.uid) like @procedure_owner
  5504.             AND o.id = c.id
  5505.             AND c.xtype = d.ss_dtype
  5506.             AND c.length = isnull(d.fixlen, c.length)
  5507.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5508.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5509.             AND c.xusertype = t.xusertype
  5510.             AND o.type = 'P'                            /* Just Procedures */
  5511.             AND c.name like @column_name
  5512.             AND c.number between @group_num_lower and @group_num_upper
  5513.         UNION ALL
  5514.         SELECT           /* return value row*/
  5515.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5516.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5517.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5518.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  5519.             COLUMN_TYPE = convert(smallint, 5),
  5520.             DATA_TYPE = convert(smallint, 4),
  5521.             TYPE_NAME = convert(sysname,'int'),
  5522.             "PRECISION" = convert(int,10),
  5523.             LENGTH = convert(int,4),
  5524.             SCALE = convert(smallint,0),
  5525.             RADIX = convert(smallint,10),
  5526.             NULLABLE = convert(smallint,0),
  5527.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5528.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5529.             SQL_DATA_TYPE = convert(smallint, 4),
  5530.             SQL_DATETIME_SUB = convert(smallint,null),
  5531.             CHAR_OCTET_LENGTH = convert(int,null),
  5532.             ORDINAL_POSITION = convert(int,0),
  5533.             IS_NULLABLE = convert(varchar(254),'NO'),
  5534.             SS_DATA_TYPE = convert(tinyint,56)
  5535.         FROM
  5536.             syscomments c, sysobjects o
  5537.         WHERE
  5538.             o.name like @procedure_name
  5539.             AND c.id = o.id
  5540.             AND user_name(o.uid) like @procedure_owner
  5541.             AND c.colid = 1
  5542.             AND o.type = 'P'                        /* Just Procedures */
  5543.             AND 'RETURN_VALUE' like @column_name
  5544.             AND c.number between @group_num_lower and @group_num_upper
  5545.         ORDER BY 1, 2, 3, 18
  5546.     end
  5547. go
  5548.  
  5549. grant execute on sp_sproc_columns to public
  5550. go
  5551.  
  5552. dump tran master with no_log
  5553. go
  5554.  
  5555. print 'creating sp_statistics'
  5556. go
  5557.  
  5558. /*    Procedure for pre-7.0 server */
  5559. CREATE PROCEDURE sp_statistics (
  5560.                  @table_name        varchar(32),
  5561.                  @table_owner        varchar(32) = null,
  5562.                  @table_qualifier    varchar(32) = null,
  5563.                  @index_name        varchar(32) = '%',
  5564.                  @is_unique         char(1) = 'N',
  5565.                  @accuracy            char(1) = 'Q')
  5566. AS
  5567.     set nocount on
  5568.     DECLARE @indid                int
  5569.     DECLARE @lastindid            int
  5570.     DECLARE @table_id            int
  5571.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  5572.  
  5573.     create table #TmpIndex(
  5574.         TABLE_QUALIFIER varchar(32) NULL,
  5575.         TABLE_OWNER     varchar(32) NULL,
  5576.         TABLE_NAME        varchar(32) NOT NULL,
  5577.         INDEX_QUALIFIER varchar(32) null,
  5578.         INDEX_NAME        varchar(32) null,
  5579.         NON_UNIQUE        smallint null,
  5580.         TYPE            smallint NOT NULL,
  5581.         SEQ_IN_INDEX    smallint null,
  5582.         COLUMN_NAME     varchar(32) null,
  5583.         COLLATION        char(1) null,
  5584.         index_id        int null,
  5585.         CARDINALITY     int null,
  5586.         PAGES            int null,
  5587.         status            smallint NOT NULL)
  5588.  
  5589.     if @table_qualifier is not null
  5590.     begin
  5591.         if db_name() <> @table_qualifier
  5592.         begin    /* If qualifier doesn't match current database */
  5593.             raiserror 20001 '~~Rush_5~~'
  5594.             return
  5595.         end
  5596.     end
  5597.  
  5598.     if @accuracy not in ('Q','E')
  5599.         begin
  5600.             raiserror 20002 '~~Rush_58~~'
  5601.             return
  5602.         end
  5603.  
  5604.     if (@@trancount <> 0 and
  5605.         charindex('6.50', @@version) = 0 and
  5606.         charindex('7.00', @@version) = 0)
  5607.     begin    /* If inside a transaction */
  5608.         raiserror 20003 '~~Rush_59~~'
  5609.         return
  5610.     end
  5611.  
  5612.     if @table_owner is null
  5613.     begin    /* If unqualified table name */
  5614.         SELECT @full_table_name = @table_name
  5615.     end
  5616.     else
  5617.     begin    /* Qualified table name */
  5618.         if @table_owner = ''
  5619.         begin    /* If empty owner name */
  5620.             SELECT @full_table_name = @table_owner
  5621.         end
  5622.         else
  5623.         begin
  5624.             SELECT @full_table_name = @table_owner + '.' + @table_name
  5625.         end
  5626.     end
  5627.     /*    Get Object ID */
  5628.     SELECT @table_id = object_id(@full_table_name)
  5629.  
  5630.     /*    Start at lowest index id */
  5631.     SELECT @indid = min(indid)
  5632.     FROM sysindexes
  5633.     WHERE id = @table_id
  5634.         AND indid > 0
  5635.         AND indid < 255
  5636.  
  5637.     WHILE @indid is not NULL
  5638.     BEGIN
  5639.         INSERT #TmpIndex    /* Add all columns that are in index */
  5640.             SELECT
  5641.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5642.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5643.                 o.name,                                 /* TABLE_NAME       */
  5644.                 o.name,                                 /* INDEX_QUALIFIER */
  5645.                 x.name,                                 /* INDEX_NAME       */
  5646.                 0,                                        /* NON_UNIQUE       */
  5647.                 1,                                        /* SQL_INDEX_CLUSTERED */
  5648.                 colid,                                    /* SEQ_IN_INDEX    */
  5649.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5650.                 'A',                                    /* COLLATION       */
  5651.                 @indid,                                 /* index_id        */
  5652.                 x.rows,                                 /* CARDINALITY       */
  5653.                 x.dpages,                                /* PAGES           */
  5654.                 x.status                                /* status            */
  5655.             FROM sysindexes x, syscolumns c, sysobjects o
  5656.             WHERE
  5657.                 x.id = @table_id
  5658.                 AND x.id = o.id
  5659.                 AND x.id = c.id
  5660.                 AND c.colid < keycnt+(x.status&16)/16    /* all but Unique Clust indices have an extra key */
  5661.                 AND x.indid = @indid
  5662.         /*
  5663.         **      Now move @indid to the next index.
  5664.         */
  5665.         SELECT @lastindid = @indid
  5666.         SELECT @indid = NULL
  5667.  
  5668.         SELECT @indid = min(indid)
  5669.         FROM sysindexes
  5670.         WHERE id = @table_id
  5671.             AND indid > @lastindid
  5672.             AND indid < 255
  5673.     END
  5674.  
  5675.     UPDATE #TmpIndex
  5676.         SET NON_UNIQUE = 1
  5677.         WHERE status&2 <> 2 /* If non-unique index */
  5678.     UPDATE #TmpIndex
  5679.         SET
  5680.             TYPE = 3,            /* SQL_INDEX_OTHER */
  5681.             CARDINALITY = NULL,
  5682.             PAGES = NULL
  5683.         WHERE index_id > 1    /* If non-clustered index */
  5684.  
  5685.     /* now add row for table statistics */
  5686.     INSERT #TmpIndex
  5687.         SELECT
  5688.             DB_NAME(),                /* TABLE_QUALIFIER */
  5689.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5690.             o.name,                 /* TABLE_NAME       */
  5691.             null,                    /* INDEX_QUALIFIER */
  5692.             null,                    /* INDEX_NAME       */
  5693.             null,                    /* NON_UNIQUE       */
  5694.             0,                        /* SQL_TABLE_STAT  */
  5695.             null,                    /* SEQ_IN_INDEX    */
  5696.             null,                    /* COLUMN_NAME       */
  5697.             null,                    /* COLLATION       */
  5698.             0,                        /* index_id        */
  5699.             x.rows,                 /* CARDINALITY       */
  5700.             x.dpages,                /* PAGES           */
  5701.             0                        /* status           */
  5702.         FROM sysindexes x, sysobjects o
  5703.         WHERE o.id = @table_id
  5704.             AND x.id = o.id
  5705.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5706.                                                 /*    then table stats are in */
  5707.                                                 /*    a row with indid =0        */
  5708.  
  5709.     if @is_unique <> 'Y'    /* If all indexes desired */
  5710.         SELECT
  5711.             TABLE_QUALIFIER,
  5712.             TABLE_OWNER,
  5713.             TABLE_NAME,
  5714.             NON_UNIQUE,
  5715.             INDEX_QUALIFIER,
  5716.             INDEX_NAME,
  5717.             TYPE,
  5718.             SEQ_IN_INDEX,
  5719.             COLUMN_NAME,
  5720.             COLLATION,
  5721.             CARDINALITY,
  5722.             PAGES,
  5723.             FILTER_CONDITION = convert(varchar(128),null)
  5724.         FROM #TmpIndex
  5725.         WHERE
  5726.             INDEX_NAME like @index_name /* If matching name */
  5727.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5728.         ORDER BY 4, 7, 6, 8
  5729.     else                    /* If only unique indexes desired */
  5730.         SELECT
  5731.             TABLE_QUALIFIER,
  5732.             TABLE_OWNER,
  5733.             TABLE_NAME,
  5734.             NON_UNIQUE,
  5735.             INDEX_QUALIFIER,
  5736.             INDEX_NAME,
  5737.             TYPE,
  5738.             SEQ_IN_INDEX,
  5739.             COLUMN_NAME,
  5740.             COLLATION,
  5741.             CARDINALITY,
  5742.             PAGES,
  5743.             FILTER_CONDITION = convert(varchar(128),null)
  5744.         FROM #TmpIndex
  5745.         WHERE
  5746.             (NON_UNIQUE = 0             /* If unique */
  5747.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  5748.             and (INDEX_NAME like @index_name    /* If matching name */
  5749.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  5750.         ORDER BY 4, 7, 6, 8
  5751.  
  5752.     DROP TABLE #TmpIndex
  5753. go
  5754.  
  5755. if (charindex('7.00', @@version) = 0)
  5756. begin
  5757.     print ''
  5758.     print ''
  5759.     print 'Warning:'
  5760.     print 'you are installing the stored procedures '
  5761.     print 'on a pre 7.0 SQL Server.'
  5762.     print 'Ignore the following errors.'
  5763. end
  5764. else
  5765.     drop proc sp_statistics
  5766. go
  5767.  
  5768. /*    Procedure for 7.0 server */
  5769. CREATE PROCEDURE sp_statistics (
  5770.                  @table_name        sysname,
  5771.                  @table_owner        sysname = null,
  5772.                  @table_qualifier    sysname = null,
  5773.                  @index_name        sysname = '%',
  5774.                  @is_unique         char(1) = 'N',
  5775.                  @accuracy            char(1) = 'Q')
  5776. AS
  5777.     set nocount on
  5778.     DECLARE @indid                int
  5779.     DECLARE @lastindid            int
  5780.     DECLARE @table_id            int
  5781.     DECLARE @full_table_name    nvarchar(257)
  5782.  
  5783.     create table #TmpIndex(
  5784.         TABLE_QUALIFIER sysname NULL,
  5785.         TABLE_OWNER     sysname NULL,
  5786.         TABLE_NAME        sysname NOT NULL,
  5787.         INDEX_QUALIFIER sysname null,
  5788.         INDEX_NAME        sysname null,
  5789.         NON_UNIQUE        smallint null,
  5790.         TYPE            smallint NOT NULL,
  5791.         SEQ_IN_INDEX    smallint null,
  5792.         COLUMN_NAME     sysname null,
  5793.         COLLATION        char(1) null,
  5794.         index_id        int null,
  5795.         CARDINALITY     int null,
  5796.         PAGES            int null,
  5797.         status            int NOT NULL)
  5798.  
  5799.     if @table_qualifier is not null
  5800.     begin
  5801.         if db_name() <> @table_qualifier
  5802.         begin    /* If qualifier doesn't match current database */
  5803.             raiserror (15250, -1,-1)
  5804.             return
  5805.         end
  5806.     end
  5807.  
  5808.     if @accuracy not in ('Q','E')
  5809.         begin
  5810.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  5811.             return
  5812.         end
  5813.  
  5814.     if @table_owner is null
  5815.     begin    /* If unqualified table name */
  5816.         SELECT @full_table_name = quotename(@table_name)
  5817.     end
  5818.     else
  5819.     begin    /* Qualified table name */
  5820.         if @table_owner = ''
  5821.         begin    /* If empty owner name */
  5822.             SELECT @full_table_name = quotename(@table_owner)
  5823.         end
  5824.         else
  5825.         begin
  5826.             SELECT @full_table_name = quotename(@table_owner) +
  5827.                 '.' + quotename(@table_name)
  5828.         end
  5829.     end
  5830.     /*    Get Object ID */
  5831.     SELECT @table_id = object_id(@full_table_name)
  5832.  
  5833.     /*    Start at lowest index id */
  5834.     SELECT @indid = min(indid)
  5835.     FROM sysindexes
  5836.     WHERE not (@table_id is null)
  5837.         AND id = @table_id
  5838.         AND indid > 0
  5839.         AND indid < 255
  5840.  
  5841.     /* Create a temp table to correct the ordinal position of the columns */
  5842.     create table #TmpColumns
  5843.     (ordinal int identity(1,1),
  5844.      colid   smallint not null)
  5845.  
  5846.     /* Load columns into the temp table */
  5847.     insert into #TmpColumns (colid)
  5848.     select c.colid
  5849.     from syscolumns c
  5850.     where c.id = @table_id
  5851.     order by c.colid
  5852.     
  5853.     WHILE @indid is not NULL
  5854.     BEGIN
  5855.         INSERT #TmpIndex    /* Add all columns that are in index */
  5856.             SELECT
  5857.                 DB_NAME(),                        /* TABLE_QUALIFIER */
  5858.                 USER_NAME(o.uid),                    /* TABLE_OWNER       */
  5859.                 o.name,                         /* TABLE_NAME       */
  5860.                 o.name,                         /* INDEX_QUALIFIER */
  5861.                 x.name,                         /* INDEX_NAME       */
  5862.                 case                            /* NON_UNIQUE       */
  5863.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  5864.                     else 0                        /* Unique index    */
  5865.                 end,
  5866.                 case                            /* TYPE        */
  5867.                     when @indid > 1 then 3                /* Non-Clustered   */
  5868.                     else 1                        /* Clustered index */
  5869.                 end,
  5870.                 tc.ordinal,                        /* SEQ_IN_INDEX    */
  5871.                 INDEX_COL(@full_table_name, indid, tc.ordinal),        /* COLUMN_NAME       */
  5872.                 'A',                            /* COLLATION       */
  5873.                 @indid,                         /* index_id        */
  5874.                 case                            /* CARDINALITY       */
  5875.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5876.                     else x.rows                     /* Clustered index */
  5877.                 end,
  5878.                 case                            /* PAGES       */
  5879.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5880.                     else x.dpages                    /* Clustered index */
  5881.                 end,
  5882.                 x.status                        /* status       */
  5883.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  5884.             WHERE
  5885.                 not (@table_id is null)
  5886.                 AND x.id = @table_id
  5887.                 AND x.id = o.id
  5888.                 AND x.id = c.id
  5889.                 AND tc.colid = c.colid
  5890.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5891.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  5892.                 AND indid = @indid
  5893.                 AND (x.status&2 = 2
  5894.                     OR @is_unique <> 'Y')
  5895.                 AND (x.status&32) = 0
  5896.         /*
  5897.         **      Now move @indid to the next index.
  5898.         */
  5899.         SELECT @lastindid = @indid
  5900.         SELECT @indid = NULL
  5901.  
  5902.         SELECT @indid = min(indid)
  5903.         FROM sysindexes
  5904.         WHERE not (@table_id is null)
  5905.             AND id = @table_id
  5906.             AND indid > @lastindid
  5907.             AND indid < 255
  5908.     END
  5909.  
  5910.     /* now add row for table statistics */
  5911.     INSERT #TmpIndex
  5912.         SELECT
  5913.             DB_NAME(),                /* TABLE_QUALIFIER */
  5914.             USER_NAME(o.uid),            /* TABLE_OWNER       */
  5915.             o.name,                 /* TABLE_NAME       */
  5916.             null,                    /* INDEX_QUALIFIER */
  5917.             null,                    /* INDEX_NAME       */
  5918.             null,                    /* NON_UNIQUE       */
  5919.             0,                    /* SQL_TABLE_STAT  */
  5920.             null,                    /* SEQ_IN_INDEX    */
  5921.             null,                    /* COLUMN_NAME       */
  5922.             null,                    /* COLLATION       */
  5923.             0,                    /* index_id        */
  5924.             x.rows,                 /* CARDINALITY       */
  5925.             x.dpages,                /* PAGES       */
  5926.             0                    /* status       */
  5927.         FROM sysindexes x, sysobjects o
  5928.         WHERE not (@table_id is null)
  5929.             AND o.id = @table_id
  5930.             AND x.id = o.id
  5931.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5932.                                 /*    then table stats are in */
  5933.                                 /*    a row with indid =0    */
  5934.  
  5935.     SELECT
  5936.         TABLE_QUALIFIER,
  5937.         TABLE_OWNER,
  5938.         TABLE_NAME,
  5939.         NON_UNIQUE,
  5940.         INDEX_QUALIFIER,
  5941.         INDEX_NAME,
  5942.         TYPE,
  5943.         SEQ_IN_INDEX,
  5944.         COLUMN_NAME,
  5945.         COLLATION,
  5946.         CARDINALITY,
  5947.         PAGES,
  5948.         FILTER_CONDITION = convert(varchar(128),null)
  5949.     FROM #TmpIndex
  5950.     WHERE
  5951.         INDEX_NAME like @index_name     /* If matching name      */
  5952.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5953.     ORDER BY 4, 7, 6, 8
  5954.  
  5955.     DROP TABLE #TmpIndex, #TmpColumns
  5956. go
  5957.  
  5958. grant execute on sp_statistics to public
  5959. go
  5960.  
  5961. dump tran master with no_log
  5962. go
  5963.  
  5964. print 'creating sp_stored_procedures'
  5965. go
  5966.  
  5967.  
  5968. /* pre 7.00 version */
  5969. create procedure sp_stored_procedures(
  5970.                         @sp_name        varchar(102) = null,
  5971.                         @sp_owner        varchar(96) = null,
  5972.                         @sp_qualifier    varchar(32) = null)
  5973. as
  5974.     declare @proc_type smallint
  5975.  
  5976.     if @sp_qualifier is not null
  5977.     begin
  5978.         if db_name() <> @sp_qualifier
  5979.         begin
  5980.             if @sp_qualifier = ''
  5981.             begin
  5982.                 /* in this case, we need to return an empty result set */
  5983.                 /* because the user has requested a database with an empty name */
  5984.                 select @sp_name = ''
  5985.                 select @sp_owner = ''
  5986.             end else
  5987.             begin    /* If qualifier doesn't match current database */
  5988.                 raiserror 20001 '~~Rush_51~~'
  5989.                 return
  5990.             end
  5991.         end
  5992.     end
  5993.  
  5994.     if @sp_name is null
  5995.     begin  /*  If procedure name not supplied, match all */
  5996.         select @sp_name = '%'
  5997.     end
  5998.     else begin
  5999.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  6000.         begin
  6001.             if exists (select * from sysobjects
  6002.                 where uid = user_id()
  6003.                     and name = @sp_name
  6004.                     and type = 'P') /* Object type of Procedure */
  6005.             begin
  6006.                 select @sp_owner = user_name()
  6007.             end
  6008.         end
  6009.     end
  6010.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  6011.         select @sp_owner = '%'
  6012.  
  6013.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  6014.  
  6015.     select
  6016.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  6017.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  6018.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  6019.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  6020.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  6021.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  6022.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6023.         PROCEDURE_TYPE = @proc_type
  6024.     from
  6025.         sysobjects o,syscomments c,sysusers u
  6026.     where
  6027.         o.name like @sp_name
  6028.         and c.colid = 1
  6029.         and user_name(o.uid) like @sp_owner
  6030.         and o.type = 'P'        /* Object type of Procedure */
  6031.         and c.id = o.id
  6032.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  6033.         and (suser_id() = 1     /* User is the System Administrator */
  6034.             or o.uid = user_id()    /* User created the object */
  6035.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6036.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6037.              from sysprotects p
  6038.              /* outer join to correlate with all rows in sysobjects */
  6039.              where p.id =* o.id
  6040.                  /*  get rows for public,current user,user's group */
  6041.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6042.                  /* check for SELECT,EXECUTE privilege */
  6043.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  6044.             ) = 1     /* final magic...compare Grants    */
  6045.         )
  6046.     order by 1, 2, 3
  6047. go
  6048.  
  6049. grant execute on sp_stored_procedures to public
  6050. go
  6051.  
  6052.  
  6053. if (charindex('7.00', @@version) > 0)
  6054.     drop procedure sp_stored_procedures
  6055. else
  6056. begin
  6057.     print ''
  6058.     print ''
  6059.     print 'Warning:'
  6060.     print 'you are installing the stored procedures '
  6061.     print 'on a pre 7.0 SQL Server.'
  6062.     print 'Ignore the following errors.'
  6063. end
  6064. go
  6065.  
  6066.  
  6067. /* 7.00 version */
  6068. create procedure sp_stored_procedures(
  6069.                         @sp_name        nvarchar(390) = null,
  6070.                         @sp_owner        nvarchar(384) = null,
  6071.                         @sp_qualifier    sysname = null)
  6072. as
  6073.     declare @proc_type smallint
  6074.  
  6075.     if @sp_qualifier is not null
  6076.     begin
  6077.         if db_name() <> @sp_qualifier
  6078.         begin
  6079.             if @sp_qualifier = ''
  6080.             begin
  6081.                 /* in this case, we need to return an empty result set */
  6082.                 /* because the user has requested a database with an empty name */
  6083.                 select @sp_name = ''
  6084.                 select @sp_owner = ''
  6085.             end else
  6086.             begin    /* If qualifier doesn't match current database */
  6087.                 raiserror (15250, -1,-1)
  6088.                 return
  6089.             end
  6090.         end
  6091.     end
  6092.  
  6093.     if @sp_name is null
  6094.     begin  /*  If procedure name not supplied, match all */
  6095.         select @sp_name = '%'
  6096.     end
  6097.     else begin
  6098.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  6099.         begin
  6100.             if exists (select * from sysobjects
  6101.                 where uid = user_id()
  6102.                     and name = @sp_name
  6103.                     and type = 'P') /* Object type of Procedure */
  6104.             begin
  6105.                 select @sp_owner = user_name()
  6106.             end
  6107.         end
  6108.     end
  6109.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  6110.         select @sp_owner = '%'
  6111.  
  6112.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  6113.  
  6114.     select
  6115.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  6116.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  6117.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6118.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  6119.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  6120.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  6121.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6122.         PROCEDURE_TYPE = @proc_type
  6123.     from
  6124.         sysobjects o,syscomments c
  6125.     where
  6126.         o.name like @sp_name
  6127.         and c.colid = 1
  6128.         and user_name(o.uid) like @sp_owner
  6129.         and o.type = 'P'        /* Object type of Procedure */
  6130.         and c.id = o.id
  6131.         and permissions (o.id)&32 <> 0
  6132.     order by 1, 2, 3
  6133. go
  6134.  
  6135. grant execute on sp_stored_procedures to public
  6136. go
  6137.  
  6138. dump tran master with no_log
  6139. go
  6140.  
  6141.  
  6142. print 'creating sp_table_privileges'
  6143. go
  6144.  
  6145. /*    Procedure for pre 6.50 server */
  6146. CREATE PROCEDURE sp_table_privileges (
  6147.             @table_name         varchar(90),
  6148.             @table_owner        varchar(90) = null,
  6149.             @table_qualifier    varchar(32) = null)
  6150. as
  6151.     set nocount on
  6152.  
  6153.     declare @table_id    int,
  6154.             @owner_id     int,
  6155.             @full_table_name char(181)
  6156.     declare @refconst int
  6157.  
  6158.     select @refconst = 1
  6159.     if    (charindex('6.00', @@version) = 0)
  6160.         select @refconst = NULL
  6161.  
  6162.     if @table_qualifier is not null
  6163.     begin
  6164.         if db_name() != @table_qualifier
  6165.         begin    /* If qualifier doesn't match current database */
  6166.             raiserror 20001 'Table qualifier must be name of current database'
  6167.             return
  6168.         end
  6169.     end
  6170.     if @table_owner is null
  6171.     begin    /* If unqualified table name */
  6172.         SELECT @full_table_name = @table_name
  6173.     end
  6174.     else
  6175.     begin    /* Qualified table name */
  6176.         SELECT @full_table_name = @table_owner + '.' + @table_name
  6177.     end
  6178.     /*    Get Object ID */
  6179.     SELECT @table_id = object_id(@full_table_name)
  6180.  
  6181.     if @@trancount != 0
  6182.     begin    /* If inside a transaction */
  6183.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  6184.         return
  6185.     end
  6186.     create table #table_priv1(
  6187.         table_qualifier            varchar(32) NOT NULL,
  6188.         table_owner             varchar(32) NOT NULL,
  6189.         table_name                varchar(32) NOT NULL,
  6190.         grantor                 varchar(32) NOT NULL,
  6191.         grantee                 varchar(32) NOT NULL,
  6192.         select_privilege        int NOT NULL,
  6193.         insert_privilege        int NOT NULL,
  6194.         update_privilege        int NOT NULL,
  6195.         delete_privilege        int NOT NULL,
  6196.         references_privilege    int NULL,
  6197.         is_grantable            varchar(3) NOT NULL,
  6198.         uid                     int NOT NULL,
  6199.         gid                     int NOT NULL)
  6200.  
  6201.     insert into #table_priv1
  6202.         select distinct
  6203.             db_name(),
  6204.             user_name(o.uid),
  6205.             o.name,
  6206.             user_name(o.uid),
  6207.             u.name,
  6208.             0,
  6209.             0,
  6210.             0,
  6211.             0,
  6212.             @refconst,
  6213.             'no',
  6214.             u.uid,
  6215.             u.gid
  6216.         from sysusers u, sysobjects o
  6217.         where o.id = @table_id and u.uid != u.gid
  6218.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  6219.                                            ** user tables, and views. */
  6220.  
  6221.     /*
  6222.     ** now add row for table owner
  6223.     */
  6224.     if exists (
  6225.         select *
  6226.             from #table_priv1
  6227.             where grantor = grantee)
  6228.     begin
  6229.         update #table_priv1
  6230.         set
  6231.             select_privilege = 1,
  6232.             update_privilege = 1,
  6233.             insert_privilege = 1,
  6234.             delete_privilege = 1,
  6235.             references_privilege = 1,
  6236.             is_grantable = 'yes'
  6237.         where grantor = grantee
  6238.     end
  6239.     else
  6240.     begin
  6241.         insert into #table_priv1
  6242.             select    db_name(),
  6243.                 user_name(o.uid),
  6244.                 o.name,
  6245.                 user_name(o.uid),
  6246.                 user_name(o.uid),
  6247.                 1,
  6248.                 1,
  6249.                 1,
  6250.                 1,
  6251.                 @refconst,
  6252.                 'yes',
  6253.                 o.uid,
  6254.                 u.gid
  6255.             from sysobjects o, sysusers u
  6256.             where o.id = @table_id and u.uid = o.uid
  6257.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  6258.                                            ** user tables, and views. */
  6259.  
  6260.     end
  6261.  
  6262.     update #table_priv1
  6263.     set select_privilege = 1
  6264.     where
  6265.         exists (
  6266.             select * from sysprotects
  6267.             where
  6268.                 id = @table_id
  6269.                 and (#table_priv1.uid = uid
  6270.                     or #table_priv1.gid = uid
  6271.                     or uid = 0)
  6272.                 and protecttype = 205
  6273.                 and action = 193)
  6274.         and not exists (
  6275.             select * from sysprotects
  6276.             where
  6277.                 id = @table_id
  6278.                 and (#table_priv1.uid = uid
  6279.                     or #table_priv1.gid = uid
  6280.                     or uid = 0)
  6281.                 and protecttype = 206
  6282.                 and action = 193)
  6283.  
  6284.     update #table_priv1
  6285.     set insert_privilege = 1
  6286.     where
  6287.         exists (
  6288.             select * from sysprotects
  6289.             where
  6290.                 id = @table_id
  6291.                 and (#table_priv1.uid = uid
  6292.                     or #table_priv1.gid = uid
  6293.                     or uid = 0)
  6294.                 and protecttype = 205
  6295.                 and action = 195)
  6296.         and not exists (
  6297.             select * from sysprotects
  6298.             where
  6299.                 id = @table_id
  6300.                 and (#table_priv1.uid = uid
  6301.                     or #table_priv1.gid = uid
  6302.                     or uid = 0)
  6303.                 and protecttype = 206
  6304.                 and action = 195)
  6305.  
  6306.     update #table_priv1
  6307.     set delete_privilege = 1
  6308.     where
  6309.         exists (
  6310.             select * from sysprotects
  6311.             where
  6312.                 id = @table_id
  6313.                 and (#table_priv1.uid = uid
  6314.                     or #table_priv1.gid = uid
  6315.                     or uid = 0)
  6316.                 and protecttype = 205
  6317.                 and action = 196)
  6318.         and not exists (select * from sysprotects
  6319.             where
  6320.                 id = @table_id
  6321.                 and (#table_priv1.uid = uid
  6322.                     or #table_priv1.gid = uid
  6323.                     or uid = 0)
  6324.                 and protecttype = 206
  6325.                 and action = 196)
  6326.  
  6327.     update #table_priv1
  6328.     set update_privilege = 1
  6329.     where
  6330.         exists (
  6331.             select * from sysprotects
  6332.             where
  6333.                 id = @table_id
  6334.                 and (#table_priv1.uid = uid
  6335.                     or #table_priv1.gid = uid
  6336.                     or uid = 0)
  6337.                 and protecttype = 205
  6338.                 and action = 197)
  6339.         and not exists (
  6340.             select * from sysprotects
  6341.             where
  6342.                 id = @table_id
  6343.                 and (#table_priv1.uid = uid
  6344.                     or #table_priv1.gid = uid
  6345.                     or uid = 0)
  6346.                 and protecttype = 206
  6347.                 and action = 197)
  6348.  
  6349.     update #table_priv1
  6350.     set references_privilege = 1
  6351.     where
  6352.         exists (
  6353.             select * from sysprotects
  6354.             where
  6355.                 id = @table_id
  6356.                 and (#table_priv1.uid = uid
  6357.                     or #table_priv1.gid = uid
  6358.                     or uid = 0)
  6359.                 and protecttype = 205
  6360.                 and action = 26)
  6361.         and not exists (
  6362.             select * from sysprotects
  6363.             where
  6364.                 id = @table_id
  6365.                 and (#table_priv1.uid = uid
  6366.                     or #table_priv1.gid = uid
  6367.                     or uid = 0)
  6368.                 and protecttype = 206
  6369.                 and action = 26)
  6370.  
  6371.     create table #table_priv2(
  6372.         table_qualifier varchar(32) NULL,
  6373.         table_owner     varchar(32) NULL,
  6374.         table_name        varchar(32) NOT NULL,
  6375.         grantor         varchar(32) NULL,
  6376.         grantee         varchar(32) NOT NULL,
  6377.         privilege        varchar(32) NOT NULL,
  6378.         is_grantable    varchar(3) NULL)
  6379.  
  6380.     insert into #table_priv2
  6381.         select
  6382.             table_qualifier,
  6383.             table_owner,
  6384.             table_name,
  6385.             grantor,
  6386.             grantee,
  6387.             'SELECT',
  6388.             is_grantable
  6389.         from #table_priv1
  6390.         where select_privilege = 1
  6391.  
  6392.  
  6393.     insert into #table_priv2
  6394.         select
  6395.             table_qualifier,
  6396.             table_owner,
  6397.             table_name,
  6398.             grantor,
  6399.             grantee,
  6400.             'INSERT',
  6401.             is_grantable
  6402.         from #table_priv1
  6403.         where insert_privilege = 1
  6404.  
  6405.  
  6406.     insert into #table_priv2
  6407.         select
  6408.             table_qualifier,
  6409.             table_owner,
  6410.             table_name,
  6411.             grantor,
  6412.             grantee,
  6413.             'DELETE',
  6414.             is_grantable
  6415.         from #table_priv1
  6416.         where delete_privilege = 1
  6417.  
  6418.  
  6419.     insert into #table_priv2
  6420.         select
  6421.             table_qualifier,
  6422.             table_owner,
  6423.             table_name,
  6424.             grantor,
  6425.             grantee,
  6426.             'UPDATE',
  6427.             is_grantable
  6428.         from #table_priv1
  6429.         where update_privilege = 1
  6430.  
  6431.     insert into #table_priv2
  6432.         select
  6433.             table_qualifier,
  6434.             table_owner,
  6435.             table_name,
  6436.             grantor,
  6437.             grantee,
  6438.             'REFERENCES',
  6439.             is_grantable
  6440.         from #table_priv1
  6441.         where references_privilege = 1
  6442.  
  6443.  
  6444.     select * from #table_priv2
  6445.     order by table_owner,table_name,privilege,grantee
  6446. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  6447. go
  6448.  
  6449. if (charindex('6.50', @@version) = 0 and
  6450.     charindex('7.00', @@version) = 0)
  6451. begin
  6452.     print ''
  6453.     print ''
  6454.     print 'Warning:'
  6455.     print 'you are installing the stored procedures '
  6456.     print 'on a pre 6.50 SQL Server.'
  6457.     print 'Ignore the following errors.'
  6458. end
  6459. else
  6460.     drop proc sp_table_privileges
  6461. go
  6462.  
  6463.  
  6464. /*    Procedure for 6.50 server */
  6465. CREATE PROCEDURE sp_table_privileges (
  6466.             @table_name         varchar(96),
  6467.             @table_owner        varchar(96) = null,
  6468.             @table_qualifier    varchar(32) = null)
  6469. as
  6470.  
  6471.     if @table_qualifier is not null
  6472.     begin
  6473.         if db_name() <> @table_qualifier
  6474.         begin    /* If qualifier doesn't match current database */
  6475.             raiserror (15250, -1,-1)
  6476.             return
  6477.         end
  6478.     end
  6479.     if @table_name is null
  6480.         select @table_name = '%'
  6481.     if @table_owner is null /* If no owner supplied, force wildcard */
  6482.         select @table_owner = '%'
  6483.  
  6484.     select
  6485.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  6486.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  6487.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  6488.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  6489.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  6490.         convert(varchar(32),case p.action
  6491.              when 193 then 'SELECT'
  6492.              when 195 then 'INSERT'
  6493.              when 196 then 'DELETE'
  6494.              when 197 then 'UPDATE'
  6495.              else 'REFERENCES'
  6496.         end) PRIVILEGE,
  6497.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  6498.             else 'YES'
  6499.         end) IS_GRANTABLE
  6500.     from sysprotects p, sysobjects o, sysusers u
  6501.     where
  6502.         p.id = o.id
  6503.         and o.type in ('U','V','S')
  6504.         and object_name(o.id) like @table_name
  6505.         and user_name(o.uid) like @table_owner
  6506.             /* expand groups */
  6507.         and ((p.uid = u.uid and u.uid <> u.gid) or
  6508.              (p.uid = u.gid and u.uid <> u.gid))
  6509.         and p.protecttype <> 206    /* only grant rows */
  6510.         and p.action in (26,193,195,196,197)
  6511.         and o.uid <> u.uid            /* no rows for owner */
  6512.         and not exists (            /* exclude revoke'd privileges */
  6513.             select *
  6514.             from sysprotects p1
  6515.             where
  6516.                 p1.protecttype = 206
  6517.                 and p1.action = p.action
  6518.                 and p1.id = p.id
  6519.                 and p1.uid = u.uid)
  6520.     union all
  6521.     select    /*    Add rows for table owner */
  6522.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  6523.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  6524.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  6525.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  6526.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  6527.         convert(varchar(32),case v.number
  6528.             when 193 then 'SELECT'
  6529.             when 195 then 'INSERT'
  6530.             when 196 then 'DELETE'
  6531.             when 197 then 'UPDATE'
  6532.             else 'REFERENCES'
  6533.         end) PRIVILEGE,
  6534.         convert(varchar(3),'YES') IS_GRANTABLE
  6535.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6536.     where
  6537.         object_name(o.id) like @table_name
  6538.         and o.type in ('U','V','S')
  6539.         and user_name(o.uid) like @table_owner
  6540.         and u.suid = 1        /* grantor is dbo of database */
  6541.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6542.         and v.number in (26,193,195,196,197)
  6543.         and not exists (    /* exclude revoke'd privileges */
  6544.             select *
  6545.             from sysprotects p1
  6546.             where
  6547.                 p1.protecttype = 206
  6548.                 and p1.action = v.number
  6549.                 and p1.id = o.id
  6550.                 and p1.uid = o.uid)
  6551.     order by 2,3,6,5
  6552. go
  6553.  
  6554.  
  6555. if (charindex('7.00', @@version) > 0)
  6556.     drop proc sp_table_privileges
  6557. else
  6558. begin
  6559.     print ''
  6560.     print ''
  6561.     print 'Warning:'
  6562.     print 'you are installing the stored procedures '
  6563.     print 'on a pre 7.0 SQL Server.'
  6564.     print 'Ignore the following errors.'
  6565. end
  6566. go
  6567.  
  6568. /*    Procedure for 7.00 server */
  6569. CREATE PROCEDURE sp_table_privileges (
  6570.             @table_name         nvarchar(384),
  6571.             @table_owner        nvarchar(384) = null,
  6572.             @table_qualifier    sysname = null)
  6573. as
  6574.  
  6575.     if @table_qualifier is not null
  6576.     begin
  6577.         if db_name() <> @table_qualifier
  6578.         begin    /* If qualifier doesn't match current database */
  6579.             raiserror (15250, -1,-1)
  6580.             return
  6581.         end
  6582.     end
  6583.     if @table_name is null
  6584.         select @table_name = '%'
  6585.     if @table_owner is null /* If no owner supplied, force wildcard */
  6586.         select @table_owner = '%'
  6587.  
  6588.     select
  6589.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6590.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6591.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6592.         convert(sysname,user_name(p.grantor)) GRANTOR,
  6593.         convert(sysname,user_name(u.uid)) GRANTEE,
  6594.         convert(varchar(32),case p.action
  6595.              when 193 then 'SELECT'
  6596.              when 195 then 'INSERT'
  6597.              when 196 then 'DELETE'
  6598.              when 197 then 'UPDATE'
  6599.              else 'REFERENCES'
  6600.         end) PRIVILEGE,
  6601.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  6602.             else 'YES'
  6603.         end) IS_GRANTABLE
  6604.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  6605.     where
  6606.         p.id = o.id
  6607.         and o.type in ('U','V','S')
  6608.         and object_name(o.id) like @table_name
  6609.         and user_name(o.uid) like @table_owner
  6610.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  6611.         and (u.uid > 0 and u.uid < 16384)
  6612.         and ((p.uid = u.uid) or
  6613.              (p.uid = m.groupuid and u.uid = m.memberuid))
  6614.         and p.protecttype <> 206    /* only grant rows */
  6615.         and p.action in (26,193,195,196,197)
  6616.         and o.uid <> u.uid            /* no rows for owner */
  6617.         and not exists (            /* exclude revoke'd privileges */
  6618.             select *
  6619.             from sysprotects p1
  6620.             where
  6621.                 p1.protecttype = 206
  6622.                 and p1.action = p.action
  6623.                 and p1.id = p.id
  6624.                 and p1.uid = u.uid)
  6625.     union all
  6626.     select    /*    Add rows for table owner */
  6627.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6628.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6629.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6630.         convert(sysname,user_name(u.uid)) GRANTOR,
  6631.         convert(sysname,user_name(o.uid)) GRANTEE,
  6632.         convert(varchar(32),case v.number
  6633.             when 193 then 'SELECT'
  6634.             when 195 then 'INSERT'
  6635.             when 196 then 'DELETE'
  6636.             when 197 then 'UPDATE'
  6637.             else 'REFERENCES'
  6638.         end) PRIVILEGE,
  6639.         convert(varchar(3),'YES') IS_GRANTABLE
  6640.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6641.     where
  6642.         object_name(o.id) like @table_name
  6643.         and o.type in ('U','V','S')
  6644.         and user_name(o.uid) like @table_owner
  6645.         and u.uid = 1        /* grantor is 'dbo' of database */
  6646.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6647.         and v.number in (26,193,195,196,197)
  6648.         and not exists (    /* exclude revoke'd privileges */
  6649.             select *
  6650.             from sysprotects p1
  6651.             where
  6652.                 p1.protecttype = 206
  6653.                 and p1.action = v.number
  6654.                 and p1.id = o.id
  6655.                 and p1.uid = o.uid)
  6656.     order by 2,3,6,5
  6657. go
  6658.  
  6659. grant execute on sp_table_privileges to public
  6660. go
  6661.  
  6662. dump tran master with no_log
  6663. go
  6664.  
  6665. print 'creating sp_tables'
  6666. go
  6667.  
  6668. /*    Procedure for 6.50 and earlier servers */
  6669. create procedure sp_tables(
  6670.                @table_name        varchar(96)    = null,
  6671.                @table_owner     varchar(96)    = null,
  6672.                @table_qualifier varchar(32)    = null,
  6673.                @table_type        varchar(100) = null)
  6674. as
  6675.     declare @type1 varchar(3)
  6676.     declare @tableindex int
  6677.  
  6678.  
  6679.     /* Special feature #1:    enumerate databases when owner and name
  6680.          are blank but qualifier is explicitly '%'.  */
  6681.     if @table_qualifier = '%' and
  6682.         @table_owner = '' and
  6683.         @table_name = ''
  6684.     begin    /* If enumerating databases */
  6685.         select
  6686.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  6687.             TABLE_OWNER = convert(varchar(32),null),
  6688.             TABLE_NAME = convert(varchar(32),null),
  6689.             TABLE_TYPE = convert(varchar(32),null),
  6690.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6691.         from master.dbo.sysdatabases d
  6692.         where d.name <> 'model'    /* eliminate MODEL database */
  6693.         order by 1
  6694.     end
  6695.  
  6696.     /* Special feature #2:    enumerate owners when qualifier and name
  6697.          are blank but owner is explicitly '%'.  */
  6698.     else if @table_qualifier = '' and
  6699.         @table_owner = '%' and
  6700.         @table_name = ''
  6701.     begin    /* If enumerating owners */
  6702.         select distinct
  6703.             TABLE_QUALIFIER = convert(varchar(32),null),
  6704.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  6705.             TABLE_NAME = convert(varchar(32),null),
  6706.             TABLE_TYPE = convert(varchar(32),null),
  6707.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6708.         from sysobjects
  6709.         order by 2
  6710.     end
  6711.  
  6712.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6713.          name are blank but table type is explicitly '%'.    */
  6714.     else if @table_qualifier = '' and
  6715.         @table_owner = '' and
  6716.         @table_name = '' and
  6717.         @table_type = '%'
  6718.     begin    /* If enumerating table types */
  6719.         select
  6720.             TABLE_QUALIFIER = convert(varchar(32),null),
  6721.             TABLE_OWNER = convert(varchar(32),null),
  6722.             TABLE_NAME = convert(varchar(32),null),
  6723.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6724.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6725.         from sysobjects o, syscolumns c
  6726.         where o.id=c.id and o.name='sysusers' and colid<=3
  6727.     end
  6728.  
  6729.     else
  6730.     begin /* end of special features - do normal processing */
  6731.         if @table_qualifier is not null
  6732.         begin
  6733.             if db_name() <> @table_qualifier
  6734.             begin
  6735.                 if @table_qualifier = ''
  6736.                 begin  /* If empty qualifier supplied */
  6737.                     /* Force an empty result set */
  6738.                     select @table_name = ''
  6739.                     select @table_owner = ''
  6740.                 end
  6741.                 else
  6742.                 begin    /* If qualifier doesn't match current database */
  6743.                     raiserror 20001 '~~Rush_5~~'
  6744.                     return
  6745.                 end
  6746.             end
  6747.         end
  6748.         if @table_type is null
  6749.         begin    /* Select all ODBC supported table types */
  6750.             select @type1 = 'SUV'
  6751.         end
  6752.         else
  6753.         begin
  6754.             /*    TableType is case sensitive if CS server */
  6755.             select @type1 = null
  6756.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6757.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6758.             if (charindex('''TABLE''',@table_type) <> 0)
  6759.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6760.             if (charindex('''VIEW''',@table_type) <> 0)
  6761.                 select @type1 = @type1 + 'V'    /* Add Views */
  6762.         end
  6763.         if @table_name is null
  6764.         begin    /*    If table name not supplied, match all */
  6765.             select @table_name = '%'
  6766.         end
  6767.         else
  6768.         begin
  6769.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6770.             begin    /* If owner not specified and table is specified */
  6771.                 if exists (select * from sysobjects
  6772.                     where uid = user_id()
  6773.                     and name = @table_name
  6774.                     and (type = 'U' or type = 'V' or type = 'S'))
  6775.                 begin    /* Override supplied owner w/owner of table */
  6776.                     select @table_owner = user_name()
  6777.                 end
  6778.             end
  6779.         end
  6780.         if @table_owner is null /* If no owner supplied, force wildcard */
  6781.             select @table_owner = '%'
  6782.         select
  6783.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  6784.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  6785.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  6786.             TABLE_TYPE = convert(varchar(32),rtrim(
  6787.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6788.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6789.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6790.         from sysusers u, sysobjects o
  6791.         where
  6792.             o.name like @table_name
  6793.             and user_name(o.uid) like @table_owner
  6794.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6795.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  6796.             and (
  6797.                 suser_id() = 1     /* User is the System Administrator */
  6798.                 or o.uid = user_id()     /* User created the object */
  6799.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6800.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6801.                     from sysprotects p
  6802.                     /* outer join to correlate with all rows in sysobjects */
  6803.                     where p.id =* o.id
  6804.                         /* get rows for public,current user,user's group */
  6805.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6806.                         /* check for SELECT,EXECUTE privilege */
  6807.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  6808.                     ) = 1    /* final magic...compare Grants      */
  6809.             )
  6810.         order by 4, 1, 2, 3
  6811.     end
  6812. go
  6813.  
  6814. if (charindex('7.00', @@version) > 0)
  6815.     drop procedure sp_tables
  6816. else
  6817. begin
  6818.     print ''
  6819.     print ''
  6820.     print 'Warning:'
  6821.     print 'you are installing the stored procedures '
  6822.     print 'on a pre 7.0 SQL Server.'
  6823.     print 'Ignore the following errors.'
  6824. end
  6825. go
  6826.  
  6827. /*    Procedure for 7.00 server */
  6828. create procedure sp_tables(
  6829.                @table_name        nvarchar(384)    = null,
  6830.                @table_owner     nvarchar(384)    = null,
  6831.                @table_qualifier sysname    = null,
  6832.                @table_type        varchar(100) = null)
  6833. as
  6834.     declare @type1 varchar(3)
  6835.     declare @tableindex int
  6836.  
  6837.  
  6838.     /* Special feature #1:    enumerate databases when owner and name
  6839.          are blank but qualifier is explicitly '%'.  */
  6840.     if @table_qualifier = '%' and
  6841.         @table_owner = '' and
  6842.         @table_name = ''
  6843.     begin    /* If enumerating databases */
  6844.         select
  6845.             TABLE_QUALIFIER = convert(sysname,d.name),
  6846.             TABLE_OWNER = convert(sysname,null),
  6847.             TABLE_NAME = convert(sysname,null),
  6848.             TABLE_TYPE = convert(varchar(32),null),
  6849.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6850.         from master.dbo.sysdatabases d
  6851.         where d.name <> 'model'    /* eliminate MODEL database */
  6852.         order by 1
  6853.     end
  6854.  
  6855.     /* Special feature #2:    enumerate owners when qualifier and name
  6856.          are blank but owner is explicitly '%'.  */
  6857.     else if @table_qualifier = '' and
  6858.         @table_owner = '%' and
  6859.         @table_name = ''
  6860.     begin    /* If enumerating owners */
  6861.         select distinct
  6862.             TABLE_QUALIFIER = convert(sysname,null),
  6863.             TABLE_OWNER = convert(sysname,user_name(uid)),
  6864.             TABLE_NAME = convert(sysname,null),
  6865.             TABLE_TYPE = convert(varchar(32),null),
  6866.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6867.         from sysobjects
  6868.         order by 2
  6869.     end
  6870.  
  6871.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6872.          name are blank but table type is explicitly '%'.    */
  6873.     else if @table_qualifier = '' and
  6874.         @table_owner = '' and
  6875.         @table_name = '' and
  6876.         @table_type = '%'
  6877.     begin    /* If enumerating table types */
  6878.         select
  6879.             TABLE_QUALIFIER = convert(sysname,null),
  6880.             TABLE_OWNER = convert(sysname,null),
  6881.             TABLE_NAME = convert(sysname,null),
  6882.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6883.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6884.         from sysobjects o, syscolumns c
  6885.         where o.id=c.id and o.name='sysusers' and colid<=3
  6886.     end
  6887.  
  6888.     else
  6889.     begin /* end of special features - do normal processing */
  6890.         if @table_qualifier is not null
  6891.         begin
  6892.             if db_name() <> @table_qualifier
  6893.             begin
  6894.                 if @table_qualifier = ''
  6895.                 begin  /* If empty qualifier supplied */
  6896.                     /* Force an empty result set */
  6897.                     select @table_name = ''
  6898.                     select @table_owner = ''
  6899.                 end
  6900.                 else
  6901.                 begin    /* If qualifier doesn't match current database */
  6902.                     raiserror (15250, -1,-1)
  6903.                     return
  6904.                 end
  6905.             end
  6906.         end
  6907.         if @table_type is null
  6908.         begin    /* Select all ODBC supported table types */
  6909.             select @type1 = 'SUV'
  6910.         end
  6911.         else
  6912.         begin
  6913.             /*    TableType is case sensitive if CS server */
  6914.             select @type1 = ''
  6915.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6916.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6917.             if (charindex('''TABLE''',@table_type) <> 0)
  6918.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6919.             if (charindex('''VIEW''',@table_type) <> 0)
  6920.                 select @type1 = @type1 + 'V'    /* Add Views */
  6921.         end
  6922.         if @table_name is null
  6923.         begin    /*    If table name not supplied, match all */
  6924.             select @table_name = '%'
  6925.         end
  6926.         else
  6927.         begin
  6928.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6929.             begin    /* If owner not specified and table is specified */
  6930.                 if exists (select * from sysobjects
  6931.                     where uid = user_id()
  6932.                     and name = @table_name
  6933.                     and (type = 'U' or type = 'V' or type = 'S'))
  6934.                 begin    /* Override supplied owner w/owner of table */
  6935.                     select @table_owner = user_name()
  6936.                 end
  6937.             end
  6938.         end
  6939.         if @table_owner is null /* If no owner supplied, force wildcard */
  6940.             select @table_owner = '%'
  6941.         select
  6942.             TABLE_QUALIFIER = convert(sysname,db_name()),
  6943.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  6944.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  6945.             TABLE_TYPE = convert(varchar(32),rtrim(
  6946.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6947.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6948.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6949.         from sysobjects o
  6950.         where
  6951.             o.name like @table_name
  6952.             and user_name(o.uid) like @table_owner
  6953.             and o.type in ('U','V','S')
  6954.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6955.             and permissions (o.id)&4096 <> 0
  6956.         order by 4, 1, 2, 3
  6957.     end
  6958. go
  6959.  
  6960.  
  6961. grant execute on sp_tables to public
  6962. go
  6963.  
  6964. dump tran master with no_log
  6965. go
  6966.  
  6967. /*-----------------------------------------------------------------------------*/
  6968. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  6969. /*-----------------------------------------------------------------------------*/
  6970.  
  6971. if object_id('sp_linkedservers', 'P') is not null
  6972.     drop proc sp_linkedservers
  6973. go
  6974. raiserror(15339,-1,-1,'sp_linkedservers')
  6975. go
  6976. create proc sp_linkedservers as
  6977.     select
  6978.         SRV_NAME = srvname,
  6979.         SRV_PROVIDERNAME = providername,
  6980.         SRV_PRODUCT = srvproduct,
  6981.         SRV_DATASOURCE = datasource,
  6982.         SRV_PROVIDERSTRING = providerstring,
  6983.         SRV_LOCATION = location,
  6984.         SRV_CAT = catalog
  6985.     from master.dbo.sysservers
  6986.     order by 1
  6987. go
  6988. grant execute on sp_linkedservers to public
  6989. go
  6990.  
  6991.  
  6992. if object_id('sp_catalogs', 'P') is not null
  6993.     drop proc sp_catalogs
  6994. go
  6995. raiserror(15339,-1,-1,'sp_catalogs')
  6996. go
  6997. create procedure sp_catalogs(
  6998.     @server_name        sysname)
  6999. as
  7000.     select
  7001.         CATALOG_NAME,
  7002.         convert (nvarchar(255),DESCRIPTION)
  7003.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  7004.     order by CATALOG_NAME
  7005. go
  7006. grant execute on sp_catalogs to public
  7007. go
  7008.  
  7009.  
  7010. if object_id('sp_tables_ex', 'P') is not null
  7011.     drop proc sp_tables_ex
  7012. go
  7013. raiserror(15339,-1,-1,'sp_tables_ex')
  7014. go
  7015. create procedure sp_tables_ex(
  7016.     @table_server        sysname,
  7017.     @table_name            sysname = null,
  7018.     @table_schema        sysname = null,
  7019.     @table_catalog        sysname = null,
  7020.     @table_type            sysname = null)
  7021. as
  7022.     declare @table_catalog_param        sysname
  7023.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  7024.         (isnull(charindex('[', @table_catalog),0) = 0) and
  7025.         (isnull(charindex('_', @table_catalog),0) = 0))
  7026.         select @table_catalog_param = @table_catalog
  7027.     else
  7028.         select @table_catalog_param = null
  7029.  
  7030.     if ((isnull(charindex('%', @table_name),0) = 0) and
  7031.         (isnull(charindex('[', @table_name),0) = 0) and
  7032.         (isnull(charindex('_', @table_name),0) = 0))
  7033.     begin    /*    If no wild carding */
  7034.         select
  7035.             TABLE_CAT = TABLE_CATALOG,
  7036.             TABLE_SCHEM = TABLE_SCHEMA,
  7037.             TABLE_NAME = TABLE_NAME,
  7038.             TABLE_TYPE = TABLE_TYPE,
  7039.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  7040.         from master.dbo.SYSREMOTE_TABLES <
  7041.                     @table_server,
  7042.                     @table_catalog_param,
  7043.                     NULL,
  7044.                     @table_name,
  7045.                     NULL >
  7046.         where (TABLE_SCHEMA like @table_schema
  7047.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7048.             and (TABLE_CATALOG like @table_catalog
  7049.             or     @table_catalog is NULL)
  7050.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  7051.             or     @table_type is NULL)
  7052.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  7053.     end
  7054.     else
  7055.     begin    /*    If wild carding */
  7056.         select
  7057.             TABLE_CAT = TABLE_CATALOG,
  7058.             TABLE_SCHEM = TABLE_SCHEMA,
  7059.             TABLE_NAME = TABLE_NAME,
  7060.             TABLE_TYPE = TABLE_TYPE,
  7061.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  7062.         from master.dbo.SYSREMOTE_TABLES <
  7063.                     @table_server,
  7064.                     @table_catalog_param,
  7065.                     NULL,
  7066.                     NULL,
  7067.                     NULL >
  7068.         where (TABLE_SCHEMA like @table_schema
  7069.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7070.             and (TABLE_CATALOG like @table_catalog
  7071.             or     @table_catalog is NULL)
  7072.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  7073.             or     @table_type is NULL)
  7074.             and TABLE_NAME like @table_name
  7075.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  7076.     end
  7077. go
  7078. grant execute on sp_tables_ex to public
  7079. go
  7080.  
  7081.  
  7082. if object_id('sp_columns_ex', 'P') is not null
  7083.     drop proc sp_columns_ex
  7084. go
  7085. raiserror(15339,-1,-1,'sp_columns_ex')
  7086. go
  7087. create procedure sp_columns_ex(
  7088.     @table_server        sysname,
  7089.     @table_name            sysname = null,
  7090.     @table_schema        sysname = null,
  7091.     @table_catalog        sysname = null,
  7092.     @column_name        sysname = null,
  7093.     @ODBCVer            int = 2)
  7094. as
  7095.  
  7096.     set nocount on
  7097.     declare
  7098.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  7099.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  7100.  
  7101.     select
  7102.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  7103.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  7104.  
  7105.     declare    
  7106.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  7107.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  7108.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  7109.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  7110.     @DBTYPE_VARIANT smallint,        @DBTYPE_DECIMAL smallint,
  7111.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  7112.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  7113.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  7114.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  7115.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  7116.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  7117.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  7118.     @DBTYPE_DBTIMESTAMP smallint
  7119.  
  7120.     select
  7121.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  7122.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  7123.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  7124.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  7125.     @DBTYPE_VARIANT        = 12,    @DBTYPE_DECIMAL        = 14,
  7126.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  7127.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  7128.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  7129.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  7130.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  7131.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  7132.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  7133.     @DBTYPE_DBTIMESTAMP    = 135
  7134.  
  7135.     declare    
  7136.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  7137.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  7138.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  7139.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  7140.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  7141.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  7142.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  7143.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  7144.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  7145.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  7146.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  7147.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  7148.  
  7149.     select     
  7150.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  7151.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  7152.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  7153.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  7154.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  7155.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  7156.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  7157.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  7158.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  7159.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  7160.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  7161.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  7162.  
  7163.     declare 
  7164.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  7165.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  7166.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  7167.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  7168.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  7169.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  7170.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  7171.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  7172.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  7173.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  7174.     @ODBC_DATETIME smallint
  7175.  
  7176.     select
  7177.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  7178.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  7179.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  7180.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  7181.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  7182.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  7183.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  7184.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  7185.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  7186.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  7187.     @ODBC_DATETIME    =    
  7188.     case @ODBCVer
  7189.         when 2 then 11
  7190.         else 93
  7191.     end
  7192.  
  7193.     create table #tmp_columns
  7194.     (    TABLE_CAT sysname NULL,
  7195.         TABLE_SCHEM sysname NULL,
  7196.         TABLE_NAME sysname    NOT NULL,
  7197.         COLUMN_NAME sysname NULL,
  7198.         DATA_TYPE smallint NOT NULL,
  7199.         TYPE_NAME sysname  NULL,
  7200.         COLUMN_SIZE int NULL,
  7201.         BUFFER_LENGTH int NULL,
  7202.         DECIMAL_DIGITS smallint NULL,
  7203.         NUM_PREC_RADIX smallint NULL,
  7204.         NULLABLE smallint NOT NULL,
  7205.         REMARKS nvarchar(255) NULL,
  7206.         COLUMN_DEF nvarchar(127) NULL,
  7207.         SQL_DATA_TYPE smallint null,
  7208.         SQL_DATETIME_SUB smallint NULL,
  7209.         CHAR_OCTET_LENGTH int NULL,
  7210.         ORDINAL_POSITION smallint,
  7211.         IS_NULLABLE varchar(254) NOT NULL,
  7212.         SS_DATA_TYPE tinyint null,
  7213.         COLUMN_FLAGS int NOT NULL)
  7214.  
  7215.     if ((isnull(charindex('%', @table_name),0) = 0) and
  7216.         (isnull(charindex('[', @table_name),0) = 0) and
  7217.         (isnull(charindex('_', @table_name),0) = 0))
  7218.     begin    /*    If no wild carding */
  7219.         insert into #tmp_columns
  7220.         select
  7221.             TABLE_CAT = c.TABLE_CATALOG,
  7222.             TABLE_SCHEM = c.TABLE_SCHEMA,
  7223.             TABLE_NAME = c.TABLE_NAME,
  7224.             COLUMN_NAME = c.COLUMN_NAME,
  7225.             DATA_TYPE =
  7226.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  7227.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  7228.                 when @DBTYPE_I4                then @ODBC_INT
  7229.                 when @DBTYPE_R4                then @ODBC_REAL
  7230.                 when @DBTYPE_R8                then @ODBC_FLOAT
  7231.                 when @DBTYPE_CY                then @ODBC_MONEY
  7232.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  7233.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  7234.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  7235.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  7236.                 when @DBTYPE_BOOL            then @ODBC_BIT
  7237.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  7238.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  7239.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  7240.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  7241.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  7242.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  7243.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  7244.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  7245.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  7246.                 when @DBTYPE_GUID            then @ODBC_GUID
  7247.                 when @DBTYPE_BYTES then
  7248.                     case
  7249.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  7250.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7251.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7252.                                  p.COLUMN_SIZE = 8
  7253.                             then @ODBC_TIMESTAMP
  7254.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7255.                             then @ODBC_BINARY
  7256.                         else @ODBC_VARBINARY
  7257.                     end
  7258.                  when @DBTYPE_STR  then
  7259.                     case
  7260.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  7261.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  7262.                         else @ODBC_VARCHAR
  7263.                     end
  7264.                 when @DBTYPE_WSTR then
  7265.                     case
  7266.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7267.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7268.                         else @ODBC_NVARCHAR
  7269.                     end
  7270.                 when @DBTYPE_BSTR then
  7271.                     case
  7272.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7273.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7274.                         else @ODBC_NVARCHAR
  7275.                     end
  7276.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  7277.             end,
  7278.             TYPE_NAME = p.TYPE_NAME,
  7279.             COLUMN_SIZE =
  7280.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  7281.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7282.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  7283.             NUM_PREC_RADIX = NULL,
  7284.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  7285.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  7286.             COLUMN_DEF = c.COLUMN_DEFAULT,
  7287.             SQL_DATA_TYPE = NULL,
  7288.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  7289.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7290.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  7291.             IS_NULLABLE =
  7292.             case c.IS_NULLABLE
  7293.                 when 1 then 'YES'
  7294.                 else 'NO'
  7295.             end,
  7296.             SS_DATA_TYPE = NULL,
  7297.             COLUMN_FLAGS = c.COLUMN_FLAGS
  7298.         from master.dbo.SYSREMOTE_COLUMNS <
  7299.                     @table_server,
  7300.                     @table_catalog,
  7301.                     NULL,
  7302.                     @table_name,
  7303.                     NULL > c
  7304.             LEFT OUTER JOIN
  7305.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  7306.                     @table_server > p
  7307.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  7308.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  7309.             where (TABLE_SCHEMA like @table_schema
  7310.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7311.                 and (c.COLUMN_NAME like @column_name
  7312.                 or     @column_name is NULL)
  7313.     end
  7314.     else
  7315.     begin    /*    If wild carding */
  7316.         insert into #tmp_columns
  7317.         select
  7318.             TABLE_CAT = c.TABLE_CATALOG,
  7319.             TABLE_SCHEM = c.TABLE_SCHEMA,
  7320.             TABLE_NAME = c.TABLE_NAME,
  7321.             COLUMN_NAME = c.COLUMN_NAME,
  7322.             DATA_TYPE =
  7323.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  7324.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  7325.                 when @DBTYPE_I4                then @ODBC_INT
  7326.                 when @DBTYPE_R4                then @ODBC_REAL
  7327.                 when @DBTYPE_R8                then @ODBC_FLOAT
  7328.                 when @DBTYPE_CY                then @ODBC_MONEY
  7329.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  7330.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  7331.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  7332.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  7333.                 when @DBTYPE_BOOL            then @ODBC_BIT
  7334.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  7335.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  7336.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  7337.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  7338.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  7339.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  7340.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  7341.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  7342.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  7343.                 when @DBTYPE_GUID            then @ODBC_GUID
  7344.                 when @DBTYPE_BYTES then
  7345.                     case
  7346.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  7347.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7348.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7349.                                  p.COLUMN_SIZE = 8
  7350.                             then @ODBC_TIMESTAMP
  7351.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7352.                             then @ODBC_BINARY
  7353.                         else @ODBC_VARBINARY
  7354.                     end
  7355.                  when @DBTYPE_STR  then
  7356.                     case
  7357.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  7358.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  7359.                         else @ODBC_VARCHAR
  7360.                     end
  7361.                 when @DBTYPE_WSTR then
  7362.                     case
  7363.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7364.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7365.                         else @ODBC_NVARCHAR
  7366.                     end
  7367.                 when @DBTYPE_BSTR then
  7368.                     case
  7369.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7370.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7371.                         else @ODBC_NVARCHAR
  7372.                     end
  7373.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  7374.             end,
  7375.             TYPE_NAME = p.TYPE_NAME,
  7376.             COLUMN_SIZE =
  7377.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  7378.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7379.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  7380.             NUM_PREC_RADIX = NULL,
  7381.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  7382.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  7383.             COLUMN_DEF = c.COLUMN_DEFAULT,
  7384.             SQL_DATA_TYPE = NULL,
  7385.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  7386.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7387.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  7388.             IS_NULLABLE =
  7389.             case c.IS_NULLABLE
  7390.                 when 1 then 'YES'
  7391.                 else 'NO'
  7392.             end,
  7393.             SS_DATA_TYPE = NULL,
  7394.             COLUMN_FLAGS = c.COLUMN_FLAGS
  7395.         from master.dbo.SYSREMOTE_COLUMNS <
  7396.                     @table_server,
  7397.                     @table_catalog,
  7398.                     NULL,
  7399.                     NULL,
  7400.                     NULL > c
  7401.             LEFT OUTER JOIN
  7402.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  7403.                     @table_server > p
  7404.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  7405.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  7406.             where (TABLE_SCHEMA like @table_schema
  7407.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7408.                 and (c.COLUMN_NAME like @column_name
  7409.                 or     @column_name is NULL)
  7410.                 and c.TABLE_NAME like @table_name
  7411.     end
  7412.  
  7413.     update #tmp_columns
  7414.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  7415.         SQL_DATETIME_SUB =
  7416.         case
  7417.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  7418.             else #tmp_columns.SQL_DATETIME_SUB
  7419.         end,
  7420.         TYPE_NAME = 
  7421.         case 
  7422.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7423.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7424.                     BUFFER_LENGTH = 8
  7425.                 then 'timestamp'
  7426.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  7427.                     #tmp_columns.SQL_DATETIME_SUB = 0
  7428.                 then 'smalldatetime'
  7429.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  7430.                     #tmp_columns.COLUMN_SIZE = 10
  7431.                 then 'smallmoney'
  7432.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  7433.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  7434.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  7435.                 then spt_dt.TYPE_NAME
  7436.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  7437.         end,
  7438.         COLUMN_SIZE =
  7439.         case
  7440.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  7441.                 spt_dt.data_precision
  7442.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  7443.                     #tmp_columns.SQL_DATETIME_SUB = 0
  7444.                 then 16
  7445.             else #tmp_columns.COLUMN_SIZE
  7446.         end,
  7447.         BUFFER_LENGTH =
  7448.         case
  7449.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  7450.                 COLUMN_SIZE+2
  7451.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  7452.             else #tmp_columns.BUFFER_LENGTH
  7453.         end,
  7454.         DECIMAL_DIGITS =
  7455.         case
  7456.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  7457.                 then #tmp_columns.SQL_DATETIME_SUB
  7458.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  7459.         end,
  7460.         NUM_PREC_RADIX = spt_dt.RADIX,
  7461.         SS_DATA_TYPE = 
  7462.         case
  7463.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  7464.                 and #tmp_columns.NULLABLE = 1
  7465.                     then @ODS_INT_NULL
  7466.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  7467.                 then @ODS_TINYINT_NOT_NULL
  7468.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  7469.                 then @ODS_SMALLINT_NOT_NULL
  7470.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  7471.                 then @ODS_INT_NOT_NULL
  7472.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  7473.                 then @ODS_FLOAT_NULL
  7474.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  7475.                 then @ODS_REAL_NOT_NULL
  7476.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  7477.                 then @ODS_FLOAT_NOT_NULL
  7478.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  7479.                 then @ODS_MONEY_NULL
  7480.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  7481.                 then @ODS_MONEY_NOT_NULL
  7482.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  7483.                 then @ODS_DATETIME_NULL
  7484.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  7485.                 then @ODS_DATETIME_NOT_NULL
  7486.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  7487.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  7488.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  7489.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  7490.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  7491.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  7492.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  7493.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7494.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7495.                     BUFFER_LENGTH = 8
  7496.                 then @ODS_TIMESTAMP
  7497.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  7498.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7499.                 then  @ODS_BINARY_NULL
  7500.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  7501.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7502.                 then @ODS_BINARY_NOT_NULL
  7503.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  7504.                 then @ODS_VARBINARY    
  7505.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  7506.                 then @ODS_CHAR_NULL
  7507.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  7508.                 then @ODS_CHAR_NOT_NULL
  7509.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  7510.             else null
  7511.         end
  7512.         from master.dbo.spt_datatype_info spt_dt
  7513.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  7514.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  7515.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  7516.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  7517.  
  7518.     select
  7519.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  7520.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  7521.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  7522.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  7523.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  7524.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  7525.             SS_DATA_TYPE
  7526.         from #tmp_columns
  7527.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  7528.  
  7529.         drop table #tmp_columns
  7530.  
  7531. go
  7532. grant execute on sp_columns_ex to public
  7533. go
  7534.  
  7535.  
  7536. if object_id('sp_table_privileges_ex', 'P') is not null
  7537.     drop proc sp_table_privileges_ex
  7538. go
  7539. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  7540. go
  7541. create procedure sp_table_privileges_ex(
  7542.     @table_server        sysname,
  7543.     @table_name            sysname = null,
  7544.     @table_schema        sysname = null,
  7545.     @table_catalog        sysname = null)
  7546. as
  7547.     select
  7548.         TABLE_CAT = TABLE_CATALOG,
  7549.         TABLE_SCHEM = TABLE_SCHEMA,
  7550.         TABLE_NAME = TABLE_NAME,
  7551.         GRANTOR = GRANTOR,
  7552.         GRANTEE = GRANTEE,
  7553.         PRIVILEGE = PRIVILEGE_TYPE,
  7554.         IS_GRANTABLE = 
  7555.         case IS_GRANTABLE
  7556.             when 1 then 'YES'
  7557.             when 0 then 'NO'
  7558.             else null
  7559.         end
  7560.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  7561.                 @table_server,
  7562.                 @table_catalog,
  7563.                 NULL,
  7564.                 NULL >
  7565.     where (TABLE_SCHEMA like @table_schema
  7566.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7567.         and (TABLE_NAME like @table_name
  7568.         or     @table_name is NULL)
  7569.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  7570. go
  7571. grant execute on sp_table_privileges_ex to public
  7572. go
  7573.  
  7574.  
  7575. if object_id('sp_column_privileges_ex', 'P') is not null
  7576.     drop proc sp_column_privileges_ex
  7577. go
  7578. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  7579. go
  7580. create procedure sp_column_privileges_ex(
  7581.     @table_server        sysname,
  7582.     @table_name            sysname = null,
  7583.     @table_schema        sysname = null,
  7584.     @table_catalog        sysname = null,
  7585.     @column_name        sysname = null)
  7586. as
  7587.     select
  7588.         TABLE_CAT = TABLE_CATALOG,
  7589.         TABLE_SCHEM = TABLE_SCHEMA,
  7590.         TABLE_NAME = TABLE_NAME,
  7591.         COLUMN_NAME = COLUMN_NAME,
  7592.         GRANTOR = GRANTOR,
  7593.         GRANTEE = GRANTEE,
  7594.         PRIVILEGE = PRIVILEGE_TYPE,
  7595.         IS_GRANTABLE = 
  7596.         case IS_GRANTABLE
  7597.             when 1 then 'YES'
  7598.             when 0 then 'NO'
  7599.             else null
  7600.         end
  7601.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  7602.         @table_server,
  7603.         @table_catalog,
  7604.         @table_schema,
  7605.         @table_name,
  7606.         NULL >
  7607.     where (COLUMN_NAME like @column_name
  7608.         or @column_name is NULL)
  7609.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  7610. go
  7611. grant execute on sp_column_privileges_ex to public
  7612. go
  7613.  
  7614.  
  7615. if object_id('sp_indexes', 'P') is not null
  7616.     drop proc sp_indexes
  7617. go
  7618. raiserror(15339,-1,-1,'sp_indexes')
  7619. go
  7620. create procedure sp_indexes(
  7621.     @table_server        sysname,
  7622.     @table_name            sysname = null,
  7623.     @table_schema        sysname = null,
  7624.     @table_catalog        sysname = null,
  7625.     @index_name            sysname = null,
  7626.     @is_unique          bit = null)
  7627. as
  7628.     select
  7629.         TABLE_CAT = TABLE_CATALOG,
  7630.         TABLE_SCHEM = TABLE_SCHEMA,
  7631.         TABLE_NAME = TABLE_NAME,
  7632.         NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  7633.         INDEX_QUALIFIER = TABLE_NAME,
  7634.         INDEX_NAME = INDEX_NAME,
  7635.         TYPE = 
  7636.         case [CLUSTERED]
  7637.             when 1 then 1
  7638.             else 3
  7639.         end,
  7640.         ORDINAL_POSITION = ORDINAL_POSITION,
  7641.         COLUMN_NAME = COLUMN_NAME,
  7642.         ASC_OR_DESC = 
  7643.         case [COLLATION]
  7644.             when 1 then 'A'
  7645.             when 2 then 'D'
  7646.             else null
  7647.         end,
  7648.         CARDINALITY = CARDINALITY,
  7649.         PAGES = 
  7650.         case [CLUSTERED]
  7651.             when 1 then PAGES
  7652.             else NULL
  7653.         end,
  7654.         FILTER_CONDITION = FILTER_CONDITION
  7655.     from master.dbo.SYSREMOTE_INDEXES <
  7656.                 @table_server,
  7657.                 @table_catalog,
  7658.                 @table_schema,
  7659.                 @index_name,
  7660.                 NULL,            /* TYPE (index type) */
  7661.                 @table_name >
  7662.     where @is_unique is null or @is_unique = [UNIQUE]
  7663.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  7664. go
  7665. grant execute on sp_indexes to public
  7666. go
  7667.  
  7668.  
  7669. if object_id('sp_foreignkeys', 'P') is not null
  7670.     drop proc sp_foreignkeys
  7671. go
  7672. raiserror(15339,-1,-1,'sp_foreignkeys')
  7673. go
  7674. create procedure sp_foreignkeys(
  7675.     @table_server        sysname,
  7676.     @pktab_name         sysname = null,
  7677.     @pktab_schema       sysname = null,
  7678.     @pktab_catalog      sysname = null,
  7679.     @fktab_name         sysname = null,
  7680.     @fktab_schema       sysname = null,
  7681.     @fktab_catalog      sysname = null)
  7682. as
  7683.     select
  7684.         PKTABLE_CAT = PK_TABLE_CATALOG,
  7685.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  7686.         PKTABLE_NAME = PK_TABLE_NAME,
  7687.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  7688.         FKTABLE_CAT = FK_TABLE_CATALOG,
  7689.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  7690.         FKTABLE_NAME = FK_TABLE_NAME,
  7691.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  7692.         KEY_SEQ = ORDINAL,
  7693.         UPDATE_RULE = 
  7694.         case UPDATE_RULE
  7695.             when 'CASCADE' then 0
  7696.             when 'NO ACTION' then 1
  7697.             when 'SET NULL' then 2
  7698.             else null
  7699.         end,
  7700.         DELETE_RULE = 
  7701.         case DELETE_RULE
  7702.             when 'CASCADE' then 0
  7703.             when 'NO ACTION' then 1
  7704.             when 'SET NULL' then 2
  7705.             else null
  7706.         end,
  7707.         FK_NAME = convert(sysname, NULL),
  7708.         PK_NAME = convert(sysname, NULL),
  7709.         DEFERRABILITY = convert(smallint, null)
  7710.  
  7711.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  7712.                 @table_server,
  7713.                 @pktab_catalog,
  7714.                 @pktab_schema,
  7715.                 @pktab_name,
  7716.                 @fktab_catalog,
  7717.                 @fktab_schema,
  7718.                 @fktab_name >
  7719.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  7720. go
  7721. grant execute on sp_foreignkeys to public
  7722. go
  7723.  
  7724.  
  7725. if object_id('sp_primarykeys', 'P') is not null
  7726.     drop proc sp_primarykeys
  7727. go
  7728. raiserror(15339,-1,-1,'sp_primarykeys')
  7729. go
  7730. create procedure sp_primarykeys(
  7731.     @table_server        sysname,
  7732.     @table_name         sysname = null,
  7733.     @table_schema       sysname = null,
  7734.     @table_catalog      sysname = null)
  7735. as
  7736.     select
  7737.         TABLE_CAT = TABLE_CATALOG,
  7738.         TABLE_SCHEM = TABLE_SCHEMA,
  7739.         TABLE_NAME = TABLE_NAME,
  7740.         COLUMN_NAME = COLUMN_NAME,
  7741.         KEY_SEQ = ORDINAL,
  7742.         PK_NAME = convert(sysname, NULL)
  7743.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  7744.                 @table_server,
  7745.                 @table_catalog,
  7746.                 @table_schema,
  7747.                 @table_name >
  7748.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  7749. go
  7750. grant execute on sp_primarykeys to public
  7751. go
  7752.  
  7753. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  7754.  
  7755.  
  7756. dump tran master with no_log
  7757. go
  7758.  
  7759. if (charindex('6.50', @@version) = 0 and
  7760.     charindex('7.00', @@version) = 0)
  7761. begin
  7762.     print ''
  7763.     print ''
  7764.     print 'Warning:'
  7765.     print 'you are installing the stored procedures '
  7766.     print 'on a pre 6.50 SQL Server.'
  7767.     print 'Ignore the following errors.'
  7768. end
  7769.  
  7770. print 'creating sp_ddopen'
  7771. go
  7772.  
  7773. /*    Procedure for pre-7.00 server */
  7774. create procedure sp_ddopen(
  7775.                @handle            int output,
  7776.                @procname        sysname,
  7777.                @scrollopt        int output,
  7778.                @ccopt            int output,
  7779.                @rows            int output,
  7780.                @p1                varchar(255) = null,
  7781.                @p2                varchar(255) = null,
  7782.                @p3                varchar(255) = null,
  7783.                @p4                varchar(255) = null,
  7784.                @p5                varchar(255) = null,
  7785.                @p6                varchar(255) = null,
  7786.                @p7                int = null,
  7787.                @ODBCVer         int = 2)
  7788. as
  7789.     set nocount on
  7790.     declare @ret int
  7791.  
  7792.     if @procname = 'sp_column_privileges'
  7793.     begin
  7794.         create table #spcolpriv (
  7795.             TABLE_QUALIFIER varchar(32) null,
  7796.             TABLE_OWNER varchar(32) null,
  7797.             TABLE_NAME varchar(32)    not null,
  7798.             COLUMN_NAME varchar(32) not null,
  7799.             GRANTOR varchar(32) null,
  7800.             GRANTEE varchar(32) not null,
  7801.             PRIVILEGE varchar(32) not null,
  7802.             IS_GRANTABLE varchar(3) null
  7803.             )
  7804.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  7805.         exec @ret = sp_cursoropen @handle output,
  7806.             'select * from #spcolpriv',
  7807.             @scrollopt output, @ccopt output, @rows output
  7808.         drop table #spcolpriv
  7809.     end
  7810.     else if @procname = 'sp_columns'
  7811.     begin
  7812.         create table #spcolumns (
  7813.             TABLE_QUALIFIER varchar(32) null,
  7814.             TABLE_OWNER varchar(32) null,
  7815.             TABLE_NAME varchar(32)    not null,
  7816.             COLUMN_NAME varchar(32) not null,
  7817.             DATA_TYPE smallint not null,
  7818.             TYPE_NAME varchar(32) not null,
  7819.             "PRECISION" int null,
  7820.             LENGTH int null,
  7821.             SCALE smallint null,
  7822.             RADIX smallint null,
  7823.             NULLABLE smallint not null,
  7824.             REMARKS varchar(254) null,
  7825.             COLUMN_DEF varchar(255) null,
  7826.             SQL_DATA_TYPE smallint not null,
  7827.             SQL_DATETIME_SUB smallint null,
  7828.             CHAR_OCTET_LENGTH int null,
  7829.             ORDINAL_POSITION int not null,
  7830.             IS_NULLABLE varchar(254) null,
  7831.             SS_DATA_TYPE tinyint null
  7832.             )
  7833.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  7834.         exec @ret = sp_cursoropen @handle output,
  7835.             'select * from #spcolumns',
  7836.             @scrollopt output, @ccopt output, @rows output
  7837.         drop table #spcolumns
  7838.     end
  7839.     else if @procname = 'sp_datatype_info'
  7840.     begin
  7841.         create table #spdatatypeinfo (
  7842.             TYPE_NAME            varchar(32)  not null,
  7843.             DATA_TYPE            smallint not null,
  7844.             "PRECISION"            int null,
  7845.             LITERAL_PREFIX        varchar(32)    null,
  7846.             LITERAL_SUFFIX        varchar(32)    null,
  7847.             CREATE_PARAMS        varchar(32)    null,
  7848.             NULLABLE            smallint   not null,
  7849.             CASE_SENSITIVE        smallint   not null,
  7850.             SEARCHABLE            smallint   not null,
  7851.             UNSIGNED_ATTRIBUTE    smallint   null,
  7852.             MONEY    smallint    not null,
  7853.             AUTO_INCREMENT        smallint    null,
  7854.             LOCAL_TYPE_NAME     varchar(32) null,
  7855.             MINIMUM_SCALE        smallint     null,
  7856.             MAXIMUM_SCALE        smallint   null,
  7857.             SQL_DATA_TYPE        smallint      not null,
  7858.             SQL_DATETIME_SUB    smallint   null,
  7859.             NUM_PREC_RADIX        int     null,
  7860.             INTERVAL_PRECISION    smallint    NULL,
  7861.             USERTYPE            smallint not null)
  7862.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  7863.         exec @ret = sp_cursoropen @handle output,
  7864.             'select * from #spdatatypeinfo',
  7865.             @scrollopt output, @ccopt output, @rows output
  7866.         drop table #spdatatypeinfo
  7867.     end
  7868.     else if @procname = 'sp_fkeys'
  7869.     begin
  7870.         create table #spfkeys (
  7871.             PKTABLE_QUALIFIER varchar(32)     null,
  7872.             PKTABLE_OWNER varchar(32)    null,
  7873.             PKTABLE_NAME varchar(32)  not null,
  7874.             PKCOLUMN_NAME varchar(32)    not null,
  7875.             FKTABLE_QUALIFIER varchar(32)    null,
  7876.             FKTABLE_OWNER varchar(32)    null,
  7877.             FKTABLE_NAME varchar(32)  not null,
  7878.             FKCOLUMN_NAME varchar(32)    not null,
  7879.             KEY_SEQ smallint not null,
  7880.             UPDATE_RULE smallint null,
  7881.             DELETE_RULE smallint null,
  7882.             FK_NAME varchar(32) null,
  7883.             PK_NAME varchar(32) null,
  7884.             DEFERRABILITY smallint null
  7885.             )
  7886.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  7887.         exec @ret = sp_cursoropen @handle output,
  7888.             'select * from #spfkeys',
  7889.             @scrollopt output, @ccopt output, @rows output
  7890.         drop table #spfkeys
  7891.     end
  7892.     else if @procname = 'sp_pkeys'
  7893.     begin
  7894.         create table #sppkeys (
  7895.             TABLE_QUALIFIER varchar(32)   null,
  7896.             TABLE_OWNER varchar(32)   null,
  7897.             TABLE_NAME varchar(32)    not null,
  7898.             COLUMN_NAME varchar(32)  not null,
  7899.             KEY_SEQ smallint not null,
  7900.             PK_NAME varchar(32) null
  7901.             )
  7902.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  7903.         exec @ret = sp_cursoropen @handle output,
  7904.             'select * from #sppkeys',
  7905.             @scrollopt output, @ccopt output, @rows output
  7906.         drop table #sppkeys
  7907.     end
  7908.     else if @procname = 'sp_special_columns'
  7909.     begin
  7910.         create table #spspeccol (
  7911.             SCOPE smallint null,
  7912.             COLUMN_NAME varchar(32) not null,
  7913.             DATA_TYPE smallint not null,
  7914.             TYPE_NAME varchar(32) not null,
  7915.             "PRECISION" int null,
  7916.             LENGTH int null,
  7917.             SCALE smallint null,
  7918.             PSEUDO_COLUMN smallint null
  7919.             )
  7920.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  7921.         exec @ret = sp_cursoropen @handle output,
  7922.             'select * from #spspeccol',
  7923.             @scrollopt output, @ccopt output, @rows output
  7924.         drop table #spspeccol
  7925.     end
  7926.     else if @procname = 'sp_sproc_columns'
  7927.     begin
  7928.         create table #spproccol (
  7929.             PROCEDURE_QUALIFIER varchar(32)  null,
  7930.             PROCEDURE_OWNER varchar(32)  null,
  7931.             PROCEDURE_NAME varchar(32)    not null,
  7932.             COLUMN_NAME varchar(32) not null,
  7933.             COLUMN_TYPE smallint not null,
  7934.             DATA_TYPE smallint not null,
  7935.             TYPE_NAME varchar(32) not null,
  7936.             "PRECISION" int null,
  7937.             LENGTH int null,
  7938.             SCALE smallint null,
  7939.             RADIX smallint null,
  7940.             NULLABLE smallint not null,
  7941.             REMARKS varchar(254) null,
  7942.             COLUMN_DEF varchar(255) null,
  7943.             SQL_DATA_TYPE smallint not null,
  7944.             SQL_DATETIME_SUB smallint null,
  7945.             CHAR_OCTET_LENGTH int null,
  7946.             ORDINAL_POSITION int not null,
  7947.             IS_NULLABLE varchar(254) null,
  7948.             SS_DATA_TYPE tinyint null
  7949.             )
  7950.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  7951.         exec @ret = sp_cursoropen @handle output,
  7952.             'select * from #spproccol',
  7953.             @scrollopt output, @ccopt output, @rows output
  7954.         drop table #spproccol
  7955.     end
  7956.     else if @procname = 'sp_statistics'
  7957.     begin
  7958.         create table #spstatistics (
  7959.             TABLE_QUALIFIER varchar(32)   null,
  7960.             TABLE_OWNER varchar(32)   null,
  7961.             TABLE_NAME varchar(32)    not null,
  7962.             NON_UNIQUE smallint null,
  7963.             INDEX_QUALIFIER varchar(32) null,
  7964.             INDEX_NAME varchar(32)    null,
  7965.             TYPE smallint not null,
  7966.             SEQ_IN_INDEX smallint null,
  7967.             COLUMN_NAME varchar(32) null,
  7968.             COLLATION char(1) null,
  7969.             CARDINALITY int null,
  7970.             PAGES int null,
  7971.             FILTER_CONDITION varchar(128) null
  7972.             )
  7973.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  7974.         exec @ret = sp_cursoropen @handle output,
  7975.             'select * from #spstatistics',
  7976.             @scrollopt output, @ccopt output, @rows output
  7977.         drop table #spstatistics
  7978.     end
  7979.     else if @procname = 'sp_stored_procedures'
  7980.     begin
  7981.         create table #spprocedures (
  7982.             PROCEDURE_QUALIFIER varchar(32)  null,
  7983.             PROCEDURE_OWNER varchar(32)  null,
  7984.             PROCEDURE_NAME varchar(32)    not null,
  7985.             NUM_INPUT_PARAMS int null,
  7986.             NUM_OUTPUT_PARAMS int null,
  7987.             NUM_RESULT_SETS int null,
  7988.             REMARKS varchar(254) null,
  7989.             PROCEDURE_TYPE smallint null
  7990.             )
  7991.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  7992.         exec @ret = sp_cursoropen @handle output,
  7993.             'select * from #spprocedures',
  7994.             @scrollopt output, @ccopt output, @rows output
  7995.         drop table #spprocedures
  7996.     end
  7997.     else if @procname = 'sp_table_privileges'
  7998.     begin
  7999.         create table #sptabpriv (
  8000.             TABLE_QUALIFIER varchar(32) null,
  8001.             TABLE_OWNER varchar(32) null,
  8002.             TABLE_NAME varchar(32)    not null,
  8003.             GRANTOR varchar(32) null,
  8004.             GRANTEE varchar(32) not null,
  8005.             PRIVILEGE varchar(32) not null,
  8006.             IS_GRANTABLE varchar(3) null
  8007.             )
  8008.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  8009.         exec @ret = sp_cursoropen @handle output,
  8010.             'select * from #sptabpriv',
  8011.             @scrollopt output, @ccopt output, @rows output
  8012.         drop table #sptabpriv
  8013.     end
  8014.     else if @procname = 'sp_tables'
  8015.     begin
  8016.         create table #sptables (
  8017.             TABLE_QUALIFIER varchar(32) null,
  8018.             TABLE_OWNER varchar(32) null,
  8019.             TABLE_NAME varchar(32)    null,
  8020.             TABLE_TYPE     varchar(32) null,
  8021.             REMARKS varchar(254) null)
  8022.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  8023.         exec @ret = sp_cursoropen @handle output,
  8024.             'select * from #sptables',
  8025.             @scrollopt output, @ccopt output, @rows output
  8026.         drop table #sptables
  8027.     end
  8028.     select @ret = isnull(@ret,0)
  8029.     return isnull(@ret,0)
  8030. go
  8031.  
  8032. if (charindex('7.00', @@version) > 0)
  8033.     drop procedure sp_ddopen
  8034. else
  8035. begin
  8036.     print ''
  8037.     print ''
  8038.     print 'Warning:'
  8039.     print 'you are installing the stored procedures '
  8040.     print 'on a pre 7.0 SQL Server.'
  8041.     print 'Ignore the following errors.'
  8042. end
  8043. go
  8044.  
  8045. /*    Procedure for 7.00 server */
  8046. create procedure sp_ddopen; 1(
  8047.                @handle            int output,
  8048.                @procname        sysname,
  8049.                @scrollopt        int output,
  8050.                @ccopt            int output,
  8051.                @rows            int output,
  8052.                @p1                nvarchar(774) = null,
  8053.                @p2                nvarchar(774) = null,
  8054.                @p3                nvarchar(774) = null,
  8055.                @p4                nvarchar(774) = null,
  8056.                @p5                nvarchar(774) = null,
  8057.                @p6                nvarchar(774) = null,
  8058.                @p7                int = null,
  8059.                @ODBCVer         int = 2)
  8060. as
  8061.     set nocount on
  8062.     declare @ret int
  8063.  
  8064.     if @procname = 'sp_column_privileges'
  8065.     begin
  8066.         exec @ret = sp_ddopen;2 @handle output, 
  8067.                             @scrollopt output, 
  8068.                             @ccopt output, 
  8069.                             @rows output, 
  8070.                             @p1,
  8071.                             @p2,
  8072.                             @p3,
  8073.                             @p4
  8074.     end
  8075.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  8076.     begin
  8077.         exec @ret = sp_ddopen;3 @handle output,
  8078.                             @procname,
  8079.                             @scrollopt output, 
  8080.                             @ccopt output, 
  8081.                             @rows output, 
  8082.                             @p1,
  8083.                             @p2,
  8084.                             @p3,
  8085.                             @p4,
  8086.                             @p5,
  8087.                             @ODBCVer
  8088.     end
  8089.     else if @procname = 'sp_datatype_info'
  8090.     begin
  8091.         exec @ret = sp_ddopen;4 @handle output,
  8092.                             @scrollopt output, 
  8093.                             @ccopt output, 
  8094.                             @rows output, 
  8095.                             @p7,
  8096.                             @ODBCVer
  8097.     end
  8098.     else if @procname = 'sp_fkeys'
  8099.     begin
  8100.         exec @ret = sp_ddopen;5 @handle output, 
  8101.                             @scrollopt output, 
  8102.                             @ccopt output, 
  8103.                             @rows output, 
  8104.                             @p1,
  8105.                             @p2,
  8106.                             @p3,
  8107.                             @p4,
  8108.                             @p5,
  8109.                             @p6
  8110.     end
  8111.     else if @procname = 'sp_pkeys'
  8112.     begin
  8113.         exec @ret = sp_ddopen;6 @handle output, 
  8114.                             @scrollopt output, 
  8115.                             @ccopt output, 
  8116.                             @rows output, 
  8117.                             @p1,
  8118.                             @p2,
  8119.                             @p3
  8120.     end
  8121.     else if @procname = 'sp_special_columns'
  8122.     begin
  8123.         exec @ret = sp_ddopen;7 @handle output, 
  8124.                             @scrollopt output, 
  8125.                             @ccopt output, 
  8126.                             @rows output, 
  8127.                             @p1,
  8128.                             @p2,
  8129.                             @p3,
  8130.                             @p4,
  8131.                             @p5,
  8132.                             @p6,
  8133.                             @ODBCVer
  8134.     end
  8135.     else if @procname = 'sp_sproc_columns'
  8136.     begin
  8137.         exec @ret = sp_ddopen;8 @handle output, 
  8138.                             @scrollopt output, 
  8139.                             @ccopt output, 
  8140.                             @rows output, 
  8141.                             @p1,
  8142.                             @p2,
  8143.                             @p3,
  8144.                             @p4,
  8145.                             @ODBCVer
  8146.     end
  8147.     else if @procname = 'sp_statistics'
  8148.     begin
  8149.         exec @ret = sp_ddopen;9 @handle output, 
  8150.                             @scrollopt output, 
  8151.                             @ccopt output, 
  8152.                             @rows output, 
  8153.                             @p1,
  8154.                             @p2,
  8155.                             @p3,
  8156.                             @p4,
  8157.                             @p5,
  8158.                             @p6
  8159.     end
  8160.     else if @procname = 'sp_stored_procedures'
  8161.     begin
  8162.         exec @ret = sp_ddopen;10 @handle output, 
  8163.                              @scrollopt output, 
  8164.                              @ccopt output, 
  8165.                              @rows output, 
  8166.                              @p1,
  8167.                              @p2,
  8168.                              @p3
  8169.     end
  8170.     else if @procname = 'sp_table_privileges'
  8171.     begin
  8172.         exec @ret = sp_ddopen;11 @handle output, 
  8173.                              @scrollopt output, 
  8174.                              @ccopt output, 
  8175.                              @rows output, 
  8176.                              @p1,
  8177.                              @p2,
  8178.                              @p3
  8179.     end
  8180.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  8181.     begin
  8182.         exec @ret = sp_ddopen;12 @handle output,
  8183.                             @procname,
  8184.                             @scrollopt output, 
  8185.                             @ccopt output, 
  8186.                             @rows output, 
  8187.                             @p1,
  8188.                             @p2,
  8189.                             @p3,
  8190.                             @p4,
  8191.                             @p5
  8192.     end
  8193.     else
  8194.         print 'Unknown dd_open procedure'
  8195.     select @ret = isnull(@ret,0)
  8196.     return isnull(@ret,0)
  8197. go
  8198.  
  8199.  
  8200. if (charindex('7.00', @@version) = 0)
  8201. begin
  8202.     print ''
  8203.     print ''
  8204.     print 'Warning:'
  8205.     print 'you are installing the stored procedures '
  8206.     print 'on a pre 7.0 SQL Server.'
  8207.     print 'Ignore the following errors.'
  8208. end
  8209. go
  8210.  
  8211. create procedure sp_ddopen; 2(
  8212.                @handle            int output,
  8213.                @scrollopt        int output,
  8214.                @ccopt            int output,
  8215.                @rows            int output,
  8216.                @p1                nvarchar(774),
  8217.                @p2                nvarchar(774),
  8218.                @p3                nvarchar(774),
  8219.                @p4                nvarchar(774))
  8220. as
  8221.     set nocount on
  8222.     declare @ret int
  8223.  
  8224.     create table #spcolpriv (
  8225.         TABLE_QUALIFIER sysname null,
  8226.         TABLE_OWNER sysname null,
  8227.         TABLE_NAME sysname not null,
  8228.         COLUMN_NAME sysname not null,
  8229.         GRANTOR sysname null,
  8230.         GRANTEE sysname not null,
  8231.         PRIVILEGE varchar(32) not null,
  8232.         IS_GRANTABLE varchar(3) null
  8233.         )
  8234.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  8235.     exec @ret = sp_cursoropen @handle output,
  8236.         'select * from #spcolpriv',
  8237.         @scrollopt output, @ccopt output, @rows output
  8238.     drop table #spcolpriv
  8239.     return @ret
  8240. go
  8241.  
  8242.  
  8243. if (charindex('7.00', @@version) = 0)
  8244. begin
  8245.     print ''
  8246.     print ''
  8247.     print 'Warning:'
  8248.     print 'you are installing the stored procedures '
  8249.     print 'on a pre 7.0 SQL Server.'
  8250.     print 'Ignore the following errors.'
  8251. end
  8252. go
  8253.  
  8254. create procedure sp_ddopen; 3(
  8255.                @handle            int output,
  8256.                @procname        sysname,
  8257.                @scrollopt        int output,
  8258.                @ccopt            int output,
  8259.                @rows            int output,
  8260.                @p1                nvarchar(774),
  8261.                @p2                nvarchar(774),
  8262.                @p3                nvarchar(774),
  8263.                @p4                nvarchar(774),
  8264.                @p5                nvarchar(774),
  8265.                @ODBCVer         int)
  8266. as
  8267.     set nocount on
  8268.     declare @ret int
  8269.  
  8270.     create table #spcolumns (
  8271.         TABLE_QUALIFIER sysname null,
  8272.         TABLE_OWNER sysname null,
  8273.         TABLE_NAME sysname not null,
  8274.         COLUMN_NAME sysname not null,
  8275.         DATA_TYPE smallint not null,
  8276.         TYPE_NAME sysname not null,
  8277.         "PRECISION" int null,
  8278.         LENGTH int null,
  8279.         SCALE smallint null,
  8280.         RADIX smallint null,
  8281.         NULLABLE smallint not null,
  8282.         REMARKS varchar(254) null,
  8283.         COLUMN_DEF nvarchar(3000) null,
  8284.         SQL_DATA_TYPE smallint not null,
  8285.         SQL_DATETIME_SUB smallint null,
  8286.         CHAR_OCTET_LENGTH int null,
  8287.         ORDINAL_POSITION int not null,
  8288.         IS_NULLABLE varchar(254) null,
  8289.         SS_DATA_TYPE tinyint null
  8290.         )
  8291.  
  8292.     if @procname = 'sp_columns'
  8293.     begin
  8294.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  8295.     end
  8296.     else
  8297.     begin
  8298.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  8299.     end
  8300.     exec @ret = sp_cursoropen @handle output,
  8301.         'select * from #spcolumns',
  8302.         @scrollopt output, @ccopt output, @rows output
  8303.     drop table #spcolumns
  8304.     return @ret
  8305. go
  8306.  
  8307.  
  8308.  
  8309. if (charindex('7.00', @@version) = 0)
  8310. begin
  8311.     print ''
  8312.     print ''
  8313.     print 'Warning:'
  8314.     print 'you are installing the stored procedures '
  8315.     print 'on a pre 7.0 SQL Server.'
  8316.     print 'Ignore the following errors.'
  8317. end
  8318. go
  8319.  
  8320. create procedure sp_ddopen; 4(
  8321.                @handle            int output,
  8322.                @scrollopt        int output,
  8323.                @ccopt            int output,
  8324.                @rows            int output,
  8325.                @p7                int,
  8326.                @ODBCVer         int)
  8327. as
  8328.     set nocount on
  8329.     declare @ret int
  8330.  
  8331.     create table #spdatatypeinfo (
  8332.         TYPE_NAME            sysname  not null,
  8333.         DATA_TYPE            smallint not null,
  8334.         "PRECISION"            int null,
  8335.         LITERAL_PREFIX        varchar(32)    null,
  8336.         LITERAL_SUFFIX        varchar(32)    null,
  8337.         CREATE_PARAMS        varchar(32)    null,
  8338.         NULLABLE            smallint   not null,
  8339.         CASE_SENSITIVE        smallint   not null,
  8340.         SEARCHABLE            smallint   not null,
  8341.         UNSIGNED_ATTRIBUTE    smallint   null,
  8342.         MONEY    smallint    not null,
  8343.         AUTO_INCREMENT        smallint    null,
  8344.         LOCAL_TYPE_NAME     sysname null,
  8345.         MINIMUM_SCALE        smallint     null,
  8346.         MAXIMUM_SCALE        smallint   null,
  8347.         SQL_DATA_TYPE        smallint      not null,
  8348.         SQL_DATETIME_SUB    smallint   null,
  8349.         NUM_PREC_RADIX        int     null,
  8350.         INTERVAL_PRECISION    smallint    NULL,
  8351.         USERTYPE            smallint not null)
  8352.  
  8353.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  8354.     exec @ret = sp_cursoropen @handle output,
  8355.         'select * from #spdatatypeinfo',
  8356.         @scrollopt output, @ccopt output, @rows output
  8357.     drop table #spdatatypeinfo
  8358.     return @ret
  8359. go
  8360.  
  8361.  
  8362.  
  8363. if (charindex('7.00', @@version) = 0)
  8364. begin
  8365.     print ''
  8366.     print ''
  8367.     print 'Warning:'
  8368.     print 'you are installing the stored procedures '
  8369.     print 'on a pre 7.0 SQL Server.'
  8370.     print 'Ignore the following errors.'
  8371. end
  8372. go
  8373.  
  8374. create procedure sp_ddopen; 5(
  8375.                @handle            int output,
  8376.                @scrollopt        int output,
  8377.                @ccopt            int output,
  8378.                @rows            int output,
  8379.                @p1                nvarchar(774),
  8380.                @p2                nvarchar(774),
  8381.                @p3                nvarchar(774),
  8382.                @p4                nvarchar(774),
  8383.                @p5                nvarchar(774),
  8384.                @p6                nvarchar(774))
  8385. as
  8386.     set nocount on
  8387.     declare @ret int
  8388.  
  8389.     create table #spfkeys (
  8390.         PKTABLE_QUALIFIER sysname     null,
  8391.         PKTABLE_OWNER sysname    null,
  8392.         PKTABLE_NAME sysname  not null,
  8393.         PKCOLUMN_NAME sysname  not null,
  8394.         FKTABLE_QUALIFIER sysname    null,
  8395.         FKTABLE_OWNER sysname    null,
  8396.         FKTABLE_NAME sysname  not null,
  8397.         FKCOLUMN_NAME sysname  not null,
  8398.         KEY_SEQ smallint not null,
  8399.         UPDATE_RULE smallint null,
  8400.         DELETE_RULE smallint null,
  8401.         FK_NAME sysname null,
  8402.         PK_NAME sysname null,
  8403.         DEFERRABILITY smallint null
  8404.         )
  8405.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  8406.     exec @ret = sp_cursoropen @handle output,
  8407.         'select * from #spfkeys',
  8408.         @scrollopt output, @ccopt output, @rows output
  8409.     drop table #spfkeys
  8410.     return @ret
  8411. go
  8412.  
  8413.  
  8414.  
  8415. if (charindex('7.00', @@version) = 0)
  8416. begin
  8417.     print ''
  8418.     print ''
  8419.     print 'Warning:'
  8420.     print 'you are installing the stored procedures '
  8421.     print 'on a pre 7.0 SQL Server.'
  8422.     print 'Ignore the following errors.'
  8423. end
  8424. go
  8425.  
  8426. create procedure sp_ddopen; 6(
  8427.                @handle            int output,
  8428.                @scrollopt        int output,
  8429.                @ccopt            int output,
  8430.                @rows            int output,
  8431.                @p1                nvarchar(774),
  8432.                @p2                nvarchar(774),
  8433.                @p3                nvarchar(774))
  8434. as
  8435.     set nocount on
  8436.     declare @ret int
  8437.  
  8438.     create table #sppkeys (
  8439.         TABLE_QUALIFIER sysname   null,
  8440.         TABLE_OWNER sysname   null,
  8441.         TABLE_NAME sysname    not null,
  8442.         COLUMN_NAME sysname  not null,
  8443.         KEY_SEQ smallint not null,
  8444.         PK_NAME sysname null
  8445.         )
  8446.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  8447.     exec @ret = sp_cursoropen @handle output,
  8448.         'select * from #sppkeys',
  8449.         @scrollopt output, @ccopt output, @rows output
  8450.     drop table #sppkeys
  8451.     return @ret
  8452. go
  8453.  
  8454.  
  8455.  
  8456. if (charindex('7.00', @@version) = 0)
  8457. begin
  8458.     print ''
  8459.     print ''
  8460.     print 'Warning:'
  8461.     print 'you are installing the stored procedures '
  8462.     print 'on a pre 7.0 SQL Server.'
  8463.     print 'Ignore the following errors.'
  8464. end
  8465. go
  8466.  
  8467. create procedure sp_ddopen; 7(
  8468.                @handle            int output,
  8469.                @scrollopt        int output,
  8470.                @ccopt            int output,
  8471.                @rows            int output,
  8472.                @p1                nvarchar(774),
  8473.                @p2                nvarchar(774),
  8474.                @p3                nvarchar(774),
  8475.                @p4                nvarchar(774),
  8476.                @p5                nvarchar(774),
  8477.                @p6                nvarchar(774),
  8478.                @ODBCVer         int)
  8479. as
  8480.     set nocount on
  8481.     declare @ret int
  8482.  
  8483.     create table #spspeccol (
  8484.         SCOPE smallint null,
  8485.         COLUMN_NAME sysname not null,
  8486.         DATA_TYPE smallint not null,
  8487.         TYPE_NAME sysname not null,
  8488.         "PRECISION" int null,
  8489.         LENGTH int null,
  8490.         SCALE smallint null,
  8491.         PSEUDO_COLUMN smallint null
  8492.         )
  8493.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  8494.     exec @ret = sp_cursoropen @handle output,
  8495.         'select * from #spspeccol',
  8496.         @scrollopt output, @ccopt output, @rows output
  8497.     drop table #spspeccol
  8498.     return @ret
  8499. go
  8500.  
  8501.  
  8502.  
  8503. if (charindex('7.00', @@version) = 0)
  8504. begin
  8505.     print ''
  8506.     print ''
  8507.     print 'Warning:'
  8508.     print 'you are installing the stored procedures '
  8509.     print 'on a pre 7.0 SQL Server.'
  8510.     print 'Ignore the following errors.'
  8511. end
  8512. go
  8513.  
  8514. create procedure sp_ddopen; 8(
  8515.                @handle            int output,
  8516.                @scrollopt        int output,
  8517.                @ccopt            int output,
  8518.                @rows            int output,
  8519.                @p1                nvarchar(774),
  8520.                @p2                nvarchar(774),
  8521.                @p3                nvarchar(774),
  8522.                @p4                nvarchar(774),
  8523.                @ODBCVer         int)
  8524. as
  8525.     set nocount on
  8526.     declare @ret int
  8527.  
  8528.     create table #spproccol (
  8529.         PROCEDURE_QUALIFIER sysname  null,
  8530.         PROCEDURE_OWNER sysname  null,
  8531.         PROCEDURE_NAME sysname not null,
  8532.         COLUMN_NAME sysname not null,
  8533.         COLUMN_TYPE smallint not null,
  8534.         DATA_TYPE smallint not null,
  8535.         TYPE_NAME sysname not null,
  8536.         "PRECISION" int null,
  8537.         LENGTH int null,
  8538.         SCALE smallint null,
  8539.         RADIX smallint null,
  8540.         NULLABLE smallint not null,
  8541.         REMARKS varchar(254) null,
  8542.         COLUMN_DEF nvarchar(3000) null,
  8543.         SQL_DATA_TYPE smallint not null,
  8544.         SQL_DATETIME_SUB smallint null,
  8545.         CHAR_OCTET_LENGTH int null,
  8546.         ORDINAL_POSITION int not null,
  8547.         IS_NULLABLE varchar(254) null,
  8548.         SS_DATA_TYPE tinyint null
  8549.         )
  8550.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  8551.     exec @ret = sp_cursoropen @handle output,
  8552.         'select * from #spproccol',
  8553.         @scrollopt output, @ccopt output, @rows output
  8554.     drop table #spproccol
  8555.     return @ret
  8556. go
  8557.  
  8558.  
  8559.  
  8560. if (charindex('7.00', @@version) = 0)
  8561. begin
  8562.     print ''
  8563.     print ''
  8564.     print 'Warning:'
  8565.     print 'you are installing the stored procedures '
  8566.     print 'on a pre 7.0 SQL Server.'
  8567.     print 'Ignore the following errors.'
  8568. end
  8569. go
  8570.  
  8571. create procedure sp_ddopen; 9(
  8572.                @handle            int output,
  8573.                @scrollopt        int output,
  8574.                @ccopt            int output,
  8575.                @rows            int output,
  8576.                @p1                nvarchar(774),
  8577.                @p2                nvarchar(774),
  8578.                @p3                nvarchar(774),
  8579.                @p4                nvarchar(774),
  8580.                @p5                nvarchar(774),
  8581.                @p6                nvarchar(774))
  8582. as
  8583.     set nocount on
  8584.     declare @ret int
  8585.  
  8586.     create table #spstatistics (
  8587.         TABLE_QUALIFIER sysname   null,
  8588.         TABLE_OWNER sysname   null,
  8589.         TABLE_NAME sysname    not null,
  8590.         NON_UNIQUE smallint null,
  8591.         INDEX_QUALIFIER sysname null,
  8592.         INDEX_NAME sysname null,
  8593.         TYPE smallint not null,
  8594.         SEQ_IN_INDEX smallint null,
  8595.         COLUMN_NAME sysname null,
  8596.         COLLATION char(1) null,
  8597.         CARDINALITY int null,
  8598.         PAGES int null,
  8599.         FILTER_CONDITION varchar(128) null
  8600.         )
  8601.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  8602.     exec @ret = sp_cursoropen @handle output,
  8603.         'select * from #spstatistics',
  8604.         @scrollopt output, @ccopt output, @rows output
  8605.     drop table #spstatistics
  8606.     return @ret
  8607. go
  8608.                
  8609.  
  8610.  
  8611. if (charindex('7.00', @@version) = 0)
  8612. begin
  8613.     print ''
  8614.     print ''
  8615.     print 'Warning:'
  8616.     print 'you are installing the stored procedures '
  8617.     print 'on a pre 7.0 SQL Server.'
  8618.     print 'Ignore the following errors.'
  8619. end
  8620. go
  8621.  
  8622. create procedure sp_ddopen; 10(
  8623.                @handle            int output,
  8624.                @scrollopt        int output,
  8625.                @ccopt            int output,
  8626.                @rows            int output,
  8627.                @p1                nvarchar(774),
  8628.                @p2                nvarchar(774),
  8629.                @p3                nvarchar(774))
  8630. as
  8631.     set nocount on
  8632.     declare @ret int
  8633.  
  8634.     create table #spprocedures (
  8635.         PROCEDURE_QUALIFIER sysname  null,
  8636.         PROCEDURE_OWNER sysname  null,
  8637.         PROCEDURE_NAME sysname not null,
  8638.         NUM_INPUT_PARAMS int null,
  8639.         NUM_OUTPUT_PARAMS int null,
  8640.         NUM_RESULT_SETS int null,
  8641.         REMARKS varchar(254) null,
  8642.         PROCEDURE_TYPE smallint null
  8643.         )
  8644.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  8645.     exec @ret = sp_cursoropen @handle output,
  8646.         'select * from #spprocedures',
  8647.         @scrollopt output, @ccopt output, @rows output
  8648.     drop table #spprocedures
  8649.     return @ret
  8650. go
  8651.  
  8652.  
  8653.  
  8654. if (charindex('7.00', @@version) = 0)
  8655. begin
  8656.     print ''
  8657.     print ''
  8658.     print 'Warning:'
  8659.     print 'you are installing the stored procedures '
  8660.     print 'on a pre 7.0 SQL Server.'
  8661.     print 'Ignore the following errors.'
  8662. end
  8663. go
  8664.  
  8665. create procedure sp_ddopen; 11(
  8666.                @handle            int output,
  8667.                @scrollopt        int output,
  8668.                @ccopt            int output,
  8669.                @rows            int output,
  8670.                @p1                nvarchar(774),
  8671.                @p2                nvarchar(774),
  8672.                @p3                nvarchar(774))
  8673. as
  8674.     set nocount on
  8675.     declare @ret int
  8676.  
  8677.     create table #sptabpriv (
  8678.         TABLE_QUALIFIER sysname null,
  8679.         TABLE_OWNER sysname null,
  8680.         TABLE_NAME sysname not null,
  8681.         GRANTOR sysname null,
  8682.         GRANTEE sysname not null,
  8683.         PRIVILEGE varchar(32) not null,
  8684.         IS_GRANTABLE varchar(3) null
  8685.         )
  8686.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  8687.     exec @ret = sp_cursoropen @handle output,
  8688.         'select * from #sptabpriv',
  8689.         @scrollopt output, @ccopt output, @rows output
  8690.     drop table #sptabpriv
  8691.     return @ret
  8692. go
  8693.  
  8694.  
  8695.  
  8696. if (charindex('7.00', @@version) = 0)
  8697. begin
  8698.     print ''
  8699.     print ''
  8700.     print 'Warning:'
  8701.     print 'you are installing the stored procedures '
  8702.     print 'on a pre 7.0 SQL Server.'
  8703.     print 'Ignore the following errors.'
  8704. end
  8705. go
  8706.  
  8707. create procedure sp_ddopen; 12(
  8708.                @handle            int output,
  8709.                @procname        sysname,
  8710.                @scrollopt        int output,
  8711.                @ccopt            int output,
  8712.                @rows            int output,
  8713.                @p1                nvarchar(774),
  8714.                @p2                nvarchar(774),
  8715.                @p3                nvarchar(774),
  8716.                @p4                nvarchar(774),
  8717.                @p5                nvarchar(774))
  8718. as
  8719.     set nocount on
  8720.     declare @ret int
  8721.  
  8722.     create table #sptables (
  8723.         TABLE_QUALIFIER sysname null,
  8724.         TABLE_OWNER sysname null,
  8725.         TABLE_NAME sysname null,
  8726.         TABLE_TYPE    varchar(32) null,
  8727.         REMARKS varchar(254) null)
  8728.     if @procname = 'sp_tables'
  8729.     begin
  8730.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  8731.     end
  8732.     else
  8733.     begin
  8734.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  8735.     end
  8736.     exec @ret = sp_cursoropen @handle output,
  8737.         'select * from #sptables',
  8738.         @scrollopt output, @ccopt output, @rows output
  8739.     drop table #sptables
  8740.     return @ret
  8741. go
  8742.  
  8743. grant execute on sp_ddopen to public
  8744. go
  8745.  
  8746. print 'creating sp_tableswc'
  8747. go
  8748.  
  8749. if (charindex('7.00', @@version) = 0)
  8750. begin
  8751.     print ''
  8752.     print ''
  8753.     print 'Warning:'
  8754.     print 'you are installing the stored procedures '
  8755.     print 'on a pre 7.0 SQL Server.'
  8756.     print 'Ignore the following errors.'
  8757. end
  8758. go
  8759.  
  8760. /*    Procedure for 7.00 server */
  8761. create procedure sp_tableswc(
  8762.                @table_name        nvarchar(384)    = null,
  8763.                @table_owner     nvarchar(384)    = null,
  8764.                @table_qualifier sysname    = null,
  8765.                @table_type        varchar(100) = null)
  8766. as
  8767.     declare @databasename    sysname
  8768.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  8769.  
  8770.     create table #sptables (
  8771.         TABLE_QUALIFIER sysname null,
  8772.         TABLE_OWNER sysname null,
  8773.         TABLE_NAME sysname null,
  8774.         TABLE_TYPE    varchar(32) null,
  8775.         REMARKS varchar(254) null)
  8776.  
  8777.     declare databases CURSOR FOR
  8778.         select name from master..sysdatabases
  8779.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  8780.         for read only
  8781.  
  8782.     open databases
  8783.     fetch next from databases into @databasename
  8784.     while (@@FETCH_STATUS <> -1)
  8785.     begin
  8786.         if (charindex('%', @databasename) = 0 and
  8787.             charindex('_', @databasename) = 0)
  8788.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  8789.             select @qualprocname = @databasename + '..sp_tables'
  8790.             insert into #sptables exec @qualprocname
  8791.                 @table_name, @table_owner, @databasename, @table_type
  8792.         end
  8793.         fetch next from databases into @databasename
  8794.     end
  8795.     deallocate databases
  8796.     select * from #sptables
  8797.         order by 4, 1, 2, 3
  8798. go
  8799.  
  8800. grant execute on sp_tableswc to public
  8801. go
  8802.  
  8803. dump tran master with no_log
  8804. go
  8805.  
  8806. /*-------------------------------------------------------------------------*/
  8807. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  8808. /*-------------------------------------------------------------------------*/
  8809.  
  8810. print ''
  8811. print 'creating spt_provider_types'
  8812. go
  8813. if (charindex('7.00', @@version) = 0)
  8814. begin /* Pre 7.00 Server */
  8815. create table spt_provider_types
  8816.     (
  8817.     ss_dtype        tinyint     not null,
  8818.     fixlen            int         null,        /* datatype len for variable, else null */
  8819.     type_name        sysname        not null,
  8820.     oledb_data_type        smallint    not null,
  8821.     best_match        bit        not null,
  8822.     is_nullable        tinyint        null,
  8823.     case_sensitive        bit        not null,
  8824.     fixed_prec_scale    bit        not null,
  8825.     is_long            bit        not null,
  8826.     auto_unique_value    tinyint        not null,
  8827.     data_precision        int         null,
  8828.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8829.     column_size        int         null,
  8830.     literal_prefix        varchar(32)    null,
  8831.     literal_suffix        varchar(32)     null,
  8832.     searchable        int        not null,
  8833.     unsigned_attribute    tinyint        null,
  8834.     local_type_name     sysname        null
  8835.     )
  8836. print ''
  8837. print ''
  8838. print 'Warning:'
  8839. print 'you are installing the stored procedures '
  8840. print 'on a pre 7.0 SQL Server.'
  8841. print 'Ignore the following errors.'
  8842. end
  8843. go
  8844. if (charindex('7.00', @@version) > 0)
  8845. begin /* 7.00 server*/
  8846. create table spt_provider_types
  8847.     (
  8848.     ss_dtype        tinyint     not null,
  8849.     fixlen            int         null,        /* datatype len for variable, else null */
  8850.     type_name        sysname        not null,
  8851.     oledb_data_type        smallint    not null,
  8852.     best_match        bit        not null,
  8853.     is_nullable        bit        null,
  8854.     case_sensitive        bit        not null,
  8855.     fixed_prec_scale    bit        not null,
  8856.     is_long            bit        not null,
  8857.     auto_unique_value    tinyint        not null,
  8858.     data_precision        int         null,
  8859.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8860.     column_size        int         null,
  8861.     literal_prefix        nvarchar(32)    null,
  8862.     literal_suffix        nvarchar(32)     null,
  8863.     searchable        int        not null,
  8864.     unsigned_attribute    tinyint        null,
  8865.     local_type_name     sysname        null
  8866.     )
  8867. end
  8868. go
  8869.  
  8870. grant select on spt_provider_types to public
  8871. go
  8872. dump tran master with no_log
  8873. go
  8874.  
  8875. /*
  8876. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  8877. */
  8878. begin tran
  8879.  
  8880. /* Get case sensitivity */
  8881. declare @case_sensitive bit
  8882. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  8883. /* Local Char */
  8884. insert into spt_provider_types values
  8885.     (
  8886.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  8887.     0,                /* fixlen */
  8888.     'char',                /* type_name */
  8889.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8890.     0,                /* best_match */
  8891.     null,                /* is_nullable */
  8892.     @case_sensitive,        /* case_sensitive */
  8893.     0,                /* fixed_prec_scale */
  8894.     0,                /* is_long */
  8895.     0,                /* auto_unique_value */
  8896.     null,                /* data_precision */
  8897.     null,                /* numeric_scale */
  8898.     null,                /* column_size */
  8899.     '''',                /* literal_prefix */
  8900.     '''',                /* literal_suffix */
  8901.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8902.     null,                /* unsigned_attribute */
  8903.     'char'                /* local_type_name */
  8904.     )
  8905.  
  8906. /* Local Varchar */
  8907. insert into spt_provider_types values
  8908.     (
  8909.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  8910.     null,                /* fixlen */
  8911.     'varchar',            /* type_name */
  8912.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8913.     1,                /* best_match */
  8914.     null,                /* is_nullable */
  8915.     @case_sensitive,        /* case_sensitive */
  8916.     0,                /* fixed_prec_scale */
  8917.     0,                /* is_long */
  8918.     0,                /* auto_unique_value */
  8919.     null,                /* data_precision */
  8920.     null,                /* numeric_scale */
  8921.     null,                /* column_size */
  8922.     '''',                /* literal_prefix */
  8923.     '''',                /* literal_suffix */
  8924.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8925.     null,                /* unsigned_attribute */
  8926.     'varchar'            /* local_type_name */
  8927.     )
  8928.  
  8929. /* Local Text */
  8930. insert into spt_provider_types values
  8931.     (
  8932.     35  /*SQLTEXT*/,        /* ss_dtype */
  8933.     null,                /* fixlen */
  8934.     'text',                /* type_name */
  8935.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8936.     0,                /* best_match */
  8937.     null,                /* is_nullable */
  8938.     @case_sensitive,        /* case_sensitive */
  8939.     0,                /* fixed_prec_scale */
  8940.     1,                /* is_long */
  8941.     0,                /* auto_unique_value */
  8942.     null,                /* data_precision */
  8943.     null,                /* numeric_scale */
  8944.     2147483647,            /* column_size */
  8945.     '''',                /* literal_prefix */
  8946.     '''',                /* literal_suffix */
  8947.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  8948.     null,                /* unsigned_attribute */
  8949.     'text'                /* local_type_name */
  8950.     )
  8951. commit tran
  8952. go
  8953. dump tran master with no_log
  8954. go
  8955.  
  8956. /*
  8957. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  8958. */
  8959. begin tran
  8960. /* Local Binary */
  8961. insert into spt_provider_types values
  8962.     (
  8963.     45 /*SQLBINARY*/,        /* ss_dtype */
  8964.     0,                /* fixlen */
  8965.     'binary',            /* type_name */
  8966.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8967.     0,                /* best_match */
  8968.     null,                /* is_nullable */
  8969.     0,                /* case_sensitive */
  8970.     0,                /* fixed_prec_scale */
  8971.     0,                /* is_long */
  8972.     0,                /* auto_unique_value */
  8973.     null,                /* data_precision */
  8974.     null,                /* numeric_scale */
  8975.     null,                /* column_size */
  8976.     '0x',                /* literal_prefix */
  8977.     null,                /* literal_suffix */
  8978.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8979.     null,                /* unsigned_attribute */
  8980.     'binary'            /* local_type_name */
  8981.     )
  8982.  
  8983. /* Local Varbinary */
  8984. insert into spt_provider_types values
  8985.     (
  8986.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  8987.     null,                /* fixlen */
  8988.     'varbinary',            /* type_name */
  8989.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8990.     1,                /* best_match */
  8991.     null,                /* is_nullable */
  8992.     0,                /* case_sensitive */
  8993.     0,                /* fixed_prec_scale */
  8994.     0,                /* is_long */
  8995.     0,                /* auto_unique_value */
  8996.     null,                /* data_precision */
  8997.     null,                /* numeric_scale */
  8998.     null,                /* column_size */
  8999.     '0x',                /* literal_prefix */
  9000.     null,                /* literal_suffix */
  9001.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9002.     null,                /* unsigned_attribute */
  9003.     'varbinary'            /* local_type_name */
  9004.     )
  9005.  
  9006. /* Local Image */
  9007. insert into spt_provider_types values
  9008.     (
  9009.     34 /*SQLIMAGE*/,        /* ss_dtype */
  9010.     null,                /* fixlen */
  9011.     'image',            /* type_name */
  9012.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9013.     0,                /* best_match */
  9014.     null,                /* is_nullable */
  9015.     0,                /* case_sensitive */
  9016.     0,                /* fixed_prec_scale */
  9017.     1,                /* is_long */
  9018.     0,                /* auto_unique_value */
  9019.     null,                /* data_precision */
  9020.     null,                /* numeric_scale */
  9021.     2147483647,            /* column_size */
  9022.     '0x',                /* literal_prefix */
  9023.     null,                /* literal_suffix */
  9024.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  9025.     null,                /* unsigned_attribute */
  9026.     'image'                /* local_type_name */
  9027.     )
  9028.  
  9029. commit tran
  9030. go
  9031. dump tran master with no_log
  9032. go
  9033.  
  9034. /*
  9035. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  9036. */
  9037. begin tran
  9038.  
  9039. /* Local Datetime */
  9040. insert into spt_provider_types values
  9041.     (
  9042.     61 /*SQLDATETIME*/,        /* ss_dtype */
  9043.     8,                /* fixlen */
  9044.     'datetime',            /* type_name */
  9045.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  9046.     1,                /* best_match */
  9047.     null,                /* is_nullable */
  9048.     0,                /* case_sensitive */
  9049.     0,                /* fixed_prec_scale */
  9050.     0,                /* is_long */
  9051.     0,                /* auto_unique_value */
  9052.     23,                /* data_precision */
  9053.     null,                /* numeric_scale */
  9054.     null,                /* column_size */
  9055.     '''',                /* literal_prefix */
  9056.     '''',                /* literal_suffix */
  9057.     4 /*DB_SEARCHABLE*/,        /* searchable */
  9058.     null,                /* unsigned_attribute */
  9059.     'datetime'            /* local_type_name */
  9060.     )
  9061.  
  9062. /* Local Smalldatetime */
  9063. insert into spt_provider_types values
  9064.     (
  9065.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  9066.     4,                /* fixlen */
  9067.     'smalldatetime',        /* type_name */
  9068.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  9069.     0,                /* best_match */
  9070.     null,                /* is_nullable */
  9071.     0,                /* case_sensitive */
  9072.     0,                /* fixed_prec_scale */
  9073.     0,                /* is_long */
  9074.     0,                /* auto_unique_value */
  9075.     16,                /* data_precision */
  9076.     null,                /* numeric_scale */
  9077.     null,                /* column_size */
  9078.     '''',                /* literal_prefix */
  9079.     '''',                /* literal_suffix */
  9080.     4 /*DB_SEARCHABLE*/,        /* searchable */
  9081.     null,                /* unsigned_attribute */
  9082.     'smalldatetime'            /* local_type_name */
  9083.     )
  9084.  
  9085. if (charindex('7.00', @@version) = 0)
  9086.     begin    /*    Add nullable type for non-Sphinx server */
  9087.     
  9088.     /* Local Datetimn */
  9089.     insert into spt_provider_types values
  9090.         (
  9091.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  9092.         4,                /* fixlen */
  9093.         'smalldatetime',        /* type_name */
  9094.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  9095.         0,                /* best_match */
  9096.         null,                /* is_nullable */
  9097.         0,                /* case_sensitive */
  9098.         0,                /* fixed_prec_scale */
  9099.         0,                /* is_long */
  9100.         0,                /* auto_unique_value */
  9101.         16,                /* data_precision */
  9102.         null,                /* numeric_scale */
  9103.         null,                /* column_size */
  9104.         '''',                /* literal_prefix */
  9105.         '''',                /* literal_suffix */
  9106.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9107.         null,                /* unsigned_attribute */
  9108.         'smalldatetime'            /* local_type_name */
  9109.         )    
  9110.     insert into spt_provider_types values
  9111.         (
  9112.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  9113.         8,                /* fixlen */
  9114.         'datetime',            /* type_name */
  9115.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  9116.         0,                /* best_match */
  9117.         null,                /* is_nullable */
  9118.         0,                /* case_sensitive */
  9119.         0,                /* fixed_prec_scale */
  9120.         0,                /* is_long */
  9121.         0,                /* auto_unique_value */
  9122.         23,                /* data_precision */
  9123.         null,                /* numeric_scale */
  9124.         null,                /* column_size */
  9125.         '''',                /* literal_prefix */
  9126.         '''',                /* literal_suffix */
  9127.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9128.         null,                /* unsigned_attribute */
  9129.         'datetime'            /* local_type_name */
  9130.         )
  9131.     end
  9132.  
  9133. commit tran
  9134. go
  9135. dump tran master with no_log
  9136. go
  9137.  
  9138. /*
  9139. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  9140. */
  9141. begin tran
  9142.  
  9143. /* Local Smallmoney */
  9144. insert into spt_provider_types values
  9145.     (
  9146.     122 /*SQLMONEY4*/,        /* ss_dtype */
  9147.     4,                /* fixlen */
  9148.     'smallmoney',            /* type_name */
  9149.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  9150.     0,                /* best_match */
  9151.     null,                /* is_nullable */
  9152.     0,                /* case_sensitive */
  9153.     1,                /* fixed_prec_scale */
  9154.     0,                /* is_long */
  9155.     0,                /* auto_unique_value */
  9156.     10,                /* data_precision */
  9157.     null,                /* numeric_scale */
  9158.     null,                /* column_size */
  9159.     '$',                /* literal_prefix */
  9160.     null,                /* literal_suffix */
  9161.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9162.     0,                /* unsigned_attribute */
  9163.     'smallmoney'            /* local_type_name */
  9164.     )    
  9165.     
  9166. /* Local Money */
  9167. insert into spt_provider_types values
  9168.     (
  9169.     60 /*SQLMONEY*/,        /* ss_dtype */
  9170.     8,                /* fixlen */
  9171.     'money',            /* type_name */
  9172.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  9173.     1,                /* best_match */
  9174.     null,                /* is_nullable */
  9175.     0,                /* case_sensitive */
  9176.     1,                /* fixed_prec_scale */
  9177.     0,                /* is_long */
  9178.     0,                /* auto_unique_value */
  9179.     19,                /* data_precision */
  9180.     null,                /* numeric_scale */
  9181.     null,                /* column_size */
  9182.     '$',                /* literal_prefix */
  9183.     null,                /* literal_suffix */
  9184.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9185.     0,                /* unsigned_attribute */
  9186.     'money'                /* local_type_name */
  9187.     )    
  9188.  
  9189. if (charindex('7.00', @@version) = 0)
  9190.     begin    /*    Add nullable type for non-Sphinx server */
  9191.  
  9192.     /* Local Moneyn */
  9193.     insert into spt_provider_types values
  9194.         (
  9195.         110 /*SQLMONEYN*/,    /* ss_dtype */
  9196.         4,            /* fixlen */
  9197.         'smallmoney',        /* type_name */
  9198.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  9199.         0,            /* best_match */
  9200.         null,            /* is_nullable */
  9201.         0,            /* case_sensitive */
  9202.         1,            /* fixed_prec_scale */
  9203.         0,            /* is_long */
  9204.         0,            /* auto_unique_value */
  9205.         10,            /* data_precision */
  9206.         null,            /* numeric_scale */
  9207.         null,            /* column_size */
  9208.         '$',            /* literal_prefix */
  9209.         null,            /* literal_suffix */
  9210.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  9211.         0,            /* unsigned_attribute */
  9212.         'smallmoney'        /* local_type_name */
  9213.         )    
  9214.     insert into spt_provider_types values
  9215.         (
  9216.         110 /*SQLMONEYN*/,    /* ss_dtype */
  9217.         8,            /* fixlen */
  9218.         'money',        /* type_name */
  9219.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  9220.         0,            /* best_match */
  9221.         null,            /* is_nullable */
  9222.         0,            /* case_sensitive */
  9223.         1,            /* fixed_prec_scale */
  9224.         0,            /* is_long */
  9225.         0,            /* auto_unique_value */
  9226.         19,            /* data_precision */
  9227.         null,            /* numeric_scale */
  9228.         null,            /* column_size */
  9229.         '$',            /* literal_prefix */
  9230.         null,            /* literal_suffix */
  9231.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  9232.         null,            /* unsigned_attribute */
  9233.         'money'            /* local_type_name */
  9234.         )    
  9235.     end
  9236.  
  9237. commit tran
  9238. go
  9239. dump tran master with no_log
  9240. go
  9241.  
  9242. /*
  9243. ** Insert the spt_provider_types rows for the numeric data types
  9244. */
  9245. begin tran
  9246.  
  9247. /* Local Float */
  9248. insert into spt_provider_types values
  9249.     (
  9250.     62 /*SQLFLT8*/,            /* ss_dtype */
  9251.     8,                /* fixlen */
  9252.     'float',            /* type_name */
  9253.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  9254.     1,                /* best_match */
  9255.     null,                /* is_nullable */
  9256.     0,                /* case_sensitive */
  9257.     0,                /* fixed_prec_scale */
  9258.     0,                /* is_long */
  9259.     0,                /* auto_unique_value */
  9260.     15,                /* data_precision */
  9261.     null,                /* numeric_scale */
  9262.     null,                /* column_size */
  9263.     null,                /* literal_prefix */
  9264.     null,                /* literal_suffix */
  9265.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9266.     0,                /* unsigned_attribute */
  9267.     'float'                /* local_type_name */
  9268.     )    
  9269.  
  9270. /* Local Real */
  9271. insert into spt_provider_types values
  9272.     (
  9273.     59 /*SQLFLT4*/,            /* ss_dtype */
  9274.     4,                /* fixlen */
  9275.     'real',                /* type_name */
  9276.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  9277.     1,                /* best_match */
  9278.     null,                /* is_nullable */
  9279.     0,                /* case_sensitive */
  9280.     0,                /* fixed_prec_scale */
  9281.     0,                /* is_long */
  9282.     0,                /* auto_unique_value */
  9283.     7,                /* data_precision */
  9284.     null,                /* numeric_scale */
  9285.     null,                /* column_size */
  9286.     null,                /* literal_prefix */
  9287.     null,                /* literal_suffix */
  9288.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9289.     0,                /* unsigned_attribute */
  9290.     'real'                /* local_type_name */
  9291.     )    
  9292.  
  9293. /* Local Int */
  9294. insert into spt_provider_types values
  9295.     (
  9296.     56 /*SQLINT4*/,            /* ss_dtype */
  9297.     4,                /* fixlen */
  9298.     'int',                /* type_name */
  9299.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  9300.     1,                /* best_match */
  9301.     null,                /* is_nullable */
  9302.     0,                /* case_sensitive */
  9303.     1,                /* fixed_prec_scale */
  9304.     0,                /* is_long */
  9305.     1,                /* auto_unique_value */
  9306.     10,                /* data_precision */
  9307.     null,                /* numeric_scale */
  9308.     null,                /* column_size */
  9309.     null,                /* literal_prefix */
  9310.     null,                /* literal_suffix */
  9311.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9312.     0,                /* unsigned_attribute */
  9313.     'int'                /* local_type_name */
  9314.     )    
  9315.  
  9316. /* Local Smallint */
  9317. insert into spt_provider_types values
  9318.     (
  9319.     52 /*SQLINT2*/,            /* ss_dtype */
  9320.     2,                /* fixlen */
  9321.     'smallint',            /* type_name */
  9322.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  9323.     1,                /* best_match */
  9324.     null,                /* is_nullable */
  9325.     0,                /* case_sensitive */
  9326.     1,                /* fixed_prec_scale */
  9327.     0,                /* is_long */
  9328.     1,                /* auto_unique_value */
  9329.     5,                /* data_precision */
  9330.     null,                /* numeric_scale */
  9331.     null,                /* column_size */
  9332.     null,                /* literal_prefix */
  9333.     null,                /* literal_suffix */
  9334.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9335.     0,                /* unsigned_attribute */
  9336.     'smallint'            /* local_type_name */
  9337.     )    
  9338.  
  9339. /* Local Tinyint */
  9340. insert into spt_provider_types values
  9341.     (
  9342.     48 /*SQLINT1*/,            /* ss_dtype */
  9343.     1,                /* fixlen */
  9344.     'tinyint',            /* type_name */
  9345.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  9346.     1,                /* best_match */
  9347.     null,                /* is_nullable */
  9348.     0,                /* case_sensitive */
  9349.     1,                /* fixed_prec_scale */
  9350.     0,                /* is_long */
  9351.     1,                /* auto_unique_value */
  9352.     3,                /* data_precision */
  9353.     null,                /* numeric_scale */
  9354.     null,                /* column_size */
  9355.     null,                /* literal_prefix */
  9356.     null,                /* literal_suffix */
  9357.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9358.     1,                /* unsigned_attribute */
  9359.     'tinyint'            /* local_type_name */
  9360.     )    
  9361. commit tran
  9362. go
  9363. dump tran master with no_log
  9364. go
  9365. begin tran    
  9366. if (charindex('6.00', @@version) > 0 or
  9367.     charindex('6.50', @@version) > 0 or
  9368.     charindex('7.00', @@version) > 0)
  9369.     begin    /*    Add 6.0 data types */
  9370.     
  9371.     /* Local Decimal */
  9372.     insert into spt_provider_types values
  9373.         (
  9374.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  9375.         0,                /* fixlen */
  9376.         'decimal',            /* type_name */
  9377.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9378.         0,                /* best_match */
  9379.         null,                /* is_nullable */
  9380.         0,                /* case_sensitive */
  9381.         1,                /* fixed_prec_scale */
  9382.         0,                /* is_long */
  9383.         1,                /* auto_unique_value */
  9384.         38,                /* data_precision */
  9385.         null,                /* numeric_scale */
  9386.         null,                /* column_size */
  9387.         null,                /* literal_prefix */
  9388.         null,                /* literal_suffix */
  9389.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9390.         0,                /* unsigned_attribute */
  9391.         'decimal'            /* local_type_name */
  9392.         )    
  9393.  
  9394.     /* Local Numeric */
  9395.     insert into spt_provider_types values
  9396.         (
  9397.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  9398.         0,                /* fixlen */
  9399.         'numeric',            /* type_name */
  9400.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9401.         1,                /* best_match */
  9402.         null,                /* is_nullable */
  9403.         0,                /* case_sensitive */
  9404.         1,                /* fixed_prec_scale */
  9405.         0,                /* is_long */
  9406.         1,                /* auto_unique_value */
  9407.         38,                /* data_precision */
  9408.         null,                /* numeric_scale */
  9409.         null,                /* column_size */
  9410.         null,                /* literal_prefix */
  9411.         null,                /* literal_suffix */
  9412.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9413.         0,                /* unsigned_attribute */
  9414.         'numeric'            /* local_type_name */
  9415.         )    
  9416.     end
  9417. commit tran
  9418. go
  9419. dump tran master with no_log
  9420. go
  9421. begin tran
  9422. if (charindex('7.00', @@version) = 0)
  9423.     begin    /*    Add nullable type for non-Sphinx server */
  9424.     
  9425.     /* Local Floatn */
  9426.     insert into spt_provider_types values
  9427.         (
  9428.         109 /*SQLFLTN*/,        /* ss_dtype */
  9429.         8,                /* fixlen */
  9430.         'float',            /* type_name */
  9431.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  9432.         0,                /* best_match */
  9433.         null,                /* is_nullable */
  9434.         0,                /* case_sensitive */
  9435.         0,                /* fixed_prec_scale */
  9436.         0,                /* is_long */
  9437.         0,                /* auto_unique_value */
  9438.         15,                /* data_precision */
  9439.         null,                /* numeric_scale */
  9440.         null,                /* column_size */
  9441.         null,                /* literal_prefix */
  9442.         null,                /* literal_suffix */
  9443.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9444.         null,                /* unsigned_attribute */
  9445.         'float'                /* local_type_name */
  9446.         )    
  9447.     insert into spt_provider_types values
  9448.         (
  9449.         109 /*SQLFLT4*/,        /* ss_dtype */
  9450.         4,                /* fixlen */
  9451.         'real',                /* type_name */
  9452.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  9453.         0,                /* best_match */
  9454.         null,                /* is_nullable */
  9455.         0,                /* case_sensitive */
  9456.         0,                /* fixed_prec_scale */
  9457.         0,                /* is_long */
  9458.         0,                /* auto_unique_value */
  9459.         7,                /* data_precision */
  9460.         null,                /* numeric_scale */
  9461.         null,                /* column_size */
  9462.         null,                /* literal_prefix */
  9463.         null,                /* literal_suffix */
  9464.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9465.         null,                /* unsigned_attribute */
  9466.         'real'                /* local_type_name */
  9467.         )    
  9468.  
  9469.     /* Local Intn */
  9470.     insert into spt_provider_types values
  9471.         (
  9472.         38 /*SQLINTN*/,            /* ss_dtype */
  9473.         4,                /* fixlen */
  9474.         'int',                /* type_name */
  9475.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  9476.         1,                /* best_match */
  9477.         null,                /* is_nullable */
  9478.         0,                /* case_sensitive */
  9479.         1,                /* fixed_prec_scale */
  9480.         0,                /* is_long */
  9481.         1,                /* auto_unique_value */
  9482.         10,                /* data_precision */
  9483.         null,                /* numeric_scale */
  9484.         null,                /* column_size */
  9485.         null,                /* literal_prefix */
  9486.         null,                /* literal_suffix */
  9487.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9488.         0,                /* unsigned_attribute */
  9489.         'int'                /* local_type_name */
  9490.         )    
  9491.     insert into spt_provider_types values
  9492.         (
  9493.         38 /*SQLINTN*/,            /* ss_dtype */
  9494.         2,                /* fixlen */
  9495.         'smallint',            /* type_name */
  9496.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  9497.         0,                /* best_match */
  9498.         null,                /* is_nullable */
  9499.         0,                /* case_sensitive */
  9500.         1,                /* fixed_prec_scale */
  9501.         0,                /* is_long */
  9502.         1,                /* auto_unique_value */
  9503.         5,                /* data_precision */
  9504.         null,                /* numeric_scale */
  9505.         null,                /* column_size */
  9506.         null,                /* literal_prefix */
  9507.         null,                /* literal_suffix */
  9508.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9509.         0,                /* unsigned_attribute */
  9510.         'smallint'            /* local_type_name */
  9511.         )    
  9512.     insert into spt_provider_types values
  9513.         (
  9514.         38 /*SQLINTN*/,            /* ss_dtype */
  9515.         1,                /* fixlen */
  9516.         'tinyint',            /* type_name */
  9517.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  9518.         0,                /* best_match */
  9519.         null,                /* is_nullable */
  9520.         0,                /* case_sensitive */
  9521.         1,                /* fixed_prec_scale */
  9522.         0,                /* is_long */
  9523.         1,                /* auto_unique_value */
  9524.         3,                /* data_precision */
  9525.         null,                /* numeric_scale */
  9526.         null,                /* column_size */
  9527.         null,                /* literal_prefix */
  9528.         null,                /* literal_suffix */
  9529.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9530.         1,                /* unsigned_attribute */
  9531.         'tinyint'            /* local_type_name */
  9532.         )    
  9533.  
  9534.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  9535.         begin    /*    Add 6.0 data types */
  9536.         
  9537.         /* Local Decimaln */
  9538.         insert into spt_provider_types values
  9539.             (
  9540.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  9541.             0,                /* fixlen */
  9542.             'decimal',            /* type_name */
  9543.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9544.             0,                /* best_match */
  9545.             null,                /* is_nullable */
  9546.             0,                /* case_sensitive */
  9547.             1,                /* fixed_prec_scale */
  9548.             0,                /* is_long */
  9549.             1,                /* auto_unique_value */
  9550.             38,                /* data_precision */
  9551.             null,                /* numeric_scale */
  9552.             null,                /* column_size */
  9553.             null,                /* literal_prefix */
  9554.             null,                /* literal_suffix */
  9555.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9556.             0,                /* unsigned_attribute */
  9557.             'decimal'            /* local_type_name */
  9558.             )    
  9559.  
  9560.         /* Local Numericn */
  9561.         insert into spt_provider_types values
  9562.             (
  9563.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  9564.             0,                /* fixlen */
  9565.             'numeric',            /* type_name */
  9566.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9567.             0,                /* best_match */
  9568.             null,                /* is_nullable */
  9569.             0,                /* case_sensitive */
  9570.             1,                /* fixed_prec_scale */
  9571.             0,                /* is_long */
  9572.             1,                /* auto_unique_value */
  9573.             38,                /* data_precision */
  9574.             null,                /* numeric_scale */
  9575.             null,                /* column_size */
  9576.             null,                /* literal_prefix */
  9577.             null,                /* literal_suffix */
  9578.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9579.             0,                /* unsigned_attribute */
  9580.             'numeric'            /* local_type_name */
  9581.             )    
  9582.         end
  9583.     end
  9584. commit tran
  9585. go
  9586. dump tran master with no_log
  9587. go
  9588.  
  9589. /*
  9590. **    Remaining data types
  9591. */
  9592. if (charindex('7.00', @@version) = 0)
  9593. begin
  9594.     print ''
  9595.     print ''
  9596.     print 'Warning:'
  9597.     print 'you are installing the stored procedures '
  9598.     print 'on a pre 7.0 SQL Server.'
  9599.     print 'Ignore the following errors.'
  9600. end
  9601. go
  9602. begin tran
  9603.  
  9604. if (charindex('7.00', @@version) > 0)
  9605.     begin
  9606.     /* Local Bit */
  9607.     insert into spt_provider_types values
  9608.         (
  9609.         50 /*SQLBIT*/,            /* ss_dtype */
  9610.         0,                /* fixlen */
  9611.         'bit',                /* type_name */
  9612.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9613.         1,                /* best_match */
  9614.         null,                /* is_nullable */
  9615.         0,                /* case_sensitive */
  9616.         0,                /* fixed_prec_scale */
  9617.         0,                /* is_long */
  9618.         0,                /* auto_unique_value */
  9619.         1,                /* data_precision */
  9620.         null,                /* numeric_scale */
  9621.         null,                /* column_size */
  9622.         null,                /* literal_prefix */
  9623.         null,                /* literal_suffix */
  9624.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9625.         null,                /* unsigned_attribute */
  9626.         'bit'                /* local_type_name */
  9627.         )        
  9628.     
  9629.     /* Local Timestamp */
  9630.     insert into spt_provider_types values
  9631.         (
  9632.         0,                /* ss_dtype */
  9633.         8,                /* fixlen */
  9634.         'timestamp',            /* type_name */
  9635.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9636.         0,                /* best_match */
  9637.         null,                /* is_nullable */
  9638.         0,                /* case_sensitive */
  9639.         0,                /* fixed_prec_scale */
  9640.         0,                /* is_long */
  9641.         0,                /* auto_unique_value */
  9642.         null,                /* data_precision */
  9643.         null,                /* numeric_scale */
  9644.         null,                /* column_size */
  9645.         '0x',                /* literal_prefix */
  9646.         null,                /* literal_suffix */
  9647.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9648.         null,                /* unsigned_attribute */
  9649.         'timestamp'            /* local_type_name */
  9650.         )    
  9651.     /* Local GUID */
  9652.     insert into spt_provider_types values
  9653.         (
  9654.         0,                /* ss_dtype */
  9655.         16,                /* fixlen */
  9656.         'uniqueidentifier',        /* type_name */
  9657.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  9658.         1,                /* best_match */
  9659.         null,                /* is_nullable */
  9660.         0,                /* case_sensitive */
  9661.         0,                /* fixed_prec_scale */
  9662.         0,                /* is_long */
  9663.         0,                /* auto_unique_value */
  9664.         null,                /* data_precision */
  9665.         null,                /* numeric_scale */
  9666.         null,                /* column_size */
  9667.         '''',                /* literal_prefix */
  9668.         '''',                /* literal_suffix */
  9669.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9670.         null,                /* unsigned_attribute */
  9671.         'uniqueidentifier'        /* local_type_name */
  9672.         )    
  9673.     
  9674.     /* Get case sensitivity */
  9675.     declare @ncase_sensitive bit
  9676.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9677.  
  9678.     /* Local NChar */
  9679.     insert into spt_provider_types values
  9680.         (
  9681.         0,                /* ss_dtype */
  9682.         0,                /* fixlen */
  9683.         'nchar',            /* type_name */
  9684.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9685.         0,                /* best_match */
  9686.         null,                /* is_nullable */
  9687.         @ncase_sensitive,        /* case_sensitive */
  9688.         0,                /* fixed_prec_scale */
  9689.         0,                /* is_long */
  9690.         0,                /* auto_unique_value */
  9691.         null,                /* data_precision */
  9692.         null,                /* numeric_scale */
  9693.         null,                /* column_size */
  9694.         'N''',                /* literal_prefix */
  9695.         '''',                /* literal_suffix */
  9696.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9697.         null,                /* unsigned_attribute */
  9698.         'nchar'                /* local_type_name */
  9699.         )    
  9700.  
  9701.     /* Local NVarChar */
  9702.     insert into spt_provider_types values
  9703.         (
  9704.         0,                /* ss_dtype */
  9705.         null,                /* fixlen */
  9706.         'nvarchar',            /* type_name */
  9707.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9708.         1,                /* best_match */
  9709.         null,                /* is_nullable */
  9710.         @ncase_sensitive,        /* case_sensitive */
  9711.         0,                /* fixed_prec_scale */
  9712.         0,                /* is_long */
  9713.         0,                /* auto_unique_value */
  9714.         null,                /* data_precision */
  9715.         null,                /* numeric_scale */
  9716.         null,                /* column_size */
  9717.         'N''',                /* literal_prefix */
  9718.         '''',                /* literal_suffix */
  9719.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9720.         null,                /* unsigned_attribute */
  9721.         'nvarchar'            /* local_type_name */
  9722.         )    
  9723.  
  9724.     /* Local NText */
  9725.     insert into spt_provider_types values
  9726.         (
  9727.         0,                /* ss_dtype */
  9728.         null,                /* fixlen */
  9729.         'ntext',            /* type_name */
  9730.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9731.         0,                /* best_match */
  9732.         null,                /* is_nullable */
  9733.         @ncase_sensitive,        /* case_sensitive */
  9734.         0,                /* fixed_prec_scale */
  9735.         1,                /* is_long */
  9736.         0,                /* auto_unique_value */
  9737.         null,                /* data_precision */
  9738.         null,                /* numeric_scale */
  9739.         1073741823,            /* column_size */
  9740.         'N''',                /* literal_prefix */
  9741.         '''',                /* literal_suffix */
  9742.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  9743.         null,                /* unsigned_attribute */
  9744.         'ntext'                /* local_type_name */
  9745.         )    
  9746.  
  9747.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  9748.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  9749.  
  9750.     end
  9751. commit tran
  9752. go
  9753. dump tran master with no_log
  9754. go
  9755. begin tran
  9756. if (charindex('7.00', @@version) = 0)
  9757.     begin
  9758.     /* Local Bit */
  9759.     insert into spt_provider_types values
  9760.         (
  9761.         50 /*SQLBIT*/,            /* ss_dtype */
  9762.         0,                /* fixlen */
  9763.         'bit',                /* type_name */
  9764.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9765.         1,                /* best_match */
  9766.         0,                /* is_nullable */
  9767.         0,                /* case_sensitive */
  9768.         0,                /* fixed_prec_scale */
  9769.         0,                /* is_long */
  9770.         0,                /* auto_unique_value */
  9771.         1,                /* data_precision */
  9772.         null,                /* numeric_scale */
  9773.         null,                /* column_size */
  9774.         null,                /* literal_prefix */
  9775.         null,                /* literal_suffix */
  9776.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9777.         null,                /* unsigned_attribute */
  9778.         'bit'                /* local_type_name */
  9779.         )    
  9780.  
  9781.     /* Local Timestamp */
  9782.     insert into spt_provider_types values
  9783.         (
  9784.         45 /*SQLBINARY*/,        /* ss_dtype */
  9785.         8,                /* fixlen */
  9786.         'timestamp',            /* type_name */
  9787.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9788.         0,                /* best_match */
  9789.         null,                /* is_nullable */
  9790.         0,                /* case_sensitive */
  9791.         0,                /* fixed_prec_scale */
  9792.         0,                /* is_long */
  9793.         0,                /* auto_unique_value */
  9794.         null,                /* data_precision */
  9795.         null,                /* numeric_scale */
  9796.         null,                /* column_size */
  9797.         '0x',                /* literal_prefix */
  9798.         null,                /* literal_suffix */
  9799.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9800.         null,                /* unsigned_attribute */
  9801.         'timestamp'            /* local_type_name */
  9802.         )        
  9803. end
  9804.  
  9805. commit tran
  9806. go
  9807. dump tran master with no_log
  9808. go
  9809.  
  9810. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  9811. go
  9812.  
  9813. dump tran master with no_log
  9814. go
  9815. if (charindex('6.00', @@version) > 0)
  9816.     begin
  9817.     if (exists (select * from sysobjects
  9818.         where name = 'spt_provider_types' and type = 'U '))
  9819.         begin
  9820.         drop table spt_provider_types
  9821.         dump tran master with no_log
  9822.         end
  9823.     end
  9824. go
  9825.  
  9826. if (charindex('7.00', @@version) = 0)
  9827. begin
  9828.     print ''
  9829.     print ''
  9830.     print 'Warning:'
  9831.     print 'you are installing the stored procedures '
  9832.     print 'on a pre 7.0 SQL Server.'
  9833.     print 'Ignore the following errors.'
  9834. end
  9835. go
  9836.  
  9837. /* 
  9838. ** (leih 2/11/98) 
  9839. ** The following SP is shared by instcat.sql and the server to add information
  9840. ** related to server language collations. The server calls it at the end of
  9841. ** collation change to update the catalog with the new language collations
  9842. ** Everything in this SP should NOT use tempdb.  
  9843. */
  9844. create procedure sp_add_server_sortinfo
  9845. as
  9846. -- spt_server_info has unigue clustered index on attribute_id
  9847. delete spt_server_info where attribute_id in (16,18)
  9848.  
  9849. --insert into spt_server_info
  9850. --    select 18,'COLLATION_SEQ',
  9851. --        'charset='+t2.name+' sort_order='+t1.name
  9852. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  9853. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  9854. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  9855. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  9856. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  9857. declare @case_sensitive bit
  9858. declare @ncase_sensitive bit
  9859. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  9860. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9861. select @sortid = value from sysconfigures where config = 1123
  9862. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  9863. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  9864.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  9865.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  9866.         from syscharsets where id = @csid
  9867.  
  9868. insert into spt_server_info
  9869.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  9870.  
  9871. if @case_sensitive = 1 /* If case sensitive server */
  9872. begin
  9873.     insert into spt_server_info
  9874.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  9875. end
  9876. else
  9877. begin
  9878.     insert into spt_server_info
  9879.         values (16,'IDENTIFIER_CASE','MIXED')
  9880. end
  9881.  
  9882.  
  9883. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  9884.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  9885. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  9886.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  9887. update spt_provider_types set case_sensitive = @case_sensitive 
  9888.     where oledb_data_type = 129 /*DBTYPE_STR*/
  9889. update spt_provider_types set case_sensitive = @ncase_sensitive
  9890.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  9891.  
  9892. go
  9893.  
  9894. if (charindex('7.00', @@version) <> 0)
  9895. begin
  9896.     exec sp_add_server_sortinfo
  9897. end
  9898. go
  9899.  
  9900. print ''
  9901. print 'creating sp_catalogs_rowset'
  9902. go
  9903.  
  9904. create procedure sp_catalogs_rowset
  9905.     (
  9906.     @catalog_name        varchar(255)
  9907.     )        
  9908. as
  9909.     select
  9910.         CATALOG_NAME    = name,
  9911.         DESCRIPTION    = convert(varchar(1),null)
  9912.     from     master.dbo.sysdatabases
  9913.     where    name = @catalog_name
  9914. go
  9915. dump tran master with no_log
  9916. go
  9917. create procedure sp_catalogs_rowset;2
  9918.     (
  9919.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9920.     )
  9921. as
  9922.     select
  9923.         CATALOG_NAME    = name,
  9924.         DESCRIPTION    = convert(varchar(1),null)
  9925.     from     master.dbo.sysdatabases
  9926.     order by 1
  9927. go
  9928. dump tran master with no_log
  9929. go
  9930.  
  9931. if (charindex('7.00', @@version) = 0)
  9932. begin
  9933.     print ''
  9934.     print ''
  9935.     print 'Warning:'
  9936.     print 'you are installing the stored procedures '
  9937.     print 'on a pre 7.0 SQL Server.'
  9938.     print 'Ignore the following errors.'
  9939. end
  9940. else
  9941.     drop proc sp_catalogs_rowset
  9942. go
  9943.  
  9944. /*    Procedure for 7.0 server */
  9945. create procedure sp_catalogs_rowset
  9946.     (
  9947.     @catalog_name        sysname
  9948.     )        
  9949. as
  9950.     select
  9951.         CATALOG_NAME    = name,
  9952.         DESCRIPTION    = convert(nvarchar(1),null)
  9953.     from     master.dbo.sysdatabases
  9954.     where    name = @catalog_name
  9955. go
  9956. dump tran master with no_log
  9957. go
  9958. create procedure sp_catalogs_rowset;2
  9959.     (
  9960.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9961.     )
  9962. as
  9963.     select
  9964.         CATALOG_NAME    = name,
  9965.         DESCRIPTION    = convert(nvarchar(1),null)
  9966.     from     master.dbo.sysdatabases
  9967.     order by 1
  9968. go
  9969. dump tran master with no_log
  9970. go
  9971. create procedure sp_catalogs_rowset;5
  9972.     (
  9973.     @server_name    sysname,
  9974.     @catalog_name    sysname = NULL
  9975.     )
  9976. as
  9977.     select    CATALOG_NAME,
  9978.         DESCRIPTION
  9979.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  9980.     order by 1
  9981. go
  9982.  
  9983. grant execute on sp_catalogs_rowset to public
  9984. go
  9985.  
  9986. dump tran master with no_log
  9987. go
  9988. if (charindex('6.00', @@version) > 0)
  9989.     begin
  9990.     if (exists (select * from sysobjects
  9991.             where name = 'sp_catalogs_rowset' and type = 'P '))
  9992.         begin
  9993.         drop procedure sp_catalogs_rowset
  9994.         dump tran master with no_log
  9995.         end
  9996.     end
  9997. go
  9998.  
  9999.  
  10000. print ''
  10001. print 'creating sp_column_privileges_rowset'
  10002. go
  10003.  
  10004. /*    Procedure for 6.0 and 6.5 server */
  10005. CREATE PROCEDURE sp_column_privileges_rowset
  10006.     (
  10007.     @table_name     varchar(255) = null,
  10008.     @table_schema    varchar(255) = null,
  10009.     @column_name    varchar(255) = null,
  10010.     @grantor    varchar(255) = null,
  10011.     @grantee    varchar(255) = null
  10012.     )
  10013. as
  10014. IF @table_name is not null
  10015.     BEGIN
  10016.     select
  10017.         GRANTOR        = user_name(p.grantor),
  10018.         GRANTEE        = user_name(u.uid),
  10019.         TABLE_CATALOG    = db_name(),
  10020.         TABLE_SCHEMA    = user_name(o.uid),
  10021.         TABLE_NAME    = o.name,
  10022.         COLUMN_NAME    = c.name,
  10023.         COLUMN_GUID    = convert(binary(16),null),
  10024.         COLUMN_PROPID    = convert(int,null),
  10025.         PRIVILEGE_TYPE    = convert(varchar(30),
  10026.                     case p.action
  10027.                     when 193 then 'SELECT'
  10028.                     when 195 then 'INSERT'
  10029.                     when 197 then 'UPDATE'
  10030.                     else 'REFERENCES'
  10031.                     end),
  10032.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10033.     from 
  10034.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  10035.     where
  10036.         o.name = @table_name
  10037.     and     o.type in ('U','V','S')
  10038.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10039.     and    c.id = o.id
  10040.     and     (@column_name is null or @column_name = c.name)
  10041.     and     c.id = p.id
  10042.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10043.     and     case 
  10044.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10045.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10046.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10047.             end & v.high <> 0            /* permission applies to this column */
  10048.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10049.     and     v.type = 'P'
  10050.     and     v.number = c.colid
  10051.     and    (@grantee is null or @grantee = user_name(u.uid))
  10052.             /* expand groups */
  10053.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10054.     and     p.protecttype <> 206    /* only grant rows */
  10055.     and     p.action in (26,193,195,197)
  10056.     and     o.uid <> u.uid            /* no rows for owner */
  10057.     and     not exists (            /* exclude revoke'd privileges */
  10058.             select *
  10059.             from sysprotects p1
  10060.             where
  10061.                 p1.protecttype = 206
  10062.             and     p1.action = p.action
  10063.             and     p1.id = p.id
  10064.             and     p1.uid = u.uid
  10065.             and     case 
  10066.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10067.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10068.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10069.                 end & v.high <> 0)            /* permission applies to this column */
  10070.     union all
  10071.     select    /*    Add rows for table owner */
  10072.         GRANTOR        = user_name(u.uid),
  10073.         GRANTEE        = user_name(o.uid),
  10074.         TABLE_CATALOG    = db_name(),
  10075.         TABLE_SCHEMA    = user_name(o.uid),
  10076.         TABLE_NAME    = o.name,
  10077.         COLUMN_NAME    = c.name,
  10078.         COLUMN_GUID    = convert(binary(16),null),
  10079.         COLUMN_PROPID    = convert(int,null),
  10080.         PRIVILEGE_TYPE    = convert(varchar(30),
  10081.                     case v.number
  10082.                     when 193 then 'SELECT'
  10083.                     when 195 then 'INSERT'
  10084.                     when 197 then 'UPDATE'
  10085.                     else 'REFERENCES'
  10086.                     end),
  10087.         IS_GRANTABLE    = convert(bit,1)    
  10088.     from 
  10089.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10090.     where
  10091.         o.name = @table_name
  10092.     and     o.type in ('U','V','S')
  10093.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10094.     and    (@grantee is null or @grantee = user_name(o.uid))
  10095.     and    c.id = o.id
  10096.     and     (@column_name is null or @column_name = c.name)
  10097.     and     u.suid = 1        /* grantor is dbo of database */
  10098.     and    (@grantor is null or @grantor = user_name(u.uid))
  10099.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10100.     and     v.number in (26,193,195,197)
  10101.     and     not exists (        /* exclude revoke'd privileges */
  10102.             select *
  10103.             from     sysprotects p1
  10104.             where    p1.protecttype = 206
  10105.             and     p1.action = v.number
  10106.             and     p1.id = o.id
  10107.             and     p1.uid = o.uid)
  10108.     order by 4, 5, 6, 9, 1, 2
  10109.     END
  10110. ELSE
  10111.     BEGIN
  10112.     select
  10113.         GRANTOR        = user_name(p.grantor),
  10114.         GRANTEE        = user_name(u.uid),
  10115.         TABLE_CATALOG    = db_name(),
  10116.         TABLE_SCHEMA    = user_name(o.uid),
  10117.         TABLE_NAME    = o.name,
  10118.         COLUMN_NAME    = c.name,
  10119.         COLUMN_GUID    = convert(binary(16),null),
  10120.         COLUMN_PROPID    = convert(int,null),
  10121.         PRIVILEGE_TYPE    = convert(varchar(30),
  10122.                     case p.action
  10123.                     when 193 then 'SELECT'
  10124.                     when 195 then 'INSERT'
  10125.                     when 197 then 'UPDATE'
  10126.                     else 'REFERENCES'
  10127.                     end),
  10128.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10129.     from 
  10130.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  10131.     where
  10132.         o.type in ('U','V','S')
  10133.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10134.     and    c.id = o.id
  10135.     and     (@column_name is null or @column_name = c.name)
  10136.     and     c.id = p.id
  10137.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10138.     and     case 
  10139.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10140.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10141.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10142.             end & v.high <> 0            /* permission applies to this column */
  10143.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10144.     and     v.type = 'P'
  10145.     and     v.number = c.colid
  10146.     and    (@grantee is null or @grantee = user_name(u.uid))
  10147.             /* expand groups */
  10148.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10149.     and     p.protecttype <> 206    /* only grant rows */
  10150.     and     p.action in (26,193,195,197)
  10151.     and     o.uid <> u.uid            /* no rows for owner */
  10152.     and     not exists (            /* exclude revoke'd privileges */
  10153.             select *
  10154.             from sysprotects p1
  10155.             where
  10156.                 p1.protecttype = 206
  10157.             and     p1.action = p.action
  10158.             and     p1.id = p.id
  10159.             and     p1.uid = u.uid
  10160.             and     case 
  10161.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10162.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10163.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10164.                 end & v.high <> 0)            /* permission applies to this column */
  10165.     union all
  10166.     select    /*    Add rows for table owner */
  10167.         GRANTOR        = user_name(u.uid),
  10168.         GRANTEE        = user_name(o.uid),
  10169.         TABLE_CATALOG    = db_name(),
  10170.         TABLE_SCHEMA    = user_name(o.uid),
  10171.         TABLE_NAME    = o.name,
  10172.         COLUMN_NAME    = c.name,
  10173.         COLUMN_GUID    = convert(binary(16),null),
  10174.         COLUMN_PROPID    = convert(int,null),
  10175.         PRIVILEGE_TYPE    = convert(varchar(30),
  10176.                     case v.number
  10177.                     when 193 then 'SELECT'
  10178.                     when 195 then 'INSERT'
  10179.                     when 197 then 'UPDATE'
  10180.                     else 'REFERENCES'
  10181.                     end),
  10182.         IS_GRANTABLE    = convert(bit,1)    
  10183.     from 
  10184.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10185.     where
  10186.          o.type in ('U','V','S')
  10187.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10188.     and    (@grantee is null or @grantee = user_name(o.uid))
  10189.     and    c.id = o.id
  10190.     and     (@column_name is null or @column_name = c.name)
  10191.     and     u.suid = 1        /* grantor is dbo of database */
  10192.     and    (@grantor is null or @grantor = user_name(u.uid))
  10193.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10194.     and     v.number in (26,193,195,197)
  10195.     and     not exists (        /* exclude revoke'd privileges */
  10196.             select *
  10197.             from     sysprotects p1
  10198.             where    p1.protecttype = 206
  10199.             and     p1.action = v.number
  10200.             and     p1.id = o.id
  10201.             and     p1.uid = o.uid)
  10202.     order by 4, 5, 6, 9, 1, 2
  10203.     END
  10204. go
  10205. dump tran master with no_log
  10206. go
  10207. CREATE PROCEDURE sp_column_privileges_rowset;2
  10208.     (
  10209.        @handle        int output,
  10210.        @scrollopt    int output,
  10211.     @ccopt        int output,
  10212.     @rows        int output,
  10213.     @table_name     varchar(255) = null,
  10214.     @table_schema    varchar(255) = null,
  10215.     @column_name    varchar(255) = null,
  10216.     @grantor    varchar(255) = null,
  10217.     @grantee    varchar(255) = null
  10218.     )
  10219. as
  10220.  
  10221. declare @ret int
  10222.  
  10223. SET NOCOUNT ON
  10224.     
  10225. create table #spcprivsrowset1
  10226.     (
  10227.     GRANTOR        sysname not null,
  10228.     GRANTEE        sysname not null,
  10229.     TABLE_CATALOG    sysname not null,
  10230.     TABLE_SCHEMA    sysname not null,
  10231.     TABLE_NAME    sysname not null,
  10232.     COLUMN_NAME    sysname not null,
  10233.     COLUMN_GUID    binary(16) null,
  10234.     COLUMN_PROPID    int null,
  10235.     PRIVILEGE_TYPE    sysname not null,
  10236.     IS_GRANTABLE    bit not null
  10237.     )
  10238.  
  10239. IF @table_name is not null
  10240.     BEGIN
  10241.     insert into #spcprivsrowset1
  10242.     select
  10243.         GRANTOR        = user_name(p.grantor),
  10244.         GRANTEE        = user_name(u.uid),
  10245.         TABLE_CATALOG    = db_name(),
  10246.         TABLE_SCHEMA    = user_name(o.uid),
  10247.         TABLE_NAME    = o.name,
  10248.         COLUMN_NAME    = c.name,
  10249.         COLUMN_GUID    = convert(binary(16),null),
  10250.         COLUMN_PROPID    = convert(int,null),
  10251.         PRIVILEGE_TYPE    = convert(varchar(30),
  10252.                     case p.action
  10253.                     when 193 then 'SELECT'
  10254.                     when 195 then 'INSERT'
  10255.                     when 197 then 'UPDATE'
  10256.                     else 'REFERENCES'
  10257.                     end),
  10258.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10259.     from 
  10260.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  10261.     where
  10262.         o.name = @table_name
  10263.     and     o.type in ('U','V','S')
  10264.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10265.     and    c.id = o.id
  10266.     and     (@column_name is null or @column_name = c.name)
  10267.     and     c.id = p.id
  10268.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10269.     and     case 
  10270.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10271.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10272.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10273.             end & v.high <> 0            /* permission applies to this column */
  10274.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10275.     and     v.type = 'P'
  10276.     and     v.number = c.colid
  10277.     and    (@grantee is null or @grantee = user_name(u.uid))
  10278.             /* expand groups */
  10279.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10280.     and     p.protecttype <> 206    /* only grant rows */
  10281.     and     p.action in (26,193,195,197)
  10282.     and     o.uid <> u.uid            /* no rows for owner */
  10283.     and     not exists (            /* exclude revoke'd privileges */
  10284.             select *
  10285.             from sysprotects p1
  10286.             where
  10287.                 p1.protecttype = 206
  10288.             and     p1.action = p.action
  10289.             and     p1.id = p.id
  10290.             and     p1.uid = u.uid
  10291.             and     case 
  10292.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10293.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10294.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10295.                 end & v.high <> 0)            /* permission applies to this column */
  10296.     union all
  10297.     select    /*    Add rows for table owner */
  10298.         GRANTOR        = user_name(u.uid),
  10299.         GRANTEE        = user_name(o.uid),
  10300.         TABLE_CATALOG    = db_name(),
  10301.         TABLE_SCHEMA    = user_name(o.uid),
  10302.         TABLE_NAME    = o.name,
  10303.         COLUMN_NAME    = c.name,
  10304.         COLUMN_GUID    = convert(binary(16),null),
  10305.         COLUMN_PROPID    = convert(int,null),
  10306.         PRIVILEGE_TYPE    = convert(varchar(30),
  10307.                     case v.number
  10308.                     when 193 then 'SELECT'
  10309.                     when 195 then 'INSERT'
  10310.                     when 197 then 'UPDATE'
  10311.                     else 'REFERENCES'
  10312.                     end),
  10313.         IS_GRANTABLE    = convert(bit,1)    
  10314.     from 
  10315.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10316.     where
  10317.         o.name = @table_name
  10318.     and     o.type in ('U','V','S')
  10319.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10320.     and    (@grantee is null or @grantee = user_name(o.uid))
  10321.     and    c.id = o.id
  10322.     and     (@column_name is null or @column_name = c.name)
  10323.     and     u.suid = 1        /* grantor is dbo of database */
  10324.     and    (@grantor is null or @grantor = user_name(u.uid))
  10325.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10326.     and     v.number in (26,193,195,197)
  10327.     and     not exists (        /* exclude revoke'd privileges */
  10328.             select *
  10329.             from     sysprotects p1
  10330.             where    p1.protecttype = 206
  10331.             and     p1.action = v.number
  10332.             and     p1.id = o.id
  10333.             and     p1.uid = o.uid)
  10334.     order by 4, 5, 6, 9, 1, 2
  10335.     END
  10336. ELSE
  10337.     BEGIN
  10338.     insert into #spcprivsrowset1
  10339.     select
  10340.         GRANTOR        = user_name(p.grantor),
  10341.         GRANTEE        = user_name(u.uid),
  10342.         TABLE_CATALOG    = db_name(),
  10343.         TABLE_SCHEMA    = user_name(o.uid),
  10344.         TABLE_NAME    = o.name,
  10345.         COLUMN_NAME    = c.name,
  10346.         COLUMN_GUID    = convert(binary(16),null),
  10347.         COLUMN_PROPID    = convert(int,null),
  10348.         PRIVILEGE_TYPE    = convert(varchar(30),
  10349.                     case p.action
  10350.                     when 193 then 'SELECT'
  10351.                     when 195 then 'INSERT'
  10352.                     when 197 then 'UPDATE'
  10353.                     else 'REFERENCES'
  10354.                     end),
  10355.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10356.     from 
  10357.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  10358.     where
  10359.         o.type in ('U','V','S')
  10360.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10361.     and    c.id = o.id
  10362.     and     (@column_name is null or @column_name = c.name)
  10363.     and     c.id = p.id
  10364.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10365.     and     case 
  10366.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10367.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10368.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10369.             end & v.high <> 0            /* permission applies to this column */
  10370.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10371.     and     v.type = 'P'
  10372.     and     v.number = c.colid
  10373.     and    (@grantee is null or @grantee = user_name(u.uid))
  10374.             /* expand groups */
  10375.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10376.     and     p.protecttype <> 206    /* only grant rows */
  10377.     and     p.action in (26,193,195,197)
  10378.     and     o.uid <> u.uid            /* no rows for owner */
  10379.     and     not exists (            /* exclude revoke'd privileges */
  10380.             select *
  10381.             from sysprotects p1
  10382.             where
  10383.                 p1.protecttype = 206
  10384.             and     p1.action = p.action
  10385.             and     p1.id = p.id
  10386.             and     p1.uid = u.uid
  10387.             and     case 
  10388.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10389.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10390.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10391.                 end & v.high <> 0)            /* permission applies to this column */
  10392.     union all
  10393.     select    /*    Add rows for table owner */
  10394.         GRANTOR        = user_name(u.uid),
  10395.         GRANTEE        = user_name(o.uid),
  10396.         TABLE_CATALOG    = db_name(),
  10397.         TABLE_SCHEMA    = user_name(o.uid),
  10398.         TABLE_NAME    = o.name,
  10399.         COLUMN_NAME    = c.name,
  10400.         COLUMN_GUID    = convert(binary(16),null),
  10401.         COLUMN_PROPID    = convert(int,null),
  10402.         PRIVILEGE_TYPE    = convert(varchar(30),
  10403.                     case v.number
  10404.                     when 193 then 'SELECT'
  10405.                     when 195 then 'INSERT'
  10406.                     when 197 then 'UPDATE'
  10407.                     else 'REFERENCES'
  10408.                     end),
  10409.         IS_GRANTABLE    = convert(bit,1)    
  10410.     from 
  10411.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10412.     where
  10413.          o.type in ('U','V','S')
  10414.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10415.     and    (@grantee is null or @grantee = user_name(o.uid))
  10416.     and    c.id = o.id
  10417.     and     (@column_name is null or @column_name = c.name)
  10418.     and     u.suid = 1        /* grantor is dbo of database */
  10419.     and    (@grantor is null or @grantor = user_name(u.uid))
  10420.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10421.     and     v.number in (26,193,195,197)
  10422.     and     not exists (        /* exclude revoke'd privileges */
  10423.             select *
  10424.             from     sysprotects p1
  10425.             where    p1.protecttype = 206
  10426.             and     p1.action = v.number
  10427.             and     p1.id = o.id
  10428.             and     p1.uid = o.uid)
  10429.     order by 4, 5, 6, 9, 1, 2
  10430.     END
  10431.  
  10432. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  10433.     @scrollopt output, @ccopt output, @rows output
  10434.  
  10435. drop table #spcprivsrowset1
  10436.  
  10437. return isnull(@ret,0)
  10438. go
  10439. dump tran master with no_log
  10440. go
  10441. CREATE PROCEDURE sp_column_privileges_rowset;3
  10442. as
  10443.     select
  10444.         GRANTOR        = convert(sysname,' '),
  10445.         GRANTEE        = convert(sysname,' '),
  10446.         TABLE_CATALOG    = convert(sysname,' '),
  10447.         TABLE_SCHEMA    = convert(sysname,' '),
  10448.         TABLE_NAME    = convert(sysname,' '),
  10449.         COLUMN_NAME    = convert(sysname,' '),
  10450.         COLUMN_GUID    = convert(binary(16),null),
  10451.         COLUMN_PROPID    = convert(int,null),
  10452.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  10453.         IS_GRANTABLE    = convert(bit,1)
  10454.     where    1=0
  10455. go
  10456. dump tran master with no_log
  10457. go
  10458.  
  10459. if (charindex('7.00', @@version) = 0)
  10460. begin
  10461.     print ''
  10462.     print ''
  10463.     print 'Warning:'
  10464.     print 'you are installing the stored procedures '
  10465.     print 'on a pre 7.0 SQL Server.'
  10466.     print 'Ignore the following errors.'
  10467. end
  10468. else
  10469.     drop proc sp_column_privileges_rowset
  10470. go
  10471.  
  10472.  
  10473. /*    Procedure for 7.0 server */
  10474. CREATE PROCEDURE sp_column_privileges_rowset
  10475.     (
  10476.     @table_name         sysname,
  10477.     @table_schema        sysname = null,
  10478.     @column_name        sysname = null,
  10479.     @grantor        sysname = null,
  10480.     @grantee        sysname = null
  10481.     )
  10482. as
  10483.     select
  10484.         GRANTOR        = user_name(p.grantor),
  10485.         GRANTEE        = user_name(u.uid),
  10486.         TABLE_CATALOG    = db_name(),
  10487.         TABLE_SCHEMA    = user_name(o.uid),
  10488.         TABLE_NAME    = o.name,
  10489.         COLUMN_NAME    = c.name,
  10490.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10491.         COLUMN_PROPID    = convert(int,null),
  10492.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10493.                     case p.action
  10494.                     when 193 then N'SELECT'
  10495.                     when 195 then N'INSERT'
  10496.                     when 197 then N'UPDATE'
  10497.                     else N'REFERENCES'
  10498.                     end),
  10499.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10500.     from 
  10501.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  10502.     where
  10503.         o.name = @table_name
  10504.     and     o.type in ('U','V','S')
  10505.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10506.     and    c.id = o.id
  10507.     and     (@column_name is null or @column_name = c.name)
  10508.     and     c.id = p.id
  10509.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10510.     and     case 
  10511.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10512.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10513.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10514.             end & v.high <> 0            /* permission applies to this column */
  10515.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10516.     and     v.type = 'P'
  10517.     and     v.number = c.colid
  10518.     and    (@grantee is null or @grantee = user_name(u.uid))
  10519.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  10520.     and     (u.uid > 0 and u.uid < 16384)
  10521.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  10522.     and     p.protecttype <> 206    /* only grant rows */
  10523.     and     p.action in (26,193,195,197)
  10524.     and     o.uid <> u.uid            /* no rows for owner */
  10525.     and     not exists (            /* exclude revoke'd privileges */
  10526.             select *
  10527.             from sysprotects p1
  10528.             where
  10529.                 p1.protecttype = 206
  10530.             and     p1.action = p.action
  10531.             and     p1.id = p.id
  10532.             and     p1.uid = u.uid
  10533.             and     case 
  10534.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10535.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10536.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10537.                 end & v.high <> 0)            /* permission applies to this column */
  10538.     union all
  10539.     select    /*    Add rows for table owner */
  10540.         GRANTOR        = user_name(u.uid),
  10541.         GRANTEE        = user_name(o.uid),
  10542.         TABLE_CATALOG    = db_name(),
  10543.         TABLE_SCHEMA    = user_name(o.uid),
  10544.         TABLE_NAME    = o.name,
  10545.         COLUMN_NAME    = c.name,
  10546.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10547.         COLUMN_PROPID    = convert(int,null),
  10548.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10549.                     case v.number
  10550.                     when 193 then N'SELECT'
  10551.                     when 195 then N'INSERT'
  10552.                     when 197 then N'UPDATE'
  10553.                     else N'REFERENCES'
  10554.                     end),
  10555.         IS_GRANTABLE    = convert(bit,1)    
  10556.     from 
  10557.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10558.     where
  10559.         o.name = @table_name
  10560.     and     o.type in ('U','V','S')
  10561.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10562.     and    (@grantee is null or @grantee = user_name(o.uid))
  10563.     and    c.id = o.id
  10564.     and     (@column_name is null or @column_name = c.name)
  10565.     and     u.suid = 1        /* grantor is dbo of database */
  10566.     and    (@grantor is null or @grantor = user_name(u.uid))
  10567.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10568.     and     v.number in (26,193,195,197)
  10569.     and     not exists (        /* exclude revoke'd privileges */
  10570.             select *
  10571.             from     sysprotects p1
  10572.             where    p1.protecttype = 206
  10573.             and     p1.action = v.number
  10574.             and     p1.id = o.id
  10575.             and     p1.uid = o.uid)
  10576.     order by 4, 5, 6, 9, 1, 2
  10577. go
  10578. dump tran master with no_log
  10579. go
  10580. CREATE PROCEDURE sp_column_privileges_rowset;2
  10581.     (
  10582.     @table_schema        sysname = null,
  10583.     @column_name        sysname = null,
  10584.     @grantor        sysname = null,
  10585.     @grantee        sysname = null
  10586.     )
  10587. as
  10588.     select
  10589.         GRANTOR        = user_name(p.grantor),
  10590.         GRANTEE        = user_name(u.uid),
  10591.         TABLE_CATALOG    = db_name(),
  10592.         TABLE_SCHEMA    = user_name(o.uid),
  10593.         TABLE_NAME    = o.name,
  10594.         COLUMN_NAME    = c.name,
  10595.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10596.         COLUMN_PROPID    = convert(int,null),
  10597.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10598.                     case p.action
  10599.                     when 193 then N'SELECT'
  10600.                     when 195 then N'INSERT'
  10601.                     when 197 then N'UPDATE'
  10602.                     else N'REFERENCES'
  10603.                     end),
  10604.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10605.     from 
  10606.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  10607.     where
  10608.         o.type in ('U','V','S')
  10609.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10610.     and    c.id = o.id
  10611.     and     (@column_name is null or @column_name = c.name)
  10612.     and     c.id = p.id
  10613.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10614.     and     case 
  10615.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10616.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10617.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10618.             end & v.high <> 0            /* permission applies to this column */
  10619.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10620.     and     v.type = 'P'
  10621.     and     v.number = c.colid
  10622.     and    (@grantee is null or @grantee = user_name(u.uid))
  10623.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  10624.     and     (u.uid > 0 and u.uid < 16384)
  10625.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  10626.     and     p.protecttype <> 206    /* only grant rows */
  10627.     and     p.action in (26,193,195,197)
  10628.     and     o.uid <> u.uid            /* no rows for owner */
  10629.     and     not exists (            /* exclude revoke'd privileges */
  10630.             select *
  10631.             from sysprotects p1
  10632.             where
  10633.                 p1.protecttype = 206
  10634.             and     p1.action = p.action
  10635.             and     p1.id = p.id
  10636.             and     p1.uid = u.uid
  10637.             and     case 
  10638.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10639.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10640.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10641.                 end & v.high <> 0)            /* permission applies to this column */
  10642.     union all
  10643.     select    /*    Add rows for table owner */
  10644.         GRANTOR        = user_name(u.uid),
  10645.         GRANTEE        = user_name(o.uid),
  10646.         TABLE_CATALOG    = db_name(),
  10647.         TABLE_SCHEMA    = user_name(o.uid),
  10648.         TABLE_NAME    = o.name,
  10649.         COLUMN_NAME    = c.name,
  10650.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10651.         COLUMN_PROPID    = convert(int,null),
  10652.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10653.                     case v.number
  10654.                     when 193 then N'SELECT'
  10655.                     when 195 then N'INSERT'
  10656.                     when 197 then N'UPDATE'
  10657.                     else N'REFERENCES'
  10658.                     end),
  10659.         IS_GRANTABLE    = convert(bit,1)    
  10660.     from 
  10661.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10662.     where
  10663.         o.type in ('U','V','S')
  10664.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10665.     and    (@grantee is null or @grantee = user_name(o.uid))
  10666.     and    c.id = o.id
  10667.     and     (@column_name is null or @column_name = c.name)
  10668.     and     u.suid = 1        /* grantor is dbo of database */
  10669.     and    (@grantor is null or @grantor = user_name(u.uid))
  10670.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10671.     and     v.number in (26,193,195,197)
  10672.     and     not exists (        /* exclude revoke'd privileges */
  10673.             select *
  10674.             from     sysprotects p1
  10675.             where    p1.protecttype = 206
  10676.             and     p1.action = v.number
  10677.             and     p1.id = o.id
  10678.             and     p1.uid = o.uid)
  10679.     order by 4, 5, 6, 9, 1, 2
  10680. go
  10681. dump tran master with no_log
  10682. go
  10683. create procedure sp_column_privileges_rowset;5
  10684.     (
  10685.     @table_server        sysname,
  10686.     @table_catalog        sysname = null,
  10687.     @table_name        sysname = null,
  10688.     @table_schema        sysname = null,
  10689.     @column_name        sysname = null,
  10690.     @grantor        sysname = null,
  10691.     @grantee        sysname = null
  10692.     )
  10693. as
  10694.     select
  10695.         GRANTOR,
  10696.         GRANTEE,
  10697.         TABLE_CATALOG,
  10698.         TABLE_SCHEMA,
  10699.         TABLE_NAME,
  10700.         COLUMN_NAME,
  10701.         COLUMN_GUID,
  10702.         COLUMN_PROPID,    
  10703.         PRIVILEGE_TYPE,
  10704.         IS_GRANTABLE    
  10705.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  10706.                 @table_server,
  10707.                 @table_catalog,
  10708.                 @table_schema,
  10709.                 @table_name,
  10710.                 @column_name,
  10711.                 @grantor,
  10712.                 @grantee>
  10713.     order by 3, 4, 5, 6, 9, 1, 2
  10714. go
  10715.  
  10716. grant execute on sp_column_privileges_rowset to public
  10717. go
  10718.  
  10719. dump tran master with no_log
  10720. go
  10721. if (charindex('6.00', @@version) > 0)
  10722.     begin
  10723.     if (exists (select * from sysobjects
  10724.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  10725.         begin
  10726.         drop procedure sp_column_privileges_rowset
  10727.         dump tran master with no_log
  10728.         end
  10729.     end
  10730. go
  10731.  
  10732.  
  10733. print ''
  10734. print 'creating sp_columns_rowset'
  10735. go
  10736.  
  10737. /*    Procedure for 6.0 and 6.50 server */
  10738. create procedure sp_columns_rowset
  10739.     (
  10740.        @table_name    varchar(255),
  10741.     @table_schema     varchar(255) = null,
  10742.     @column_name    varchar(255) = null
  10743.     )
  10744. as     
  10745.     set nocount on
  10746.  
  10747.     /* Declare bindings */
  10748.     DECLARE @TABLECATALOG    sysname,
  10749.         @TABLESCHEMA     sysname,
  10750.         @TABLENAME       sysname,
  10751.         @COLUMNNAME      sysname,
  10752.         @ORDINALPOSITION int
  10753.  
  10754.     /* Declare tracking variables */
  10755.     DECLARE @currCatalog sysname,
  10756.          @currSchema  sysname,
  10757.         @currName    sysname,
  10758.         @currPos     int
  10759.  
  10760.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  10761.     CREATE TABLE #columns_rowset
  10762.     (
  10763.         TABLE_CATALOG            sysname,
  10764.         TABLE_SCHEMA            sysname,
  10765.         TABLE_NAME            sysname,
  10766.         COLUMN_NAME            sysname,
  10767.         COLUMN_GUID            binary(16)        null,
  10768.         COLUMN_PROPID            int            null,
  10769.         ORDINAL_POSITION        int,
  10770.         COLUMN_HASDEFAULT        bit,
  10771.         COLUMN_DEFAULT            varchar(255),
  10772.         COLUMN_FLAGS            int,
  10773.         IS_NULLABLE            bit,
  10774.         DATA_TYPE            smallint,
  10775.         TYPE_GUID            binary(16)        null,
  10776.         CHARACTER_MAXIMUM_LENGTH    int            null,
  10777.         CHARACTER_OCTET_LENGTH        int            null,
  10778.         NUMERIC_PRECISION        smallint        null,
  10779.         NUMERIC_SCALE            smallint        null, 
  10780.         DATETIME_PRECISION        int            null,
  10781.         CHARACTER_SET_CATALOG        sysname            null,
  10782.         CHARACTER_SET_SCHEMA        sysname            null,
  10783.         CHARACTER_SET_NAME        sysname            null,
  10784.         COLLATION_CATALOG        sysname            null,
  10785.         COLLATION_SCHEMA        sysname            null,
  10786.         COLLATION_NAME            sysname            null,
  10787.         DOMAIN_CATALOG            sysname         null,
  10788.         DOMAIN_SCHEMA            sysname         null,
  10789.         DOMAIN_NAME            sysname         null,
  10790.         DESCRIPTION            varchar(1)
  10791.     )        
  10792.  
  10793.     /* Declare cursor for fixup */
  10794.     DECLARE curColRowset CURSOR
  10795.     FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  10796.         FROM #columns_rowset
  10797.         ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION
  10798.  
  10799.  
  10800.     /* Store results into the temp table */
  10801.     insert into #columns_rowset
  10802.         (
  10803.         TABLE_CATALOG,
  10804.         TABLE_SCHEMA,
  10805.         TABLE_NAME,
  10806.         COLUMN_NAME,
  10807.         COLUMN_GUID,
  10808.         COLUMN_PROPID,
  10809.         ORDINAL_POSITION,
  10810.         COLUMN_HASDEFAULT,
  10811.         COLUMN_DEFAULT,
  10812.         COLUMN_FLAGS,
  10813.         IS_NULLABLE,
  10814.         DATA_TYPE,
  10815.         TYPE_GUID,
  10816.         CHARACTER_MAXIMUM_LENGTH,
  10817.         CHARACTER_OCTET_LENGTH,
  10818.         NUMERIC_PRECISION,
  10819.         NUMERIC_SCALE,
  10820.         DATETIME_PRECISION,
  10821.         CHARACTER_SET_CATALOG,
  10822.         CHARACTER_SET_SCHEMA,
  10823.         CHARACTER_SET_NAME,
  10824.         COLLATION_CATALOG,
  10825.         COLLATION_SCHEMA,
  10826.         COLLATION_NAME,
  10827.         DOMAIN_CATALOG,
  10828.         DOMAIN_SCHEMA,
  10829.         DOMAIN_NAME,
  10830.         DESCRIPTION
  10831.         )
  10832.     select
  10833.         TABLE_CATALOG        = db_name(),
  10834.         TABLE_SCHEMA        = user_name(o.uid),
  10835.         TABLE_NAME        = o.name,
  10836.         COLUMN_NAME        = c.name,
  10837.         COLUMN_GUID        = convert(binary(16),null),
  10838.         COLUMN_PROPID        = convert(int,null),
  10839.         ORDINAL_POSITION    = convert(int, c.colid),
  10840.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10841.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  10842.         COLUMN_FLAGS        = convert(int,
  10843.                         case when d.is_long = 1 
  10844.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10845.                         else 0
  10846.                         end
  10847.                     |    case when d.fixlen is not null
  10848.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10849.                         else 0
  10850.                         end
  10851.                     |    case when c.status&8 = 8
  10852.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10853.                         else 0
  10854.                         end
  10855.                     |    case 
  10856.                         when d.type_name = 'timestamp'
  10857.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10858.                         when (c.status&128) != 128
  10859.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  10860.                         else 0 
  10861.                         end),
  10862.         IS_NULLABLE        = convert(bit,c.status&8),
  10863.         DATA_TYPE        = d.oledb_data_type,
  10864.         TYPE_GUID        = convert(binary(16),null),
  10865.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10866.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10867.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10868.                         then coalesce(d.column_size,c.length)
  10869.                         else null 
  10870.                         end),
  10871.         CHARACTER_OCTET_LENGTH    = convert(int,
  10872.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10873.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10874.                         then coalesce(d.column_size,c.length)
  10875.                         else null 
  10876.                         end),
  10877.         NUMERIC_PRECISION    = convert(smallint,
  10878.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  10879.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10880.         NUMERIC_SCALE        = convert(smallint, 
  10881.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10882.         DATETIME_PRECISION    = convert(int,
  10883.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10884.                             when data_precision = 23 then 3 else 0 end),
  10885.         CHARACTER_SET_CATALOG    = convert(sysname,
  10886.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10887.                         then 'master' 
  10888.                         else null 
  10889.                         end),
  10890.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10891.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10892.                         then 'dbo' 
  10893.                         else null 
  10894.                         end),
  10895.         CHARACTER_SET_NAME    = convert(sysname,
  10896.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10897.                         then a_cha.name 
  10898.                         else null 
  10899.                         end),
  10900.         COLLATION_CATALOG    = convert(sysname,
  10901.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10902.                         then 'master' 
  10903.                         else null 
  10904.                         end),
  10905.         COLLATION_SCHEMA    = convert(sysname,
  10906.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10907.                         then 'dbo' 
  10908.                         else null 
  10909.                         end),
  10910.         COLLATION_NAME        = convert(sysname,
  10911.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10912.                         then b_cha.name 
  10913.                         else null 
  10914.                         end),
  10915.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10916.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10917.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10918.         DESCRIPTION        = convert(varchar(1),null)    
  10919.     
  10920.     from
  10921.         syscolumns c,
  10922.         syscomments m,
  10923.         sysobjects o,
  10924.         master.dbo.spt_provider_types d,
  10925.         systypes t,
  10926.         sysusers u,
  10927.         master.dbo.sysconfigures    cfg,
  10928.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10929.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  10930.     where
  10931.         o.name = @table_name
  10932.     and     o.type in ('U','V','S')
  10933.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  10934.     and     (
  10935.         suser_id() = 1     /* User is the System Administrator */
  10936.         or o.uid = user_id()     /* User created the object */
  10937.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  10938.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  10939.             from sysprotects p
  10940.             where p.id = o.id
  10941.             /* get rows for public,current user,user's group */
  10942.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  10943.             /* check for SELECT,EXECUTE privilege */
  10944.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  10945.             ) = 1    /* final magic...compare Grants      */
  10946.            )
  10947.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10948.     and    (@column_name is null or @column_name = c.name)
  10949.     and     o.id = c.id
  10950.     and     t.type = d.ss_dtype
  10951.     and    (t.usertype != 80 or d.type_name='timestamp')
  10952.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10953.     and     c.usertype = t.usertype
  10954.     and    c.cdefault *= m.id
  10955.     and    m.colid = 1
  10956.     and    cfg.comment = 'default sortorder ID' 
  10957.     and    a_cha.type = 1001 /* type is charset */
  10958.     and    b_cha.type = 2001 /* type is sortorder */
  10959.     and    a_cha.id = b_cha.csid
  10960.     and     b_cha.id = cfg.value
  10961.     order by 2, 3, c.colid
  10962.  
  10963.     /* Fix ordinal positions */
  10964.     OPEN curColRowset
  10965.     FETCH NEXT FROM curColRowset
  10966.     INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  10967.     
  10968.     /* Initialize tracking variables */
  10969.     SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL
  10970.  
  10971.     WHILE @@FETCH_STATUS = 0
  10972.     BEGIN
  10973.         /* Check to see if the catalog/schema/table */
  10974.         /* has changed since last row               */
  10975.         IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
  10976.             @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
  10977.         BEGIN
  10978.             /* Increment ordinal count */
  10979.             SELECT @currPos = @currPos + 1
  10980.         END
  10981.         ELSE
  10982.         BEGIN
  10983.             /* Set the catalog/schema/table to new values */
  10984.             SELECT @currCatalog = @TABLECATALOG,
  10985.                    @currSchema  = @TABLESCHEMA,
  10986.                    @currName    = @TABLENAME,
  10987.                    @currPos     = 1
  10988.         END
  10989.  
  10990.         /* Check for holes in the ordinal position order */
  10991.         if (@currPos < @ORDINALPOSITION)
  10992.         BEGIN        
  10993.             /* Update the ordinal position */
  10994.             UPDATE #columns_rowset
  10995.             SET    ORDINAL_POSITION = @currPos
  10996.             WHERE  TABLE_CATALOG = @TABLECATALOG
  10997.               AND  TABLE_SCHEMA  = @TABLESCHEMA
  10998.               AND  TABLE_NAME    = @TABLENAME
  10999.               AND  COLUMN_NAME   = @COLUMNNAME
  11000.         END
  11001.  
  11002.         /* Fetch next result */
  11003.         FETCH NEXT FROM curColRowset
  11004.         INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11005.     END
  11006.  
  11007.     /* Cleanup cursor */
  11008.     CLOSE curColRowset
  11009.     DEALLOCATE curColRowset
  11010.  
  11011.     set nocount off
  11012.  
  11013.     /* Output the result set */
  11014.     select *
  11015.     from #columns_rowset
  11016.     order by 2, 3, 7
  11017. go
  11018.  
  11019. dump tran master with no_log
  11020. go
  11021.  
  11022. create procedure sp_columns_rowset;2
  11023.     (
  11024.     @table_schema     varchar(255) = null,
  11025.     @column_name    varchar(255) = null
  11026.     )
  11027. as     
  11028.     set nocount on
  11029.  
  11030.     /* Declare bindings */
  11031.     DECLARE @TABLECATALOG    sysname,
  11032.         @TABLESCHEMA     sysname,
  11033.         @TABLENAME       sysname,
  11034.         @COLUMNNAME      sysname,
  11035.         @ORDINALPOSITION int
  11036.  
  11037.     /* Declare tracking variables */
  11038.     DECLARE @currCatalog sysname,
  11039.          @currSchema  sysname,
  11040.         @currName    sysname,
  11041.         @currPos     int
  11042.  
  11043.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  11044.     CREATE TABLE #columns_rowset
  11045.     (
  11046.         TABLE_CATALOG            sysname,
  11047.         TABLE_SCHEMA            sysname,
  11048.         TABLE_NAME            sysname,
  11049.         COLUMN_NAME            sysname,
  11050.         COLUMN_GUID            binary(16)        null,
  11051.         COLUMN_PROPID            int            null,
  11052.         ORDINAL_POSITION        int            null,
  11053.         COLUMN_HASDEFAULT        bit,
  11054.         COLUMN_DEFAULT            varchar(255),
  11055.         COLUMN_FLAGS            int,
  11056.         IS_NULLABLE            bit,
  11057.         DATA_TYPE            smallint,
  11058.         TYPE_GUID            binary(16)        null,
  11059.         CHARACTER_MAXIMUM_LENGTH    int            null,
  11060.         CHARACTER_OCTET_LENGTH        int            null,
  11061.         NUMERIC_PRECISION        smallint        null,
  11062.         NUMERIC_SCALE            smallint        null, 
  11063.         DATETIME_PRECISION        int            null,
  11064.         CHARACTER_SET_CATALOG        sysname            null,
  11065.         CHARACTER_SET_SCHEMA        sysname            null,
  11066.         CHARACTER_SET_NAME        sysname            null,
  11067.         COLLATION_CATALOG        sysname            null,
  11068.         COLLATION_SCHEMA        sysname            null,
  11069.         COLLATION_NAME            sysname            null,
  11070.         DOMAIN_CATALOG            sysname         null,
  11071.         DOMAIN_SCHEMA            sysname         null,
  11072.         DOMAIN_NAME            sysname         null,
  11073.         DESCRIPTION            varchar(1)
  11074.     )        
  11075.  
  11076.     /* Declare cursor for fixup */
  11077.     DECLARE curColRowset CURSOR
  11078.     FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  11079.         FROM #columns_rowset
  11080.         ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION
  11081.  
  11082.     /* Store results into the temp table */
  11083.     insert into #columns_rowset
  11084.         (
  11085.         TABLE_CATALOG,
  11086.         TABLE_SCHEMA,
  11087.         TABLE_NAME,
  11088.         COLUMN_NAME,
  11089.         COLUMN_GUID,
  11090.         COLUMN_PROPID,
  11091.         ORDINAL_POSITION,
  11092.         COLUMN_HASDEFAULT,
  11093.         COLUMN_DEFAULT,
  11094.         COLUMN_FLAGS,
  11095.         IS_NULLABLE,
  11096.         DATA_TYPE,
  11097.         TYPE_GUID,
  11098.         CHARACTER_MAXIMUM_LENGTH,
  11099.         CHARACTER_OCTET_LENGTH,
  11100.         NUMERIC_PRECISION,
  11101.         NUMERIC_SCALE,
  11102.         DATETIME_PRECISION,
  11103.         CHARACTER_SET_CATALOG,
  11104.         CHARACTER_SET_SCHEMA,
  11105.         CHARACTER_SET_NAME,
  11106.         COLLATION_CATALOG,
  11107.         COLLATION_SCHEMA,
  11108.         COLLATION_NAME,
  11109.         DOMAIN_CATALOG,
  11110.         DOMAIN_SCHEMA,
  11111.         DOMAIN_NAME,
  11112.         DESCRIPTION
  11113.         )
  11114.     select
  11115.         TABLE_CATALOG        = db_name(),
  11116.         TABLE_SCHEMA        = user_name(o.uid),
  11117.         TABLE_NAME        = o.name,
  11118.         COLUMN_NAME        = c.name,
  11119.         COLUMN_GUID        = convert(binary(16),null),
  11120.         COLUMN_PROPID        = convert(int,null),
  11121.         ORDINAL_POSITION    = convert(int, c.colid),
  11122.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  11123.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  11124.         COLUMN_FLAGS        = convert(int,
  11125.                         case when d.is_long = 1 
  11126.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  11127.                         else 0
  11128.                         end
  11129.                     |    case when d.fixlen is not null
  11130.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  11131.                         else 0
  11132.                         end
  11133.                     |    case when c.status&8 = 8
  11134.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  11135.                         else 0
  11136.                         end
  11137.                     |    case 
  11138.                         when d.type_name = 'timestamp'
  11139.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  11140.                         when (c.status&128) != 128
  11141.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  11142.                         else 0 
  11143.                         end),
  11144.         IS_NULLABLE        = convert(bit,c.status&8),
  11145.         DATA_TYPE        = d.oledb_data_type,
  11146.         TYPE_GUID        = convert(binary(16),null),
  11147.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  11148.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11149.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11150.                         then coalesce(d.column_size,c.length)
  11151.                         else null 
  11152.                         end),
  11153.         CHARACTER_OCTET_LENGTH    = convert(int,
  11154.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11155.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11156.                         then coalesce(d.column_size,c.length)
  11157.                         else null 
  11158.                         end),
  11159.         NUMERIC_PRECISION    = convert(smallint,
  11160.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  11161.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  11162.         NUMERIC_SCALE        = convert(smallint, 
  11163.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  11164.         DATETIME_PRECISION    = convert(int,
  11165.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  11166.                             when data_precision = 23 then 3 else 0 end),
  11167.         CHARACTER_SET_CATALOG    = convert(sysname,
  11168.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11169.                         then 'master' 
  11170.                         else null 
  11171.                         end),
  11172.         CHARACTER_SET_SCHEMA    = convert(sysname,
  11173.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11174.                         then 'dbo' 
  11175.                         else null 
  11176.                         end),
  11177.         CHARACTER_SET_NAME    = convert(sysname,
  11178.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11179.                         then a_cha.name 
  11180.                         else null 
  11181.                         end),
  11182.         COLLATION_CATALOG    = convert(sysname,
  11183.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11184.                         then 'master' 
  11185.                         else null 
  11186.                         end),
  11187.         COLLATION_SCHEMA    = convert(sysname,
  11188.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11189.                         then 'dbo' 
  11190.                         else null 
  11191.                         end),
  11192.         COLLATION_NAME        = convert(sysname,
  11193.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11194.                         then b_cha.name 
  11195.                         else null 
  11196.                         end),
  11197.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  11198.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  11199.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  11200.         DESCRIPTION        = convert(varchar(1),null)    
  11201.     
  11202.     from
  11203.         syscolumns c,
  11204.         syscomments m,
  11205.         sysobjects o,
  11206.         master.dbo.spt_provider_types d,
  11207.         systypes t,
  11208.         sysusers u,
  11209.         master.dbo.sysconfigures    cfg,
  11210.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  11211.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  11212.     where
  11213.          o.type in ('U','V','S')
  11214.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  11215.     and     (
  11216.         suser_id() = 1     /* User is the System Administrator */
  11217.         or o.uid = user_id()     /* User created the object */
  11218.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  11219.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  11220.             from sysprotects p
  11221.             where p.id = o.id
  11222.             /* get rows for public,current user,user's group */
  11223.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  11224.             /* check for SELECT,EXECUTE privilege */
  11225.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  11226.             ) = 1    /* final magic...compare Grants      */
  11227.            )
  11228.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  11229.     and    (@column_name is null or @column_name = c.name)
  11230.     and     o.id = c.id
  11231.     and     t.type = d.ss_dtype
  11232.     and    (t.usertype != 80 or d.type_name='timestamp')
  11233.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  11234.     and     c.usertype = t.usertype
  11235.     and    c.cdefault *= m.id
  11236.     and     m.colid = 1
  11237.     and    cfg.comment = 'default sortorder ID' 
  11238.     and    a_cha.type = 1001 /* type is charset */
  11239.     and    b_cha.type = 2001 /* type is sortorder */
  11240.     and    a_cha.id = b_cha.csid
  11241.     and     b_cha.id = cfg.value
  11242.     order by 2, 3, c.colid
  11243.     
  11244.     /* Fix ordinal positions */
  11245.     OPEN curColRowset
  11246.     FETCH NEXT FROM curColRowset
  11247.     INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11248.     
  11249.     /* Initialize tracking variables */
  11250.     SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL
  11251.  
  11252.     WHILE @@FETCH_STATUS = 0
  11253.     BEGIN
  11254.         /* Check to see if the catalog/schema/table */
  11255.         /* has changed since last row               */
  11256.         IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
  11257.             @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
  11258.         BEGIN
  11259.             /* Increment ordinal count */
  11260.             SELECT @currPos = @currPos + 1
  11261.         END
  11262.         ELSE
  11263.         BEGIN
  11264.             /* Set the catalog/schema/table to new values */
  11265.             SELECT @currCatalog = @TABLECATALOG,
  11266.                    @currSchema  = @TABLESCHEMA,
  11267.                    @currName    = @TABLENAME,
  11268.                    @currPos     = 1
  11269.         END
  11270.  
  11271.         /* Check for holes in the ordinal position order */
  11272.         if (@currPos < @ORDINALPOSITION)
  11273.         BEGIN        
  11274.             /* Update the ordinal position */
  11275.             UPDATE #columns_rowset
  11276.             SET    ORDINAL_POSITION = @currPos
  11277.             WHERE  TABLE_CATALOG = @TABLECATALOG
  11278.               AND  TABLE_SCHEMA  = @TABLESCHEMA
  11279.               AND  TABLE_NAME    = @TABLENAME
  11280.               AND  COLUMN_NAME   = @COLUMNNAME
  11281.         END
  11282.  
  11283.         /* Fetch next result */
  11284.         FETCH NEXT FROM curColRowset
  11285.         INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11286.     END
  11287.  
  11288.     /* Cleanup cursor */
  11289.     CLOSE curColRowset
  11290.     DEALLOCATE curColRowset
  11291.  
  11292.     set nocount off
  11293.  
  11294.     /* Output the result set */
  11295.     select *
  11296.     from #columns_rowset
  11297.     order by 2, 3, 7
  11298. go
  11299.  
  11300. dump tran master with no_log
  11301. go
  11302.  
  11303. if (charindex('7.00', @@version) = 0)
  11304. begin
  11305.     print ''
  11306.     print ''
  11307.     print 'Warning:'
  11308.     print 'you are installing the stored procedures '
  11309.     print 'on a pre 7.0 SQL Server.'
  11310.     print 'Ignore the following errors.'
  11311. end
  11312. else
  11313.     drop proc sp_columns_rowset
  11314. go
  11315.  
  11316. /*    Procedure for 7.0 server */
  11317. create procedure sp_columns_rowset
  11318.     (
  11319.        @table_name    sysname,
  11320.     @table_schema     sysname = NULL,
  11321.     @column_name    sysname = NULL
  11322.     )
  11323. as
  11324.     set nocount on
  11325.  
  11326.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  11327.     CREATE TABLE #columns_rowset
  11328.     (
  11329.         TABLE_CATALOG            sysname,
  11330.         TABLE_SCHEMA            sysname,
  11331.         TABLE_NAME            sysname,
  11332.         COLUMN_NAME            sysname,
  11333.         COLUMN_GUID            uniqueidentifier    null,
  11334.         COLUMN_PROPID            int            null,
  11335.         ORDINAL_POSITION        int            IDENTITY(1,1),
  11336.         COLUMN_HASDEFAULT        bit,
  11337.         COLUMN_DEFAULT            nvarchar(2000),
  11338.         COLUMN_FLAGS            int,
  11339.         IS_NULLABLE            bit,
  11340.         DATA_TYPE            smallint,
  11341.         TYPE_GUID            uniqueidentifier    null,
  11342.         CHARACTER_MAXIMUM_LENGTH    int            null,
  11343.         CHARACTER_OCTET_LENGTH        int            null,
  11344.         NUMERIC_PRECISION        smallint        null,
  11345.         NUMERIC_SCALE            smallint        null, 
  11346.         DATETIME_PRECISION        int            null,
  11347.         CHARACTER_SET_CATALOG        sysname            null,
  11348.         CHARACTER_SET_SCHEMA        sysname            null,
  11349.         CHARACTER_SET_NAME        sysname            null,
  11350.         COLLATION_CATALOG        sysname            null,
  11351.         COLLATION_SCHEMA        sysname            null,
  11352.         COLLATION_NAME            sysname            null,
  11353.         DOMAIN_CATALOG            sysname         null,
  11354.         DOMAIN_SCHEMA            sysname         null,
  11355.         DOMAIN_NAME            sysname         null,
  11356.         DESCRIPTION            nvarchar(1)
  11357.     )        
  11358.  
  11359.     /* Store results into the temp table */
  11360.     insert into #columns_rowset
  11361.         (
  11362.         TABLE_CATALOG,
  11363.         TABLE_SCHEMA,
  11364.         TABLE_NAME,
  11365.         COLUMN_NAME,
  11366.         COLUMN_GUID,
  11367.         COLUMN_PROPID,
  11368.         COLUMN_HASDEFAULT,
  11369.         COLUMN_DEFAULT,
  11370.         COLUMN_FLAGS,
  11371.         IS_NULLABLE,
  11372.         DATA_TYPE,
  11373.         TYPE_GUID,
  11374.         CHARACTER_MAXIMUM_LENGTH,
  11375.         CHARACTER_OCTET_LENGTH,
  11376.         NUMERIC_PRECISION,
  11377.         NUMERIC_SCALE,
  11378.         DATETIME_PRECISION,
  11379.         CHARACTER_SET_CATALOG,
  11380.         CHARACTER_SET_SCHEMA,
  11381.         CHARACTER_SET_NAME,
  11382.         COLLATION_CATALOG,
  11383.         COLLATION_SCHEMA,
  11384.         COLLATION_NAME,
  11385.         DOMAIN_CATALOG,
  11386.         DOMAIN_SCHEMA,
  11387.         DOMAIN_NAME,
  11388.         DESCRIPTION
  11389.         )
  11390.     select
  11391.         TABLE_CATALOG        = db_name(),
  11392.         TABLE_SCHEMA        = user_name(o.uid),
  11393.         TABLE_NAME        = o.name,
  11394.         COLUMN_NAME        = c.name,
  11395.         COLUMN_GUID        = convert(uniqueidentifier,null),
  11396.         COLUMN_PROPID        = convert(int,null),
  11397.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  11398.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  11399.                         substring(m.text,2,datalength(m.text)/2-2)),
  11400.         COLUMN_FLAGS        = convert(int,
  11401.                         case when d.is_long = 1 
  11402.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  11403.                         else 0
  11404.                         end
  11405.                     |    case when d.fixlen is not null
  11406.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  11407.                         else 0
  11408.                         end
  11409.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  11410.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  11411.                         else 0
  11412.                         end
  11413.                     |    case 
  11414.                         when d.type_name = 'timestamp'
  11415.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  11416.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  11417.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  11418.                         else 0 
  11419.                         end),
  11420.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  11421.         DATA_TYPE        = d.oledb_data_type,
  11422.         TYPE_GUID        = convert(uniqueidentifier,null),
  11423.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  11424.                         case 
  11425.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11426.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11427.                         then coalesce(d.column_size,c.length)
  11428.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11429.                         then coalesce(d.column_size,c.length/2)
  11430.                         else null 
  11431.                         end),
  11432.         CHARACTER_OCTET_LENGTH    = convert(int,
  11433.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11434.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11435.                         then coalesce(d.column_size,c.length)
  11436.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11437.                         then coalesce(d.column_size*2,c.length)
  11438.                         else null 
  11439.                         end),
  11440.         NUMERIC_PRECISION    = convert(smallint,
  11441.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  11442.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  11443.         NUMERIC_SCALE        = convert(smallint, 
  11444.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  11445.         DATETIME_PRECISION    = convert(int,
  11446.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  11447.                             when data_precision = 23 then 3 else 0 end),
  11448.         CHARACTER_SET_CATALOG    = convert(sysname,
  11449.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11450.                         then N'master' 
  11451.                         else null 
  11452.                         end),
  11453.         CHARACTER_SET_SCHEMA    = convert(sysname,
  11454.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11455.                         then N'dbo' 
  11456.                         else null 
  11457.                         end),
  11458.         CHARACTER_SET_NAME    = convert(sysname,
  11459.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11460.                         then a_cha.name 
  11461.                         else null 
  11462.                         end),
  11463.         COLLATION_CATALOG    = convert(sysname,
  11464.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11465.                         then N'master' 
  11466.                         else null 
  11467.                         end),
  11468.         COLLATION_SCHEMA    = convert(sysname,
  11469.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11470.                         then N'dbo' 
  11471.                         else null 
  11472.                         end),
  11473.         COLLATION_NAME        = convert(sysname,
  11474.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11475.                         then b_cha.name 
  11476.                         else null 
  11477.                         end),
  11478.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  11479.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  11480.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  11481.         DESCRIPTION        = convert(nvarchar(1),null)    
  11482.  
  11483.     from
  11484.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  11485.         sysobjects o,
  11486.         master.dbo.spt_provider_types d,
  11487.         systypes t,
  11488.         master.dbo.sysconfigures    cfg,
  11489.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  11490.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  11491.     where
  11492.         permissions(o.id, c.name) <> 0
  11493.     and    o.name = @table_name
  11494.     and     o.type in ('U','V','S')
  11495.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  11496.     and    (@column_name is null or @column_name = c.name)
  11497.     and     o.id = c.id
  11498.     and     t.xtype = d.ss_dtype
  11499.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  11500.     and     c.xusertype = t.xusertype
  11501.     and    cfg.comment = 'default sortorder id' 
  11502.     and    a_cha.type = 1001 /* type is charset */
  11503.     and    b_cha.type = 2001 /* type is sortorder */
  11504.     and    a_cha.id = b_cha.csid
  11505.     and     b_cha.id = cfg.value
  11506.     order by 2, 3, c.colorder
  11507.  
  11508.     set nocount off
  11509.  
  11510.     /* Output the result set */
  11511.     select *
  11512.     from #columns_rowset
  11513.     order by 2, 3, 7
  11514. go
  11515.  
  11516. dump tran master with no_log
  11517. go
  11518.  
  11519. /*    Procedure for 7.0 server */
  11520. create procedure sp_columns_rowset;2
  11521.     (
  11522.     @table_schema     sysname = NULL,
  11523.     @column_name    sysname = NULL
  11524.     )
  11525. as     
  11526.     set nocount on
  11527.  
  11528.     /* Declare bindings */
  11529.     DECLARE @TABLECATALOG    sysname,
  11530.         @TABLESCHEMA     sysname,
  11531.         @TABLENAME       sysname,
  11532.         @COLUMNNAME      sysname,
  11533.         @ORDINALPOSITION int
  11534.  
  11535.     /* Declare tracking variables */
  11536.     DECLARE @currCatalog sysname,
  11537.          @currSchema  sysname,
  11538.         @currName    sysname,
  11539.         @currPos     int
  11540.  
  11541.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  11542.     CREATE TABLE #columns_rowset
  11543.     (
  11544.         TABLE_CATALOG            sysname,
  11545.         TABLE_SCHEMA            sysname,
  11546.         TABLE_NAME            sysname,
  11547.         COLUMN_NAME            sysname,
  11548.         COLUMN_GUID            uniqueidentifier    null,
  11549.         COLUMN_PROPID            int            null,
  11550.         ORDINAL_POSITION        int            null,
  11551.         COLUMN_HASDEFAULT        bit,
  11552.         COLUMN_DEFAULT            nvarchar(2000),
  11553.         COLUMN_FLAGS            int,
  11554.         IS_NULLABLE            bit,
  11555.         DATA_TYPE            smallint,
  11556.         TYPE_GUID            uniqueidentifier    null,
  11557.         CHARACTER_MAXIMUM_LENGTH    int            null,
  11558.         CHARACTER_OCTET_LENGTH        int            null,
  11559.         NUMERIC_PRECISION        smallint        null,
  11560.         NUMERIC_SCALE            smallint        null, 
  11561.         DATETIME_PRECISION        int            null,
  11562.         CHARACTER_SET_CATALOG        sysname            null,
  11563.         CHARACTER_SET_SCHEMA        sysname            null,
  11564.         CHARACTER_SET_NAME        sysname            null,
  11565.         COLLATION_CATALOG        sysname            null,
  11566.         COLLATION_SCHEMA        sysname            null,
  11567.         COLLATION_NAME            sysname            null,
  11568.         DOMAIN_CATALOG            sysname         null,
  11569.         DOMAIN_SCHEMA            sysname         null,
  11570.         DOMAIN_NAME            sysname         null,
  11571.         DESCRIPTION            nvarchar(1)
  11572.     )        
  11573.  
  11574.     /* Declare cursor for fixup */
  11575.     DECLARE curColRowset CURSOR
  11576.     FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  11577.         FROM #columns_rowset
  11578.         ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION
  11579.  
  11580.     /* Store results into the temp table */
  11581.     insert into #columns_rowset
  11582.         (
  11583.         TABLE_CATALOG,
  11584.         TABLE_SCHEMA,
  11585.         TABLE_NAME,
  11586.         COLUMN_NAME,
  11587.         COLUMN_GUID,
  11588.         COLUMN_PROPID,
  11589.         ORDINAL_POSITION,
  11590.         COLUMN_HASDEFAULT,
  11591.         COLUMN_DEFAULT,
  11592.         COLUMN_FLAGS,
  11593.         IS_NULLABLE,
  11594.         DATA_TYPE,
  11595.         TYPE_GUID,
  11596.         CHARACTER_MAXIMUM_LENGTH,
  11597.         CHARACTER_OCTET_LENGTH,
  11598.         NUMERIC_PRECISION,
  11599.         NUMERIC_SCALE,
  11600.         DATETIME_PRECISION,
  11601.         CHARACTER_SET_CATALOG,
  11602.         CHARACTER_SET_SCHEMA,
  11603.         CHARACTER_SET_NAME,
  11604.         COLLATION_CATALOG,
  11605.         COLLATION_SCHEMA,
  11606.         COLLATION_NAME,
  11607.         DOMAIN_CATALOG,
  11608.         DOMAIN_SCHEMA,
  11609.         DOMAIN_NAME,
  11610.         DESCRIPTION
  11611.         )
  11612.  
  11613.     select
  11614.         TABLE_CATALOG        = db_name(),
  11615.         TABLE_SCHEMA        = user_name(o.uid),
  11616.         TABLE_NAME        = o.name,
  11617.         COLUMN_NAME        = c.name,
  11618.         COLUMN_GUID        = convert(uniqueidentifier,null),
  11619.         COLUMN_PROPID        = convert(int,null),
  11620.         ORDINAL_POSITION    = convert(int, c.colorder),
  11621.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  11622.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  11623.                         substring(m.text,2,datalength(m.text)/2-2)),
  11624.         COLUMN_FLAGS        = convert(int,
  11625.                         case when d.is_long = 1 
  11626.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  11627.                         else 0
  11628.                         end
  11629.                     |    case when d.fixlen is not null
  11630.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  11631.                         else 0
  11632.                         end
  11633.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  11634.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  11635.                         else 0
  11636.                         end
  11637.                     |    case 
  11638.                         when d.type_name = 'timestamp'
  11639.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  11640.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  11641.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  11642.                         else 0 
  11643.                         end),
  11644.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  11645.         DATA_TYPE        = d.oledb_data_type,
  11646.         TYPE_GUID        = convert(uniqueidentifier,null),
  11647.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  11648.                         case 
  11649.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11650.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11651.                         then coalesce(d.column_size,c.length)
  11652.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11653.                         then coalesce(d.column_size,c.length/2)
  11654.                         else null 
  11655.                         end),
  11656.         CHARACTER_OCTET_LENGTH    = convert(int,
  11657.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11658.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11659.                         then coalesce(d.column_size,c.length)
  11660.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11661.                         then coalesce(d.column_size*2,c.length)
  11662.                         else null 
  11663.                         end),
  11664.         NUMERIC_PRECISION    = convert(smallint,
  11665.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  11666.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  11667.         NUMERIC_SCALE        = convert(smallint, 
  11668.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  11669.         DATETIME_PRECISION    = convert(int,
  11670.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  11671.                             when data_precision = 23 then 3 else 0 end),
  11672.         CHARACTER_SET_CATALOG    = convert(sysname,
  11673.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11674.                         then N'master' 
  11675.                         else null 
  11676.                         end),
  11677.         CHARACTER_SET_SCHEMA    = convert(sysname,
  11678.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11679.                         then N'dbo' 
  11680.                         else null 
  11681.                         end),
  11682.         CHARACTER_SET_NAME    = convert(sysname,
  11683.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11684.                         then a_cha.name 
  11685.                         else null 
  11686.                         end),
  11687.         COLLATION_CATALOG    = convert(sysname,
  11688.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11689.                         then N'master' 
  11690.                         else null 
  11691.                         end),
  11692.         COLLATION_SCHEMA    = convert(sysname,
  11693.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11694.                         then N'dbo' 
  11695.                         else null 
  11696.                         end),
  11697.         COLLATION_NAME        = convert(sysname,
  11698.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11699.                         then b_cha.name 
  11700.                         else null 
  11701.                         end),
  11702.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  11703.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  11704.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  11705.         DESCRIPTION        = convert(nvarchar(1),null)    
  11706.     
  11707.     from
  11708.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  11709.         sysobjects o,
  11710.         master.dbo.spt_provider_types d,
  11711.         systypes t,
  11712.         master.dbo.sysconfigures    cfg,
  11713.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  11714.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  11715.     where
  11716.         permissions(o.id, c.name) <> 0
  11717.     and     o.type in ('U','V','S')
  11718.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  11719.     and    (@column_name is null or @column_name = c.name)
  11720.     and     o.id = c.id
  11721.     and     t.xtype = d.ss_dtype
  11722.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  11723.     and     c.xusertype = t.xusertype
  11724.     and    cfg.comment = 'default sortorder id' 
  11725.     and     a_cha.type = 1001 /* type is charset */
  11726.     and     b_cha.type = 2001 /* type is sortorder */
  11727.     and    a_cha.id = b_cha.csid
  11728.     and     b_cha.id = cfg.value
  11729.     order by 2, 3, c.colorder
  11730.  
  11731.     /* Fix ordinal positions */
  11732.     OPEN curColRowset
  11733.     FETCH NEXT FROM curColRowset
  11734.     INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11735.     
  11736.     /* Initialize tracking variables */
  11737.     SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL
  11738.  
  11739.     WHILE @@FETCH_STATUS = 0
  11740.     BEGIN
  11741.         /* Check to see if the catalog/schema/table */
  11742.         /* has changed since last row               */
  11743.         IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
  11744.             @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
  11745.         BEGIN
  11746.             /* Increment ordinal count */
  11747.             SELECT @currPos = @currPos + 1
  11748.         END
  11749.         ELSE
  11750.         BEGIN
  11751.             /* Set the catalog/schema/table to new values */
  11752.             SELECT @currCatalog = @TABLECATALOG,
  11753.                    @currSchema  = @TABLESCHEMA,
  11754.                    @currName    = @TABLENAME,
  11755.                    @currPos     = 1
  11756.         END
  11757.  
  11758.         /* Check for holes in the ordinal position order */
  11759.         if (@currPos < @ORDINALPOSITION)
  11760.         BEGIN        
  11761.             /* Update the ordinal position */
  11762.             UPDATE #columns_rowset
  11763.             SET    ORDINAL_POSITION = @currPos
  11764.             WHERE  TABLE_CATALOG = @TABLECATALOG
  11765.               AND  TABLE_SCHEMA  = @TABLESCHEMA
  11766.               AND  TABLE_NAME    = @TABLENAME
  11767.               AND  COLUMN_NAME   = @COLUMNNAME
  11768.         END
  11769.  
  11770.         /* Fetch next result */
  11771.         FETCH NEXT FROM curColRowset
  11772.         INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11773.     END
  11774.  
  11775.     /* Cleanup cursor */
  11776.     CLOSE curColRowset
  11777.     DEALLOCATE curColRowset
  11778.  
  11779.     set nocount off
  11780.  
  11781.     /* Output the result set */
  11782.     select     *
  11783.     from #columns_rowset
  11784.     order by 2, 3, 7
  11785. go
  11786.  
  11787. dump tran master with no_log
  11788. go
  11789.  
  11790. /*    Procedure for 7.0 server */
  11791. create procedure sp_columns_rowset;5
  11792.     (
  11793.     @table_server        sysname,
  11794.     @table_catalog        sysname = null,
  11795.     @table_name        sysname = null,
  11796.     @table_schema        sysname = null,
  11797.     @column_name        sysname = null
  11798.     )
  11799. as
  11800.     select
  11801.         TABLE_CATALOG,
  11802.         TABLE_SCHEMA,
  11803.         TABLE_NAME,
  11804.         COLUMN_NAME,
  11805.         COLUMN_GUID,
  11806.         COLUMN_PROPID,
  11807.         ORDINAL_POSITION,
  11808.         COLUMN_HASDEFAULT,
  11809.         COLUMN_DEFAULT,
  11810.         COLUMN_FLAGS,
  11811.         IS_NULLABLE,
  11812.         DATA_TYPE,
  11813.         TYPE_GUID,
  11814.         CHARACTER_MAXIMUM_LENGTH,
  11815.         CHARACTER_OCTET_LENGTH,
  11816.         NUMERIC_PRECISION,
  11817.         NUMERIC_SCALE,
  11818.         DATETIME_PRECISION,
  11819.         CHARACTER_SET_CATALOG,
  11820.         CHARACTER_SET_SCHEMA,
  11821.         CHARACTER_SET_NAME,
  11822.         COLLATION_CATALOG,
  11823.         COLLATION_SCHEMA,
  11824.         COLLATION_NAME,
  11825.         DOMAIN_CATALOG,
  11826.         DOMAIN_SCHEMA,
  11827.         DOMAIN_NAME,
  11828.         DESCRIPTION
  11829.     from master.dbo.SYSREMOTE_COLUMNS <
  11830.                 @table_server,
  11831.                 @table_catalog,
  11832.                 @table_schema,
  11833.                 @table_name,
  11834.                 @column_name > 
  11835.     order by 1, 2, 3, 7
  11836. go
  11837.  
  11838.  
  11839. dump tran master with no_log
  11840. go
  11841.  
  11842. grant execute on sp_columns_rowset to public
  11843. go
  11844.  
  11845. dump tran master with no_log
  11846. go
  11847. if (charindex('6.00', @@version) > 0)
  11848.     begin
  11849.     if (exists (select * from sysobjects
  11850.             where name = 'sp_columns_rowset' and type = 'P '))
  11851.         begin
  11852.         drop procedure sp_columns_rowset
  11853.         dump tran master with no_log
  11854.         end
  11855.     end
  11856. go
  11857.  
  11858. print ''
  11859. print 'creating sp_foreign_keys_rowset'
  11860. go
  11861.  
  11862. /*    Procedure for 6.0 and 6.5 server */
  11863. CREATE PROCEDURE sp_foreign_keys_rowset
  11864.     (
  11865.      @pk_table_name        varchar(255) = null,
  11866.     @pk_table_schema    varchar(255) = null,
  11867.     @pk_table_catalog    varchar(255) = null,
  11868.     @fk_table_name        varchar(255) = null,
  11869.     @fk_table_schema    varchar(255) = null,
  11870.     @fk_table_catalog    varchar(255) = null
  11871.     )
  11872. as
  11873. IF @pk_table_name is not null
  11874.     BEGIN
  11875.     select
  11876.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11877.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11878.         PK_TABLE_NAME         = o1.name,
  11879.         PK_COLUMN_NAME         = c1.name,
  11880.         PK_COLUMN_GUID        = convert(binary(16),null),
  11881.         PK_COLUMN_PROPID    = convert(int,null),
  11882.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11883.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11884.         FK_TABLE_NAME         = o2.name,
  11885.         FK_COLUMN_NAME         = c2.name,
  11886.         FK_COLUMN_GUID        = convert(binary(16),null),
  11887.         FK_COLUMN_PROPID    = convert(int,null),
  11888.         ORDINAL                = convert(int,1),
  11889.         UPDATE_RULE            = 'NO ACTION',
  11890.         DELETE_RULE         = 'NO ACTION',
  11891.         PK_NAME                = object_name(s.constid),
  11892.         FK_NAME                = object_name(r.constid),
  11893.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11894.     from
  11895.         sysobjects o1, sysobjects o2,
  11896.         syscolumns c1, syscolumns c2,
  11897.         sysreferences r, sysconstraints s
  11898.     where    
  11899.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11900.     and    o1.name = @pk_table_name
  11901.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11902.     and    o1.id = r.rkeyid
  11903.     and s.id = o1.id and (s.status & 0xf) = 1
  11904.     and    o1.id = c1.id
  11905.     and    c1.colid = r.rkey1
  11906.     and    r.fkeyid = o2.id
  11907.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11908.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11909.     and    o2.id = c2.id    
  11910.     and    c2.colid = r.fkey1
  11911.     union all
  11912.     select
  11913.         db_name(r.rkeydbid),
  11914.         user_name(o1.uid),
  11915.         o1.name,
  11916.         c1.name,
  11917.         convert(binary(16),null),
  11918.         convert(int,null),
  11919.         db_name(r.fkeydbid),
  11920.         user_name(o2.uid),
  11921.         o2.name,
  11922.         c2.name,
  11923.         convert(binary(16),null),
  11924.         convert(int,null),
  11925.         convert(int,2),
  11926.         'NO ACTION',
  11927.         'NO ACTION',
  11928.         object_name(s.constid),
  11929.         object_name(r.constid),
  11930.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11931.     from
  11932.         sysobjects o1, sysobjects o2,
  11933.         syscolumns c1, syscolumns c2,
  11934.         sysreferences r, sysconstraints s
  11935.     where    
  11936.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11937.     and    o1.name = @pk_table_name
  11938.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11939.     and    o1.id = r.rkeyid
  11940.     and s.id = o1.id and (s.status & 0xf) = 1
  11941.      and    o1.id = c1.id
  11942.     and    c1.colid = r.rkey2
  11943.     and    r.keycnt >= 2
  11944.     and r.fkeyid = o2.id
  11945.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11946.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11947.     and    o2.id = c2.id    
  11948.     and    c2.colid = r.fkey2
  11949.     union all
  11950.     select
  11951.         db_name(r.rkeydbid),
  11952.         user_name(o1.uid),
  11953.         o1.name,
  11954.         c1.name,
  11955.         convert(binary(16),null),
  11956.         convert(int,null),
  11957.         db_name(r.fkeydbid),
  11958.         user_name(o2.uid),
  11959.         o2.name,
  11960.         c2.name,
  11961.         convert(binary(16),null),
  11962.         convert(int,null),
  11963.         convert(int,3),
  11964.         'NO ACTION',
  11965.         'NO ACTION',
  11966.         object_name(s.constid),
  11967.         object_name(r.constid),
  11968.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11969.     from
  11970.         sysobjects o1, sysobjects o2,
  11971.         syscolumns c1, syscolumns c2,
  11972.         sysreferences r, sysconstraints s
  11973.     where    
  11974.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11975.     and    o1.name = @pk_table_name
  11976.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11977.     and    o1.id = r.rkeyid
  11978.     and s.id = o1.id and (s.status & 0xf) = 1
  11979.     and    o1.id = c1.id
  11980.     and    c1.colid = r.rkey3
  11981.     and    r.keycnt >= 3
  11982.     and r.fkeyid = o2.id
  11983.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11984.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11985.     and    o2.id = c2.id    
  11986.     and    c2.colid = r.fkey3
  11987.     union all
  11988.     select
  11989.         db_name(r.rkeydbid),
  11990.         user_name(o1.uid),
  11991.         o1.name,
  11992.         c1.name,
  11993.         convert(binary(16),null),
  11994.         convert(int,null),
  11995.         db_name(r.fkeydbid),
  11996.         user_name(o2.uid),
  11997.         o2.name,
  11998.         c2.name,
  11999.         convert(binary(16),null),
  12000.         convert(int,null),
  12001.         convert(int,4),
  12002.         'NO ACTION',
  12003.         'NO ACTION',
  12004.         object_name(s.constid),
  12005.         object_name(r.constid),
  12006.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12007.     from
  12008.         sysobjects o1, sysobjects o2,
  12009.         syscolumns c1, syscolumns c2,
  12010.         sysreferences r, sysconstraints s
  12011.     where    
  12012.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12013.     and    o1.name = @pk_table_name
  12014.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12015.     and    o1.id = r.rkeyid
  12016.     and s.id = o1.id and (s.status & 0xf) = 1
  12017.     and    r.keycnt >= 4
  12018.     and    o1.id = c1.id
  12019.     and    c1.colid = r.rkey4
  12020.     and    r.fkeyid = o2.id
  12021.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12022.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12023.     and    o2.id = c2.id    
  12024.     and    c2.colid = r.fkey4
  12025.     union all
  12026.     select
  12027.         db_name(r.rkeydbid),
  12028.         user_name(o1.uid),
  12029.         o1.name,
  12030.         c1.name,
  12031.         convert(binary(16),null),
  12032.         convert(int,null),
  12033.         db_name(r.fkeydbid),
  12034.         user_name(o2.uid),
  12035.         o2.name,
  12036.         c2.name,
  12037.         convert(binary(16),null),
  12038.         convert(int,null),
  12039.         convert(int,5),
  12040.         'NO ACTION',
  12041.         'NO ACTION',
  12042.         object_name(s.constid),
  12043.         object_name(r.constid),
  12044.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12045.     from
  12046.         sysobjects o1, sysobjects o2,
  12047.         syscolumns c1, syscolumns c2,
  12048.         sysreferences r, sysconstraints s
  12049.     where    
  12050.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12051.     and    o1.name = @pk_table_name
  12052.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12053.     and    o1.id = r.rkeyid
  12054.     and s.id = o1.id and (s.status & 0xf) = 1
  12055.     and    r.keycnt >= 5
  12056.     and    o1.id = c1.id
  12057.     and    c1.colid = r.rkey5
  12058.     and r.fkeyid = o2.id
  12059.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12060.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12061.     and    o2.id = c2.id    
  12062.     and    c2.colid = r.fkey5
  12063.     union all
  12064.     select
  12065.         db_name(r.rkeydbid),
  12066.         user_name(o1.uid),
  12067.         o1.name,
  12068.         c1.name,
  12069.         convert(binary(16),null),
  12070.         convert(int,null),
  12071.         db_name(r.fkeydbid),
  12072.         user_name(o2.uid),
  12073.         o2.name,
  12074.         c2.name,
  12075.         convert(binary(16),null),
  12076.         convert(int,null),
  12077.         convert(int,6),
  12078.         'NO ACTION',
  12079.         'NO ACTION',
  12080.         object_name(s.constid),
  12081.         object_name(r.constid),
  12082.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12083.     from
  12084.         sysobjects o1, sysobjects o2,
  12085.         syscolumns c1, syscolumns c2,
  12086.         sysreferences r, sysconstraints s
  12087.     where    
  12088.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12089.     and    o1.name = @pk_table_name
  12090.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12091.     and    o1.id = r.rkeyid
  12092.     and s.id = o1.id and (s.status & 0xf) = 1
  12093.     and    r.keycnt >= 6
  12094.     and    o1.id = c1.id
  12095.     and    c1.colid = r.rkey6
  12096.     and r.fkeyid = o2.id
  12097.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12098.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12099.     and    o2.id = c2.id    
  12100.     and    c2.colid = r.fkey6
  12101.     union all
  12102.     select
  12103.         db_name(r.rkeydbid),
  12104.         user_name(o1.uid),
  12105.         o1.name,
  12106.         c1.name,
  12107.         convert(binary(16),null),
  12108.         convert(int,null),
  12109.         db_name(r.fkeydbid),
  12110.         user_name(o2.uid),
  12111.         o2.name,
  12112.         c2.name,
  12113.         convert(binary(16),null),
  12114.         convert(int,null),
  12115.         convert(int,7),
  12116.         'NO ACTION',
  12117.         'NO ACTION',
  12118.         object_name(s.constid),
  12119.         object_name(r.constid),
  12120.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12121.     from
  12122.         sysobjects o1, sysobjects o2,
  12123.         syscolumns c1, syscolumns c2,
  12124.         sysreferences r, sysconstraints s
  12125.     where    
  12126.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12127.     and    o1.name = @pk_table_name
  12128.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12129.     and    o1.id = r.rkeyid
  12130.     and s.id = o1.id and (s.status & 0xf) = 1
  12131.     and    r.keycnt >= 7
  12132.     and    o1.id = c1.id
  12133.     and    c1.colid = r.rkey7
  12134.     and r.fkeyid = o2.id
  12135.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12136.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12137.     and    o2.id = c2.id    
  12138.     and    c2.colid = r.fkey7
  12139.     union all
  12140.     select
  12141.         db_name(r.rkeydbid),
  12142.         user_name(o1.uid),
  12143.         o1.name,
  12144.         c1.name,
  12145.         convert(binary(16),null),
  12146.         convert(int,null),
  12147.         db_name(r.fkeydbid),
  12148.         user_name(o2.uid),
  12149.         o2.name,
  12150.         c2.name,
  12151.         convert(binary(16),null),
  12152.         convert(int,null),
  12153.         convert(int,8),
  12154.         'NO ACTION',
  12155.         'NO ACTION',
  12156.         object_name(s.constid),
  12157.         object_name(r.constid),
  12158.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12159.     from
  12160.         sysobjects o1, sysobjects o2,
  12161.         syscolumns c1, syscolumns c2,
  12162.         sysreferences r, sysconstraints s
  12163.     where    
  12164.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12165.     and    o1.name = @pk_table_name
  12166.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12167.     and    o1.id = r.rkeyid
  12168.     and s.id = o1.id and (s.status & 0xf) = 1
  12169.     and    r.keycnt >= 8
  12170.     and    o1.id = c1.id
  12171.     and    c1.colid = r.rkey8
  12172.     and r.fkeyid = o2.id
  12173.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12174.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12175.     and    o2.id = c2.id    
  12176.     and    c2.colid = r.fkey8
  12177.     union all
  12178.     select
  12179.         db_name(r.rkeydbid),
  12180.         user_name(o1.uid),
  12181.         o1.name,
  12182.         c1.name,
  12183.         convert(binary(16),null),
  12184.         convert(int,null),
  12185.         db_name(r.fkeydbid),
  12186.         user_name(o2.uid),
  12187.         o2.name,
  12188.         c2.name,
  12189.         convert(binary(16),null),
  12190.         convert(int,null),
  12191.         convert(int,9),
  12192.         'NO ACTION',
  12193.         'NO ACTION',
  12194.         object_name(s.constid),
  12195.         object_name(r.constid),
  12196.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12197.     from
  12198.         sysobjects o1, sysobjects o2,
  12199.         syscolumns c1, syscolumns c2,
  12200.         sysreferences r, sysconstraints s
  12201.     where    
  12202.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12203.     and    o1.name = @pk_table_name
  12204.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12205.     and    o1.id = r.rkeyid
  12206.     and s.id = o1.id and (s.status & 0xf) = 1
  12207.     and    r.keycnt >= 9
  12208.     and    o1.id = c1.id
  12209.     and    c1.colid = r.rkey9
  12210.     and r.fkeyid = o2.id
  12211.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12212.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12213.     and    o2.id = c2.id    
  12214.     and    c2.colid = r.fkey9
  12215.     union all
  12216.     select
  12217.         db_name(r.rkeydbid),
  12218.         user_name(o1.uid),
  12219.         o1.name,
  12220.         c1.name,
  12221.         convert(binary(16),null),
  12222.         convert(int,null),
  12223.         db_name(r.fkeydbid),
  12224.         user_name(o2.uid),
  12225.         o2.name,
  12226.         c2.name,
  12227.         convert(binary(16),null),
  12228.         convert(int,null),
  12229.         convert(int,10),
  12230.         'NO ACTION',
  12231.         'NO ACTION',
  12232.         object_name(s.constid),
  12233.         object_name(r.constid),
  12234.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/    
  12235.     from
  12236.         sysobjects o1, sysobjects o2,
  12237.         syscolumns c1, syscolumns c2,
  12238.         sysreferences r, sysconstraints s
  12239.     where    
  12240.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12241.     and    o1.name = @pk_table_name
  12242.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12243.     and    o1.id = r.rkeyid
  12244.     and s.id = o1.id and (s.status & 0xf) = 1
  12245.     and    r.keycnt >= 10
  12246.     and    o1.id = c1.id
  12247.     and    c1.colid = r.rkey10
  12248.     and r.fkeyid = o2.id
  12249.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12250.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12251.     and    o2.id = c2.id    
  12252.     and    c2.colid = r.fkey10
  12253.     union all
  12254.     select
  12255.         db_name(r.rkeydbid),
  12256.         user_name(o1.uid),
  12257.         o1.name,
  12258.         c1.name,
  12259.         convert(binary(16),null),
  12260.         convert(int,null),
  12261.         db_name(r.fkeydbid),
  12262.         user_name(o2.uid),
  12263.         o2.name,
  12264.         c2.name,
  12265.         convert(binary(16),null),
  12266.         convert(int,null),
  12267.         convert(int,11),
  12268.         'NO ACTION',
  12269.         'NO ACTION',
  12270.         object_name(s.constid),
  12271.         object_name(r.constid),
  12272.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12273.     from
  12274.         sysobjects o1, sysobjects o2,
  12275.         syscolumns c1, syscolumns c2,
  12276.         sysreferences r, sysconstraints s
  12277.     where    
  12278.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12279.     and    o1.name = @pk_table_name
  12280.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12281.     and    o1.id = r.rkeyid
  12282.     and s.id = o1.id and (s.status & 0xf) = 1
  12283.     and    r.keycnt >= 11
  12284.     and    o1.id = c1.id
  12285.     and    c1.colid = r.rkey11
  12286.     and r.fkeyid = o2.id
  12287.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12288.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12289.     and    o2.id = c2.id    
  12290.     and    c2.colid = r.fkey11
  12291.     union all
  12292.     select
  12293.         db_name(r.rkeydbid),
  12294.         user_name(o1.uid),
  12295.         o1.name,
  12296.         c1.name,
  12297.         convert(binary(16),null),
  12298.         convert(int,null),
  12299.         db_name(r.fkeydbid),
  12300.         user_name(o2.uid),
  12301.         o2.name,
  12302.         c2.name,
  12303.         convert(binary(16),null),
  12304.         convert(int,null),
  12305.         convert(int,12),
  12306.         'NO ACTION',
  12307.         'NO ACTION',
  12308.         object_name(s.constid),
  12309.         object_name(r.constid),
  12310.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12311.     from
  12312.         sysobjects o1, sysobjects o2,
  12313.         syscolumns c1, syscolumns c2,
  12314.         sysreferences r, sysconstraints s
  12315.     where    
  12316.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12317.     and    o1.name = @pk_table_name
  12318.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12319.     and    o1.id = r.rkeyid
  12320.     and s.id = o1.id and (s.status & 0xf) = 1
  12321.     and    r.keycnt >= 12
  12322.     and    o1.id = c1.id
  12323.     and    c1.colid = r.rkey12
  12324.     and r.fkeyid = o2.id
  12325.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12326.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12327.     and    o2.id = c2.id    
  12328.     and    c2.colid = r.fkey12
  12329.     union all
  12330.     select
  12331.         db_name(r.rkeydbid),
  12332.         user_name(o1.uid),
  12333.         o1.name,
  12334.         c1.name,
  12335.         convert(binary(16),null),
  12336.         convert(int,null),
  12337.         db_name(r.fkeydbid),
  12338.         user_name(o2.uid),
  12339.         o2.name,
  12340.         c2.name,
  12341.         convert(binary(16),null),
  12342.         convert(int,null),
  12343.         convert(int,13),
  12344.         'NO ACTION',
  12345.         'NO ACTION',
  12346.         object_name(s.constid),
  12347.         object_name(r.constid),
  12348.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12349.     from
  12350.         sysobjects o1, sysobjects o2,
  12351.         syscolumns c1, syscolumns c2,
  12352.         sysreferences r, sysconstraints s
  12353.     where    
  12354.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12355.     and    o1.name = @pk_table_name
  12356.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12357.     and    o1.id = r.rkeyid
  12358.     and s.id = o1.id and (s.status & 0xf) = 1
  12359.     and    r.keycnt >= 13
  12360.     and    o1.id = c1.id
  12361.     and    c1.colid = r.rkey13
  12362.     and r.fkeyid = o2.id
  12363.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12364.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12365.     and    o2.id = c2.id    
  12366.     and    c2.colid = r.fkey13
  12367.     union all
  12368.     select
  12369.         db_name(r.rkeydbid),
  12370.         user_name(o1.uid),
  12371.         o1.name,
  12372.         c1.name,
  12373.         convert(binary(16),null),
  12374.         convert(int,null),
  12375.         db_name(r.fkeydbid),
  12376.         user_name(o2.uid),
  12377.         o2.name,
  12378.         c2.name,
  12379.         convert(binary(16),null),
  12380.         convert(int,null),
  12381.         convert(int,14),
  12382.         'NO ACTION',
  12383.         'NO ACTION',
  12384.         object_name(s.constid),
  12385.         object_name(r.constid),
  12386.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12387.     from
  12388.         sysobjects o1, sysobjects o2,
  12389.         syscolumns c1, syscolumns c2,
  12390.         sysreferences r, sysconstraints s
  12391.     where    
  12392.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12393.     and    o1.name = @pk_table_name
  12394.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12395.     and    o1.id = r.rkeyid
  12396.     and s.id = o1.id and (s.status & 0xf) = 1
  12397.     and    r.keycnt >= 14
  12398.     and    o1.id = c1.id
  12399.     and    c1.colid = r.rkey14
  12400.     and r.fkeyid = o2.id
  12401.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12402.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12403.     and    o2.id = c2.id    
  12404.     and    c2.colid = r.fkey14
  12405.     union all
  12406.     select
  12407.         db_name(r.rkeydbid),
  12408.         user_name(o1.uid),
  12409.         o1.name,
  12410.         c1.name,
  12411.         convert(binary(16),null),
  12412.         convert(int,null),
  12413.         db_name(r.fkeydbid),
  12414.         user_name(o2.uid),
  12415.         o2.name,
  12416.         c2.name,
  12417.         convert(binary(16),null),
  12418.         convert(int,null),
  12419.         convert(int,15),
  12420.         'NO ACTION',
  12421.         'NO ACTION',
  12422.         object_name(s.constid),
  12423.         object_name(r.constid),
  12424.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12425.     from
  12426.         sysobjects o1, sysobjects o2,
  12427.         syscolumns c1, syscolumns c2,
  12428.         sysreferences r, sysconstraints s
  12429.     where    
  12430.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12431.     and    o1.name = @pk_table_name
  12432.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12433.     and    o1.id = r.rkeyid
  12434.     and s.id = o1.id and (s.status & 0xf) = 1
  12435.     and    r.keycnt >= 15
  12436.     and    o1.id = c1.id
  12437.     and    c1.colid = r.rkey15
  12438.     and r.fkeyid = o2.id
  12439.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12440.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12441.     and    o2.id = c2.id    
  12442.     and    c2.colid = r.fkey15
  12443.     union all
  12444.     select
  12445.         db_name(r.rkeydbid),
  12446.         user_name(o1.uid),
  12447.         o1.name,
  12448.         c1.name,
  12449.         convert(binary(16),null),
  12450.         convert(int,null),
  12451.         db_name(r.fkeydbid),
  12452.         user_name(o2.uid),
  12453.         o2.name,
  12454.         c2.name,
  12455.         convert(binary(16),null),
  12456.         convert(int,null),
  12457.         convert(int,16),
  12458.         'NO ACTION',
  12459.         'NO ACTION',
  12460.         object_name(s.constid),
  12461.         object_name(r.constid),
  12462.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12463.     from
  12464.         sysobjects o1, sysobjects o2,
  12465.         syscolumns c1, syscolumns c2,
  12466.         sysreferences r, sysconstraints s
  12467.     where    
  12468.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12469.     and    o1.name = @pk_table_name
  12470.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12471.     and    o1.id = r.rkeyid
  12472.     and s.id = o1.id and (s.status & 0xf) = 1
  12473.     and    r.keycnt >= 16
  12474.     and    o1.id = c1.id
  12475.     and    c1.colid = r.rkey16
  12476.     and r.fkeyid = o2.id
  12477.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12478.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12479.     and    o2.id = c2.id    
  12480.     and    c2.colid = r.fkey16
  12481.     order by 8,9,2,3,13
  12482.     END
  12483. ELSE IF @fk_table_name is not null
  12484.     BEGIN
  12485.     select
  12486.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12487.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12488.         PK_TABLE_NAME         = o1.name,
  12489.         PK_COLUMN_NAME         = c1.name,
  12490.         PK_COLUMN_GUID        = convert(binary(16),null),
  12491.         PK_COLUMN_PROPID    = convert(int,null),
  12492.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12493.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12494.         FK_TABLE_NAME         = o2.name,
  12495.         FK_COLUMN_NAME         = c2.name,
  12496.         FK_COLUMN_GUID        = convert(binary(16),null),
  12497.         FK_COLUMN_PROPID    = convert(int,null),
  12498.         ORDINAL                = convert(int,1),
  12499.         UPDATE_RULE            = 'NO ACTION',
  12500.         DELETE_RULE         = 'NO ACTION',
  12501.         PK_NAME                = object_name(s.constid),
  12502.         FK_NAME                = object_name(r.constid),
  12503.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12504.     from
  12505.         sysobjects o1, sysobjects o2,
  12506.         syscolumns c1, syscolumns c2,
  12507.         sysreferences r, sysconstraints s
  12508.     where    
  12509.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12510.     and    o2.name = @fk_table_name
  12511.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12512.     and    o2.id = r.fkeyid
  12513.     and    o2.id = c2.id
  12514.     and    c2.colid = r.fkey1
  12515.     and r.rkeyid = o1.id
  12516.     and s.id = o1.id and (s.status & 0xf) = 1
  12517.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12518.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12519.     and    o1.id = c1.id    
  12520.     and    c1.colid = r.rkey1
  12521.     union    all
  12522.     select
  12523.         db_name(r.rkeydbid),
  12524.         user_name(o1.uid),
  12525.         o1.name,
  12526.         c1.name,
  12527.         convert(binary(16),null),
  12528.         convert(int,null),
  12529.         db_name(r.fkeydbid),
  12530.         user_name(o2.uid),
  12531.         o2.name,
  12532.         c2.name,
  12533.         convert(binary(16),null),
  12534.         convert(int,null),
  12535.         convert(int,2),
  12536.         'NO ACTION',
  12537.         'NO ACTION',
  12538.         object_name(s.constid),
  12539.         object_name(r.constid),
  12540.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12541.     from
  12542.         sysobjects o1, sysobjects o2,
  12543.         syscolumns c1, syscolumns c2,
  12544.         sysreferences r, sysconstraints s
  12545.     where    
  12546.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12547.     and    o2.name = @fk_table_name
  12548.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12549.     and    o2.id = r.fkeyid
  12550.     and    r.keycnt >= 2
  12551.     and    o2.id = c2.id
  12552.     and    c2.colid = r.fkey2
  12553.     and r.rkeyid = o1.id
  12554.     and s.id = o1.id and (s.status & 0xf) = 1
  12555.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12556.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12557.     and    o1.id = c1.id    
  12558.     and    c1.colid = r.rkey2
  12559.     union    all
  12560.     select
  12561.         db_name(r.rkeydbid),
  12562.         user_name(o1.uid),
  12563.         o1.name,
  12564.         c1.name,
  12565.         convert(binary(16),null),
  12566.         convert(int,null),
  12567.         db_name(r.fkeydbid),
  12568.         user_name(o2.uid),
  12569.         o2.name,
  12570.         c2.name,
  12571.         convert(binary(16),null),
  12572.         convert(int,null),
  12573.         convert(int,3),
  12574.         'NO ACTION',
  12575.         'NO ACTION',
  12576.         object_name(s.constid),
  12577.         object_name(r.constid),
  12578.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12579.     from
  12580.         sysobjects o1, sysobjects o2,
  12581.         syscolumns c1, syscolumns c2,
  12582.         sysreferences r, sysconstraints s
  12583.     where    
  12584.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12585.     and    o2.name = @fk_table_name
  12586.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12587.     and    o2.id = r.fkeyid
  12588.     and    r.keycnt >= 3
  12589.     and    o2.id = c2.id
  12590.     and    c2.colid = r.fkey3
  12591.     and r.rkeyid = o1.id
  12592.     and s.id = o1.id and (s.status & 0xf) = 1
  12593.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12594.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12595.     and    o1.id = c1.id    
  12596.     and    c1.colid = r.rkey3
  12597.     union    all
  12598.     select
  12599.         db_name(r.rkeydbid),
  12600.         user_name(o1.uid),
  12601.         o1.name,
  12602.         c1.name,
  12603.         convert(binary(16),null),
  12604.         convert(int,null),
  12605.         db_name(r.fkeydbid),
  12606.         user_name(o2.uid),
  12607.         o2.name,
  12608.         c2.name,
  12609.         convert(binary(16),null),
  12610.         convert(int,null),
  12611.         convert(int,4),
  12612.         'NO ACTION',
  12613.         'NO ACTION',
  12614.         object_name(s.constid),
  12615.         object_name(r.constid),
  12616.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12617.     from
  12618.         sysobjects o1, sysobjects o2,
  12619.         syscolumns c1, syscolumns c2,
  12620.         sysreferences r, sysconstraints s
  12621.     where    
  12622.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12623.     and    o2.name = @fk_table_name
  12624.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12625.     and    o2.id = r.fkeyid
  12626.     and    r.keycnt >= 4
  12627.     and    o2.id = c2.id
  12628.     and    c2.colid = r.fkey4
  12629.     and r.rkeyid = o1.id
  12630.     and s.id = o1.id and (s.status & 0xf) = 1
  12631.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12632.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12633.     and    o1.id = c1.id    
  12634.     and    c1.colid = r.rkey4
  12635.     union    all
  12636.     select
  12637.         db_name(r.rkeydbid),
  12638.         user_name(o1.uid),
  12639.         o1.name,
  12640.         c1.name,
  12641.         convert(binary(16),null),
  12642.         convert(int,null),
  12643.         db_name(r.fkeydbid),
  12644.         user_name(o2.uid),
  12645.         o2.name,
  12646.         c2.name,
  12647.         convert(binary(16),null),
  12648.         convert(int,null),
  12649.         convert(int,5),
  12650.         'NO ACTION',
  12651.         'NO ACTION',
  12652.         object_name(s.constid),
  12653.         object_name(r.constid),
  12654.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12655.     from
  12656.         sysobjects o1, sysobjects o2,
  12657.         syscolumns c1, syscolumns c2,
  12658.         sysreferences r, sysconstraints s
  12659.     where    
  12660.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12661.     and    o2.name = @fk_table_name
  12662.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12663.     and    o2.id = r.fkeyid
  12664.     and    r.keycnt >= 5
  12665.     and    o2.id = c2.id
  12666.     and    c2.colid = r.fkey5
  12667.     and r.rkeyid = o1.id
  12668.     and s.id = o1.id and (s.status & 0xf) = 1
  12669.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12670.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12671.     and    o1.id = c1.id    
  12672.     and    c1.colid = r.rkey5
  12673.     union    all
  12674.     select
  12675.         db_name(r.rkeydbid),
  12676.         user_name(o1.uid),
  12677.         o1.name,
  12678.         c1.name,
  12679.         convert(binary(16),null),
  12680.         convert(int,null),
  12681.         db_name(r.fkeydbid),
  12682.         user_name(o2.uid),
  12683.         o2.name,
  12684.         c2.name,
  12685.         convert(binary(16),null),
  12686.         convert(int,null),
  12687.         convert(int,6),
  12688.         'NO ACTION',
  12689.         'NO ACTION',
  12690.         object_name(s.constid),
  12691.         object_name(r.constid),
  12692.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12693.     from
  12694.         sysobjects o1, sysobjects o2,
  12695.         syscolumns c1, syscolumns c2,
  12696.         sysreferences r, sysconstraints s
  12697.     where    
  12698.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12699.     and    o2.name = @fk_table_name
  12700.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12701.     and    o2.id = r.fkeyid
  12702.     and    r.keycnt >= 6
  12703.     and    o2.id = c2.id
  12704.     and    c2.colid = r.fkey6
  12705.     and r.rkeyid = o1.id
  12706.     and s.id = o1.id and (s.status & 0xf) = 1
  12707.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12708.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12709.     and    o1.id = c1.id    
  12710.     and    c1.colid = r.rkey6
  12711.     union    all
  12712.     select
  12713.         db_name(r.rkeydbid),
  12714.         user_name(o1.uid),
  12715.         o1.name,
  12716.         c1.name,
  12717.         convert(binary(16),null),
  12718.         convert(int,null),
  12719.         db_name(r.fkeydbid),
  12720.         user_name(o2.uid),
  12721.         o2.name,
  12722.         c2.name,
  12723.         convert(binary(16),null),
  12724.         convert(int,null),
  12725.         convert(int,7),
  12726.         'NO ACTION',
  12727.         'NO ACTION',
  12728.         object_name(s.constid),
  12729.         object_name(r.constid),
  12730.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12731.     from
  12732.         sysobjects o1, sysobjects o2,
  12733.         syscolumns c1, syscolumns c2,
  12734.         sysreferences r, sysconstraints s
  12735.     where    
  12736.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12737.     and    o2.name = @fk_table_name
  12738.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12739.     and    o2.id = r.fkeyid
  12740.     and    r.keycnt >= 7
  12741.     and    o2.id = c2.id
  12742.     and    c2.colid = r.fkey7
  12743.     and    r.rkeyid = o1.id
  12744.     and s.id = o1.id and (s.status & 0xf) = 1
  12745.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12746.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12747.     and    o1.id = c1.id    
  12748.     and    c1.colid = r.rkey7
  12749.     union    all
  12750.     select
  12751.         db_name(r.rkeydbid),
  12752.         user_name(o1.uid),
  12753.         o1.name,
  12754.         c1.name,
  12755.         convert(binary(16),null),
  12756.         convert(int,null),
  12757.         db_name(r.fkeydbid),
  12758.         user_name(o2.uid),
  12759.         o2.name,
  12760.         c2.name,
  12761.         convert(binary(16),null),
  12762.         convert(int,null),
  12763.         convert(int,8),
  12764.         'NO ACTION',
  12765.         'NO ACTION',
  12766.         object_name(s.constid),
  12767.         object_name(r.constid),
  12768.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12769.     from
  12770.         sysobjects o1, sysobjects o2,
  12771.         syscolumns c1, syscolumns c2,
  12772.         sysreferences r, sysconstraints s
  12773.     where    
  12774.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12775.     and    o2.name = @fk_table_name
  12776.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12777.     and    o2.id = r.fkeyid
  12778.     and    r.keycnt >= 8
  12779.     and    o2.id = c2.id
  12780.     and    c2.colid = r.fkey8
  12781.     and r.rkeyid = o1.id
  12782.     and s.id = o1.id and (s.status & 0xf) = 1
  12783.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12784.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12785.     and    o1.id = c1.id    
  12786.     and    c1.colid = r.rkey8
  12787.     union    all
  12788.     select
  12789.         db_name(r.rkeydbid),
  12790.         user_name(o1.uid),
  12791.         o1.name,
  12792.         c1.name,
  12793.         convert(binary(16),null),
  12794.         convert(int,null),
  12795.         db_name(r.fkeydbid),
  12796.         user_name(o2.uid),
  12797.         o2.name,
  12798.         c2.name,
  12799.         convert(binary(16),null),
  12800.         convert(int,null),
  12801.         convert(int,9),
  12802.         'NO ACTION',
  12803.         'NO ACTION',
  12804.         object_name(s.constid),
  12805.         object_name(r.constid),
  12806.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12807.     from
  12808.         sysobjects o1, sysobjects o2,
  12809.         syscolumns c1, syscolumns c2,
  12810.         sysreferences r, sysconstraints s
  12811.     where    
  12812.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12813.     and    o2.name = @fk_table_name
  12814.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12815.     and    o2.id = r.fkeyid
  12816.     and    r.keycnt >= 9
  12817.     and    o2.id = c2.id
  12818.     and    c2.colid = r.fkey9
  12819.     and r.rkeyid = o1.id
  12820.     and s.id = o1.id and (s.status & 0xf) = 1
  12821.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12822.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12823.     and    o1.id = c1.id    
  12824.     and    c1.colid = r.rkey9
  12825.     union    all
  12826.     select
  12827.         db_name(r.rkeydbid),
  12828.         user_name(o1.uid),
  12829.         o1.name,
  12830.         c1.name,
  12831.         convert(binary(16),null),
  12832.         convert(int,null),
  12833.         db_name(r.fkeydbid),
  12834.         user_name(o2.uid),
  12835.         o2.name,
  12836.         c2.name,
  12837.         convert(binary(16),null),
  12838.         convert(int,null),
  12839.         convert(int,10),
  12840.         'NO ACTION',
  12841.         'NO ACTION',
  12842.         object_name(s.constid),
  12843.         object_name(r.constid),
  12844.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12845.     from
  12846.         sysobjects o1, sysobjects o2,
  12847.         syscolumns c1, syscolumns c2,
  12848.         sysreferences r, sysconstraints s
  12849.     where    
  12850.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12851.     and    o2.name = @fk_table_name
  12852.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12853.     and    o2.id = r.fkeyid
  12854.     and    r.keycnt >= 10
  12855.     and    o2.id = c2.id
  12856.     and    c2.colid = r.fkey10
  12857.     and r.rkeyid = o1.id
  12858.     and s.id = o1.id and (s.status & 0xf) = 1
  12859.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12860.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12861.     and    o1.id = c1.id    
  12862.     and    c1.colid = r.rkey10
  12863.     union    all
  12864.     select
  12865.         db_name(r.rkeydbid),
  12866.         user_name(o1.uid),
  12867.         o1.name,
  12868.         c1.name,
  12869.         convert(binary(16),null),
  12870.         convert(int,null),
  12871.         db_name(r.fkeydbid),
  12872.         user_name(o2.uid),
  12873.         o2.name,
  12874.         c2.name,
  12875.         convert(binary(16),null),
  12876.         convert(int,null),
  12877.         convert(int,11),
  12878.         'NO ACTION',
  12879.         'NO ACTION',
  12880.         object_name(s.constid),
  12881.         object_name(r.constid),
  12882.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12883.     from
  12884.         sysobjects o1, sysobjects o2,
  12885.         syscolumns c1, syscolumns c2,
  12886.         sysreferences r, sysconstraints s
  12887.     where    
  12888.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12889.     and    o2.name = @fk_table_name
  12890.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12891.     and    o2.id = r.fkeyid
  12892.     and    r.keycnt >= 11
  12893.     and    o2.id = c2.id
  12894.     and    c2.colid = r.fkey11
  12895.     and r.rkeyid = o1.id
  12896.     and s.id = o1.id and (s.status & 0xf) = 1
  12897.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12898.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12899.     and    o1.id = c1.id    
  12900.     and    c1.colid = r.rkey11
  12901.     union    all
  12902.     select
  12903.         db_name(r.rkeydbid),
  12904.         user_name(o1.uid),
  12905.         o1.name,
  12906.         c1.name,
  12907.         convert(binary(16),null),
  12908.         convert(int,null),
  12909.         db_name(r.fkeydbid),
  12910.         user_name(o2.uid),
  12911.         o2.name,
  12912.         c2.name,
  12913.         convert(binary(16),null),
  12914.         convert(int,null),
  12915.         convert(int,12),
  12916.         'NO ACTION',
  12917.         'NO ACTION',
  12918.         object_name(s.constid),
  12919.         object_name(r.constid),
  12920.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12921.     from
  12922.         sysobjects o1, sysobjects o2,
  12923.         syscolumns c1, syscolumns c2,
  12924.         sysreferences r, sysconstraints s
  12925.     where    
  12926.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12927.     and    o2.name = @fk_table_name
  12928.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12929.     and    o2.id = r.fkeyid
  12930.     and    r.keycnt >= 12
  12931.     and    o2.id = c2.id
  12932.     and    c2.colid = r.fkey12
  12933.     and r.rkeyid = o1.id
  12934.     and s.id = o1.id and (s.status & 0xf) = 1
  12935.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12936.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12937.     and    o1.id = c1.id    
  12938.     and    c1.colid = r.rkey12
  12939.     union    all
  12940.     select
  12941.         db_name(r.rkeydbid),
  12942.         user_name(o1.uid),
  12943.         o1.name,
  12944.         c1.name,
  12945.         convert(binary(16),null),
  12946.         convert(int,null),
  12947.         db_name(r.fkeydbid),
  12948.         user_name(o2.uid),
  12949.         o2.name,
  12950.         c2.name,
  12951.         convert(binary(16),null),
  12952.         convert(int,null),
  12953.         convert(int,13),
  12954.         'NO ACTION',
  12955.         'NO ACTION',
  12956.         object_name(s.constid),
  12957.         object_name(r.constid),
  12958.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12959.     from
  12960.         sysobjects o1, sysobjects o2,
  12961.         syscolumns c1, syscolumns c2,
  12962.         sysreferences r, sysconstraints s
  12963.     where    
  12964.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12965.     and    o2.name = @fk_table_name
  12966.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12967.     and    o2.id = r.fkeyid
  12968.     and    r.keycnt >= 13
  12969.     and    o2.id = c2.id
  12970.     and    c2.colid = r.fkey13
  12971.     and r.rkeyid = o1.id
  12972.     and s.id = o1.id and (s.status & 0xf) = 1
  12973.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12974.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12975.     and    o1.id = c1.id    
  12976.     and    c1.colid = r.rkey13
  12977.     union    all
  12978.     select
  12979.         db_name(r.rkeydbid),
  12980.         user_name(o1.uid),
  12981.         o1.name,
  12982.         c1.name,
  12983.         convert(binary(16),null),
  12984.         convert(int,null),
  12985.         db_name(r.fkeydbid),
  12986.         user_name(o2.uid),
  12987.         o2.name,
  12988.         c2.name,
  12989.         convert(binary(16),null),
  12990.         convert(int,null),
  12991.         convert(int,14),
  12992.         'NO ACTION',
  12993.         'NO ACTION',
  12994.         object_name(s.constid),
  12995.         object_name(r.constid),
  12996.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12997.     from
  12998.         sysobjects o1, sysobjects o2,
  12999.         syscolumns c1, syscolumns c2,
  13000.         sysreferences r, sysconstraints s
  13001.     where    
  13002.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13003.     and    o2.name = @fk_table_name
  13004.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13005.     and    o2.id = r.fkeyid
  13006.     and    r.keycnt >= 14
  13007.     and    o2.id = c2.id
  13008.     and    c2.colid = r.fkey14
  13009.     and    r.rkeyid = o1.id
  13010.     and s.id = o1.id and (s.status & 0xf) = 1
  13011.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13012.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13013.     and    o1.id = c1.id    
  13014.     and    c1.colid = r.rkey14
  13015.     union    all
  13016.     select
  13017.         db_name(r.rkeydbid),
  13018.         user_name(o1.uid),
  13019.         o1.name,
  13020.         c1.name,
  13021.         convert(binary(16),null),
  13022.         convert(int,null),
  13023.         db_name(r.fkeydbid),
  13024.         user_name(o2.uid),
  13025.         o2.name,
  13026.         c2.name,
  13027.         convert(binary(16),null),
  13028.         convert(int,null),
  13029.         convert(int,15),
  13030.         'NO ACTION',
  13031.         'NO ACTION',
  13032.         object_name(s.constid),
  13033.         object_name(r.constid),
  13034.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13035.     from
  13036.         sysobjects o1, sysobjects o2,
  13037.         syscolumns c1, syscolumns c2,
  13038.         sysreferences r, sysconstraints s
  13039.     where    
  13040.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13041.     and    o2.name = @fk_table_name
  13042.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13043.     and    o2.id = r.fkeyid
  13044.     and    r.keycnt >= 15
  13045.     and    o2.id = c2.id
  13046.     and    c2.colid = r.fkey15
  13047.     and r.rkeyid = o1.id
  13048.     and s.id = o1.id and (s.status & 0xf) = 1
  13049.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13050.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13051.     and    o1.id = c1.id    
  13052.     and    c1.colid = r.rkey15
  13053.     union    all
  13054.     select
  13055.         db_name(r.rkeydbid),
  13056.         user_name(o1.uid),
  13057.         o1.name,
  13058.         c1.name,
  13059.         convert(binary(16),null),
  13060.         convert(int,null),
  13061.         db_name(r.fkeydbid),
  13062.         user_name(o2.uid),
  13063.         o2.name,
  13064.         c2.name,
  13065.         convert(binary(16),null),
  13066.         convert(int,null),
  13067.         convert(int,16),
  13068.         'NO ACTION',
  13069.         'NO ACTION',
  13070.         object_name(s.constid),
  13071.         object_name(r.constid),
  13072.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13073.     from
  13074.         sysobjects o1, sysobjects o2,
  13075.         syscolumns c1, syscolumns c2,
  13076.         sysreferences r, sysconstraints s
  13077.     where    
  13078.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13079.     and    o2.name = @fk_table_name
  13080.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13081.     and    o2.id = r.fkeyid
  13082.     and    r.keycnt >= 16
  13083.     and    o2.id = c2.id
  13084.     and    c2.colid = r.fkey16
  13085.     and r.rkeyid = o1.id
  13086.     and s.id = o1.id and (s.status & 0xf) = 1
  13087.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13088.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13089.     and    o1.id = c1.id    
  13090.     and    c1.colid = r.rkey16
  13091.     order by 8,9,2,3,13
  13092.     END
  13093. ELSE
  13094.     BEGIN
  13095.     select
  13096.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13097.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13098.         PK_TABLE_NAME         = o1.name,
  13099.         PK_COLUMN_NAME         = c1.name,
  13100.         PK_COLUMN_GUID        = convert(binary(16),null),
  13101.         PK_COLUMN_PROPID    = convert(int,null),
  13102.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13103.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13104.         FK_TABLE_NAME         = o2.name,
  13105.         FK_COLUMN_NAME         = c2.name,
  13106.         FK_COLUMN_GUID        = convert(binary(16),null),
  13107.         FK_COLUMN_PROPID    = convert(int,null),
  13108.         ORDINAL                = convert(int,1),
  13109.         UPDATE_RULE            = 'NO ACTION',
  13110.         DELETE_RULE         = 'NO ACTION',
  13111.         PK_NAME                = object_name(s.constid),
  13112.         FK_NAME                = object_name(r.constid),
  13113.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13114.     from
  13115.         sysobjects o1, sysobjects o2,
  13116.         syscolumns c1, syscolumns c2,
  13117.         sysreferences r, sysconstraints s
  13118.     where    
  13119.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13120.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13121.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13122.     and    o1.id = r.rkeyid
  13123.     and s.id = o1.id and (s.status & 0xf) = 1
  13124.     and    o1.id = c1.id
  13125.     and    c1.colid = r.rkey1
  13126.     and r.fkeyid = o2.id
  13127.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13128.     and    o2.id = c2.id    
  13129.     and    c2.colid = r.fkey1
  13130.     union all
  13131.     select
  13132.         db_name(r.rkeydbid),
  13133.         user_name(o1.uid),
  13134.         o1.name,
  13135.         c1.name,
  13136.         convert(binary(16),null),
  13137.         convert(int,null),
  13138.         db_name(r.fkeydbid),
  13139.         user_name(o2.uid),
  13140.         o2.name,
  13141.         c2.name,
  13142.         convert(binary(16),null),
  13143.         convert(int,null),
  13144.         convert(int,2),
  13145.         'NO ACTION',
  13146.         'NO ACTION',
  13147.         object_name(s.constid),
  13148.         object_name(r.constid),
  13149.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13150.     from
  13151.         sysobjects o1, sysobjects o2,
  13152.         syscolumns c1, syscolumns c2,
  13153.         sysreferences r, sysconstraints s
  13154.     where    
  13155.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13156.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13157.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13158.     and    o1.id = r.rkeyid
  13159.     and s.id = o1.id and (s.status & 0xf) = 1
  13160.     and    r.keycnt >= 2
  13161.     and    o1.id = c1.id
  13162.     and    c1.colid = r.rkey2
  13163.     and r.fkeyid = o2.id
  13164.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13165.     and    o2.id = c2.id    
  13166.     and    c2.colid = r.fkey2
  13167.     union all
  13168.     select
  13169.         db_name(r.rkeydbid),
  13170.         user_name(o1.uid),
  13171.         o1.name,
  13172.         c1.name,
  13173.         convert(binary(16),null),
  13174.         convert(int,null),
  13175.         db_name(r.fkeydbid),
  13176.         user_name(o2.uid),
  13177.         o2.name,
  13178.         c2.name,
  13179.         convert(binary(16),null),
  13180.         convert(int,null),
  13181.         convert(int,3),
  13182.         'NO ACTION',
  13183.         'NO ACTION',
  13184.         object_name(s.constid),
  13185.         object_name(r.constid),
  13186.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13187.     from
  13188.         sysobjects o1, sysobjects o2,
  13189.         syscolumns c1, syscolumns c2,
  13190.         sysreferences r, sysconstraints s
  13191.     where    
  13192.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13193.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13194.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13195.     and    o1.id = r.rkeyid
  13196.     and s.id = o1.id and (s.status & 0xf) = 1
  13197.     and    r.keycnt >= 3
  13198.     and    o1.id = c1.id
  13199.     and    c1.colid = r.rkey3
  13200.     and r.fkeyid = o2.id
  13201.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13202.     and    o2.id = c2.id    
  13203.     and    c2.colid = r.fkey3
  13204.     union all
  13205.     select
  13206.         db_name(r.rkeydbid),
  13207.         user_name(o1.uid),
  13208.         o1.name,
  13209.         c1.name,
  13210.         convert(binary(16),null),
  13211.         convert(int,null),
  13212.         db_name(r.fkeydbid),
  13213.         user_name(o2.uid),
  13214.         o2.name,
  13215.         c2.name,
  13216.         convert(binary(16),null),
  13217.         convert(int,null),
  13218.         convert(int,4),
  13219.         'NO ACTION',
  13220.         'NO ACTION',
  13221.         object_name(s.constid),
  13222.         object_name(r.constid),
  13223.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13224.     from
  13225.         sysobjects o1, sysobjects o2,
  13226.         syscolumns c1, syscolumns c2,
  13227.         sysreferences r, sysconstraints s
  13228.     where    
  13229.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13230.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13231.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13232.     and    o1.id = r.rkeyid
  13233.     and s.id = o1.id and (s.status & 0xf) = 1
  13234.     and    r.keycnt >= 4
  13235.     and    o1.id = c1.id
  13236.     and    c1.colid = r.rkey4
  13237.     and r.fkeyid = o2.id
  13238.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13239.     and    o2.id = c2.id    
  13240.     and    c2.colid = r.fkey4
  13241.     union all
  13242.     select
  13243.         db_name(r.rkeydbid),
  13244.         user_name(o1.uid),
  13245.         o1.name,
  13246.         c1.name,
  13247.         convert(binary(16),null),
  13248.         convert(int,null),
  13249.         db_name(r.fkeydbid),
  13250.         user_name(o2.uid),
  13251.         o2.name,
  13252.         c2.name,
  13253.         convert(binary(16),null),
  13254.         convert(int,null),
  13255.         convert(int,5),
  13256.         'NO ACTION',
  13257.         'NO ACTION',
  13258.         object_name(s.constid),
  13259.         object_name(r.constid),
  13260.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13261.     from
  13262.         sysobjects o1, sysobjects o2,
  13263.         syscolumns c1, syscolumns c2,
  13264.         sysreferences r, sysconstraints s
  13265.     where    
  13266.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13267.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13268.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13269.     and    o1.id = r.rkeyid
  13270.     and s.id = o1.id and (s.status & 0xf) = 1
  13271.     and    r.keycnt >= 5
  13272.     and    o1.id = c1.id
  13273.     and    c1.colid = r.rkey5
  13274.     and r.fkeyid = o2.id
  13275.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13276.     and    o2.id = c2.id    
  13277.     and    c2.colid = r.fkey5
  13278.     union all
  13279.     select
  13280.         db_name(r.rkeydbid),
  13281.         user_name(o1.uid),
  13282.         o1.name,
  13283.         c1.name,
  13284.         convert(binary(16),null),
  13285.         convert(int,null),
  13286.         db_name(r.fkeydbid),
  13287.         user_name(o2.uid),
  13288.         o2.name,
  13289.         c2.name,
  13290.         convert(binary(16),null),
  13291.         convert(int,null),
  13292.         convert(int,6),
  13293.         'NO ACTION',
  13294.         'NO ACTION',
  13295.         object_name(s.constid),
  13296.         object_name(r.constid),
  13297.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13298.     from
  13299.         sysobjects o1, sysobjects o2,
  13300.         syscolumns c1, syscolumns c2,
  13301.         sysreferences r, sysconstraints s
  13302.     where    
  13303.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13304.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13305.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13306.     and    o1.id = r.rkeyid
  13307.     and s.id = o1.id and (s.status & 0xf) = 1
  13308.     and    r.keycnt >= 6
  13309.     and    o1.id = c1.id
  13310.     and    c1.colid = r.rkey6
  13311.     and r.fkeyid = o2.id
  13312.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13313.     and    o2.id = c2.id    
  13314.     and    c2.colid = r.fkey6
  13315.     union all
  13316.     select
  13317.         db_name(r.rkeydbid),
  13318.         user_name(o1.uid),
  13319.         o1.name,
  13320.         c1.name,
  13321.         convert(binary(16),null),
  13322.         convert(int,null),
  13323.         db_name(r.fkeydbid),
  13324.         user_name(o2.uid),
  13325.         o2.name,
  13326.         c2.name,
  13327.         convert(binary(16),null),
  13328.         convert(int,null),
  13329.         convert(int,7),
  13330.         'NO ACTION',
  13331.         'NO ACTION',
  13332.         object_name(s.constid),
  13333.         object_name(r.constid),
  13334.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13335.     from
  13336.         sysobjects o1, sysobjects o2,
  13337.         syscolumns c1, syscolumns c2,
  13338.         sysreferences r, sysconstraints s
  13339.     where    
  13340.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13341.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13342.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13343.     and    o1.id = r.rkeyid
  13344.     and s.id = o1.id and (s.status & 0xf) = 1
  13345.     and    r.keycnt >= 7
  13346.     and    o1.id = c1.id
  13347.     and    c1.colid = r.rkey7
  13348.     and r.fkeyid = o2.id
  13349.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13350.     and    o2.id = c2.id    
  13351.     and    c2.colid = r.fkey7
  13352.     union all
  13353.     select
  13354.         db_name(r.rkeydbid),
  13355.         user_name(o1.uid),
  13356.         o1.name,
  13357.         c1.name,
  13358.         convert(binary(16),null),
  13359.         convert(int,null),
  13360.         db_name(r.fkeydbid),
  13361.         user_name(o2.uid),
  13362.         o2.name,
  13363.         c2.name,
  13364.         convert(binary(16),null),
  13365.         convert(int,null),
  13366.         convert(int,8),
  13367.         'NO ACTION',
  13368.         'NO ACTION',
  13369.         object_name(s.constid),
  13370.         object_name(r.constid),
  13371.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13372.     from
  13373.         sysobjects o1, sysobjects o2,
  13374.         syscolumns c1, syscolumns c2,
  13375.         sysreferences r, sysconstraints s
  13376.     where    
  13377.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13378.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13379.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13380.     and    o1.id = r.rkeyid
  13381.     and s.id = o1.id and (s.status & 0xf) = 1
  13382.     and    r.keycnt >= 8
  13383.     and    o1.id = c1.id
  13384.     and    c1.colid = r.rkey8
  13385.     and r.fkeyid = o2.id
  13386.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13387.     and    o2.id = c2.id    
  13388.     and    c2.colid = r.fkey8
  13389.     union all
  13390.     select
  13391.         db_name(r.rkeydbid),
  13392.         user_name(o1.uid),
  13393.         o1.name,
  13394.         c1.name,
  13395.         convert(binary(16),null),
  13396.         convert(int,null),
  13397.         db_name(r.fkeydbid),
  13398.         user_name(o2.uid),
  13399.         o2.name,
  13400.         c2.name,
  13401.         convert(binary(16),null),
  13402.         convert(int,null),
  13403.         convert(int,9),
  13404.         'NO ACTION',
  13405.         'NO ACTION',
  13406.         object_name(s.constid),
  13407.         object_name(r.constid),
  13408.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13409.     from
  13410.         sysobjects o1, sysobjects o2,
  13411.         syscolumns c1, syscolumns c2,
  13412.         sysreferences r, sysconstraints s
  13413.     where    
  13414.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13415.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13416.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13417.     and    o1.id = r.rkeyid
  13418.     and s.id = o1.id and (s.status & 0xf) = 1
  13419.     and    r.keycnt >= 9
  13420.     and    o1.id = c1.id
  13421.     and    c1.colid = r.rkey9
  13422.     and r.fkeyid = o2.id
  13423.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13424.     and    o2.id = c2.id    
  13425.     and    c2.colid = r.fkey9
  13426.     union all
  13427.     select
  13428.         db_name(r.rkeydbid),
  13429.         user_name(o1.uid),
  13430.         o1.name,
  13431.         c1.name,
  13432.         convert(binary(16),null),
  13433.         convert(int,null),
  13434.         db_name(r.fkeydbid),
  13435.         user_name(o2.uid),
  13436.         o2.name,
  13437.         c2.name,
  13438.         convert(binary(16),null),
  13439.         convert(int,null),
  13440.         convert(int,10),
  13441.         'NO ACTION',
  13442.         'NO ACTION',
  13443.         object_name(s.constid),
  13444.         object_name(r.constid),
  13445.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13446.     from
  13447.         sysobjects o1, sysobjects o2,
  13448.         syscolumns c1, syscolumns c2,
  13449.         sysreferences r, sysconstraints s
  13450.     where    
  13451.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13452.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13453.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13454.     and    o1.id = r.rkeyid
  13455.     and s.id = o1.id and (s.status & 0xf) = 1
  13456.     and    r.keycnt >= 10
  13457.     and    o1.id = c1.id
  13458.     and    c1.colid = r.rkey10
  13459.     and r.fkeyid = o2.id
  13460.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13461.     and    o2.id = c2.id    
  13462.     and    c2.colid = r.fkey10
  13463.     union all
  13464.     select
  13465.         db_name(r.rkeydbid),
  13466.         user_name(o1.uid),
  13467.         o1.name,
  13468.         c1.name,
  13469.         convert(binary(16),null),
  13470.         convert(int,null),
  13471.         db_name(r.fkeydbid),
  13472.         user_name(o2.uid),
  13473.         o2.name,
  13474.         c2.name,
  13475.         convert(binary(16),null),
  13476.         convert(int,null),
  13477.         convert(int,11),
  13478.         'NO ACTION',
  13479.         'NO ACTION',
  13480.         object_name(s.constid),
  13481.         object_name(r.constid),
  13482.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13483.     from
  13484.         sysobjects o1, sysobjects o2,
  13485.         syscolumns c1, syscolumns c2,
  13486.         sysreferences r, sysconstraints s
  13487.     where    
  13488.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13489.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13490.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13491.     and    o1.id = r.rkeyid
  13492.     and s.id = o1.id and (s.status & 0xf) = 1
  13493.     and    r.keycnt >= 11
  13494.     and    o1.id = c1.id
  13495.     and    c1.colid = r.rkey11
  13496.     and r.fkeyid = o2.id
  13497.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13498.     and    o2.id = c2.id    
  13499.     and    c2.colid = r.fkey11
  13500.     union all
  13501.     select
  13502.         db_name(r.rkeydbid),
  13503.         user_name(o1.uid),
  13504.         o1.name,
  13505.         c1.name,
  13506.         convert(binary(16),null),
  13507.         convert(int,null),
  13508.         db_name(r.fkeydbid),
  13509.         user_name(o2.uid),
  13510.         o2.name,
  13511.         c2.name,
  13512.         convert(binary(16),null),
  13513.         convert(int,null),
  13514.         convert(int,12),
  13515.         'NO ACTION',
  13516.         'NO ACTION',
  13517.         object_name(s.constid),
  13518.         object_name(r.constid),
  13519.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13520.     from
  13521.         sysobjects o1, sysobjects o2,
  13522.         syscolumns c1, syscolumns c2,
  13523.         sysreferences r, sysconstraints s
  13524.     where    
  13525.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13526.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13527.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13528.     and    o1.id = r.rkeyid
  13529.     and s.id = o1.id and (s.status & 0xf) = 1
  13530.     and    r.keycnt >= 12
  13531.     and    o1.id = c1.id
  13532.     and    c1.colid = r.rkey12
  13533.     and r.fkeyid = o2.id
  13534.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13535.     and    o2.id = c2.id    
  13536.     and    c2.colid = r.fkey12
  13537.     union all
  13538.     select
  13539.         db_name(r.rkeydbid),
  13540.         user_name(o1.uid),
  13541.         o1.name,
  13542.         c1.name,
  13543.         convert(binary(16),null),
  13544.         convert(int,null),
  13545.         db_name(r.fkeydbid),
  13546.         user_name(o2.uid),
  13547.         o2.name,
  13548.         c2.name,
  13549.         convert(binary(16),null),
  13550.         convert(int,null),
  13551.         convert(int,13),
  13552.         'NO ACTION',
  13553.         'NO ACTION',
  13554.         object_name(s.constid),
  13555.         object_name(r.constid),
  13556.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13557.     from
  13558.         sysobjects o1, sysobjects o2,
  13559.         syscolumns c1, syscolumns c2,
  13560.         sysreferences r, sysconstraints s
  13561.     where    
  13562.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13563.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13564.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13565.     and    o1.id = r.rkeyid
  13566.     and s.id = o1.id and (s.status & 0xf) = 1
  13567.     and    r.keycnt >= 13
  13568.     and    o1.id = c1.id
  13569.     and    c1.colid = r.rkey13
  13570.     and r.fkeyid = o2.id
  13571.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13572.     and    o2.id = c2.id    
  13573.     and    c2.colid = r.fkey13
  13574.     union all
  13575.     select
  13576.         db_name(r.rkeydbid),
  13577.         user_name(o1.uid),
  13578.         o1.name,
  13579.         c1.name,
  13580.         convert(binary(16),null),
  13581.         convert(int,null),
  13582.         db_name(r.fkeydbid),
  13583.         user_name(o2.uid),
  13584.         o2.name,
  13585.         c2.name,
  13586.         convert(binary(16),null),
  13587.         convert(int,null),
  13588.         convert(int,14),
  13589.         'NO ACTION',
  13590.         'NO ACTION',
  13591.         object_name(s.constid),
  13592.         object_name(r.constid),
  13593.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13594.     from
  13595.         sysobjects o1, sysobjects o2,
  13596.         syscolumns c1, syscolumns c2,
  13597.         sysreferences r, sysconstraints s
  13598.     where    
  13599.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13600.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13601.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13602.     and    o1.id = r.rkeyid
  13603.     and s.id = o1.id and (s.status & 0xf) = 1
  13604.     and    r.keycnt >= 14
  13605.     and    o1.id = c1.id
  13606.     and    c1.colid = r.rkey14
  13607.     and r.fkeyid = o2.id
  13608.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13609.     and    o2.id = c2.id    
  13610.     and    c2.colid = r.fkey14
  13611.     union all
  13612.     select
  13613.         db_name(r.rkeydbid),
  13614.         user_name(o1.uid),
  13615.         o1.name,
  13616.         c1.name,
  13617.         convert(binary(16),null),
  13618.         convert(int,null),
  13619.         db_name(r.fkeydbid),
  13620.         user_name(o2.uid),
  13621.         o2.name,
  13622.         c2.name,
  13623.         convert(binary(16),null),
  13624.         convert(int,null),
  13625.         convert(int,15),
  13626.         'NO ACTION',
  13627.         'NO ACTION',
  13628.         object_name(s.constid),
  13629.         object_name(r.constid),
  13630.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13631.     from
  13632.         sysobjects o1, sysobjects o2,
  13633.         syscolumns c1, syscolumns c2,
  13634.         sysreferences r, sysconstraints s
  13635.     where    
  13636.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13637.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13638.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13639.     and    o1.id = r.rkeyid
  13640.     and s.id = o1.id and (s.status & 0xf) = 1
  13641.     and    r.keycnt >= 15
  13642.     and    o1.id = c1.id
  13643.     and    c1.colid = r.rkey15
  13644.     and r.fkeyid = o2.id
  13645.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13646.     and    o2.id = c2.id    
  13647.     and    c2.colid = r.fkey15
  13648.     union all
  13649.     select
  13650.         db_name(r.rkeydbid),
  13651.         user_name(o1.uid),
  13652.         o1.name,
  13653.         c1.name,
  13654.         convert(binary(16),null),
  13655.         convert(int,null),
  13656.         db_name(r.fkeydbid),
  13657.         user_name(o2.uid),
  13658.         o2.name,
  13659.         c2.name,
  13660.         convert(binary(16),null),
  13661.         convert(int,null),
  13662.         convert(int,16),
  13663.         'NO ACTION',
  13664.         'NO ACTION',
  13665.         object_name(s.constid),
  13666.         object_name(r.constid),
  13667.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13668.     from
  13669.         sysobjects o1, sysobjects o2,
  13670.         syscolumns c1, syscolumns c2,
  13671.         sysreferences r, sysconstraints s
  13672.     where    
  13673.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13674.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13675.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13676.     and    o1.id = r.rkeyid
  13677.     and s.id = o1.id and (s.status & 0xf) = 1
  13678.     and    o1.id = c1.id
  13679.     and    r.keycnt >= 16
  13680.     and    c1.colid = r.rkey16
  13681.     and r.fkeyid = o2.id
  13682.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13683.     and    o2.id = c2.id    
  13684.     and    c2.colid = r.fkey16
  13685.     order by 8,9,2,3,13
  13686.     END
  13687. go
  13688. dump tran master with no_log
  13689. go
  13690. CREATE PROCEDURE sp_foreign_keys_rowset;2
  13691.     (
  13692.        @handle            int output,
  13693.        @scrollopt        int output,
  13694.     @ccopt            int output,
  13695.     @rows            int output,
  13696.      @pk_table_name        varchar(255) = null,
  13697.     @pk_table_schema    varchar(255) = null,
  13698.     @pk_table_catalog    varchar(255) = null,
  13699.     @fk_table_name        varchar(255) = null,
  13700.     @fk_table_schema    varchar(255) = null,
  13701.     @fk_table_catalog    varchar(255) = null
  13702.     )
  13703. as
  13704.     declare @ret int
  13705.     
  13706.     SET NOCOUNT ON
  13707.  
  13708.     create table #spfkeysrowset1
  13709.         (
  13710.         PK_TABLE_CATALOG    sysname not null,
  13711.         PK_TABLE_SCHEMA        sysname not null,
  13712.         PK_TABLE_NAME         sysname not null,
  13713.         PK_COLUMN_NAME         sysname not null,
  13714.         PK_COLUMN_GUID        binary(16) null,
  13715.         PK_COLUMN_PROPID    int null,
  13716.         FK_TABLE_CATALOG    sysname not null,
  13717.         FK_TABLE_SCHEMA        sysname not null,
  13718.         FK_TABLE_NAME         sysname not null,
  13719.         FK_COLUMN_NAME         sysname not null,
  13720.         FK_COLUMN_GUID        binary(16) null,
  13721.         FK_COLUMN_PROPID    int null,
  13722.         ORDINAL                int not null,
  13723.         UPDATE_RULE            sysname not null,
  13724.         DELETE_RULE         sysname not null,
  13725.         PK_NAME                sysname not null,
  13726.         FK_NAME                sysname not null,
  13727.         DEFERRABILITY        smallint not null        
  13728.         )
  13729.  
  13730. IF @pk_table_name is not null
  13731.     BEGIN
  13732.     insert into #spfkeysrowset1
  13733.     select
  13734.         db_name(r.rkeydbid),
  13735.         user_name(o1.uid),
  13736.         o1.name,
  13737.         c1.name,
  13738.         convert(binary(16),null),
  13739.         convert(int,null),
  13740.         db_name(r.fkeydbid),
  13741.         user_name(o2.uid),
  13742.         o2.name,
  13743.         c2.name,
  13744.         convert(binary(16),null),
  13745.         convert(int,null),
  13746.         convert(int,1),
  13747.         'NO ACTION',
  13748.         'NO ACTION',
  13749.         object_name(s.constid),
  13750.         object_name(r.constid),
  13751.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13752.     from
  13753.         sysobjects o1, sysobjects o2,
  13754.         syscolumns c1, syscolumns c2,
  13755.         sysreferences r, sysconstraints s
  13756.     where    
  13757.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13758.     and    o1.name = @pk_table_name
  13759.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13760.     and    o1.id = r.rkeyid
  13761.     and s.id = o1.id and (s.status & 0xf) = 1
  13762.     and    o1.id = c1.id
  13763.     and    c1.colid = r.rkey1
  13764.     and r.fkeyid = o2.id
  13765.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13766.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13767.     and    o2.id = c2.id    
  13768.     and    c2.colid = r.fkey1
  13769.     union all
  13770.     select
  13771.         db_name(r.rkeydbid),
  13772.         user_name(o1.uid),
  13773.         o1.name,
  13774.         c1.name,
  13775.         convert(binary(16),null),
  13776.         convert(int,null),
  13777.         db_name(r.fkeydbid),
  13778.         user_name(o2.uid),
  13779.         o2.name,
  13780.         c2.name,
  13781.         convert(binary(16),null),
  13782.         convert(int,null),
  13783.         convert(int,2),
  13784.         'NO ACTION',
  13785.         'NO ACTION',
  13786.         object_name(s.constid),
  13787.         object_name(r.constid),
  13788.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13789.     from
  13790.         sysobjects o1, sysobjects o2,
  13791.         syscolumns c1, syscolumns c2,
  13792.         sysreferences r, sysconstraints s
  13793.     where    
  13794.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13795.     and    o1.name = @pk_table_name
  13796.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13797.     and    o1.id = r.rkeyid
  13798.     and s.id = o1.id and (s.status & 0xf) = 1
  13799.     and    o1.id = c1.id
  13800.     and    c1.colid = r.rkey2
  13801.     and    r.keycnt >= 2
  13802.     and r.fkeyid = o2.id
  13803.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13804.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13805.     and    o2.id = c2.id    
  13806.     and    c2.colid = r.fkey2
  13807.     union all
  13808.     select
  13809.         db_name(r.rkeydbid),
  13810.         user_name(o1.uid),
  13811.         o1.name,
  13812.         c1.name,
  13813.         convert(binary(16),null),
  13814.         convert(int,null),
  13815.         db_name(r.fkeydbid),
  13816.         user_name(o2.uid),
  13817.         o2.name,
  13818.         c2.name,
  13819.         convert(binary(16),null),
  13820.         convert(int,null),
  13821.         convert(int,3),
  13822.         'NO ACTION',
  13823.         'NO ACTION',
  13824.         object_name(s.constid),
  13825.         object_name(r.constid),
  13826.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13827.     from
  13828.         sysobjects o1, sysobjects o2,
  13829.         syscolumns c1, syscolumns c2,
  13830.         sysreferences r, sysconstraints s
  13831.     where    
  13832.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13833.     and    o1.name = @pk_table_name
  13834.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13835.     and    o1.id = r.rkeyid
  13836.     and s.id = o1.id and (s.status & 0xf) = 1
  13837.     and    o1.id = c1.id
  13838.     and    c1.colid = r.rkey3
  13839.     and    r.keycnt >= 3
  13840.     and r.fkeyid = o2.id
  13841.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13842.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13843.     and    o2.id = c2.id    
  13844.     and    c2.colid = r.fkey3
  13845.     union all
  13846.     select
  13847.         db_name(r.rkeydbid),
  13848.         user_name(o1.uid),
  13849.         o1.name,
  13850.         c1.name,
  13851.         convert(binary(16),null),
  13852.         convert(int,null),
  13853.         db_name(r.fkeydbid),
  13854.         user_name(o2.uid),
  13855.         o2.name,
  13856.         c2.name,
  13857.         convert(binary(16),null),
  13858.         convert(int,null),
  13859.         convert(int,4),
  13860.         'NO ACTION',
  13861.         'NO ACTION',
  13862.         object_name(s.constid),
  13863.         object_name(r.constid),
  13864.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13865.     from
  13866.         sysobjects o1, sysobjects o2,
  13867.         syscolumns c1, syscolumns c2,
  13868.         sysreferences r, sysconstraints s
  13869.     where    
  13870.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13871.     and    o1.name = @pk_table_name
  13872.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13873.     and    o1.id = r.rkeyid
  13874.     and s.id = o1.id and (s.status & 0xf) = 1
  13875.     and    r.keycnt >= 4
  13876.     and    o1.id = c1.id
  13877.     and    c1.colid = r.rkey4
  13878.     and r.fkeyid = o2.id
  13879.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13880.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13881.     and    o2.id = c2.id    
  13882.     and    c2.colid = r.fkey4
  13883.     union all
  13884.     select
  13885.         db_name(r.rkeydbid),
  13886.         user_name(o1.uid),
  13887.         o1.name,
  13888.         c1.name,
  13889.         convert(binary(16),null),
  13890.         convert(int,null),
  13891.         db_name(r.fkeydbid),
  13892.         user_name(o2.uid),
  13893.         o2.name,
  13894.         c2.name,
  13895.         convert(binary(16),null),
  13896.         convert(int,null),
  13897.         convert(int,5),
  13898.         'NO ACTION',
  13899.         'NO ACTION',
  13900.         object_name(s.constid),
  13901.         object_name(r.constid),
  13902.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13903.     from
  13904.         sysobjects o1, sysobjects o2,
  13905.         syscolumns c1, syscolumns c2,
  13906.         sysreferences r, sysconstraints s
  13907.     where    
  13908.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13909.     and    o1.name = @pk_table_name
  13910.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13911.     and    o1.id = r.rkeyid
  13912.     and s.id = o1.id and (s.status & 0xf) = 1
  13913.     and    r.keycnt >= 5
  13914.     and    o1.id = c1.id
  13915.     and    c1.colid = r.rkey5
  13916.     and r.fkeyid = o2.id
  13917.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13918.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13919.     and    o2.id = c2.id    
  13920.     and    c2.colid = r.fkey5
  13921.     union all
  13922.     select
  13923.         db_name(r.rkeydbid),
  13924.         user_name(o1.uid),
  13925.         o1.name,
  13926.         c1.name,
  13927.         convert(binary(16),null),
  13928.         convert(int,null),
  13929.         db_name(r.fkeydbid),
  13930.         user_name(o2.uid),
  13931.         o2.name,
  13932.         c2.name,
  13933.         convert(binary(16),null),
  13934.         convert(int,null),
  13935.         convert(int,6),
  13936.         'NO ACTION',
  13937.         'NO ACTION',
  13938.         object_name(s.constid),
  13939.         object_name(r.constid),
  13940.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13941.     from
  13942.         sysobjects o1, sysobjects o2,
  13943.         syscolumns c1, syscolumns c2,
  13944.         sysreferences r, sysconstraints s
  13945.     where    
  13946.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13947.     and    o1.name = @pk_table_name
  13948.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13949.     and    o1.id = r.rkeyid
  13950.     and s.id = o1.id and (s.status & 0xf) = 1
  13951.     and    r.keycnt >= 6
  13952.     and    o1.id = c1.id
  13953.     and    c1.colid = r.rkey6
  13954.     and r.fkeyid = o2.id
  13955.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13956.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13957.     and    o2.id = c2.id    
  13958.     and    c2.colid = r.fkey6
  13959.     union all
  13960.     select
  13961.         db_name(r.rkeydbid),
  13962.         user_name(o1.uid),
  13963.         o1.name,
  13964.         c1.name,
  13965.         convert(binary(16),null),
  13966.         convert(int,null),
  13967.         db_name(r.fkeydbid),
  13968.         user_name(o2.uid),
  13969.         o2.name,
  13970.         c2.name,
  13971.         convert(binary(16),null),
  13972.         convert(int,null),
  13973.         convert(int,7),
  13974.         'NO ACTION',
  13975.         'NO ACTION',
  13976.         object_name(s.constid),
  13977.         object_name(r.constid),
  13978.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13979.     from
  13980.         sysobjects o1, sysobjects o2,
  13981.         syscolumns c1, syscolumns c2,
  13982.         sysreferences r, sysconstraints s
  13983.     where    
  13984.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13985.     and    o1.name = @pk_table_name
  13986.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13987.     and    o1.id = r.rkeyid
  13988.     and s.id = o1.id and (s.status & 0xf) = 1
  13989.     and    r.keycnt >= 7
  13990.     and    o1.id = c1.id
  13991.     and    c1.colid = r.rkey7
  13992.     and r.fkeyid = o2.id
  13993.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13994.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13995.     and    o2.id = c2.id    
  13996.     and    c2.colid = r.fkey7
  13997.     union all
  13998.     select
  13999.         db_name(r.rkeydbid),
  14000.         user_name(o1.uid),
  14001.         o1.name,
  14002.         c1.name,
  14003.         convert(binary(16),null),
  14004.         convert(int,null),
  14005.         db_name(r.fkeydbid),
  14006.         user_name(o2.uid),
  14007.         o2.name,
  14008.         c2.name,
  14009.         convert(binary(16),null),
  14010.         convert(int,null),
  14011.         convert(int,8),
  14012.         'NO ACTION',
  14013.         'NO ACTION',
  14014.         object_name(s.constid),
  14015.         object_name(r.constid),
  14016.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14017.     from
  14018.         sysobjects o1, sysobjects o2,
  14019.         syscolumns c1, syscolumns c2,
  14020.         sysreferences r, sysconstraints s
  14021.     where    
  14022.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14023.     and    o1.name = @pk_table_name
  14024.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14025.     and    o1.id = r.rkeyid
  14026.     and s.id = o1.id and (s.status & 0xf) = 1
  14027.     and    r.keycnt >= 8
  14028.     and    o1.id = c1.id
  14029.     and    c1.colid = r.rkey8
  14030.     and r.fkeyid = o2.id
  14031.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14032.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14033.     and    o2.id = c2.id    
  14034.     and    c2.colid = r.fkey8
  14035.     union all
  14036.     select
  14037.         db_name(r.rkeydbid),
  14038.         user_name(o1.uid),
  14039.         o1.name,
  14040.         c1.name,
  14041.         convert(binary(16),null),
  14042.         convert(int,null),
  14043.         db_name(r.fkeydbid),
  14044.         user_name(o2.uid),
  14045.         o2.name,
  14046.         c2.name,
  14047.         convert(binary(16),null),
  14048.         convert(int,null),
  14049.         convert(int,9),
  14050.         'NO ACTION',
  14051.         'NO ACTION',
  14052.         object_name(s.constid),
  14053.         object_name(r.constid),
  14054.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14055.     from
  14056.         sysobjects o1, sysobjects o2,
  14057.         syscolumns c1, syscolumns c2,
  14058.         sysreferences r, sysconstraints s
  14059.     where    
  14060.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14061.     and    o1.name = @pk_table_name
  14062.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14063.     and    o1.id = r.rkeyid
  14064.     and s.id = o1.id and (s.status & 0xf) = 1
  14065.     and    r.keycnt >= 9
  14066.     and    o1.id = c1.id
  14067.     and    c1.colid = r.rkey9
  14068.     and r.fkeyid = o2.id
  14069.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14070.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14071.     and    o2.id = c2.id    
  14072.     and    c2.colid = r.fkey9
  14073.     union all
  14074.     select
  14075.         db_name(r.rkeydbid),
  14076.         user_name(o1.uid),
  14077.         o1.name,
  14078.         c1.name,
  14079.         convert(binary(16),null),
  14080.         convert(int,null),
  14081.         db_name(r.fkeydbid),
  14082.         user_name(o2.uid),
  14083.         o2.name,
  14084.         c2.name,
  14085.         convert(binary(16),null),
  14086.         convert(int,null),
  14087.         convert(int,10),
  14088.         'NO ACTION',
  14089.         'NO ACTION',
  14090.         object_name(s.constid),
  14091.         object_name(r.constid),
  14092.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14093.     from
  14094.         sysobjects o1, sysobjects o2,
  14095.         syscolumns c1, syscolumns c2,
  14096.         sysreferences r, sysconstraints s
  14097.     where    
  14098.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14099.     and    o1.name = @pk_table_name
  14100.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14101.     and    o1.id = r.rkeyid
  14102.     and s.id = o1.id and (s.status & 0xf) = 1
  14103.     and    r.keycnt >= 10
  14104.     and    o1.id = c1.id
  14105.     and    c1.colid = r.rkey10
  14106.     and r.fkeyid = o2.id
  14107.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14108.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14109.     and    o2.id = c2.id    
  14110.     and    c2.colid = r.fkey10
  14111.     union all
  14112.     select
  14113.         db_name(r.rkeydbid),
  14114.         user_name(o1.uid),
  14115.         o1.name,
  14116.         c1.name,
  14117.         convert(binary(16),null),
  14118.         convert(int,null),
  14119.         db_name(r.fkeydbid),
  14120.         user_name(o2.uid),
  14121.         o2.name,
  14122.         c2.name,
  14123.         convert(binary(16),null),
  14124.         convert(int,null),
  14125.         convert(int,11),
  14126.         'NO ACTION',
  14127.         'NO ACTION',
  14128.         object_name(s.constid),
  14129.         object_name(r.constid),
  14130.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14131.     from
  14132.         sysobjects o1, sysobjects o2,
  14133.         syscolumns c1, syscolumns c2,
  14134.         sysreferences r, sysconstraints s
  14135.     where    
  14136.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14137.     and    o1.name = @pk_table_name
  14138.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14139.     and    o1.id = r.rkeyid
  14140.     and s.id = o1.id and (s.status & 0xf) = 1
  14141.     and    r.keycnt >= 11
  14142.     and    o1.id = c1.id
  14143.     and    c1.colid = r.rkey11
  14144.     and r.fkeyid = o2.id
  14145.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14146.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14147.     and    o2.id = c2.id    
  14148.     and    c2.colid = r.fkey11
  14149.     union all
  14150.     select
  14151.         db_name(r.rkeydbid),
  14152.         user_name(o1.uid),
  14153.         o1.name,
  14154.         c1.name,
  14155.         convert(binary(16),null),
  14156.         convert(int,null),
  14157.         db_name(r.fkeydbid),
  14158.         user_name(o2.uid),
  14159.         o2.name,
  14160.         c2.name,
  14161.         convert(binary(16),null),
  14162.         convert(int,null),
  14163.         convert(int,12),
  14164.         'NO ACTION',
  14165.         'NO ACTION',
  14166.         object_name(s.constid),
  14167.         object_name(r.constid),
  14168.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14169.     from
  14170.         sysobjects o1, sysobjects o2,
  14171.         syscolumns c1, syscolumns c2,
  14172.         sysreferences r, sysconstraints s
  14173.     where    
  14174.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14175.     and    o1.name = @pk_table_name
  14176.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14177.     and    o1.id = r.rkeyid
  14178.     and s.id = o1.id and (s.status & 0xf) = 1
  14179.     and    r.keycnt >= 12
  14180.     and    o1.id = c1.id
  14181.     and    c1.colid = r.rkey12
  14182.     and r.fkeyid = o2.id
  14183.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14184.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14185.     and    o2.id = c2.id    
  14186.     and    c2.colid = r.fkey12
  14187.     union all
  14188.     select
  14189.         db_name(r.rkeydbid),
  14190.         user_name(o1.uid),
  14191.         o1.name,
  14192.         c1.name,
  14193.         convert(binary(16),null),
  14194.         convert(int,null),
  14195.         db_name(r.fkeydbid),
  14196.         user_name(o2.uid),
  14197.         o2.name,
  14198.         c2.name,
  14199.         convert(binary(16),null),
  14200.         convert(int,null),
  14201.         convert(int,13),
  14202.         'NO ACTION',
  14203.         'NO ACTION',
  14204.         object_name(s.constid),
  14205.         object_name(r.constid),
  14206.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14207.     from
  14208.         sysobjects o1, sysobjects o2,
  14209.         syscolumns c1, syscolumns c2,
  14210.         sysreferences r, sysconstraints s
  14211.     where    
  14212.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14213.     and    o1.name = @pk_table_name
  14214.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14215.     and    o1.id = r.rkeyid
  14216.     and s.id = o1.id and (s.status & 0xf) = 1
  14217.     and    r.keycnt >= 13
  14218.     and    o1.id = c1.id
  14219.     and    c1.colid = r.rkey13
  14220.     and r.fkeyid = o2.id
  14221.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14222.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14223.     and    o2.id = c2.id    
  14224.     and    c2.colid = r.fkey13
  14225.     union all
  14226.     select
  14227.         db_name(r.rkeydbid),
  14228.         user_name(o1.uid),
  14229.         o1.name,
  14230.         c1.name,
  14231.         convert(binary(16),null),
  14232.         convert(int,null),
  14233.         db_name(r.fkeydbid),
  14234.         user_name(o2.uid),
  14235.         o2.name,
  14236.         c2.name,
  14237.         convert(binary(16),null),
  14238.         convert(int,null),
  14239.         convert(int,14),
  14240.         'NO ACTION',
  14241.         'NO ACTION',
  14242.         object_name(s.constid),
  14243.         object_name(r.constid),
  14244.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14245.     from
  14246.         sysobjects o1, sysobjects o2,
  14247.         syscolumns c1, syscolumns c2,
  14248.         sysreferences r, sysconstraints s
  14249.     where    
  14250.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14251.     and    o1.name = @pk_table_name
  14252.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14253.     and    o1.id = r.rkeyid
  14254.     and s.id = o1.id and (s.status & 0xf) = 1
  14255.     and    r.keycnt >= 14
  14256.     and    o1.id = c1.id
  14257.     and    c1.colid = r.rkey14
  14258.     and r.fkeyid = o2.id
  14259.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14260.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14261.     and    o2.id = c2.id    
  14262.     and    c2.colid = r.fkey14
  14263.     union all
  14264.     select
  14265.         db_name(r.rkeydbid),
  14266.         user_name(o1.uid),
  14267.         o1.name,
  14268.         c1.name,
  14269.         convert(binary(16),null),
  14270.         convert(int,null),
  14271.         db_name(r.fkeydbid),
  14272.         user_name(o2.uid),
  14273.         o2.name,
  14274.         c2.name,
  14275.         convert(binary(16),null),
  14276.         convert(int,null),
  14277.         convert(int,15),
  14278.         'NO ACTION',
  14279.         'NO ACTION',
  14280.         object_name(s.constid),
  14281.         object_name(r.constid),
  14282.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14283.     from
  14284.         sysobjects o1, sysobjects o2,
  14285.         syscolumns c1, syscolumns c2,
  14286.         sysreferences r, sysconstraints s
  14287.     where    
  14288.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14289.     and    o1.name = @pk_table_name
  14290.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14291.     and    o1.id = r.rkeyid
  14292.     and s.id = o1.id and (s.status & 0xf) = 1
  14293.     and    r.keycnt >= 15
  14294.     and    o1.id = c1.id
  14295.     and    c1.colid = r.rkey15
  14296.     and r.fkeyid = o2.id
  14297.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14298.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14299.     and    o2.id = c2.id    
  14300.     and    c2.colid = r.fkey15
  14301.     union all
  14302.     select
  14303.         db_name(r.rkeydbid),
  14304.         user_name(o1.uid),
  14305.         o1.name,
  14306.         c1.name,
  14307.         convert(binary(16),null),
  14308.         convert(int,null),
  14309.         db_name(r.fkeydbid),
  14310.         user_name(o2.uid),
  14311.         o2.name,
  14312.         c2.name,
  14313.         convert(binary(16),null),
  14314.         convert(int,null),
  14315.         convert(int,16),
  14316.         'NO ACTION',
  14317.         'NO ACTION',
  14318.         object_name(s.constid),
  14319.         object_name(r.constid),
  14320.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14321.     from
  14322.         sysobjects o1, sysobjects o2,
  14323.         syscolumns c1, syscolumns c2,
  14324.         sysreferences r, sysconstraints s
  14325.     where    
  14326.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14327.     and    o1.name = @pk_table_name
  14328.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14329.     and    o1.id = r.rkeyid
  14330.     and s.id = o1.id and (s.status & 0xf) = 1
  14331.     and    r.keycnt >= 16
  14332.     and    o1.id = c1.id
  14333.     and    c1.colid = r.rkey16
  14334.     and r.fkeyid = o2.id
  14335.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14336.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14337.     and    o2.id = c2.id    
  14338.     and    c2.colid = r.fkey16
  14339.     order by 8,9,2,3,13
  14340.     END
  14341. ELSE IF @fk_table_name is not null
  14342.     BEGIN
  14343.     insert into #spfkeysrowset1
  14344.     select
  14345.         db_name(r.rkeydbid),
  14346.         user_name(o1.uid),
  14347.         o1.name,
  14348.         c1.name,
  14349.         convert(binary(16),null),
  14350.         convert(int,null),
  14351.         db_name(r.fkeydbid),
  14352.         user_name(o2.uid),
  14353.         o2.name,
  14354.         c2.name,
  14355.         convert(binary(16),null),
  14356.         convert(int,null),
  14357.         convert(int,1),
  14358.         'NO ACTION',
  14359.         'NO ACTION',
  14360.         object_name(s.constid),
  14361.         object_name(r.constid),
  14362.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14363.     from
  14364.         sysobjects o1, sysobjects o2,
  14365.         syscolumns c1, syscolumns c2,
  14366.         sysreferences r, sysconstraints s
  14367.     where    
  14368.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14369.     and    o2.name = @fk_table_name
  14370.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14371.     and    o2.id = r.fkeyid
  14372.     and    o2.id = c2.id
  14373.     and    c2.colid = r.fkey1
  14374.     and r.rkeyid = o1.id
  14375.     and s.id = o1.id and (s.status & 0xf) = 1
  14376.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14377.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14378.     and    o1.id = c1.id    
  14379.     and    c1.colid = r.rkey1
  14380.     union    all
  14381.     select
  14382.         db_name(r.rkeydbid),
  14383.         user_name(o1.uid),
  14384.         o1.name,
  14385.         c1.name,
  14386.         convert(binary(16),null),
  14387.         convert(int,null),
  14388.         db_name(r.fkeydbid),
  14389.         user_name(o2.uid),
  14390.         o2.name,
  14391.         c2.name,
  14392.         convert(binary(16),null),
  14393.         convert(int,null),
  14394.         convert(int,2),
  14395.         'NO ACTION',
  14396.         'NO ACTION',
  14397.         object_name(s.constid),
  14398.         object_name(r.constid),
  14399.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14400.     from
  14401.         sysobjects o1, sysobjects o2,
  14402.         syscolumns c1, syscolumns c2,
  14403.         sysreferences r, sysconstraints s
  14404.     where    
  14405.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14406.     and    o2.name = @fk_table_name
  14407.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14408.     and    o2.id = r.fkeyid
  14409.     and    r.keycnt >= 2
  14410.     and    o2.id = c2.id
  14411.     and    c2.colid = r.fkey2
  14412.     and r.rkeyid = o1.id
  14413.     and s.id = o1.id and (s.status & 0xf) = 1
  14414.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14415.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14416.     and    o1.id = c1.id    
  14417.     and    c1.colid = r.rkey2
  14418.     union    all
  14419.     select
  14420.         db_name(r.rkeydbid),
  14421.         user_name(o1.uid),
  14422.         o1.name,
  14423.         c1.name,
  14424.         convert(binary(16),null),
  14425.         convert(int,null),
  14426.         db_name(r.fkeydbid),
  14427.         user_name(o2.uid),
  14428.         o2.name,
  14429.         c2.name,
  14430.         convert(binary(16),null),
  14431.         convert(int,null),
  14432.         convert(int,3),
  14433.         'NO ACTION',
  14434.         'NO ACTION',
  14435.         object_name(s.constid),
  14436.         object_name(r.constid),
  14437.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14438.     from
  14439.         sysobjects o1, sysobjects o2,
  14440.         syscolumns c1, syscolumns c2,
  14441.         sysreferences r, sysconstraints s
  14442.     where    
  14443.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14444.     and    o2.name = @fk_table_name
  14445.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14446.     and    o2.id = r.fkeyid
  14447.     and    r.keycnt >= 3
  14448.     and    o2.id = c2.id
  14449.     and    c2.colid = r.fkey3
  14450.     and    r.rkeyid = o1.id
  14451.     and s.id = o1.id and (s.status & 0xf) = 1
  14452.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14453.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14454.     and    o1.id = c1.id    
  14455.     and    c1.colid = r.rkey3
  14456.     union    all
  14457.     select
  14458.         db_name(r.rkeydbid),
  14459.         user_name(o1.uid),
  14460.         o1.name,
  14461.         c1.name,
  14462.         convert(binary(16),null),
  14463.         convert(int,null),
  14464.         db_name(r.fkeydbid),
  14465.         user_name(o2.uid),
  14466.         o2.name,
  14467.         c2.name,
  14468.         convert(binary(16),null),
  14469.         convert(int,null),
  14470.         convert(int,4),
  14471.         'NO ACTION',
  14472.         'NO ACTION',
  14473.         object_name(s.constid),
  14474.         object_name(r.constid),
  14475.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14476.     from
  14477.         sysobjects o1, sysobjects o2,
  14478.         syscolumns c1, syscolumns c2,
  14479.         sysreferences r, sysconstraints s
  14480.     where    
  14481.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14482.     and    o2.name = @fk_table_name
  14483.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14484.     and    o2.id = r.fkeyid
  14485.     and    r.keycnt >= 4
  14486.     and    o2.id = c2.id
  14487.     and    c2.colid = r.fkey4
  14488.     and r.rkeyid = o1.id
  14489.     and s.id = o1.id and (s.status & 0xf) = 1
  14490.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14491.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14492.     and    o1.id = c1.id    
  14493.     and    c1.colid = r.rkey4
  14494.     union    all
  14495.     select
  14496.         db_name(r.rkeydbid),
  14497.         user_name(o1.uid),
  14498.         o1.name,
  14499.         c1.name,
  14500.         convert(binary(16),null),
  14501.         convert(int,null),
  14502.         db_name(r.fkeydbid),
  14503.         user_name(o2.uid),
  14504.         o2.name,
  14505.         c2.name,
  14506.         convert(binary(16),null),
  14507.         convert(int,null),
  14508.         convert(int,5),
  14509.         'NO ACTION',
  14510.         'NO ACTION',
  14511.         object_name(s.constid),
  14512.         object_name(r.constid),
  14513.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14514.     from
  14515.         sysobjects o1, sysobjects o2,
  14516.         syscolumns c1, syscolumns c2,
  14517.         sysreferences r, sysconstraints s
  14518.     where    
  14519.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14520.     and    o2.name = @fk_table_name
  14521.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14522.     and    o2.id = r.fkeyid
  14523.     and    r.keycnt >= 5
  14524.     and    o2.id = c2.id
  14525.     and    c2.colid = r.fkey5
  14526.     and r.rkeyid = o1.id
  14527.     and s.id = o1.id and (s.status & 0xf) = 1
  14528.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14529.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14530.     and    o1.id = c1.id    
  14531.     and    c1.colid = r.rkey5
  14532.     union    all
  14533.     select
  14534.         db_name(r.rkeydbid),
  14535.         user_name(o1.uid),
  14536.         o1.name,
  14537.         c1.name,
  14538.         convert(binary(16),null),
  14539.         convert(int,null),
  14540.         db_name(r.fkeydbid),
  14541.         user_name(o2.uid),
  14542.         o2.name,
  14543.         c2.name,
  14544.         convert(binary(16),null),
  14545.         convert(int,null),
  14546.         convert(int,6),
  14547.         'NO ACTION',
  14548.         'NO ACTION',
  14549.         object_name(s.constid),
  14550.         object_name(r.constid),
  14551.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14552.     from
  14553.         sysobjects o1, sysobjects o2,
  14554.         syscolumns c1, syscolumns c2,
  14555.         sysreferences r, sysconstraints s
  14556.     where    
  14557.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14558.     and    o2.name = @fk_table_name
  14559.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14560.     and    o2.id = r.fkeyid
  14561.     and    r.keycnt >= 6
  14562.     and    o2.id = c2.id
  14563.     and    c2.colid = r.fkey6
  14564.     and r.rkeyid = o1.id
  14565.     and s.id = o1.id and (s.status & 0xf) = 1
  14566.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14567.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14568.     and    o1.id = c1.id    
  14569.     and    c1.colid = r.rkey6
  14570.     union    all
  14571.     select
  14572.         db_name(r.rkeydbid),
  14573.         user_name(o1.uid),
  14574.         o1.name,
  14575.         c1.name,
  14576.         convert(binary(16),null),
  14577.         convert(int,null),
  14578.         db_name(r.fkeydbid),
  14579.         user_name(o2.uid),
  14580.         o2.name,
  14581.         c2.name,
  14582.         convert(binary(16),null),
  14583.         convert(int,null),
  14584.         convert(int,7),
  14585.         'NO ACTION',
  14586.         'NO ACTION',
  14587.         object_name(s.constid),
  14588.         object_name(r.constid),
  14589.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14590.     from
  14591.         sysobjects o1, sysobjects o2,
  14592.         syscolumns c1, syscolumns c2,
  14593.         sysreferences r, sysconstraints s
  14594.     where    
  14595.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14596.     and    o2.name = @fk_table_name
  14597.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14598.     and    o2.id = r.fkeyid
  14599.     and    r.keycnt >= 7
  14600.     and    o2.id = c2.id
  14601.     and    c2.colid = r.fkey7
  14602.     and r.rkeyid = o1.id
  14603.     and s.id = o1.id and (s.status & 0xf) = 1
  14604.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14605.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14606.     and    o1.id = c1.id    
  14607.     and    c1.colid = r.rkey7
  14608.     union    all
  14609.     select
  14610.         db_name(r.rkeydbid),
  14611.         user_name(o1.uid),
  14612.         o1.name,
  14613.         c1.name,
  14614.         convert(binary(16),null),
  14615.         convert(int,null),
  14616.         db_name(r.fkeydbid),
  14617.         user_name(o2.uid),
  14618.         o2.name,
  14619.         c2.name,
  14620.         convert(binary(16),null),
  14621.         convert(int,null),
  14622.         convert(int,8),
  14623.         'NO ACTION',
  14624.         'NO ACTION',
  14625.         object_name(s.constid),
  14626.         object_name(r.constid),
  14627.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14628.     from
  14629.         sysobjects o1, sysobjects o2,
  14630.         syscolumns c1, syscolumns c2,
  14631.         sysreferences r, sysconstraints s
  14632.     where    
  14633.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14634.     and    o2.name = @fk_table_name
  14635.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14636.     and    o2.id = r.fkeyid
  14637.     and    r.keycnt >= 8
  14638.     and    o2.id = c2.id
  14639.     and    c2.colid = r.fkey8
  14640.     and r.rkeyid = o1.id
  14641.     and s.id = o1.id and (s.status & 0xf) = 1
  14642.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14643.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14644.     and    o1.id = c1.id    
  14645.     and    c1.colid = r.rkey8
  14646.     union    all
  14647.     select
  14648.         db_name(r.rkeydbid),
  14649.         user_name(o1.uid),
  14650.         o1.name,
  14651.         c1.name,
  14652.         convert(binary(16),null),
  14653.         convert(int,null),
  14654.         db_name(r.fkeydbid),
  14655.         user_name(o2.uid),
  14656.         o2.name,
  14657.         c2.name,
  14658.         convert(binary(16),null),
  14659.         convert(int,null),
  14660.         convert(int,9),
  14661.         'NO ACTION',
  14662.         'NO ACTION',
  14663.         object_name(s.constid),
  14664.         object_name(r.constid),
  14665.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14666.     from
  14667.         sysobjects o1, sysobjects o2,
  14668.         syscolumns c1, syscolumns c2,
  14669.         sysreferences r, sysconstraints s
  14670.     where    
  14671.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14672.     and    o2.name = @fk_table_name
  14673.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14674.     and    o2.id = r.fkeyid
  14675.     and    r.keycnt >= 9
  14676.     and    o2.id = c2.id
  14677.     and    c2.colid = r.fkey9
  14678.     and r.rkeyid = o1.id
  14679.     and s.id = o1.id and (s.status & 0xf) = 1
  14680.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14681.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14682.     and    o1.id = c1.id    
  14683.     and    c1.colid = r.rkey9
  14684.     union    all
  14685.     select
  14686.         db_name(r.rkeydbid),
  14687.         user_name(o1.uid),
  14688.         o1.name,
  14689.         c1.name,
  14690.         convert(binary(16),null),
  14691.         convert(int,null),
  14692.         db_name(r.fkeydbid),
  14693.         user_name(o2.uid),
  14694.         o2.name,
  14695.         c2.name,
  14696.         convert(binary(16),null),
  14697.         convert(int,null),
  14698.         convert(int,10),
  14699.         'NO ACTION',
  14700.         'NO ACTION',
  14701.         object_name(s.constid),
  14702.         object_name(r.constid),
  14703.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14704.     from
  14705.         sysobjects o1, sysobjects o2,
  14706.         syscolumns c1, syscolumns c2,
  14707.         sysreferences r, sysconstraints s
  14708.     where    
  14709.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14710.     and    o2.name = @fk_table_name
  14711.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14712.     and    o2.id = r.fkeyid
  14713.     and    r.keycnt >= 10
  14714.     and    o2.id = c2.id
  14715.     and    c2.colid = r.fkey10
  14716.     and r.rkeyid = o1.id
  14717.     and s.id = o1.id and (s.status & 0xf) = 1
  14718.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14719.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14720.     and    o1.id = c1.id    
  14721.     and    c1.colid = r.rkey10
  14722.     union    all
  14723.     select
  14724.         db_name(r.rkeydbid),
  14725.         user_name(o1.uid),
  14726.         o1.name,
  14727.         c1.name,
  14728.         convert(binary(16),null),
  14729.         convert(int,null),
  14730.         db_name(r.fkeydbid),
  14731.         user_name(o2.uid),
  14732.         o2.name,
  14733.         c2.name,
  14734.         convert(binary(16),null),
  14735.         convert(int,null),
  14736.         convert(int,11),
  14737.         'NO ACTION',
  14738.         'NO ACTION',
  14739.         object_name(s.constid),
  14740.         object_name(r.constid),
  14741.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14742.     from
  14743.         sysobjects o1, sysobjects o2,
  14744.         syscolumns c1, syscolumns c2,
  14745.         sysreferences r, sysconstraints s
  14746.     where    
  14747.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14748.     and    o2.name = @fk_table_name
  14749.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14750.     and    o2.id = r.fkeyid
  14751.     and    r.keycnt >= 11
  14752.     and    o2.id = c2.id
  14753.     and    c2.colid = r.fkey11
  14754.     and r.rkeyid = o1.id
  14755.     and s.id = o1.id and (s.status & 0xf) = 1
  14756.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14757.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14758.     and    o1.id = c1.id    
  14759.     and    c1.colid = r.rkey11
  14760.     union    all
  14761.     select
  14762.         db_name(r.rkeydbid),
  14763.         user_name(o1.uid),
  14764.         o1.name,
  14765.         c1.name,
  14766.         convert(binary(16),null),
  14767.         convert(int,null),
  14768.         db_name(r.fkeydbid),
  14769.         user_name(o2.uid),
  14770.         o2.name,
  14771.         c2.name,
  14772.         convert(binary(16),null),
  14773.         convert(int,null),
  14774.         convert(int,12),
  14775.         'NO ACTION',
  14776.         'NO ACTION',
  14777.         object_name(s.constid),
  14778.         object_name(r.constid),
  14779.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  14780.     from
  14781.         sysobjects o1, sysobjects o2,
  14782.         syscolumns c1, syscolumns c2,
  14783.         sysreferences r, sysconstraints s
  14784.     where    
  14785.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14786.     and    o2.name = @fk_table_name
  14787.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14788.     and    o2.id = r.fkeyid
  14789.     and    r.keycnt >= 12
  14790.     and    o2.id = c2.id
  14791.     and    c2.colid = r.fkey12
  14792.     and r.rkeyid = o1.id
  14793.     and s.id = o1.id and (s.status & 0xf) = 1
  14794.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14795.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14796.     and    o1.id = c1.id    
  14797.     and    c1.colid = r.rkey12
  14798.     union    all
  14799.     select
  14800.         db_name(r.rkeydbid),
  14801.         user_name(o1.uid),
  14802.         o1.name,
  14803.         c1.name,
  14804.         convert(binary(16),null),
  14805.         convert(int,null),
  14806.         db_name(r.fkeydbid),
  14807.         user_name(o2.uid),
  14808.         o2.name,
  14809.         c2.name,
  14810.         convert(binary(16),null),
  14811.         convert(int,null),
  14812.         convert(int,13),
  14813.         'NO ACTION',
  14814.         'NO ACTION',
  14815.         object_name(s.constid),
  14816.         object_name(r.constid),
  14817.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14818.     from
  14819.         sysobjects o1, sysobjects o2,
  14820.         syscolumns c1, syscolumns c2,
  14821.         sysreferences r, sysconstraints s
  14822.     where    
  14823.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14824.     and    o2.name = @fk_table_name
  14825.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14826.     and    o2.id = r.fkeyid
  14827.     and    r.keycnt >= 13
  14828.     and    o2.id = c2.id
  14829.     and    c2.colid = r.fkey13
  14830.     and r.rkeyid = o1.id
  14831.     and s.id = o1.id and (s.status & 0xf) = 1
  14832.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14833.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14834.     and    o1.id = c1.id    
  14835.     and    c1.colid = r.rkey13
  14836.     union    all
  14837.     select
  14838.         db_name(r.rkeydbid),
  14839.         user_name(o1.uid),
  14840.         o1.name,
  14841.         c1.name,
  14842.         convert(binary(16),null),
  14843.         convert(int,null),
  14844.         db_name(r.fkeydbid),
  14845.         user_name(o2.uid),
  14846.         o2.name,
  14847.         c2.name,
  14848.         convert(binary(16),null),
  14849.         convert(int,null),
  14850.         convert(int,14),
  14851.         'NO ACTION',
  14852.         'NO ACTION',
  14853.         object_name(s.constid),
  14854.         object_name(r.constid),
  14855.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14856.     from
  14857.         sysobjects o1, sysobjects o2,
  14858.         syscolumns c1, syscolumns c2,
  14859.         sysreferences r, sysconstraints s
  14860.     where    
  14861.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14862.     and    o2.name = @fk_table_name
  14863.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14864.     and    o2.id = r.fkeyid
  14865.     and    r.keycnt >= 14
  14866.     and    o2.id = c2.id
  14867.     and    c2.colid = r.fkey14
  14868.     and r.rkeyid = o1.id
  14869.     and s.id = o1.id and (s.status & 0xf) = 1
  14870.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14871.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14872.     and    o1.id = c1.id    
  14873.     and    c1.colid = r.rkey14
  14874.     union    all
  14875.     select
  14876.         db_name(r.rkeydbid),
  14877.         user_name(o1.uid),
  14878.         o1.name,
  14879.         c1.name,
  14880.         convert(binary(16),null),
  14881.         convert(int,null),
  14882.         db_name(r.fkeydbid),
  14883.         user_name(o2.uid),
  14884.         o2.name,
  14885.         c2.name,
  14886.         convert(binary(16),null),
  14887.         convert(int,null),
  14888.         convert(int,15),
  14889.         'NO ACTION',
  14890.         'NO ACTION',
  14891.         object_name(s.constid),
  14892.         object_name(r.constid),
  14893.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14894.     from
  14895.         sysobjects o1, sysobjects o2,
  14896.         syscolumns c1, syscolumns c2,
  14897.         sysreferences r, sysconstraints s
  14898.     where    
  14899.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14900.     and    o2.name = @fk_table_name
  14901.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14902.     and    o2.id = r.fkeyid
  14903.     and    r.keycnt >= 15
  14904.     and    o2.id = c2.id
  14905.     and    c2.colid = r.fkey15
  14906.     and r.rkeyid = o1.id
  14907.     and s.id = o1.id and (s.status & 0xf) = 1
  14908.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14909.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14910.     and    o1.id = c1.id    
  14911.     and    c1.colid = r.rkey15
  14912.     union    all
  14913.     select
  14914.         db_name(r.rkeydbid),
  14915.         user_name(o1.uid),
  14916.         o1.name,
  14917.         c1.name,
  14918.         convert(binary(16),null),
  14919.         convert(int,null),
  14920.         db_name(r.fkeydbid),
  14921.         user_name(o2.uid),
  14922.         o2.name,
  14923.         c2.name,
  14924.         convert(binary(16),null),
  14925.         convert(int,null),
  14926.         convert(int,16),
  14927.         'NO ACTION',
  14928.         'NO ACTION',
  14929.         object_name(s.constid),
  14930.         object_name(r.constid),
  14931.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14932.     from
  14933.         sysobjects o1, sysobjects o2,
  14934.         syscolumns c1, syscolumns c2,
  14935.         sysreferences r, sysconstraints s
  14936.     where    
  14937.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14938.     and    o2.name = @fk_table_name
  14939.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14940.     and    o2.id = r.fkeyid
  14941.     and    r.keycnt >= 16
  14942.     and    o2.id = c2.id
  14943.     and    c2.colid = r.fkey16
  14944.     and r.rkeyid = o1.id
  14945.     and s.id = o1.id and (s.status & 0xf) = 1
  14946.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14947.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14948.     and    o1.id = c1.id    
  14949.     and    c1.colid = r.rkey16
  14950.     order by 8,9,2,3,13
  14951.     END
  14952. ELSE
  14953.     BEGIN
  14954.     insert into #spfkeysrowset1
  14955.     select
  14956.         db_name(r.rkeydbid),
  14957.         user_name(o1.uid),
  14958.         o1.name,
  14959.         c1.name,
  14960.         convert(binary(16),null),
  14961.         convert(int,null),
  14962.         db_name(r.fkeydbid),
  14963.         user_name(o2.uid),
  14964.         o2.name,
  14965.         c2.name,
  14966.         convert(binary(16),null),
  14967.         convert(int,null),
  14968.         convert(int,1),
  14969.         'NO ACTION',
  14970.         'NO ACTION',
  14971.         object_name(s.constid),
  14972.         object_name(r.constid),
  14973.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14974.     from
  14975.         sysobjects o1, sysobjects o2,
  14976.         syscolumns c1, syscolumns c2,
  14977.         sysreferences r, sysconstraints s
  14978.     where    
  14979.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14980.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14981.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14982.     and    o1.id = r.rkeyid
  14983.     and s.id = o1.id and (s.status & 0xf) = 1
  14984.     and    o1.id = c1.id
  14985.     and    c1.colid = r.rkey1
  14986.     and r.fkeyid = o2.id
  14987.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14988.     and    o2.id = c2.id    
  14989.     and    c2.colid = r.fkey1
  14990.     union all
  14991.     select
  14992.         db_name(r.rkeydbid),
  14993.         user_name(o1.uid),
  14994.         o1.name,
  14995.         c1.name,
  14996.         convert(binary(16),null),
  14997.         convert(int,null),
  14998.         db_name(r.fkeydbid),
  14999.         user_name(o2.uid),
  15000.         o2.name,
  15001.         c2.name,
  15002.         convert(binary(16),null),
  15003.         convert(int,null),
  15004.         convert(int,2),
  15005.         'NO ACTION',
  15006.         'NO ACTION',
  15007.         object_name(s.constid),
  15008.         object_name(r.constid),
  15009.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15010.     from
  15011.         sysobjects o1, sysobjects o2,
  15012.         syscolumns c1, syscolumns c2,
  15013.         sysreferences r, sysconstraints s
  15014.     where    
  15015.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15016.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15017.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15018.     and    o1.id = r.rkeyid
  15019.     and s.id = o1.id and (s.status & 0xf) = 1
  15020.     and    r.keycnt >= 2
  15021.     and    o1.id = c1.id
  15022.     and    c1.colid = r.rkey2
  15023.     and r.fkeyid = o2.id
  15024.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15025.     and    o2.id = c2.id    
  15026.     and    c2.colid = r.fkey2
  15027.     union all
  15028.     select
  15029.         db_name(r.rkeydbid),
  15030.         user_name(o1.uid),
  15031.         o1.name,
  15032.         c1.name,
  15033.         convert(binary(16),null),
  15034.         convert(int,null),
  15035.         db_name(r.fkeydbid),
  15036.         user_name(o2.uid),
  15037.         o2.name,
  15038.         c2.name,
  15039.         convert(binary(16),null),
  15040.         convert(int,null),
  15041.         convert(int,3),
  15042.         'NO ACTION',
  15043.         'NO ACTION',
  15044.         object_name(s.constid),
  15045.         object_name(r.constid),
  15046.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15047.     from
  15048.         sysobjects o1, sysobjects o2,
  15049.         syscolumns c1, syscolumns c2,
  15050.         sysreferences r, sysconstraints s
  15051.     where    
  15052.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15053.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15054.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15055.     and    o1.id = r.rkeyid
  15056.     and s.id = o1.id and (s.status & 0xf) = 1
  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.     union all
  15065.     select
  15066.         db_name(r.rkeydbid),
  15067.         user_name(o1.uid),
  15068.         o1.name,
  15069.         c1.name,
  15070.         convert(binary(16),null),
  15071.         convert(int,null),
  15072.         db_name(r.fkeydbid),
  15073.         user_name(o2.uid),
  15074.         o2.name,
  15075.         c2.name,
  15076.         convert(binary(16),null),
  15077.         convert(int,null),
  15078.         convert(int,4),
  15079.         'NO ACTION',
  15080.         'NO ACTION',
  15081.         object_name(s.constid),
  15082.         object_name(r.constid),
  15083.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15084.     from
  15085.         sysobjects o1, sysobjects o2,
  15086.         syscolumns c1, syscolumns c2,
  15087.         sysreferences r, sysconstraints s
  15088.     where    
  15089.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15090.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15091.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15092.     and    o1.id = r.rkeyid
  15093.     and s.id = o1.id and (s.status & 0xf) = 1
  15094.     and    r.keycnt >= 4
  15095.     and    o1.id = c1.id
  15096.     and    c1.colid = r.rkey4
  15097.     and r.fkeyid = o2.id
  15098.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15099.     and    o2.id = c2.id    
  15100.     and    c2.colid = r.fkey4
  15101.     union all
  15102.     select
  15103.         db_name(r.rkeydbid),
  15104.         user_name(o1.uid),
  15105.         o1.name,
  15106.         c1.name,
  15107.         convert(binary(16),null),
  15108.         convert(int,null),
  15109.         db_name(r.fkeydbid),
  15110.         user_name(o2.uid),
  15111.         o2.name,
  15112.         c2.name,
  15113.         convert(binary(16),null),
  15114.         convert(int,null),
  15115.         convert(int,5),
  15116.         'NO ACTION',
  15117.         'NO ACTION',
  15118.         object_name(s.constid),
  15119.         object_name(r.constid),
  15120.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15121.     from
  15122.         sysobjects o1, sysobjects o2,
  15123.         syscolumns c1, syscolumns c2,
  15124.         sysreferences r, sysconstraints s
  15125.     where    
  15126.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15127.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15128.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15129.     and    o1.id = r.rkeyid
  15130.     and s.id = o1.id and (s.status & 0xf) = 1
  15131.     and    r.keycnt >= 5
  15132.     and    o1.id = c1.id
  15133.     and    c1.colid = r.rkey5
  15134.     and r.fkeyid = o2.id
  15135.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15136.     and    o2.id = c2.id    
  15137.     and    c2.colid = r.fkey5
  15138.     union all
  15139.     select
  15140.         db_name(r.rkeydbid),
  15141.         user_name(o1.uid),
  15142.         o1.name,
  15143.         c1.name,
  15144.         convert(binary(16),null),
  15145.         convert(int,null),
  15146.         db_name(r.fkeydbid),
  15147.         user_name(o2.uid),
  15148.         o2.name,
  15149.         c2.name,
  15150.         convert(binary(16),null),
  15151.         convert(int,null),
  15152.         convert(int,6),
  15153.         'NO ACTION',
  15154.         'NO ACTION',
  15155.         object_name(s.constid),
  15156.         object_name(r.constid),
  15157.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15158.     from
  15159.         sysobjects o1, sysobjects o2,
  15160.         syscolumns c1, syscolumns c2,
  15161.         sysreferences r, sysconstraints s
  15162.     where    
  15163.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15164.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15165.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15166.     and    o1.id = r.rkeyid
  15167.     and s.id = o1.id and (s.status & 0xf) = 1
  15168.     and    r.keycnt >= 6
  15169.     and    o1.id = c1.id
  15170.     and    c1.colid = r.rkey6
  15171.     and r.fkeyid = o2.id
  15172.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15173.     and    o2.id = c2.id    
  15174.     and    c2.colid = r.fkey6
  15175.     union all
  15176.     select
  15177.         db_name(r.rkeydbid),
  15178.         user_name(o1.uid),
  15179.         o1.name,
  15180.         c1.name,
  15181.         convert(binary(16),null),
  15182.         convert(int,null),
  15183.         db_name(r.fkeydbid),
  15184.         user_name(o2.uid),
  15185.         o2.name,
  15186.         c2.name,
  15187.         convert(binary(16),null),
  15188.         convert(int,null),
  15189.         convert(int,7),
  15190.         'NO ACTION',
  15191.         'NO ACTION',
  15192.         object_name(s.constid),
  15193.         object_name(r.constid),
  15194.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15195.     from
  15196.         sysobjects o1, sysobjects o2,
  15197.         syscolumns c1, syscolumns c2,
  15198.         sysreferences r, sysconstraints s
  15199.     where    
  15200.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15201.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15202.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15203.     and    o1.id = r.rkeyid
  15204.     and s.id = o1.id and (s.status & 0xf) = 1
  15205.     and    r.keycnt >= 7
  15206.     and    o1.id = c1.id
  15207.     and    c1.colid = r.rkey7
  15208.     and r.fkeyid = o2.id
  15209.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15210.     and    o2.id = c2.id    
  15211.     and    c2.colid = r.fkey7
  15212.     union all
  15213.     select
  15214.         db_name(r.rkeydbid),
  15215.         user_name(o1.uid),
  15216.         o1.name,
  15217.         c1.name,
  15218.         convert(binary(16),null),
  15219.         convert(int,null),
  15220.         db_name(r.fkeydbid),
  15221.         user_name(o2.uid),
  15222.         o2.name,
  15223.         c2.name,
  15224.         convert(binary(16),null),
  15225.         convert(int,null),
  15226.         convert(int,8),
  15227.         'NO ACTION',
  15228.         'NO ACTION',
  15229.         object_name(s.constid),
  15230.         object_name(r.constid),
  15231.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15232.     from
  15233.         sysobjects o1, sysobjects o2,
  15234.         syscolumns c1, syscolumns c2,
  15235.         sysreferences r, sysconstraints s
  15236.     where    
  15237.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15238.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15239.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15240.     and    o1.id = r.rkeyid
  15241.     and s.id = o1.id and (s.status & 0xf) = 1
  15242.     and    r.keycnt >= 8
  15243.     and    o1.id = c1.id
  15244.     and    c1.colid = r.rkey8
  15245.     and r.fkeyid = o2.id
  15246.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15247.     and    o2.id = c2.id    
  15248.     and    c2.colid = r.fkey8
  15249.     union all
  15250.     select
  15251.         db_name(r.rkeydbid),
  15252.         user_name(o1.uid),
  15253.         o1.name,
  15254.         c1.name,
  15255.         convert(binary(16),null),
  15256.         convert(int,null),
  15257.         db_name(r.fkeydbid),
  15258.         user_name(o2.uid),
  15259.         o2.name,
  15260.         c2.name,
  15261.         convert(binary(16),null),
  15262.         convert(int,null),
  15263.         convert(int,9),
  15264.         'NO ACTION',
  15265.         'NO ACTION',
  15266.         object_name(s.constid),
  15267.         object_name(r.constid),
  15268.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15269.     from
  15270.         sysobjects o1, sysobjects o2,
  15271.         syscolumns c1, syscolumns c2,
  15272.         sysreferences r, sysconstraints s
  15273.     where    
  15274.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15275.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15276.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15277.     and    o1.id = r.rkeyid
  15278.     and s.id = o1.id and (s.status & 0xf) = 1
  15279.     and    r.keycnt >= 9
  15280.     and    o1.id = c1.id
  15281.     and    c1.colid = r.rkey9
  15282.     and r.fkeyid = o2.id
  15283.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15284.     and    o2.id = c2.id    
  15285.     and    c2.colid = r.fkey9
  15286.     union all
  15287.     select
  15288.         db_name(r.rkeydbid),
  15289.         user_name(o1.uid),
  15290.         o1.name,
  15291.         c1.name,
  15292.         convert(binary(16),null),
  15293.         convert(int,null),
  15294.         db_name(r.fkeydbid),
  15295.         user_name(o2.uid),
  15296.         o2.name,
  15297.         c2.name,
  15298.         convert(binary(16),null),
  15299.         convert(int,null),
  15300.         convert(int,10),
  15301.         'NO ACTION',
  15302.         'NO ACTION',
  15303.         object_name(s.constid),
  15304.         object_name(r.constid),
  15305.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15306.     from
  15307.         sysobjects o1, sysobjects o2,
  15308.         syscolumns c1, syscolumns c2,
  15309.         sysreferences r, sysconstraints s
  15310.     where    
  15311.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15312.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15313.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15314.     and    o1.id = r.rkeyid
  15315.     and s.id = o1.id and (s.status & 0xf) = 1
  15316.     and    r.keycnt >= 10
  15317.     and    o1.id = c1.id
  15318.     and    c1.colid = r.rkey10
  15319.     and r.fkeyid = o2.id
  15320.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15321.     and    o2.id = c2.id    
  15322.     and    c2.colid = r.fkey10
  15323.     union all
  15324.     select
  15325.         db_name(r.rkeydbid),
  15326.         user_name(o1.uid),
  15327.         o1.name,
  15328.         c1.name,
  15329.         convert(binary(16),null),
  15330.         convert(int,null),
  15331.         db_name(r.fkeydbid),
  15332.         user_name(o2.uid),
  15333.         o2.name,
  15334.         c2.name,
  15335.         convert(binary(16),null),
  15336.         convert(int,null),
  15337.         convert(int,11),
  15338.         'NO ACTION',
  15339.         'NO ACTION',
  15340.         object_name(s.constid),
  15341.         object_name(r.constid),
  15342.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15343.     from
  15344.         sysobjects o1, sysobjects o2,
  15345.         syscolumns c1, syscolumns c2,
  15346.         sysreferences r, sysconstraints s
  15347.     where    
  15348.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15349.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15350.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15351.     and    o1.id = r.rkeyid
  15352.     and s.id = o1.id and (s.status & 0xf) = 1
  15353.     and    r.keycnt >= 11
  15354.     and    o1.id = c1.id
  15355.     and    c1.colid = r.rkey11
  15356.     and r.fkeyid = o2.id
  15357.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15358.     and    o2.id = c2.id    
  15359.     and    c2.colid = r.fkey11
  15360.     union all
  15361.     select
  15362.         db_name(r.rkeydbid),
  15363.         user_name(o1.uid),
  15364.         o1.name,
  15365.         c1.name,
  15366.         convert(binary(16),null),
  15367.         convert(int,null),
  15368.         db_name(r.fkeydbid),
  15369.         user_name(o2.uid),
  15370.         o2.name,
  15371.         c2.name,
  15372.         convert(binary(16),null),
  15373.         convert(int,null),
  15374.         convert(int,12),
  15375.         'NO ACTION',
  15376.         'NO ACTION',
  15377.         object_name(s.constid),
  15378.         object_name(r.constid),
  15379.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15380.     from
  15381.         sysobjects o1, sysobjects o2,
  15382.         syscolumns c1, syscolumns c2,
  15383.         sysreferences r, sysconstraints s
  15384.     where    
  15385.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15386.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15387.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15388.     and    o1.id = r.rkeyid
  15389.     and s.id = o1.id and (s.status & 0xf) = 1
  15390.     and    r.keycnt >= 12
  15391.     and    o1.id = c1.id
  15392.     and    c1.colid = r.rkey12
  15393.     and r.fkeyid = o2.id
  15394.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15395.     and    o2.id = c2.id    
  15396.     and    c2.colid = r.fkey12
  15397.     union all
  15398.     select
  15399.         db_name(r.rkeydbid),
  15400.         user_name(o1.uid),
  15401.         o1.name,
  15402.         c1.name,
  15403.         convert(binary(16),null),
  15404.         convert(int,null),
  15405.         db_name(r.fkeydbid),
  15406.         user_name(o2.uid),
  15407.         o2.name,
  15408.         c2.name,
  15409.         convert(binary(16),null),
  15410.         convert(int,null),
  15411.         convert(int,13),
  15412.         'NO ACTION',
  15413.         'NO ACTION',
  15414.         object_name(s.constid),
  15415.         object_name(r.constid),
  15416.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15417.     from
  15418.         sysobjects o1, sysobjects o2,
  15419.         syscolumns c1, syscolumns c2,
  15420.         sysreferences r, sysconstraints s
  15421.     where    
  15422.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15423.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15424.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15425.     and    o1.id = r.rkeyid
  15426.     and s.id = o1.id and (s.status & 0xf) = 1
  15427.     and    r.keycnt >= 13
  15428.     and    o1.id = c1.id
  15429.     and    c1.colid = r.rkey13
  15430.     and r.fkeyid = o2.id
  15431.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15432.     and    o2.id = c2.id    
  15433.     and    c2.colid = r.fkey13
  15434.     union all
  15435.     select
  15436.         db_name(r.rkeydbid),
  15437.         user_name(o1.uid),
  15438.         o1.name,
  15439.         c1.name,
  15440.         convert(binary(16),null),
  15441.         convert(int,null),
  15442.         db_name(r.fkeydbid),
  15443.         user_name(o2.uid),
  15444.         o2.name,
  15445.         c2.name,
  15446.         convert(binary(16),null),
  15447.         convert(int,null),
  15448.         convert(int,14),
  15449.         'NO ACTION',
  15450.         'NO ACTION',
  15451.         object_name(s.constid),
  15452.         object_name(r.constid),
  15453.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15454.     from
  15455.         sysobjects o1, sysobjects o2,
  15456.         syscolumns c1, syscolumns c2,
  15457.         sysreferences r, sysconstraints s
  15458.     where    
  15459.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15460.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15461.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15462.     and    o1.id = r.rkeyid
  15463.     and s.id = o1.id and (s.status & 0xf) = 1
  15464.     and    r.keycnt >= 14
  15465.     and    o1.id = c1.id
  15466.     and    c1.colid = r.rkey14
  15467.     and r.fkeyid = o2.id
  15468.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15469.     and    o2.id = c2.id    
  15470.     and    c2.colid = r.fkey14
  15471.     union all
  15472.     select
  15473.         db_name(r.rkeydbid),
  15474.         user_name(o1.uid),
  15475.         o1.name,
  15476.         c1.name,
  15477.         convert(binary(16),null),
  15478.         convert(int,null),
  15479.         db_name(r.fkeydbid),
  15480.         user_name(o2.uid),
  15481.         o2.name,
  15482.         c2.name,
  15483.         convert(binary(16),null),
  15484.         convert(int,null),
  15485.         convert(int,15),
  15486.         'NO ACTION',
  15487.         'NO ACTION',
  15488.         object_name(s.constid),
  15489.         object_name(r.constid),
  15490.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15491.     from
  15492.         sysobjects o1, sysobjects o2,
  15493.         syscolumns c1, syscolumns c2,
  15494.         sysreferences r, sysconstraints s
  15495.     where    
  15496.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15497.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15498.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15499.     and    o1.id = r.rkeyid
  15500.     and s.id = o1.id and (s.status & 0xf) = 1
  15501.     and    r.keycnt >= 15
  15502.     and    o1.id = c1.id
  15503.     and    c1.colid = r.rkey15
  15504.     and r.fkeyid = o2.id
  15505.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15506.     and    o2.id = c2.id    
  15507.     and    c2.colid = r.fkey15
  15508.     union all
  15509.     select
  15510.         db_name(r.rkeydbid),
  15511.         user_name(o1.uid),
  15512.         o1.name,
  15513.         c1.name,
  15514.         convert(binary(16),null),
  15515.         convert(int,null),
  15516.         db_name(r.fkeydbid),
  15517.         user_name(o2.uid),
  15518.         o2.name,
  15519.         c2.name,
  15520.         convert(binary(16),null),
  15521.         convert(int,null),
  15522.         convert(int,16),
  15523.         'NO ACTION',
  15524.         'NO ACTION',
  15525.         object_name(s.constid),
  15526.         object_name(r.constid),
  15527.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15528.     from
  15529.         sysobjects o1, sysobjects o2,
  15530.         syscolumns c1, syscolumns c2,
  15531.         sysreferences r, sysconstraints s
  15532.     where    
  15533.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15534.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15535.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15536.     and    o1.id = r.rkeyid
  15537.     and s.id = o1.id and (s.status & 0xf) = 1
  15538.     and    o1.id = c1.id
  15539.     and    r.keycnt >= 16
  15540.     and    c1.colid = r.rkey16
  15541.     and r.fkeyid = o2.id
  15542.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15543.     and    o2.id = c2.id    
  15544.     and    c2.colid = r.fkey16
  15545.     order by 8,9,2,3,13
  15546.     END
  15547.  
  15548.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  15549.         @scrollopt output, @ccopt output, @rows output
  15550.  
  15551.     drop table #spfkeysrowset1
  15552.        return isnull(@ret,0)
  15553. go
  15554. dump tran master with no_log
  15555. go
  15556. CREATE PROCEDURE sp_foreign_keys_rowset;3
  15557. as
  15558.     select
  15559.         PK_TABLE_CATALOG    = convert(sysname,' '),
  15560.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  15561.         PK_TABLE_NAME         = convert(sysname,' '),
  15562.         PK_COLUMN_NAME         = convert(sysname,' '),
  15563.         PK_COLUMN_GUID        = convert(binary(16),null),
  15564.         PK_COLUMN_PROPID    = convert(int,null),
  15565.         FK_TABLE_CATALOG    = convert(sysname,' '),
  15566.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  15567.         FK_TABLE_NAME         = convert(sysname,' '),
  15568.         FK_COLUMN_NAME         = convert(sysname,' '),
  15569.         FK_COLUMN_GUID        = convert(binary(16),null),
  15570.         FK_COLUMN_PROPID    = convert(int,null),
  15571.         ORDINAL                = convert(int,1),
  15572.         UPDATE_RULE            = 'NO ACTION',
  15573.         DELETE_RULE         = 'NO ACTION',
  15574.         PK_NAME                = convert(sysname, ' '),
  15575.         FK_NAME                = convert(sysname, ' '),
  15576.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15577.     where    1=0
  15578. go
  15579. dump tran master with no_log
  15580. go
  15581.  
  15582.  
  15583. if (charindex('7.00', @@version) = 0)
  15584. begin
  15585.     print ''
  15586.     print ''
  15587.     print 'Warning:'
  15588.     print 'you are installing the stored procedures '
  15589.     print 'on a pre 7.0 SQL Server.'
  15590.     print 'Ignore the following errors.'
  15591. end
  15592. else
  15593.     drop proc sp_foreign_keys_rowset
  15594. go
  15595.  
  15596.  
  15597. /*    Procedure for 7.0 server */
  15598. CREATE PROCEDURE sp_foreign_keys_rowset
  15599.     (
  15600.     @pk_table_name        sysname,
  15601.     @pk_table_schema    sysname = null,
  15602.     @fk_table_name        sysname = null,
  15603.     @fk_table_schema    sysname = null,
  15604.     @fk_table_catalog    sysname = null
  15605.     )
  15606. as
  15607.     select
  15608.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15609.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15610.         PK_TABLE_NAME         = o1.name,
  15611.         PK_COLUMN_NAME         = c1.name,
  15612.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15613.         PK_COLUMN_PROPID    = convert(int,null),
  15614.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15615.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15616.         FK_TABLE_NAME         = o2.name,
  15617.         FK_COLUMN_NAME         = c2.name,
  15618.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15619.         FK_COLUMN_PROPID    = convert(int,null),
  15620.         ORDINAL                = convert(int,1),
  15621.         UPDATE_RULE            = N'NO ACTION',
  15622.         DELETE_RULE         = N'NO ACTION',
  15623.         PK_NAME                = object_name(s.constid),
  15624.         FK_NAME                = object_name(r.constid),
  15625.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15626.     from
  15627.         sysobjects o1, sysobjects o2,
  15628.         syscolumns c1, syscolumns c2,
  15629.         sysreferences r, sysconstraints s
  15630.     where    
  15631.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15632.     and    o1.name = @pk_table_name
  15633.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15634.     and    o1.id = r.rkeyid
  15635.     and s.id = o1.id and (s.status & 0xf) = 1
  15636.     and    o1.id = c1.id
  15637.     and    c1.colid = r.rkey1
  15638.     and r.fkeyid = o2.id
  15639.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15640.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15641.     and    o2.id = c2.id    
  15642.     and    c2.colid = r.fkey1
  15643.     union all
  15644.     select
  15645.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15646.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15647.         PK_TABLE_NAME         = o1.name,
  15648.         PK_COLUMN_NAME         = c1.name,
  15649.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15650.         PK_COLUMN_PROPID    = convert(int,null),
  15651.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15652.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15653.         FK_TABLE_NAME         = o2.name,
  15654.         FK_COLUMN_NAME         = c2.name,
  15655.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15656.         FK_COLUMN_PROPID    = convert(int,null),
  15657.         ORDINAL                = convert(int,2),
  15658.         UPDATE_RULE            = N'NO ACTION',
  15659.         DELETE_RULE         = N'NO ACTION',
  15660.         PK_NAME                = object_name(s.constid),
  15661.         FK_NAME                = object_name(r.constid),
  15662.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15663.     from
  15664.         sysobjects o1, sysobjects o2,
  15665.         syscolumns c1, syscolumns c2,
  15666.         sysreferences r, sysconstraints s
  15667.     where    
  15668.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15669.     and    o1.name = @pk_table_name
  15670.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15671.     and    o1.id = r.rkeyid
  15672.     and s.id = o1.id and (s.status & 0xf) = 1
  15673.     and    o1.id = c1.id
  15674.     and    c1.colid = r.rkey2
  15675.     and    r.keycnt >= 2
  15676.     and r.fkeyid = o2.id
  15677.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15678.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15679.     and    o2.id = c2.id    
  15680.     and    c2.colid = r.fkey2
  15681.     union all
  15682.     select
  15683.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15684.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15685.         PK_TABLE_NAME         = o1.name,
  15686.         PK_COLUMN_NAME         = c1.name,
  15687.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15688.         PK_COLUMN_PROPID    = convert(int,null),
  15689.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15690.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15691.         FK_TABLE_NAME         = o2.name,
  15692.         FK_COLUMN_NAME         = c2.name,
  15693.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15694.         FK_COLUMN_PROPID    = convert(int,null),
  15695.         ORDINAL                = convert(int,3),
  15696.         UPDATE_RULE            = N'NO ACTION',
  15697.         DELETE_RULE         = N'NO ACTION',
  15698.         PK_NAME                = object_name(s.constid),
  15699.         FK_NAME                = object_name(r.constid),
  15700.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15701.     from
  15702.         sysobjects o1, sysobjects o2,
  15703.         syscolumns c1, syscolumns c2,
  15704.         sysreferences r, sysconstraints s
  15705.     where    
  15706.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15707.     and    o1.name = @pk_table_name
  15708.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15709.     and    o1.id = r.rkeyid
  15710.     and s.id = o1.id and (s.status & 0xf) = 1
  15711.     and    o1.id = c1.id
  15712.     and    c1.colid = r.rkey3
  15713.     and    r.keycnt >= 3
  15714.     and r.fkeyid = o2.id
  15715.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15716.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15717.     and    o2.id = c2.id    
  15718.     and    c2.colid = r.fkey3
  15719.     union all
  15720.     select
  15721.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15722.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15723.         PK_TABLE_NAME         = o1.name,
  15724.         PK_COLUMN_NAME         = c1.name,
  15725.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15726.         PK_COLUMN_PROPID    = convert(int,null),
  15727.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15728.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15729.         FK_TABLE_NAME         = o2.name,
  15730.         FK_COLUMN_NAME         = c2.name,
  15731.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15732.         FK_COLUMN_PROPID    = convert(int,null),
  15733.         ORDINAL                = convert(int,4),
  15734.         UPDATE_RULE            = N'NO ACTION',
  15735.         DELETE_RULE         = N'NO ACTION',
  15736.         PK_NAME                = object_name(s.constid),
  15737.         FK_NAME                = object_name(r.constid),
  15738.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15739.     from
  15740.         sysobjects o1, sysobjects o2,
  15741.         syscolumns c1, syscolumns c2,
  15742.         sysreferences r, sysconstraints s
  15743.     where    
  15744.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15745.     and    o1.name = @pk_table_name
  15746.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15747.     and    o1.id = r.rkeyid
  15748.     and s.id = o1.id and (s.status & 0xf) = 1
  15749.     and    r.keycnt >= 4
  15750.     and    o1.id = c1.id
  15751.     and    c1.colid = r.rkey4
  15752.     and r.fkeyid = o2.id
  15753.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15754.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15755.     and    o2.id = c2.id    
  15756.     and    c2.colid = r.fkey4
  15757.     union all
  15758.     select
  15759.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15760.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15761.         PK_TABLE_NAME         = o1.name,
  15762.         PK_COLUMN_NAME         = c1.name,
  15763.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15764.         PK_COLUMN_PROPID    = convert(int,null),
  15765.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15766.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15767.         FK_TABLE_NAME         = o2.name,
  15768.         FK_COLUMN_NAME         = c2.name,
  15769.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15770.         FK_COLUMN_PROPID    = convert(int,null),
  15771.         ORDINAL                = convert(int,5),
  15772.         UPDATE_RULE            = N'NO ACTION',
  15773.         DELETE_RULE         = N'NO ACTION',
  15774.         PK_NAME                = object_name(s.constid),
  15775.         FK_NAME                = object_name(r.constid),
  15776.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15777.     from
  15778.         sysobjects o1, sysobjects o2,
  15779.         syscolumns c1, syscolumns c2,
  15780.         sysreferences r, sysconstraints s
  15781.     where    
  15782.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15783.     and    o1.name = @pk_table_name
  15784.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15785.     and    o1.id = r.rkeyid
  15786.     and s.id = o1.id and (s.status & 0xf) = 1
  15787.     and    r.keycnt >= 5
  15788.     and    o1.id = c1.id
  15789.     and    c1.colid = r.rkey5
  15790.     and r.fkeyid = o2.id
  15791.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15792.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15793.     and    o2.id = c2.id    
  15794.     and    c2.colid = r.fkey5
  15795.     union all
  15796.     select
  15797.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15798.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15799.         PK_TABLE_NAME         = o1.name,
  15800.         PK_COLUMN_NAME         = c1.name,
  15801.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15802.         PK_COLUMN_PROPID    = convert(int,null),
  15803.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15804.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15805.         FK_TABLE_NAME         = o2.name,
  15806.         FK_COLUMN_NAME         = c2.name,
  15807.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15808.         FK_COLUMN_PROPID    = convert(int,null),
  15809.         ORDINAL                = convert(int,6),
  15810.         UPDATE_RULE            = N'NO ACTION',
  15811.         DELETE_RULE         = N'NO ACTION',
  15812.         PK_NAME                = object_name(s.constid),
  15813.         FK_NAME                = object_name(r.constid),
  15814.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15815.     from
  15816.         sysobjects o1, sysobjects o2,
  15817.         syscolumns c1, syscolumns c2,
  15818.         sysreferences r, sysconstraints s
  15819.     where    
  15820.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15821.     and    o1.name = @pk_table_name
  15822.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15823.     and    o1.id = r.rkeyid
  15824.     and s.id = o1.id and (s.status & 0xf) = 1
  15825.     and    r.keycnt >= 6
  15826.     and    o1.id = c1.id
  15827.     and    c1.colid = r.rkey6
  15828.     and r.fkeyid = o2.id
  15829.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15830.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15831.     and    o2.id = c2.id    
  15832.     and    c2.colid = r.fkey6
  15833.     union all
  15834.     select
  15835.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15836.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15837.         PK_TABLE_NAME         = o1.name,
  15838.         PK_COLUMN_NAME         = c1.name,
  15839.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15840.         PK_COLUMN_PROPID    = convert(int,null),
  15841.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15842.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15843.         FK_TABLE_NAME         = o2.name,
  15844.         FK_COLUMN_NAME         = c2.name,
  15845.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15846.         FK_COLUMN_PROPID    = convert(int,null),
  15847.         ORDINAL                = convert(int,7),
  15848.         UPDATE_RULE            = N'NO ACTION',
  15849.         DELETE_RULE         = N'NO ACTION',
  15850.         PK_NAME                = object_name(s.constid),
  15851.         FK_NAME                = object_name(r.constid),
  15852.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15853.     from
  15854.         sysobjects o1, sysobjects o2,
  15855.         syscolumns c1, syscolumns c2,
  15856.         sysreferences r, sysconstraints s
  15857.     where    
  15858.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15859.     and    o1.name = @pk_table_name
  15860.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15861.     and    o1.id = r.rkeyid
  15862.     and s.id = o1.id and (s.status & 0xf) = 1
  15863.     and    r.keycnt >= 7
  15864.     and    o1.id = c1.id
  15865.     and    c1.colid = r.rkey7
  15866.     and r.fkeyid = o2.id
  15867.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15868.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15869.     and    o2.id = c2.id    
  15870.     and    c2.colid = r.fkey7
  15871.     union all
  15872.     select
  15873.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15874.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15875.         PK_TABLE_NAME         = o1.name,
  15876.         PK_COLUMN_NAME         = c1.name,
  15877.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15878.         PK_COLUMN_PROPID    = convert(int,null),
  15879.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15880.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15881.         FK_TABLE_NAME         = o2.name,
  15882.         FK_COLUMN_NAME         = c2.name,
  15883.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15884.         FK_COLUMN_PROPID    = convert(int,null),
  15885.         ORDINAL                = convert(int,8),
  15886.         UPDATE_RULE            = N'NO ACTION',
  15887.         DELETE_RULE         = N'NO ACTION',
  15888.         PK_NAME                = object_name(s.constid),
  15889.         FK_NAME                = object_name(r.constid),
  15890.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15891.     from
  15892.         sysobjects o1, sysobjects o2,
  15893.         syscolumns c1, syscolumns c2,
  15894.         sysreferences r, sysconstraints s
  15895.     where    
  15896.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15897.     and    o1.name = @pk_table_name
  15898.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15899.     and    o1.id = r.rkeyid
  15900.     and s.id = o1.id and (s.status & 0xf) = 1
  15901.     and    r.keycnt >= 8
  15902.     and    o1.id = c1.id
  15903.     and    c1.colid = r.rkey8
  15904.     and r.fkeyid = o2.id
  15905.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15906.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15907.     and    o2.id = c2.id    
  15908.     and    c2.colid = r.fkey8
  15909.     union all
  15910.     select
  15911.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15912.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15913.         PK_TABLE_NAME         = o1.name,
  15914.         PK_COLUMN_NAME         = c1.name,
  15915.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15916.         PK_COLUMN_PROPID    = convert(int,null),
  15917.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15918.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15919.         FK_TABLE_NAME         = o2.name,
  15920.         FK_COLUMN_NAME         = c2.name,
  15921.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15922.         FK_COLUMN_PROPID    = convert(int,null),
  15923.         ORDINAL                = convert(int,9),
  15924.         UPDATE_RULE            = N'NO ACTION',
  15925.         DELETE_RULE         = N'NO ACTION',
  15926.         PK_NAME                = object_name(s.constid),
  15927.         FK_NAME                = object_name(r.constid),
  15928.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15929.     from
  15930.         sysobjects o1, sysobjects o2,
  15931.         syscolumns c1, syscolumns c2,
  15932.         sysreferences r, sysconstraints s
  15933.     where    
  15934.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15935.     and    o1.name = @pk_table_name
  15936.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15937.     and    o1.id = r.rkeyid
  15938.     and s.id = o1.id and (s.status & 0xf) = 1
  15939.     and    r.keycnt >= 9
  15940.     and    o1.id = c1.id
  15941.     and    c1.colid = r.rkey9
  15942.     and r.fkeyid = o2.id
  15943.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15944.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15945.     and    o2.id = c2.id    
  15946.     and    c2.colid = r.fkey9
  15947.     union all
  15948.     select
  15949.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15950.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15951.         PK_TABLE_NAME         = o1.name,
  15952.         PK_COLUMN_NAME         = c1.name,
  15953.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15954.         PK_COLUMN_PROPID    = convert(int,null),
  15955.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15956.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15957.         FK_TABLE_NAME         = o2.name,
  15958.         FK_COLUMN_NAME         = c2.name,
  15959.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15960.         FK_COLUMN_PROPID    = convert(int,null),
  15961.         ORDINAL                = convert(int,10),
  15962.         UPDATE_RULE            = N'NO ACTION',
  15963.         DELETE_RULE         = N'NO ACTION',
  15964.         PK_NAME                = object_name(s.constid),
  15965.         FK_NAME                = object_name(r.constid),
  15966.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15967.     from
  15968.         sysobjects o1, sysobjects o2,
  15969.         syscolumns c1, syscolumns c2,
  15970.         sysreferences r, sysconstraints s
  15971.     where    
  15972.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15973.     and    o1.name = @pk_table_name
  15974.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15975.     and    o1.id = r.rkeyid
  15976.     and s.id = o1.id and (s.status & 0xf) = 1
  15977.     and    r.keycnt >= 10
  15978.     and    o1.id = c1.id
  15979.     and    c1.colid = r.rkey10
  15980.     and r.fkeyid = o2.id
  15981.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15982.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15983.     and    o2.id = c2.id    
  15984.     and    c2.colid = r.fkey10
  15985.     union all
  15986.     select
  15987.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15988.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15989.         PK_TABLE_NAME         = o1.name,
  15990.         PK_COLUMN_NAME         = c1.name,
  15991.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15992.         PK_COLUMN_PROPID    = convert(int,null),
  15993.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15994.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15995.         FK_TABLE_NAME         = o2.name,
  15996.         FK_COLUMN_NAME         = c2.name,
  15997.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15998.         FK_COLUMN_PROPID    = convert(int,null),
  15999.         ORDINAL                = convert(int,11),
  16000.         UPDATE_RULE            = N'NO ACTION',
  16001.         DELETE_RULE         = N'NO ACTION',
  16002.         PK_NAME                = object_name(s.constid),
  16003.         FK_NAME                = object_name(r.constid),
  16004.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16005.     from
  16006.         sysobjects o1, sysobjects o2,
  16007.         syscolumns c1, syscolumns c2,
  16008.         sysreferences r, sysconstraints s
  16009.     where    
  16010.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16011.     and    o1.name = @pk_table_name
  16012.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16013.     and    o1.id = r.rkeyid
  16014.     and s.id = o1.id and (s.status & 0xf) = 1
  16015.     and    r.keycnt >= 11
  16016.     and    o1.id = c1.id
  16017.     and    c1.colid = r.rkey11
  16018.     and r.fkeyid = o2.id
  16019.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16020.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16021.     and    o2.id = c2.id    
  16022.     and    c2.colid = r.fkey11
  16023.     union all
  16024.     select
  16025.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16026.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16027.         PK_TABLE_NAME         = o1.name,
  16028.         PK_COLUMN_NAME         = c1.name,
  16029.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16030.         PK_COLUMN_PROPID    = convert(int,null),
  16031.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16032.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16033.         FK_TABLE_NAME         = o2.name,
  16034.         FK_COLUMN_NAME         = c2.name,
  16035.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16036.         FK_COLUMN_PROPID    = convert(int,null),
  16037.         ORDINAL                = convert(int,12),
  16038.         UPDATE_RULE            = N'NO ACTION',
  16039.         DELETE_RULE         = N'NO ACTION',
  16040.         PK_NAME                = object_name(s.constid),
  16041.         FK_NAME                = object_name(r.constid),
  16042.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16043.     from
  16044.         sysobjects o1, sysobjects o2,
  16045.         syscolumns c1, syscolumns c2,
  16046.         sysreferences r, sysconstraints s
  16047.     where    
  16048.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16049.     and    o1.name = @pk_table_name
  16050.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16051.     and    o1.id = r.rkeyid
  16052.     and s.id = o1.id and (s.status & 0xf) = 1
  16053.     and    r.keycnt >= 12
  16054.     and    o1.id = c1.id
  16055.     and    c1.colid = r.rkey12
  16056.     and r.fkeyid = o2.id
  16057.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16058.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16059.     and    o2.id = c2.id    
  16060.     and    c2.colid = r.fkey12
  16061.     union all
  16062.     select
  16063.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16064.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16065.         PK_TABLE_NAME         = o1.name,
  16066.         PK_COLUMN_NAME         = c1.name,
  16067.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16068.         PK_COLUMN_PROPID    = convert(int,null),
  16069.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16070.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16071.         FK_TABLE_NAME         = o2.name,
  16072.         FK_COLUMN_NAME         = c2.name,
  16073.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16074.         FK_COLUMN_PROPID    = convert(int,null),
  16075.         ORDINAL                = convert(int,13),
  16076.         UPDATE_RULE            = N'NO ACTION',
  16077.         DELETE_RULE         = N'NO ACTION',
  16078.         PK_NAME                = object_name(s.constid),
  16079.         FK_NAME                = object_name(r.constid),
  16080.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16081.     from
  16082.         sysobjects o1, sysobjects o2,
  16083.         syscolumns c1, syscolumns c2,
  16084.         sysreferences r, sysconstraints s
  16085.     where    
  16086.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16087.     and    o1.name = @pk_table_name
  16088.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16089.     and    o1.id = r.rkeyid
  16090.     and s.id = o1.id and (s.status & 0xf) = 1
  16091.     and    r.keycnt >= 13
  16092.     and    o1.id = c1.id
  16093.     and    c1.colid = r.rkey13
  16094.     and r.fkeyid = o2.id
  16095.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16096.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16097.     and    o2.id = c2.id    
  16098.     and    c2.colid = r.fkey13
  16099.     union all
  16100.     select
  16101.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16102.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16103.         PK_TABLE_NAME         = o1.name,
  16104.         PK_COLUMN_NAME         = c1.name,
  16105.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16106.         PK_COLUMN_PROPID    = convert(int,null),
  16107.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16108.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16109.         FK_TABLE_NAME         = o2.name,
  16110.         FK_COLUMN_NAME         = c2.name,
  16111.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16112.         FK_COLUMN_PROPID    = convert(int,null),
  16113.         ORDINAL                = convert(int,14),
  16114.         UPDATE_RULE            = N'NO ACTION',
  16115.         DELETE_RULE         = N'NO ACTION',
  16116.         PK_NAME                = object_name(s.constid),
  16117.         FK_NAME                = object_name(r.constid),
  16118.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16119.     from
  16120.         sysobjects o1, sysobjects o2,
  16121.         syscolumns c1, syscolumns c2,
  16122.         sysreferences r, sysconstraints s
  16123.     where    
  16124.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16125.     and    o1.name = @pk_table_name
  16126.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16127.     and    o1.id = r.rkeyid
  16128.     and s.id = o1.id and (s.status & 0xf) = 1
  16129.     and    r.keycnt >= 14
  16130.     and    o1.id = c1.id
  16131.     and    c1.colid = r.rkey14
  16132.     and r.fkeyid = o2.id
  16133.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16134.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16135.     and    o2.id = c2.id    
  16136.     and    c2.colid = r.fkey14
  16137.     union all
  16138.     select
  16139.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16140.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16141.         PK_TABLE_NAME         = o1.name,
  16142.         PK_COLUMN_NAME         = c1.name,
  16143.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16144.         PK_COLUMN_PROPID    = convert(int,null),
  16145.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16146.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16147.         FK_TABLE_NAME         = o2.name,
  16148.         FK_COLUMN_NAME         = c2.name,
  16149.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16150.         FK_COLUMN_PROPID    = convert(int,null),
  16151.         ORDINAL                = convert(int,15),
  16152.         UPDATE_RULE            = N'NO ACTION',
  16153.         DELETE_RULE         = N'NO ACTION',
  16154.         PK_NAME                = object_name(s.constid),
  16155.         FK_NAME                = object_name(r.constid),
  16156.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16157.     from
  16158.         sysobjects o1, sysobjects o2,
  16159.         syscolumns c1, syscolumns c2,
  16160.         sysreferences r, sysconstraints s
  16161.     where    
  16162.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16163.     and    o1.name = @pk_table_name
  16164.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16165.     and    o1.id = r.rkeyid
  16166.     and s.id = o1.id and (s.status & 0xf) = 1
  16167.     and    r.keycnt >= 15
  16168.     and    o1.id = c1.id
  16169.     and    c1.colid = r.rkey15
  16170.     and r.fkeyid = o2.id
  16171.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16172.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16173.     and    o2.id = c2.id    
  16174.     and    c2.colid = r.fkey15
  16175.     union all
  16176.     select
  16177.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16178.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16179.         PK_TABLE_NAME         = o1.name,
  16180.         PK_COLUMN_NAME         = c1.name,
  16181.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16182.         PK_COLUMN_PROPID    = convert(int,null),
  16183.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16184.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16185.         FK_TABLE_NAME         = o2.name,
  16186.         FK_COLUMN_NAME         = c2.name,
  16187.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16188.         FK_COLUMN_PROPID    = convert(int,null),
  16189.         ORDINAL                = convert(int,16),
  16190.         UPDATE_RULE            = N'NO ACTION',
  16191.         DELETE_RULE         = N'NO ACTION',
  16192.         PK_NAME                = object_name(s.constid),
  16193.         FK_NAME                = object_name(r.constid),
  16194.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16195.     from
  16196.         sysobjects o1, sysobjects o2,
  16197.         syscolumns c1, syscolumns c2,
  16198.         sysreferences r, sysconstraints s
  16199.     where    
  16200.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16201.     and    o1.name = @pk_table_name
  16202.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16203.     and    o1.id = r.rkeyid
  16204.     and s.id = o1.id and (s.status & 0xf) = 1
  16205.     and    r.keycnt >= 16
  16206.     and    o1.id = c1.id
  16207.     and    c1.colid = r.rkey16
  16208.     and r.fkeyid = o2.id
  16209.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16210.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16211.     and    o2.id = c2.id    
  16212.     and    c2.colid = r.fkey16
  16213.     order by 8,9,2,3,13
  16214. go
  16215. dump tran master with no_log
  16216. go
  16217. CREATE PROCEDURE sp_foreign_keys_rowset;2
  16218.     (
  16219.     @fk_table_name        sysname,
  16220.     @fk_table_schema    sysname = null,
  16221.     @pk_table_name        sysname = null,
  16222.     @pk_table_schema    sysname = null,
  16223.     @pk_table_catalog    sysname = null
  16224.     )
  16225. as
  16226.     select
  16227.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16228.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16229.         PK_TABLE_NAME         = o1.name,
  16230.         PK_COLUMN_NAME         = c1.name,
  16231.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16232.         PK_COLUMN_PROPID    = convert(int,null),
  16233.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16234.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16235.         FK_TABLE_NAME         = o2.name,
  16236.         FK_COLUMN_NAME         = c2.name,
  16237.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16238.         FK_COLUMN_PROPID    = convert(int,null),
  16239.         ORDINAL                = convert(int,1),
  16240.         UPDATE_RULE            = N'NO ACTION',
  16241.         DELETE_RULE         = N'NO ACTION',
  16242.         PK_NAME                = object_name(s.constid),
  16243.         FK_NAME                = object_name(r.constid),
  16244.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16245.     from
  16246.         sysobjects o1, sysobjects o2,
  16247.         syscolumns c1, syscolumns c2,
  16248.         sysreferences r, sysconstraints s
  16249.     where    
  16250.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16251.     and    o2.name = @fk_table_name
  16252.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16253.     and    o2.id = r.fkeyid
  16254.     and    o2.id = c2.id
  16255.     and    c2.colid = r.fkey1
  16256.     and r.rkeyid = o1.id
  16257.     and s.id = o1.id and (s.status & 0xf) = 1
  16258.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16259.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16260.     and    o1.id = c1.id    
  16261.     and    c1.colid = r.rkey1
  16262.     union    all
  16263.     select
  16264.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16265.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16266.         PK_TABLE_NAME         = o1.name,
  16267.         PK_COLUMN_NAME         = c1.name,
  16268.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16269.         PK_COLUMN_PROPID    = convert(int,null),
  16270.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16271.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16272.         FK_TABLE_NAME         = o2.name,
  16273.         FK_COLUMN_NAME         = c2.name,
  16274.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16275.         FK_COLUMN_PROPID    = convert(int,null),
  16276.         ORDINAL                = convert(int,2),
  16277.         UPDATE_RULE            = N'NO ACTION',
  16278.         DELETE_RULE         = N'NO ACTION',
  16279.         PK_NAME                = object_name(s.constid),
  16280.         FK_NAME                = object_name(r.constid),
  16281.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16282.     from
  16283.         sysobjects o1, sysobjects o2,
  16284.         syscolumns c1, syscolumns c2,
  16285.         sysreferences r, sysconstraints s
  16286.     where    
  16287.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16288.     and    o2.name = @fk_table_name
  16289.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16290.     and    o2.id = r.fkeyid
  16291.     and    r.keycnt >= 2
  16292.     and    o2.id = c2.id
  16293.     and    c2.colid = r.fkey2
  16294.     and    r.rkeyid = o1.id
  16295.     and s.id = o1.id and (s.status & 0xf) = 1
  16296.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16297.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16298.     and    o1.id = c1.id    
  16299.     and    c1.colid = r.rkey2
  16300.     union    all
  16301.     select
  16302.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16303.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16304.         PK_TABLE_NAME         = o1.name,
  16305.         PK_COLUMN_NAME         = c1.name,
  16306.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16307.         PK_COLUMN_PROPID    = convert(int,null),
  16308.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16309.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16310.         FK_TABLE_NAME         = o2.name,
  16311.         FK_COLUMN_NAME         = c2.name,
  16312.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16313.         FK_COLUMN_PROPID    = convert(int,null),
  16314.         ORDINAL                = convert(int,3),
  16315.         UPDATE_RULE            = N'NO ACTION',
  16316.         DELETE_RULE         = N'NO ACTION',
  16317.         PK_NAME                = object_name(s.constid),
  16318.         FK_NAME                = object_name(r.constid),
  16319.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16320.     from
  16321.         sysobjects o1, sysobjects o2,
  16322.         syscolumns c1, syscolumns c2,
  16323.         sysreferences r, sysconstraints s
  16324.     where    
  16325.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16326.     and    o2.name = @fk_table_name
  16327.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16328.     and    o2.id = r.fkeyid
  16329.     and    r.keycnt >= 3
  16330.     and    o2.id = c2.id
  16331.     and    c2.colid = r.fkey3
  16332.     and r.rkeyid = o1.id
  16333.     and s.id = o1.id and (s.status & 0xf) = 1
  16334.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16335.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16336.     and    o1.id = c1.id    
  16337.     and    c1.colid = r.rkey3
  16338.     union    all
  16339.     select
  16340.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16341.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16342.         PK_TABLE_NAME         = o1.name,
  16343.         PK_COLUMN_NAME         = c1.name,
  16344.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16345.         PK_COLUMN_PROPID    = convert(int,null),
  16346.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16347.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16348.         FK_TABLE_NAME         = o2.name,
  16349.         FK_COLUMN_NAME         = c2.name,
  16350.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16351.         FK_COLUMN_PROPID    = convert(int,null),
  16352.         ORDINAL                = convert(int,4),
  16353.         UPDATE_RULE            = N'NO ACTION',
  16354.         DELETE_RULE         = N'NO ACTION',
  16355.         PK_NAME                = object_name(s.constid),
  16356.         FK_NAME                = object_name(r.constid),
  16357.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16358.     from
  16359.         sysobjects o1, sysobjects o2,
  16360.         syscolumns c1, syscolumns c2,
  16361.         sysreferences r, sysconstraints s
  16362.     where    
  16363.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16364.     and    o2.name = @fk_table_name
  16365.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16366.     and    o2.id = r.fkeyid
  16367.     and    r.keycnt >= 4
  16368.     and    o2.id = c2.id
  16369.     and    c2.colid = r.fkey4
  16370.     and r.rkeyid = o1.id
  16371.     and s.id = o1.id and (s.status & 0xf) = 1
  16372.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16373.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16374.     and    o1.id = c1.id    
  16375.     and    c1.colid = r.rkey4
  16376.     union    all
  16377.     select
  16378.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16379.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16380.         PK_TABLE_NAME         = o1.name,
  16381.         PK_COLUMN_NAME         = c1.name,
  16382.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16383.         PK_COLUMN_PROPID    = convert(int,null),
  16384.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16385.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16386.         FK_TABLE_NAME         = o2.name,
  16387.         FK_COLUMN_NAME         = c2.name,
  16388.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16389.         FK_COLUMN_PROPID    = convert(int,null),
  16390.         ORDINAL                = convert(int,5),
  16391.         UPDATE_RULE            = N'NO ACTION',
  16392.         DELETE_RULE         = N'NO ACTION',
  16393.         PK_NAME                = object_name(s.constid),
  16394.         FK_NAME                = object_name(r.constid),
  16395.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16396.     from
  16397.         sysobjects o1, sysobjects o2,
  16398.         syscolumns c1, syscolumns c2,
  16399.         sysreferences r, sysconstraints s
  16400.     where    
  16401.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16402.     and    o2.name = @fk_table_name
  16403.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16404.     and    o2.id = r.fkeyid
  16405.     and    r.keycnt >= 5
  16406.     and    o2.id = c2.id
  16407.     and    c2.colid = r.fkey5
  16408.     and    r.rkeyid = o1.id
  16409.     and s.id = o1.id and (s.status & 0xf) = 1
  16410.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16411.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16412.     and    o1.id = c1.id    
  16413.     and    c1.colid = r.rkey5
  16414.     union    all
  16415.     select
  16416.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16417.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16418.         PK_TABLE_NAME         = o1.name,
  16419.         PK_COLUMN_NAME         = c1.name,
  16420.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16421.         PK_COLUMN_PROPID    = convert(int,null),
  16422.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16423.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16424.         FK_TABLE_NAME         = o2.name,
  16425.         FK_COLUMN_NAME         = c2.name,
  16426.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16427.         FK_COLUMN_PROPID    = convert(int,null),
  16428.         ORDINAL                = convert(int,6),
  16429.         UPDATE_RULE            = N'NO ACTION',
  16430.         DELETE_RULE         = N'NO ACTION',
  16431.         PK_NAME                = object_name(s.constid),
  16432.         FK_NAME                = object_name(r.constid),
  16433.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16434.     from
  16435.         sysobjects o1, sysobjects o2,
  16436.         syscolumns c1, syscolumns c2,
  16437.         sysreferences r, sysconstraints s
  16438.     where    
  16439.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16440.     and    o2.name = @fk_table_name
  16441.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16442.     and    o2.id = r.fkeyid
  16443.     and    r.keycnt >= 6
  16444.     and    o2.id = c2.id
  16445.     and    c2.colid = r.fkey6
  16446.     and r.rkeyid = o1.id
  16447.     and s.id = o1.id and (s.status & 0xf) = 1
  16448.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16449.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16450.     and    o1.id = c1.id    
  16451.     and    c1.colid = r.rkey6
  16452.     union    all
  16453.     select
  16454.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16455.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16456.         PK_TABLE_NAME         = o1.name,
  16457.         PK_COLUMN_NAME         = c1.name,
  16458.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16459.         PK_COLUMN_PROPID    = convert(int,null),
  16460.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16461.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16462.         FK_TABLE_NAME         = o2.name,
  16463.         FK_COLUMN_NAME         = c2.name,
  16464.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16465.         FK_COLUMN_PROPID    = convert(int,null),
  16466.         ORDINAL                = convert(int,7),
  16467.         UPDATE_RULE            = N'NO ACTION',
  16468.         DELETE_RULE         = N'NO ACTION',
  16469.         PK_NAME                = object_name(s.constid),
  16470.         FK_NAME                = object_name(r.constid),
  16471.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16472.     from
  16473.         sysobjects o1, sysobjects o2,
  16474.         syscolumns c1, syscolumns c2,
  16475.         sysreferences r, sysconstraints s
  16476.     where    
  16477.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16478.     and    o2.name = @fk_table_name
  16479.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16480.     and    o2.id = r.fkeyid
  16481.     and    r.keycnt >= 7
  16482.     and    o2.id = c2.id
  16483.     and    c2.colid = r.fkey7
  16484.     and    r.rkeyid = o1.id
  16485.     and s.id = o1.id and (s.status & 0xf) = 1
  16486.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16487.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16488.     and    o1.id = c1.id    
  16489.     and    c1.colid = r.rkey7
  16490.     union    all
  16491.     select
  16492.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16493.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16494.         PK_TABLE_NAME         = o1.name,
  16495.         PK_COLUMN_NAME         = c1.name,
  16496.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16497.         PK_COLUMN_PROPID    = convert(int,null),
  16498.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16499.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16500.         FK_TABLE_NAME         = o2.name,
  16501.         FK_COLUMN_NAME         = c2.name,
  16502.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16503.         FK_COLUMN_PROPID    = convert(int,null),
  16504.         ORDINAL                = convert(int,8),
  16505.         UPDATE_RULE            = N'NO ACTION',
  16506.         DELETE_RULE         = N'NO ACTION',
  16507.         PK_NAME                = object_name(s.constid),
  16508.         FK_NAME                = object_name(r.constid),
  16509.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16510.     from
  16511.         sysobjects o1, sysobjects o2,
  16512.         syscolumns c1, syscolumns c2,
  16513.         sysreferences r, sysconstraints s
  16514.     where    
  16515.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16516.     and    o2.name = @fk_table_name
  16517.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16518.     and    o2.id = r.fkeyid
  16519.     and    r.keycnt >= 8
  16520.     and    o2.id = c2.id
  16521.     and    c2.colid = r.fkey8
  16522.     and r.rkeyid = o1.id
  16523.     and s.id = o1.id and (s.status & 0xf) = 1
  16524.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16525.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16526.     and    o1.id = c1.id    
  16527.     and    c1.colid = r.rkey8
  16528.     union    all
  16529.     select
  16530.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16531.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16532.         PK_TABLE_NAME         = o1.name,
  16533.         PK_COLUMN_NAME         = c1.name,
  16534.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16535.         PK_COLUMN_PROPID    = convert(int,null),
  16536.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16537.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16538.         FK_TABLE_NAME         = o2.name,
  16539.         FK_COLUMN_NAME         = c2.name,
  16540.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16541.         FK_COLUMN_PROPID    = convert(int,null),
  16542.         ORDINAL                = convert(int,9),
  16543.         UPDATE_RULE            = N'NO ACTION',
  16544.         DELETE_RULE         = N'NO ACTION',
  16545.         PK_NAME                = object_name(s.constid),
  16546.         FK_NAME                = object_name(r.constid),
  16547.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16548.     from
  16549.         sysobjects o1, sysobjects o2,
  16550.         syscolumns c1, syscolumns c2,
  16551.         sysreferences r, sysconstraints s
  16552.     where    
  16553.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16554.     and    o2.name = @fk_table_name
  16555.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16556.     and    o2.id = r.fkeyid
  16557.     and    r.keycnt >= 9
  16558.     and    o2.id = c2.id
  16559.     and    c2.colid = r.fkey9
  16560.     and    r.rkeyid = o1.id
  16561.     and s.id = o1.id and (s.status & 0xf) = 1
  16562.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16563.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16564.     and    o1.id = c1.id    
  16565.     and    c1.colid = r.rkey9
  16566.     union    all
  16567.     select
  16568.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16569.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16570.         PK_TABLE_NAME         = o1.name,
  16571.         PK_COLUMN_NAME         = c1.name,
  16572.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16573.         PK_COLUMN_PROPID    = convert(int,null),
  16574.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16575.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16576.         FK_TABLE_NAME         = o2.name,
  16577.         FK_COLUMN_NAME         = c2.name,
  16578.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16579.         FK_COLUMN_PROPID    = convert(int,null),
  16580.         ORDINAL                = convert(int,10),
  16581.         UPDATE_RULE            = N'NO ACTION',
  16582.         DELETE_RULE         = N'NO ACTION',
  16583.         PK_NAME                = object_name(s.constid),
  16584.         FK_NAME                = object_name(r.constid),
  16585.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16586.     from
  16587.         sysobjects o1, sysobjects o2,
  16588.         syscolumns c1, syscolumns c2,
  16589.         sysreferences r, sysconstraints s
  16590.     where    
  16591.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16592.     and    o2.name = @fk_table_name
  16593.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16594.     and    o2.id = r.fkeyid
  16595.     and    r.keycnt >= 10
  16596.     and    o2.id = c2.id
  16597.     and    c2.colid = r.fkey10
  16598.     and r.rkeyid = o1.id
  16599.     and s.id = o1.id and (s.status & 0xf) = 1
  16600.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16601.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16602.     and    o1.id = c1.id    
  16603.     and    c1.colid = r.rkey10
  16604.     union    all
  16605.     select
  16606.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16607.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16608.         PK_TABLE_NAME         = o1.name,
  16609.         PK_COLUMN_NAME         = c1.name,
  16610.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16611.         PK_COLUMN_PROPID    = convert(int,null),
  16612.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16613.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16614.         FK_TABLE_NAME         = o2.name,
  16615.         FK_COLUMN_NAME         = c2.name,
  16616.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16617.         FK_COLUMN_PROPID    = convert(int,null),
  16618.         ORDINAL                = convert(int,11),
  16619.         UPDATE_RULE            = N'NO ACTION',
  16620.         DELETE_RULE         = N'NO ACTION',
  16621.         PK_NAME                = object_name(s.constid),
  16622.         FK_NAME                = object_name(r.constid),
  16623.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16624.     from
  16625.         sysobjects o1, sysobjects o2,
  16626.         syscolumns c1, syscolumns c2,
  16627.         sysreferences r, sysconstraints s
  16628.     where    
  16629.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16630.     and    o2.name = @fk_table_name
  16631.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16632.     and    o2.id = r.fkeyid
  16633.     and    r.keycnt >= 11
  16634.     and    o2.id = c2.id
  16635.     and    c2.colid = r.fkey11
  16636.     and r.rkeyid = o1.id
  16637.     and s.id = o1.id and (s.status & 0xf) = 1
  16638.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16639.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16640.     and    o1.id = c1.id    
  16641.     and    c1.colid = r.rkey11
  16642.     union    all
  16643.     select
  16644.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16645.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16646.         PK_TABLE_NAME         = o1.name,
  16647.         PK_COLUMN_NAME         = c1.name,
  16648.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16649.         PK_COLUMN_PROPID    = convert(int,null),
  16650.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16651.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16652.         FK_TABLE_NAME         = o2.name,
  16653.         FK_COLUMN_NAME         = c2.name,
  16654.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16655.         FK_COLUMN_PROPID    = convert(int,null),
  16656.         ORDINAL                = convert(int,12),
  16657.         UPDATE_RULE            = N'NO ACTION',
  16658.         DELETE_RULE         = N'NO ACTION',
  16659.         PK_NAME                = object_name(s.constid),
  16660.         FK_NAME                = object_name(r.constid),
  16661.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16662.     from
  16663.         sysobjects o1, sysobjects o2,
  16664.         syscolumns c1, syscolumns c2,
  16665.         sysreferences r, sysconstraints s
  16666.     where    
  16667.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16668.     and    o2.name = @fk_table_name
  16669.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16670.     and    o2.id = r.fkeyid
  16671.     and    r.keycnt >= 12
  16672.     and    o2.id = c2.id
  16673.     and    c2.colid = r.fkey12
  16674.     and r.rkeyid = o1.id
  16675.     and s.id = o1.id and (s.status & 0xf) = 1
  16676.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16677.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16678.     and    o1.id = c1.id    
  16679.     and    c1.colid = r.rkey12
  16680.     union    all
  16681.     select
  16682.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16683.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16684.         PK_TABLE_NAME         = o1.name,
  16685.         PK_COLUMN_NAME         = c1.name,
  16686.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16687.         PK_COLUMN_PROPID    = convert(int,null),
  16688.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16689.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16690.         FK_TABLE_NAME         = o2.name,
  16691.         FK_COLUMN_NAME         = c2.name,
  16692.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16693.         FK_COLUMN_PROPID    = convert(int,null),
  16694.         ORDINAL                = convert(int,13),
  16695.         UPDATE_RULE            = N'NO ACTION',
  16696.         DELETE_RULE         = N'NO ACTION',
  16697.         PK_NAME                = object_name(s.constid),
  16698.         FK_NAME                = object_name(r.constid),
  16699.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16700.     from
  16701.         sysobjects o1, sysobjects o2,
  16702.         syscolumns c1, syscolumns c2,
  16703.         sysreferences r, sysconstraints s
  16704.     where    
  16705.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16706.     and    o2.name = @fk_table_name
  16707.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16708.     and    o2.id = r.fkeyid
  16709.     and    r.keycnt >= 13
  16710.     and    o2.id = c2.id
  16711.     and    c2.colid = r.fkey13
  16712.     and r.rkeyid = o1.id
  16713.     and s.id = o1.id and (s.status & 0xf) = 1
  16714.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16715.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16716.     and    o1.id = c1.id    
  16717.     and    c1.colid = r.rkey13
  16718.     union    all
  16719.     select
  16720.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16721.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16722.         PK_TABLE_NAME         = o1.name,
  16723.         PK_COLUMN_NAME         = c1.name,
  16724.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16725.         PK_COLUMN_PROPID    = convert(int,null),
  16726.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16727.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16728.         FK_TABLE_NAME         = o2.name,
  16729.         FK_COLUMN_NAME         = c2.name,
  16730.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16731.         FK_COLUMN_PROPID    = convert(int,null),
  16732.         ORDINAL                = convert(int,14),
  16733.         UPDATE_RULE            = N'NO ACTION',
  16734.         DELETE_RULE         = N'NO ACTION',
  16735.         PK_NAME                = object_name(s.constid),
  16736.         FK_NAME                = object_name(r.constid),
  16737.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16738.     from
  16739.         sysobjects o1, sysobjects o2,
  16740.         syscolumns c1, syscolumns c2,
  16741.         sysreferences r, sysconstraints s
  16742.     where    
  16743.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16744.     and    o2.name = @fk_table_name
  16745.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16746.     and    o2.id = r.fkeyid
  16747.     and    r.keycnt >= 14
  16748.     and    o2.id = c2.id
  16749.     and    c2.colid = r.fkey14
  16750.     and r.rkeyid = o1.id
  16751.     and s.id = o1.id and (s.status & 0xf) = 1
  16752.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16753.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16754.     and    o1.id = c1.id    
  16755.     and    c1.colid = r.rkey14
  16756.     union    all
  16757.     select
  16758.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16759.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16760.         PK_TABLE_NAME         = o1.name,
  16761.         PK_COLUMN_NAME         = c1.name,
  16762.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16763.         PK_COLUMN_PROPID    = convert(int,null),
  16764.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16765.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16766.         FK_TABLE_NAME         = o2.name,
  16767.         FK_COLUMN_NAME         = c2.name,
  16768.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16769.         FK_COLUMN_PROPID    = convert(int,null),
  16770.         ORDINAL                = convert(int,15),
  16771.         UPDATE_RULE            = N'NO ACTION',
  16772.         DELETE_RULE         = N'NO ACTION',
  16773.         PK_NAME                = object_name(s.constid),
  16774.         FK_NAME                = object_name(r.constid),
  16775.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  16776.     from
  16777.         sysobjects o1, sysobjects o2,
  16778.         syscolumns c1, syscolumns c2,
  16779.         sysreferences r, sysconstraints s
  16780.     where    
  16781.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16782.     and    o2.name = @fk_table_name
  16783.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16784.     and    o2.id = r.fkeyid
  16785.     and    r.keycnt >= 15
  16786.     and    o2.id = c2.id
  16787.     and    c2.colid = r.fkey15
  16788.     and r.rkeyid = o1.id
  16789.     and s.id = o1.id and (s.status & 0xf) = 1
  16790.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16791.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16792.     and    o1.id = c1.id    
  16793.     and    c1.colid = r.rkey15
  16794.     union    all
  16795.     select
  16796.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16797.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16798.         PK_TABLE_NAME         = o1.name,
  16799.         PK_COLUMN_NAME         = c1.name,
  16800.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16801.         PK_COLUMN_PROPID    = convert(int,null),
  16802.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16803.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16804.         FK_TABLE_NAME         = o2.name,
  16805.         FK_COLUMN_NAME         = c2.name,
  16806.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16807.         FK_COLUMN_PROPID    = convert(int,null),
  16808.         ORDINAL                = convert(int,16),
  16809.         UPDATE_RULE            = N'NO ACTION',
  16810.         DELETE_RULE         = N'NO ACTION',
  16811.         PK_NAME                = object_name(s.constid),
  16812.         FK_NAME                = object_name(r.constid),
  16813.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  16814.     from
  16815.         sysobjects o1, sysobjects o2,
  16816.         syscolumns c1, syscolumns c2,
  16817.         sysreferences r, sysconstraints s
  16818.     where    
  16819.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16820.     and    o2.name = @fk_table_name
  16821.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16822.     and    o2.id = r.fkeyid
  16823.     and    r.keycnt >= 16
  16824.     and    o2.id = c2.id
  16825.     and    c2.colid = r.fkey16
  16826.     and r.rkeyid = o1.id
  16827.     and s.id = o1.id and (s.status & 0xf) = 1
  16828.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16829.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16830.     and    o1.id = c1.id    
  16831.     and    c1.colid = r.rkey16
  16832.     order by 8,9,2,3,13
  16833. go
  16834. dump tran master with no_log
  16835. go
  16836. CREATE PROCEDURE sp_foreign_keys_rowset;3
  16837.     (
  16838.     @pk_table_schema    sysname = null,
  16839.     @pk_table_catalog    sysname = null,
  16840.     @fk_table_schema    sysname = null,
  16841.     @fk_table_catalog    sysname = null
  16842.     )
  16843. as
  16844.     select
  16845.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16846.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16847.         PK_TABLE_NAME         = o1.name,
  16848.         PK_COLUMN_NAME         = c1.name,
  16849.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16850.         PK_COLUMN_PROPID    = convert(int,null),
  16851.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16852.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16853.         FK_TABLE_NAME         = o2.name,
  16854.         FK_COLUMN_NAME         = c2.name,
  16855.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16856.         FK_COLUMN_PROPID    = convert(int,null),
  16857.         ORDINAL                = convert(int,1),
  16858.         UPDATE_RULE            = N'NO ACTION',
  16859.         DELETE_RULE         = N'NO ACTION',
  16860.         PK_NAME                = object_name(s.constid),
  16861.         FK_NAME                = object_name(r.constid),
  16862.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  16863.     from
  16864.         sysobjects o1, sysobjects o2,
  16865.         syscolumns c1, syscolumns c2,
  16866.         sysreferences r, sysconstraints s
  16867.     where    
  16868.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16869.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16870.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16871.     and    o1.id = r.rkeyid
  16872.     and s.id = o1.id and (s.status & 0xf) = 1
  16873.     and    o1.id = c1.id
  16874.     and    c1.colid = r.rkey1
  16875.     and r.fkeyid = o2.id
  16876.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16877.     and    o2.id = c2.id    
  16878.     and    c2.colid = r.fkey1
  16879.     union all
  16880.     select
  16881.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16882.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16883.         PK_TABLE_NAME         = o1.name,
  16884.         PK_COLUMN_NAME         = c1.name,
  16885.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16886.         PK_COLUMN_PROPID    = convert(int,null),
  16887.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16888.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16889.         FK_TABLE_NAME         = o2.name,
  16890.         FK_COLUMN_NAME         = c2.name,
  16891.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16892.         FK_COLUMN_PROPID    = convert(int,null),
  16893.         ORDINAL                = convert(int,2),
  16894.         UPDATE_RULE            = N'NO ACTION',
  16895.         DELETE_RULE         = N'NO ACTION',
  16896.         PK_NAME                = object_name(s.constid),
  16897.         FK_NAME                = object_name(r.constid),
  16898.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16899.     from
  16900.         sysobjects o1, sysobjects o2,
  16901.         syscolumns c1, syscolumns c2,
  16902.         sysreferences r, sysconstraints s
  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    o1.id = r.rkeyid
  16908.     and s.id = o1.id and (s.status & 0xf) = 1
  16909.     and    r.keycnt >= 2
  16910.     and    o1.id = c1.id
  16911.     and    c1.colid = r.rkey2
  16912.     and r.fkeyid = o2.id
  16913.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16914.     and    o2.id = c2.id    
  16915.     and    c2.colid = r.fkey2
  16916.     union all
  16917.     select
  16918.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16919.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16920.         PK_TABLE_NAME         = o1.name,
  16921.         PK_COLUMN_NAME         = c1.name,
  16922.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16923.         PK_COLUMN_PROPID    = convert(int,null),
  16924.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16925.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16926.         FK_TABLE_NAME         = o2.name,
  16927.         FK_COLUMN_NAME         = c2.name,
  16928.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16929.         FK_COLUMN_PROPID    = convert(int,null),
  16930.         ORDINAL                = convert(int,3),
  16931.         UPDATE_RULE            = N'NO ACTION',
  16932.         DELETE_RULE         = N'NO ACTION',
  16933.         PK_NAME                = object_name(s.constid),
  16934.         FK_NAME                = object_name(r.constid),
  16935.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16936.     from
  16937.         sysobjects o1, sysobjects o2,
  16938.         syscolumns c1, syscolumns c2,
  16939.         sysreferences r, sysconstraints s
  16940.     where    
  16941.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16942.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16943.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16944.     and    o1.id = r.rkeyid
  16945.     and s.id = o1.id and (s.status & 0xf) = 1
  16946.     and    r.keycnt >= 3
  16947.     and    o1.id = c1.id
  16948.     and    c1.colid = r.rkey3
  16949.     and r.fkeyid = o2.id
  16950.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16951.     and    o2.id = c2.id    
  16952.     and    c2.colid = r.fkey3
  16953.     union all
  16954.     select
  16955.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16956.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16957.         PK_TABLE_NAME         = o1.name,
  16958.         PK_COLUMN_NAME         = c1.name,
  16959.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16960.         PK_COLUMN_PROPID    = convert(int,null),
  16961.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16962.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16963.         FK_TABLE_NAME         = o2.name,
  16964.         FK_COLUMN_NAME         = c2.name,
  16965.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16966.         FK_COLUMN_PROPID    = convert(int,null),
  16967.         ORDINAL                = convert(int,4),
  16968.         UPDATE_RULE            = N'NO ACTION',
  16969.         DELETE_RULE         = N'NO ACTION',
  16970.         PK_NAME                = object_name(s.constid),
  16971.         FK_NAME                = object_name(r.constid),
  16972.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16973.     from
  16974.         sysobjects o1, sysobjects o2,
  16975.         syscolumns c1, syscolumns c2,
  16976.         sysreferences r, sysconstraints s
  16977.     where    
  16978.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16979.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16980.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16981.     and    o1.id = r.rkeyid
  16982.     and s.id = o1.id and (s.status & 0xf) = 1
  16983.     and    r.keycnt >= 4
  16984.     and    o1.id = c1.id
  16985.     and    c1.colid = r.rkey4
  16986.     and r.fkeyid = o2.id
  16987.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16988.     and    o2.id = c2.id    
  16989.     and    c2.colid = r.fkey4
  16990.     union all
  16991.     select
  16992.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16993.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16994.         PK_TABLE_NAME         = o1.name,
  16995.         PK_COLUMN_NAME         = c1.name,
  16996.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16997.         PK_COLUMN_PROPID    = convert(int,null),
  16998.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16999.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17000.         FK_TABLE_NAME         = o2.name,
  17001.         FK_COLUMN_NAME         = c2.name,
  17002.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17003.         FK_COLUMN_PROPID    = convert(int,null),
  17004.         ORDINAL                = convert(int,5),
  17005.         UPDATE_RULE            = N'NO ACTION',
  17006.         DELETE_RULE         = N'NO ACTION',
  17007.         PK_NAME                = object_name(s.constid),
  17008.         FK_NAME                = object_name(r.constid),
  17009.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17010.     from
  17011.         sysobjects o1, sysobjects o2,
  17012.         syscolumns c1, syscolumns c2,
  17013.         sysreferences r, sysconstraints s
  17014.     where    
  17015.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17016.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17017.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17018.     and    o1.id = r.rkeyid
  17019.     and s.id = o1.id and (s.status & 0xf) = 1
  17020.     and    r.keycnt >= 5
  17021.     and    o1.id = c1.id
  17022.     and    c1.colid = r.rkey5
  17023.     and r.fkeyid = o2.id
  17024.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17025.     and    o2.id = c2.id    
  17026.     and    c2.colid = r.fkey5
  17027.     union all
  17028.     select
  17029.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17030.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17031.         PK_TABLE_NAME         = o1.name,
  17032.         PK_COLUMN_NAME         = c1.name,
  17033.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17034.         PK_COLUMN_PROPID    = convert(int,null),
  17035.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17036.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17037.         FK_TABLE_NAME         = o2.name,
  17038.         FK_COLUMN_NAME         = c2.name,
  17039.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17040.         FK_COLUMN_PROPID    = convert(int,null),
  17041.         ORDINAL                = convert(int,6),
  17042.         UPDATE_RULE            = N'NO ACTION',
  17043.         DELETE_RULE         = N'NO ACTION',
  17044.         PK_NAME                = object_name(s.constid),
  17045.         FK_NAME                = object_name(r.constid),
  17046.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17047.     from
  17048.         sysobjects o1, sysobjects o2,
  17049.         syscolumns c1, syscolumns c2,
  17050.         sysreferences r, sysconstraints s
  17051.     where    
  17052.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17053.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17054.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17055.     and    o1.id = r.rkeyid
  17056.     and s.id = o1.id and (s.status & 0xf) = 1
  17057.     and    r.keycnt >= 6
  17058.     and    o1.id = c1.id
  17059.     and    c1.colid = r.rkey6
  17060.     and r.fkeyid = o2.id
  17061.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17062.     and    o2.id = c2.id    
  17063.     and    c2.colid = r.fkey6
  17064.     union all
  17065.     select
  17066.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17067.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17068.         PK_TABLE_NAME         = o1.name,
  17069.         PK_COLUMN_NAME         = c1.name,
  17070.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17071.         PK_COLUMN_PROPID    = convert(int,null),
  17072.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17073.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17074.         FK_TABLE_NAME         = o2.name,
  17075.         FK_COLUMN_NAME         = c2.name,
  17076.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17077.         FK_COLUMN_PROPID    = convert(int,null),
  17078.         ORDINAL                = convert(int,7),
  17079.         UPDATE_RULE            = N'NO ACTION',
  17080.         DELETE_RULE         = N'NO ACTION',
  17081.         PK_NAME                = object_name(s.constid),
  17082.         FK_NAME                = object_name(r.constid),
  17083.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17084.     from
  17085.         sysobjects o1, sysobjects o2,
  17086.         syscolumns c1, syscolumns c2,
  17087.         sysreferences r, sysconstraints s
  17088.     where    
  17089.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17090.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17091.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17092.     and    o1.id = r.rkeyid
  17093.     and s.id = o1.id and (s.status & 0xf) = 1
  17094.     and    r.keycnt >= 7
  17095.     and    o1.id = c1.id
  17096.     and    c1.colid = r.rkey7
  17097.     and r.fkeyid = o2.id
  17098.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17099.     and    o2.id = c2.id    
  17100.     and    c2.colid = r.fkey7
  17101.     union all
  17102.     select
  17103.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17104.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17105.         PK_TABLE_NAME         = o1.name,
  17106.         PK_COLUMN_NAME         = c1.name,
  17107.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17108.         PK_COLUMN_PROPID    = convert(int,null),
  17109.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17110.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17111.         FK_TABLE_NAME         = o2.name,
  17112.         FK_COLUMN_NAME         = c2.name,
  17113.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17114.         FK_COLUMN_PROPID    = convert(int,null),
  17115.         ORDINAL                = convert(int,8),
  17116.         UPDATE_RULE            = N'NO ACTION',
  17117.         DELETE_RULE         = N'NO ACTION',
  17118.         PK_NAME                = object_name(s.constid),
  17119.         FK_NAME                = object_name(r.constid),
  17120.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17121.     from
  17122.         sysobjects o1, sysobjects o2,
  17123.         syscolumns c1, syscolumns c2,
  17124.         sysreferences r, sysconstraints s
  17125.     where    
  17126.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17127.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17128.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17129.     and    o1.id = r.rkeyid
  17130.     and s.id = o1.id and (s.status & 0xf) = 1
  17131.     and    r.keycnt >= 8
  17132.     and    o1.id = c1.id
  17133.     and    c1.colid = r.rkey8
  17134.     and r.fkeyid = o2.id
  17135.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17136.     and    o2.id = c2.id    
  17137.     and    c2.colid = r.fkey8
  17138.     union all
  17139.     select
  17140.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17141.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17142.         PK_TABLE_NAME         = o1.name,
  17143.         PK_COLUMN_NAME         = c1.name,
  17144.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17145.         PK_COLUMN_PROPID    = convert(int,null),
  17146.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17147.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17148.         FK_TABLE_NAME         = o2.name,
  17149.         FK_COLUMN_NAME         = c2.name,
  17150.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17151.         FK_COLUMN_PROPID    = convert(int,null),
  17152.         ORDINAL                = convert(int,9),
  17153.         UPDATE_RULE            = N'NO ACTION',
  17154.         DELETE_RULE         = N'NO ACTION',
  17155.         PK_NAME                = object_name(s.constid),
  17156.         FK_NAME                = object_name(r.constid),
  17157.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17158.     from
  17159.         sysobjects o1, sysobjects o2,
  17160.         syscolumns c1, syscolumns c2,
  17161.         sysreferences r, sysconstraints s
  17162.     where    
  17163.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17164.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17165.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17166.     and    o1.id = r.rkeyid
  17167.     and s.id = o1.id and (s.status & 0xf) = 1
  17168.     and    r.keycnt >= 9
  17169.     and    o1.id = c1.id
  17170.     and    c1.colid = r.rkey9
  17171.     and r.fkeyid = o2.id
  17172.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17173.     and    o2.id = c2.id    
  17174.     and    c2.colid = r.fkey9
  17175.     union all
  17176.     select
  17177.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17178.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17179.         PK_TABLE_NAME         = o1.name,
  17180.         PK_COLUMN_NAME         = c1.name,
  17181.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17182.         PK_COLUMN_PROPID    = convert(int,null),
  17183.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17184.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17185.         FK_TABLE_NAME         = o2.name,
  17186.         FK_COLUMN_NAME         = c2.name,
  17187.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17188.         FK_COLUMN_PROPID    = convert(int,null),
  17189.         ORDINAL                = convert(int,10),
  17190.         UPDATE_RULE            = N'NO ACTION',
  17191.         DELETE_RULE         = N'NO ACTION',
  17192.         PK_NAME                = object_name(s.constid),
  17193.         FK_NAME                = object_name(r.constid),
  17194.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17195.     from
  17196.         sysobjects o1, sysobjects o2,
  17197.         syscolumns c1, syscolumns c2,
  17198.         sysreferences r, sysconstraints s
  17199.     where    
  17200.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17201.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17202.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17203.     and    o1.id = r.rkeyid
  17204.     and s.id = o1.id and (s.status & 0xf) = 1
  17205.     and    r.keycnt >= 10
  17206.     and    o1.id = c1.id
  17207.     and    c1.colid = r.rkey10
  17208.     and r.fkeyid = o2.id
  17209.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17210.     and    o2.id = c2.id    
  17211.     and    c2.colid = r.fkey10
  17212.     union all
  17213.     select
  17214.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17215.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17216.         PK_TABLE_NAME         = o1.name,
  17217.         PK_COLUMN_NAME         = c1.name,
  17218.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17219.         PK_COLUMN_PROPID    = convert(int,null),
  17220.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17221.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17222.         FK_TABLE_NAME         = o2.name,
  17223.         FK_COLUMN_NAME         = c2.name,
  17224.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17225.         FK_COLUMN_PROPID    = convert(int,null),
  17226.         ORDINAL                = convert(int,11),
  17227.         UPDATE_RULE            = N'NO ACTION',
  17228.         DELETE_RULE         = N'NO ACTION',
  17229.         PK_NAME                = object_name(s.constid),
  17230.         FK_NAME                = object_name(r.constid),
  17231.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17232.     from
  17233.         sysobjects o1, sysobjects o2,
  17234.         syscolumns c1, syscolumns c2,
  17235.         sysreferences r, sysconstraints s
  17236.     where    
  17237.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17238.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17239.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17240.     and    o1.id = r.rkeyid
  17241.     and s.id = o1.id and (s.status & 0xf) = 1
  17242.     and    r.keycnt >= 11
  17243.     and    o1.id = c1.id
  17244.     and    c1.colid = r.rkey11
  17245.     and r.fkeyid = o2.id
  17246.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17247.     and    o2.id = c2.id    
  17248.     and    c2.colid = r.fkey11
  17249.     union all
  17250.     select
  17251.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17252.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17253.         PK_TABLE_NAME         = o1.name,
  17254.         PK_COLUMN_NAME         = c1.name,
  17255.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17256.         PK_COLUMN_PROPID    = convert(int,null),
  17257.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17258.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17259.         FK_TABLE_NAME         = o2.name,
  17260.         FK_COLUMN_NAME         = c2.name,
  17261.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17262.         FK_COLUMN_PROPID    = convert(int,null),
  17263.         ORDINAL                = convert(int,12),
  17264.         UPDATE_RULE            = N'NO ACTION',
  17265.         DELETE_RULE         = N'NO ACTION',
  17266.         PK_NAME                = object_name(s.constid),
  17267.         FK_NAME                = object_name(r.constid),
  17268.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17269.     from
  17270.         sysobjects o1, sysobjects o2,
  17271.         syscolumns c1, syscolumns c2,
  17272.         sysreferences r, sysconstraints s
  17273.     where    
  17274.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17275.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17276.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17277.     and    o1.id = r.rkeyid
  17278.     and s.id = o1.id and (s.status & 0xf) = 1
  17279.     and    r.keycnt >= 12
  17280.     and    o1.id = c1.id
  17281.     and    c1.colid = r.rkey12
  17282.     and r.fkeyid = o2.id
  17283.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17284.     and    o2.id = c2.id    
  17285.     and    c2.colid = r.fkey12
  17286.     union all
  17287.     select
  17288.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17289.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17290.         PK_TABLE_NAME         = o1.name,
  17291.         PK_COLUMN_NAME         = c1.name,
  17292.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17293.         PK_COLUMN_PROPID    = convert(int,null),
  17294.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17295.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17296.         FK_TABLE_NAME         = o2.name,
  17297.         FK_COLUMN_NAME         = c2.name,
  17298.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17299.         FK_COLUMN_PROPID    = convert(int,null),
  17300.         ORDINAL                = convert(int,13),
  17301.         UPDATE_RULE            = N'NO ACTION',
  17302.         DELETE_RULE         = N'NO ACTION',
  17303.         PK_NAME                = object_name(s.constid),
  17304.         FK_NAME                = object_name(r.constid),
  17305.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17306.     from
  17307.         sysobjects o1, sysobjects o2,
  17308.         syscolumns c1, syscolumns c2,
  17309.         sysreferences r, sysconstraints s
  17310.     where    
  17311.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17312.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17313.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17314.     and    o1.id = r.rkeyid
  17315.     and s.id = o1.id and (s.status & 0xf) = 1
  17316.     and    r.keycnt >= 13
  17317.     and    o1.id = c1.id
  17318.     and    c1.colid = r.rkey13
  17319.     and r.fkeyid = o2.id
  17320.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17321.     and    o2.id = c2.id    
  17322.     and    c2.colid = r.fkey13
  17323.     union all
  17324.     select
  17325.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17326.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17327.         PK_TABLE_NAME         = o1.name,
  17328.         PK_COLUMN_NAME         = c1.name,
  17329.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17330.         PK_COLUMN_PROPID    = convert(int,null),
  17331.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17332.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17333.         FK_TABLE_NAME         = o2.name,
  17334.         FK_COLUMN_NAME         = c2.name,
  17335.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17336.         FK_COLUMN_PROPID    = convert(int,null),
  17337.         ORDINAL                = convert(int,14),
  17338.         UPDATE_RULE            = N'NO ACTION',
  17339.         DELETE_RULE         = N'NO ACTION',
  17340.         PK_NAME                = object_name(s.constid),
  17341.         FK_NAME                = object_name(r.constid),
  17342.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17343.     from
  17344.         sysobjects o1, sysobjects o2,
  17345.         syscolumns c1, syscolumns c2,
  17346.         sysreferences r, sysconstraints s
  17347.     where    
  17348.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17349.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17350.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17351.     and    o1.id = r.rkeyid
  17352.     and s.id = o1.id and (s.status & 0xf) = 1
  17353.     and    r.keycnt >= 14
  17354.     and    o1.id = c1.id
  17355.     and    c1.colid = r.rkey14
  17356.     and r.fkeyid = o2.id
  17357.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17358.     and    o2.id = c2.id    
  17359.     and    c2.colid = r.fkey14
  17360.     union all
  17361.     select
  17362.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17363.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17364.         PK_TABLE_NAME         = o1.name,
  17365.         PK_COLUMN_NAME         = c1.name,
  17366.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17367.         PK_COLUMN_PROPID    = convert(int,null),
  17368.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17369.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17370.         FK_TABLE_NAME         = o2.name,
  17371.         FK_COLUMN_NAME         = c2.name,
  17372.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17373.         FK_COLUMN_PROPID    = convert(int,null),
  17374.         ORDINAL                = convert(int,15),
  17375.         UPDATE_RULE            = N'NO ACTION',
  17376.         DELETE_RULE         = N'NO ACTION',
  17377.         PK_NAME                = object_name(s.constid),
  17378.         FK_NAME                = object_name(r.constid),
  17379.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17380.     from
  17381.         sysobjects o1, sysobjects o2,
  17382.         syscolumns c1, syscolumns c2,
  17383.         sysreferences r, sysconstraints s
  17384.     where    
  17385.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17386.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17387.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17388.     and    o1.id = r.rkeyid
  17389.     and s.id = o1.id and (s.status & 0xf) = 1
  17390.     and    r.keycnt >= 15
  17391.     and    o1.id = c1.id
  17392.     and    c1.colid = r.rkey15
  17393.     and r.fkeyid = o2.id
  17394.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17395.     and    o2.id = c2.id    
  17396.     and    c2.colid = r.fkey15
  17397.     union all
  17398.     select
  17399.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17400.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17401.         PK_TABLE_NAME         = o1.name,
  17402.         PK_COLUMN_NAME         = c1.name,
  17403.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17404.         PK_COLUMN_PROPID    = convert(int,null),
  17405.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17406.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17407.         FK_TABLE_NAME         = o2.name,
  17408.         FK_COLUMN_NAME         = c2.name,
  17409.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17410.         FK_COLUMN_PROPID    = convert(int,null),
  17411.         ORDINAL                = convert(int,16),
  17412.         UPDATE_RULE            = N'NO ACTION',
  17413.         DELETE_RULE         = N'NO ACTION',
  17414.         PK_NAME                = object_name(s.constid),
  17415.         FK_NAME                = object_name(r.constid),
  17416.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17417.     from
  17418.         sysobjects o1, sysobjects o2,
  17419.         syscolumns c1, syscolumns c2,
  17420.         sysreferences r, sysconstraints s
  17421.     where    
  17422.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17423.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17424.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17425.     and    o1.id = r.rkeyid
  17426.     and s.id = o1.id and (s.status & 0xf) = 1
  17427.     and    o1.id = c1.id
  17428.     and    r.keycnt >= 16
  17429.     and    c1.colid = r.rkey16
  17430.     and r.fkeyid = o2.id
  17431.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17432.     and    o2.id = c2.id    
  17433.     and    c2.colid = r.fkey16
  17434.     order by 8,9,2,3,13
  17435. go
  17436. dump tran master with no_log
  17437. go
  17438. create procedure sp_foreign_keys_rowset;5
  17439.     (
  17440.     @server_name        sysname,
  17441.     @pk_catalog_name    sysname = null,
  17442.     @fk_catalog_name    sysname = null,
  17443.     @pk_table_name        sysname = null,
  17444.     @pk_table_schema    sysname = null,
  17445.     @fk_table_name        sysname = null,
  17446.     @fk_table_schema    sysname = null
  17447.     )
  17448. as
  17449.     select
  17450.         PK_TABLE_CATALOG,
  17451.         PK_TABLE_SCHEMA,    
  17452.         PK_TABLE_NAME,     
  17453.         PK_COLUMN_NAME,     
  17454.         PK_COLUMN_GUID,    
  17455.         PK_COLUMN_PROPID,
  17456.         FK_TABLE_CATALOG,
  17457.         FK_TABLE_SCHEMA, 
  17458.         FK_TABLE_NAME,     
  17459.         FK_COLUMN_NAME,     
  17460.         FK_COLUMN_GUID,    
  17461.         FK_COLUMN_PROPID,
  17462.         ORDINAL,        
  17463.         UPDATE_RULE,    
  17464.         DELETE_RULE
  17465.         --PK_NAME,        
  17466.         --FK_NAME,
  17467.         --DEFERRABILITY    
  17468.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  17469.                 @server_name,
  17470.                 @pk_catalog_name,
  17471.                 @pk_table_schema,
  17472.                 @pk_table_name,
  17473.                 @fk_catalog_name,
  17474.                 @fk_table_schema,
  17475.                 @fk_table_name >
  17476.     order by 7,8,9,1,2,3,13
  17477. go
  17478.  
  17479. grant execute on sp_foreign_keys_rowset to public
  17480. go
  17481.  
  17482. dump tran master with no_log
  17483. go
  17484. if (charindex('6.00', @@version) > 0)
  17485.     begin
  17486.     if (exists (select * from sysobjects
  17487.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  17488.         begin
  17489.         drop procedure sp_foreign_keys_rowset
  17490.         dump tran master with no_log
  17491.         end
  17492.     end
  17493. go
  17494.  
  17495.  
  17496. print ''
  17497. print 'creating sp_indexes_rowset'
  17498. go
  17499.  
  17500. /*    6.0 and 6.5 version */
  17501. create procedure sp_indexes_rowset
  17502.     (
  17503.     @table_name    varchar(255), 
  17504.     @index_name    varchar(255) = null,
  17505.     @table_schema    varchar(255) = null     
  17506.     )
  17507. as
  17508.     select    TABLE_CATALOG        = db_name(),
  17509.         TABLE_SCHEMA        = user_name(o.uid),
  17510.         TABLE_NAME        = o.name,
  17511.         INDEX_CATALOG        = db_name(),        
  17512.         INDEX_SCHEMA        = user_name(o.uid),
  17513.         INDEX_NAME        = x.name,
  17514.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17515.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17516.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17517.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17518.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17519.         INITIAL_SIZE        = convert(int,null),
  17520.         NULLS            = convert(int,null),
  17521.         SORT_BOOKMARKS        = convert(bit,0),
  17522.         AUTO_UPDATE        = convert(bit,1),
  17523.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17524.         ORDINAL_POSITION    = convert(int,c.colid),
  17525.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17526.         COLUMN_GUID        = convert(binary(16),null),
  17527.         COLUMN_PROPID        = convert(int,null),
  17528.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17529.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17530.         PAGES            = convert(int, x.dpages),
  17531.         FILTER_CONDITION    = convert(varchar(1),null),
  17532.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  17533.         
  17534.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  17535.     where    o.type in ('U')
  17536.     and     o.name = @table_name
  17537.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17538.     and    (@index_name is null or @index_name = x.name)
  17539.     and     x.id = o.id
  17540.     and     x.id = c.id
  17541.     and      c.colid < x.keycnt+(x.status&16)/16
  17542.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17543.     and     (
  17544.         suser_id() = 1     /* User is the System Administrator */
  17545.         or o.uid = user_id()     /* User created the object */
  17546.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17547.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17548.             from sysprotects p
  17549.             where p.id = o.id
  17550.             /* get rows for public,current user,user's group */
  17551.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17552.             /* check for SELECT,EXECUTE privilege */
  17553.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17554.             ) = 1    /* final magic...compare Grants      */
  17555.            )
  17556.     order by 8 desc, 4, 5, 6, 17
  17557. go
  17558. dump tran master with no_log
  17559. go
  17560. create procedure sp_indexes_rowset;2
  17561.     (
  17562.     @index_name    varchar(255) = null,
  17563.     @table_schema    varchar(255) = null     
  17564.     )
  17565. as
  17566.     select    TABLE_CATALOG        = db_name(),
  17567.         TABLE_SCHEMA        = user_name(o.uid),
  17568.         TABLE_NAME        = o.name,
  17569.         INDEX_CATALOG        = db_name(),        
  17570.         INDEX_SCHEMA        = user_name(o.uid),
  17571.         INDEX_NAME        = x.name,
  17572.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17573.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17574.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17575.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17576.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17577.         INITIAL_SIZE        = convert(int,null),
  17578.         NULLS            = convert(int,null),
  17579.         SORT_BOOKMARKS        = convert(bit,0),
  17580.         AUTO_UPDATE        = convert(bit,1),
  17581.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17582.         ORDINAL_POSITION    = convert(int,c.colid),
  17583.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17584.         COLUMN_GUID        = convert(binary(16),null),
  17585.         COLUMN_PROPID        = convert(int,null),
  17586.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17587.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17588.         PAGES            = convert(int, x.dpages),
  17589.         FILTER_CONDITION    = convert(varchar(1),null),
  17590.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  17591.         
  17592.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  17593.     where    o.type in ('U')
  17594.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17595.     and    (@index_name is null or @index_name = x.name)
  17596.     and     x.id = o.id
  17597.     and     x.id = c.id
  17598.     and      c.colid < x.keycnt+(x.status&16)/16
  17599.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17600.     and     (
  17601.         suser_id() = 1     /* User is the System Administrator */
  17602.         or o.uid = user_id()     /* User created the object */
  17603.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17604.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17605.             from sysprotects p
  17606.             where p.id = o.id
  17607.             /* get rows for public,current user,user's group */
  17608.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17609.             /* check for SELECT,EXECUTE privilege */
  17610.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17611.             ) = 1    /* final magic...compare Grants      */
  17612.            )
  17613.     order by 8 desc, 4, 5, 6, 17
  17614. go
  17615. dump tran master with no_log
  17616. go
  17617.  
  17618.  
  17619. if (charindex('7.00', @@version) > 0)
  17620.     drop procedure sp_indexes_rowset
  17621. else
  17622. begin
  17623.     print ''
  17624.     print ''
  17625.     print 'Warning:'
  17626.     print 'you are installing the stored procedures '
  17627.     print 'on a pre 7.0 SQL Server.'
  17628.     print 'Ignore the following errors.'
  17629. end
  17630. go
  17631.  
  17632. /*     7.0 version */
  17633. create procedure sp_indexes_rowset
  17634.     (
  17635.     @table_name    sysname, 
  17636.     @index_name    sysname = null,
  17637.     @table_schema    sysname = null     
  17638.     )
  17639. as
  17640.     select    TABLE_CATALOG        = db_name(),
  17641.         TABLE_SCHEMA        = user_name(o.uid),
  17642.         TABLE_NAME        = o.name,
  17643.         INDEX_CATALOG        = db_name(),        
  17644.         INDEX_SCHEMA        = user_name(o.uid),
  17645.         INDEX_NAME        = x.name,
  17646.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17647.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17648.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17649.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17650.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17651.         INITIAL_SIZE        = convert(int,null),
  17652.         NULLS            = convert(int,null),
  17653.         SORT_BOOKMARKS        = convert(bit,0),
  17654.         AUTO_UPDATE        = convert(bit,1),
  17655.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17656.         ORDINAL_POSITION    = convert(int,c.colid),
  17657.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17658.         COLUMN_GUID        = convert(uniqueidentifier,null),
  17659.         COLUMN_PROPID        = convert(int,null),
  17660.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17661.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17662.         PAGES            = convert(int, x.dpages),
  17663.         FILTER_CONDITION    = convert(nvarchar(1),null),
  17664.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  17665.         
  17666.     from    sysobjects o, sysindexes x, syscolumns c
  17667.     where    o.type in ('U')
  17668.     and     o.name = @table_name
  17669.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17670.     and    (@index_name is null or @index_name = x.name)
  17671.     and     x.id = o.id
  17672.     and     x.id = c.id
  17673.     and      c.colid < x.keycnt+(x.status&16)/16
  17674.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  17675.     and     permissions(o.id) <> 0
  17676.     and     (x.status&32) = 0  -- No hypothetical indexes
  17677.     order by 8 desc, 4, 5, 6, 17
  17678. go
  17679. dump tran master with no_log
  17680. go
  17681. create procedure sp_indexes_rowset;2
  17682.     (
  17683.     @index_name    sysname = null,
  17684.     @table_schema    sysname = null     
  17685.     )
  17686. as
  17687.     select    TABLE_CATALOG        = db_name(),
  17688.         TABLE_SCHEMA        = user_name(o.uid),
  17689.         TABLE_NAME        = o.name,
  17690.         INDEX_CATALOG        = db_name(),        
  17691.         INDEX_SCHEMA        = user_name(o.uid),
  17692.         INDEX_NAME        = x.name,
  17693.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17694.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17695.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17696.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17697.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17698.         INITIAL_SIZE        = convert(int,null),
  17699.         NULLS            = convert(int,null),
  17700.         SORT_BOOKMARKS        = convert(bit,0),
  17701.         AUTO_UPDATE        = convert(bit,1),
  17702.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17703.         ORDINAL_POSITION    = convert(int,c.colid),
  17704.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17705.         COLUMN_GUID        = convert(uniqueidentifier,null),
  17706.         COLUMN_PROPID        = convert(int,null),
  17707.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17708.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17709.         PAGES            = convert(int, x.dpages),
  17710.         FILTER_CONDITION    = convert(nvarchar(1),null),
  17711.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  17712.         
  17713.     from    sysobjects o, sysindexes x, syscolumns c
  17714.     where    o.type in ('U')
  17715.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17716.     and    (@index_name is null or @index_name = x.name)
  17717.     and     x.id = o.id
  17718.     and     x.id = c.id
  17719.     and      c.colid < x.keycnt+(x.status&16)/16
  17720.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  17721.     and     permissions(o.id) <> 0
  17722.     and     (x.status&32) = 0  -- No hypothetical indexes
  17723.     order by 8 desc, 4, 5, 6, 17
  17724. go
  17725. dump tran master with no_log
  17726. go
  17727. create procedure sp_indexes_rowset;5
  17728.     (
  17729.     @table_server        sysname,
  17730.     @table_catalog        sysname = null,
  17731.     @table_name        sysname = null,
  17732.     @index_name        sysname = null,
  17733.     @table_schema        sysname = null
  17734.     )
  17735. as
  17736.     select
  17737.         TABLE_CATALOG,    
  17738.         TABLE_SCHEMA,    
  17739.         TABLE_NAME,    
  17740.         INDEX_CATALOG,        
  17741.         INDEX_SCHEMA,    
  17742.         INDEX_NAME,    
  17743.         PRIMARY_KEY,    
  17744.         "UNIQUE",    
  17745.         "CLUSTERED",    
  17746.         "TYPE",        
  17747.         FILL_FACTOR,    
  17748.         INITIAL_SIZE,    
  17749.         NULLS,        
  17750.         SORT_BOOKMARKS,    
  17751.         AUTO_UPDATE,    
  17752.         NULL_COLLATION,    
  17753.         ORDINAL_POSITION,
  17754.         COLUMN_NAME,    
  17755.         COLUMN_GUID,    
  17756.         COLUMN_PROPID,    
  17757.         COLLATION,    
  17758.         CARDINALITY,    
  17759.         PAGES,        
  17760.         FILTER_CONDITION
  17761.     --    INTEGRATED
  17762.     from master.dbo.SYSREMOTE_INDEXES <
  17763.                 @table_server,
  17764.                 @table_catalog,
  17765.                 @table_schema,
  17766.                         @index_name,
  17767.                 NULL,            /* TYPE (index type) */
  17768.                 @table_name >
  17769.     order by 8 desc, 4, 5, 6, 17
  17770. go
  17771.  
  17772. grant execute on sp_indexes_rowset to public
  17773. go
  17774.  
  17775. dump tran master with no_log
  17776. go
  17777. if (charindex('6.00', @@version) > 0)
  17778.     begin
  17779.     if (exists (select * from sysobjects
  17780.             where name = 'sp_indexes_rowset' and type = 'P '))
  17781.         begin
  17782.         drop procedure sp_indexes_rowset
  17783.         dump tran master with no_log
  17784.         end
  17785.     end
  17786. go
  17787.  
  17788.  
  17789. print ''
  17790. print 'creating sp_primary_keys_rowset'
  17791. go
  17792.  
  17793. /*    Procedure for 6.0 and 6.5 servers */
  17794. create procedure sp_primary_keys_rowset
  17795.     (
  17796.        @table_name        varchar(255),
  17797.     @table_schema         varchar(244) = null
  17798.     )
  17799. as
  17800.     select    TABLE_CATALOG    = db_name(),
  17801.             TABLE_SCHEMA    = user_name(o.uid),
  17802.             TABLE_NAME        = o.name,    
  17803.             COLUMN_NAME        = c.name,
  17804.             COLUMN_GUID        = convert(binary(16),null),
  17805.             COLUMN_PROPID    = convert(int,null),
  17806.             ORDINAL            = convert(int,c1.colid),
  17807.             PK_NAME            = i.name
  17808.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17809.     where    o.type in ('U')
  17810.     and     o.name = @table_name
  17811.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17812.     and     o.id = c.id
  17813.     and     o.id = i.id
  17814.     and     (i.status & 0x800) = 0x800
  17815.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17816.     and     c1.id = c.id
  17817.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17818.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17819.     and     (
  17820.         suser_id() = 1     /* User is the System Administrator */
  17821.         or o.uid = user_id()     /* User created the object */
  17822.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17823.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17824.             from sysprotects p
  17825.             where p.id = o.id
  17826.             /* get rows for public,current user,user's group */
  17827.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17828.             /* check for SELECT,EXECUTE privilege */
  17829.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17830.             ) = 1    /* final magic...compare Grants      */
  17831.            )
  17832.     order by 2, 3
  17833. go
  17834. dump tran master with no_log
  17835. go
  17836. create procedure sp_primary_keys_rowset;2
  17837.     (
  17838.      @table_schema         varchar(244) = null
  17839.     )
  17840. as
  17841.     select    TABLE_CATALOG    = db_name(),
  17842.             TABLE_SCHEMA    = user_name(o.uid),
  17843.             TABLE_NAME        = o.name,    
  17844.             COLUMN_NAME        = c.name,
  17845.             COLUMN_GUID        = convert(binary(16),null),
  17846.             COLUMN_PROPID    = convert(int,null),
  17847.             ORDINAL            = convert(int,c1.colid),
  17848.             PK_NAME            = i.name
  17849.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17850.     where    o.type in ('U')
  17851.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17852.     and     o.id = c.id
  17853.     and     o.id = i.id
  17854.     and     (i.status & 0x800) = 0x800
  17855.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17856.     and     c1.id = c.id
  17857.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17858.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17859.     and     (
  17860.         suser_id() = 1     /* User is the System Administrator */
  17861.         or o.uid = user_id()     /* User created the object */
  17862.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17863.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17864.             from sysprotects p
  17865.             where p.id = o.id
  17866.             /* get rows for public,current user,user's group */
  17867.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17868.             /* check for SELECT,EXECUTE privilege */
  17869.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17870.             ) = 1    /* final magic...compare Grants      */
  17871.            )
  17872.     order by 2, 3
  17873. go
  17874.  
  17875. if (charindex('7.00', @@version) = 0)
  17876. begin
  17877.     print ''
  17878.     print ''
  17879.     print 'Warning:'
  17880.     print 'you are installing the stored procedures '
  17881.     print 'on a pre 7.0 SQL Server.'
  17882.     print 'Ignore the following errors.'
  17883. end
  17884. else
  17885.     drop proc sp_primary_keys_rowset
  17886. go
  17887.  
  17888. /*    Procedure for 7.0 servers */
  17889. create procedure sp_primary_keys_rowset
  17890.     (
  17891.        @table_name        sysname,
  17892.     @table_schema         sysname = null
  17893.     )
  17894. as
  17895.     select    TABLE_CATALOG    = db_name(),
  17896.             TABLE_SCHEMA    = user_name(o.uid),
  17897.             TABLE_NAME        = o.name,    
  17898.             COLUMN_NAME        = c.name,
  17899.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17900.             COLUMN_PROPID    = convert(int,null),
  17901.             ORDINAL            = convert(int,c1.colid),
  17902.             PK_NAME            = i.name
  17903.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17904.     where    o.type in ('U')
  17905.     and     o.name = @table_name
  17906.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17907.     and     o.id = c.id
  17908.     and     o.id = i.id
  17909.     and     (i.status & 0x800) = 0x800
  17910.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17911.     and     c1.id = c.id
  17912.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17913.     and    permissions(o.id) <> 0
  17914.     order by 2, 3
  17915. go
  17916. dump tran master with no_log
  17917. go
  17918. create procedure sp_primary_keys_rowset;2
  17919.     (
  17920.      @table_schema     sysname = null
  17921.     )
  17922. as
  17923.     select    TABLE_CATALOG    = db_name(),
  17924.             TABLE_SCHEMA    = user_name(o.uid),
  17925.             TABLE_NAME        = o.name,    
  17926.             COLUMN_NAME        = c.name,
  17927.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17928.             COLUMN_PROPID    = convert(int,null),
  17929.             ORDINAL            = convert(int,c1.colid),
  17930.             PK_NAME            = i.name
  17931.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17932.     where    o.type in ('U')
  17933.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17934.     and     o.id = c.id
  17935.     and     o.id = i.id
  17936.     and     (i.status & 0x800) = 0x800
  17937.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17938.     and     c1.id = c.id
  17939.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17940.     and    permissions(o.id) <> 0
  17941.     order by 2, 3
  17942. go
  17943. dump tran master with no_log
  17944. go
  17945. create procedure sp_primary_keys_rowset;3
  17946.     (
  17947.        @table_name        sysname = null,
  17948.     @table_schema         sysname = null
  17949.     )
  17950. as
  17951. IF @table_name is not NULL
  17952.     BEGIN
  17953.     select    TABLE_CATALOG    = db_name(),
  17954.             TABLE_SCHEMA    = user_name(o.uid),
  17955.             TABLE_NAME        = o.name,    
  17956.             COLUMN_NAME        = c.name,
  17957.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17958.             COLUMN_PROPID    = convert(int,null),
  17959.             ORDINAL            = convert(int,c1.colid),
  17960.             PK_NAME            = i.name
  17961.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17962.     where    o.type in ('U')
  17963.     and     o.name = @table_name
  17964.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17965.     and     o.id = c.id
  17966.     and     o.id = i.id
  17967.     and     (i.status & 0x800) = 0x800
  17968.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17969.     and     c1.id = c.id
  17970.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17971.     and    permissions(o.id) <> 0
  17972.     END
  17973. ELSE
  17974.     BEGIN
  17975.     select    TABLE_CATALOG    = db_name(),
  17976.             TABLE_SCHEMA    = user_name(o.uid),
  17977.             TABLE_NAME        = o.name,    
  17978.             COLUMN_NAME        = c.name,
  17979.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17980.             COLUMN_PROPID    = convert(int,null),
  17981.             ORDINAL            = convert(int,c1.colid),
  17982.             PK_NAME            = i.name
  17983.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17984.     where    o.type in ('U')
  17985.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17986.     and     o.id = c.id
  17987.     and     o.id = i.id
  17988.     and     (i.status & 0x800) = 0x800
  17989.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17990.     and     c1.id = c.id
  17991.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17992.     and    permissions(o.id) <> 0
  17993.     END
  17994. go
  17995. dump tran master with no_log
  17996. go
  17997. create procedure sp_primary_keys_rowset;5
  17998.     (
  17999.     @table_server        sysname,
  18000.     @table_catalog        sysname = null,
  18001.     @table_name            sysname = null,
  18002.     @table_schema        sysname = null
  18003.     )
  18004. as
  18005.     select
  18006.         TABLE_CATALOG,    
  18007.         TABLE_SCHEMA,    
  18008.         TABLE_NAME,    
  18009.         COLUMN_NAME,    
  18010.         COLUMN_GUID,    
  18011.         COLUMN_PROPID,    
  18012.         ORDINAL
  18013.         --PK_NAME        
  18014.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  18015.                 @table_server,
  18016.                 @table_catalog,
  18017.                 @table_schema,
  18018.                 @table_name >
  18019.     order by 1,2,3
  18020. go
  18021.  
  18022. grant execute on sp_primary_keys_rowset to public
  18023. go
  18024.  
  18025. dump tran master with no_log
  18026. go
  18027. if (charindex('6.00', @@version) > 0)
  18028.     begin
  18029.     if (exists (select * from sysobjects
  18030.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  18031.         begin
  18032.         drop procedure sp_primary_keys_rowset
  18033.         dump tran master with no_log
  18034.         end
  18035.     end
  18036. go
  18037.  
  18038.  
  18039. print ''
  18040. print 'creating sp_provider_types_rowset'
  18041. go
  18042.  
  18043. /*    Procedure for 6.0 and 6.50 servers */
  18044. create proc sp_provider_types_rowset
  18045.     (
  18046.     @data_type     smallint = null,
  18047.     @best_match    tinyint  = null
  18048.     )
  18049. as
  18050.     select
  18051.         TYPE_NAME         = case when t.usertype = 80 then t.name 
  18052.                         else d.type_name 
  18053.                         end,
  18054.         DATA_TYPE         = d.oledb_data_type,                            
  18055.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18056.                             then @@max_precision
  18057.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  18058.         LITERAL_PREFIX         = d.literal_prefix,
  18059.         LITERAL_SUFFIX         = d.literal_suffix,
  18060.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  18061.         IS_NULLABLE        = t.allownulls,
  18062.         CASE_SENSITIVE        = d.case_sensitive,
  18063.         SEARCHABLE         = d.searchable,
  18064.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  18065.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  18066.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  18067.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  18068.                         else d.local_type_name
  18069.                         end,
  18070.         MINIMUM_SCALE        = convert(smallint,
  18071.                         case 
  18072.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  18073.                         else null 
  18074.                         end),
  18075.         MAXIMUM_SCALE        = convert(smallint,
  18076.                         case 
  18077.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18078.                         then @@max_precision
  18079.                         else null 
  18080.                         end),
  18081.         GUID            = convert(binary(16),null),
  18082.         TYPELIB            = convert(varchar(1),null),
  18083.         VERSION            = convert(varchar(1),null),
  18084.         IS_LONG            = d.is_long,
  18085.         BEST_MATCH        = case when t.usertype = 80
  18086.                         then convert(bit,0)
  18087.                         else d.best_match
  18088.                         end,
  18089.         IS_FIXEDLENGTH        = convert(bit, 
  18090.                         case when d.fixlen is null then 0 else 1 end)
  18091.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  18092.     where    d.ss_dtype = t.type 
  18093.     and    t.usertype <= 100
  18094.     and    t.usertype <> 18 /* sysname */
  18095.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  18096.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  18097.     and     t.usertype *= e.user_type 
  18098.     and     e.AUTO_INCREMENT = 0
  18099.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  18100.     and    (@data_type is null or d.oledb_data_type = @data_type)
  18101.     and    (@best_match is null or d.best_match = @best_match)
  18102.     order by 2
  18103. go
  18104. dump tran master with no_log
  18105. go
  18106.  
  18107. if (charindex('7.00', @@version) = 0)
  18108. begin
  18109.     print ''
  18110.     print ''
  18111.     print 'Warning:'
  18112.     print 'you are installing the stored procedures '
  18113.     print 'on a pre 7.0 SQL Server.'
  18114.     print 'Ignore the following errors.'
  18115. end
  18116. else
  18117.     drop proc sp_provider_types_rowset
  18118. go
  18119.  
  18120. /*    Procedure for 7.0 server */
  18121. create proc sp_provider_types_rowset
  18122.     (
  18123.     @data_type     smallint = null,
  18124.     @best_match    tinyint  = null
  18125.     )
  18126. as
  18127.     select
  18128.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  18129.                         else d.type_name 
  18130.                         end,
  18131.         DATA_TYPE         = d.oledb_data_type,                            
  18132.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18133.                             then @@max_precision
  18134.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  18135.         LITERAL_PREFIX         = d.literal_prefix,
  18136.         LITERAL_SUFFIX         = d.literal_suffix,
  18137.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  18138.         IS_NULLABLE        = t.allownulls,
  18139.         CASE_SENSITIVE        = d.case_sensitive,
  18140.         SEARCHABLE         = d.searchable,
  18141.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  18142.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  18143.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  18144.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  18145.                         else d.local_type_name
  18146.                         end,
  18147.         MINIMUM_SCALE        = convert(smallint,
  18148.                         case 
  18149.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  18150.                         else null 
  18151.                         end),
  18152.         MAXIMUM_SCALE        = convert(smallint,
  18153.                         case 
  18154.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18155.                         then @@max_precision
  18156.                         else null 
  18157.                         end),
  18158.         GUID            = convert(uniqueidentifier,null),
  18159.         TYPELIB            = convert(nvarchar(1),null),
  18160.         VERSION            = convert(nvarchar(1),null),
  18161.         IS_LONG            = d.is_long,
  18162.         BEST_MATCH        = case    when t.usertype = 80
  18163.                         then convert(bit,0)
  18164.                         else d.best_match
  18165.                         end,
  18166.         IS_FIXEDLENGTH        = convert(bit, 
  18167.                         case when d.fixlen is null then 0 else 1 end)
  18168.  
  18169.     from master.dbo.spt_provider_types d
  18170.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  18171.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  18172.             t.xusertype = e.user_type
  18173.             and e.AUTO_INCREMENT = 0
  18174.     where    (@data_type is null or d.oledb_data_type = @data_type)
  18175.     and    (@best_match is null or d.best_match = @best_match)    
  18176.     and    t.usertype <= 255
  18177.     and    t.usertype <> 18 /* sysname */
  18178.     order by 2
  18179. go
  18180.  
  18181. grant execute on sp_provider_types_rowset to public
  18182. go
  18183.  
  18184. dump tran master with no_log
  18185. go
  18186. if (charindex('6.00', @@version) > 0)
  18187.     begin
  18188.     if (exists (select * from sysobjects
  18189.         where name = 'sp_provider_types_rowset' and type = 'P '))
  18190.         begin
  18191.         drop procedure sp_provider_types_rowset
  18192.         dump tran master with no_log
  18193.         end
  18194.     end
  18195. go
  18196.  
  18197.  
  18198. print ''
  18199. print 'creating sp_procedure_params_rowset'
  18200. go
  18201.  
  18202. /*    Procedure for 6.0 and 6.50 servers */
  18203. create procedure sp_procedure_params_rowset
  18204.     (
  18205.        @procedure_name        varchar(255) = null,
  18206.     @group_number        int = null,
  18207.     @procedure_schema     varchar(255) = null,
  18208.     @parameter_name        varchar(255) = null
  18209.     )
  18210. as
  18211. IF @procedure_name is not null
  18212.     BEGIN
  18213.     select
  18214.         PROCEDURE_CATALOG     = db_name(),
  18215.         PROCEDURE_SCHEMA     = user_name(),
  18216.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18217.         PARAMETER_NAME         = c.name,
  18218.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18219.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18220.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18221.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18222.         IS_NULLABLE        = convert(bit,
  18223.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18224.                         then 0 else 1
  18225.                         end),
  18226.         DATA_TYPE        = d.oledb_data_type,
  18227.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18228.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18229.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18230.                         then coalesce(d.column_size,c.length)
  18231.                         else null 
  18232.                         end),
  18233.         CHARACTER_OCTET_LENGTH    = convert(int,
  18234.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18235.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18236.                         then coalesce(d.column_size,c.length)
  18237.                         else null 
  18238.                         end),
  18239.         NUMERIC_PRECISION    = convert(smallint,
  18240.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  18241.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18242.         NUMERIC_SCALE        = convert(smallint, 
  18243.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18244.         DESCRIPTION        = convert(varchar(1),null),
  18245.         TYPE_NAME        = d.type_name,
  18246.         LOCAL_TYPE_NAME        = d.local_type_name
  18247.     
  18248.     from
  18249.         syscolumns c,
  18250.         sysobjects o,
  18251.         master.dbo.spt_provider_types d,
  18252.         systypes t
  18253.     where
  18254.         o.name = @procedure_name
  18255.     and    o.type = 'P'                            /* Just Procedures */
  18256.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18257.     and    o.id = c.id
  18258.     and    c.number = @group_number
  18259.     and    c.type = d.ss_dtype
  18260.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18261.     and    c.usertype = t.usertype
  18262.     and    (t.usertype != 80 or d.type_name='timestamp')
  18263.     and    (@parameter_name is null or @parameter_name = c.name)
  18264.     UNION ALL
  18265.     SELECT           /* return value row*/
  18266.         PROCEDURE_CATALOG     = db_name(),
  18267.         PROCEDURE_SCHEMA     = user_name(),
  18268.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18269.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18270.         ORDINAL_POSITION     = convert(smallint,0),
  18271.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18272.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18273.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18274.         IS_NULLABLE        = convert(bit,0),
  18275.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18276.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18277.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18278.         NUMERIC_PRECISION    = convert(smallint,10),
  18279.         NUMERIC_SCALE        = convert(smallint,null),
  18280.         DESCRIPTION        = convert(varchar(1),null),
  18281.         TYPE_NAME        = convert(sysname,'int'),
  18282.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18283.     from
  18284.         syscomments c,
  18285.         sysobjects o
  18286.     where
  18287.         o.name = @procedure_name
  18288.     and    o.type = 'P'                        /* Just Procedures */
  18289.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18290.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18291.     and    c.id = o.id
  18292.     and    c.number = @group_number
  18293.     and c.colid = 1
  18294.     order by 2, 3, 5
  18295.     END
  18296. ELSE
  18297.     BEGIN
  18298.     select
  18299.         PROCEDURE_CATALOG     = db_name(),
  18300.         PROCEDURE_SCHEMA     = user_name(),
  18301.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18302.         PARAMETER_NAME         = c.name,
  18303.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18304.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18305.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18306.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18307.         IS_NULLABLE        = convert(bit,
  18308.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18309.                         then 0 else 1
  18310.                         end),
  18311.         DATA_TYPE        = d.oledb_data_type,
  18312.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18313.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18314.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18315.                         then coalesce(d.column_size,c.length)
  18316.                         else null 
  18317.                         end),
  18318.         CHARACTER_OCTET_LENGTH    = convert(int,
  18319.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18320.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18321.                         then coalesce(d.column_size,c.length)
  18322.                         else null 
  18323.                         end),
  18324.         NUMERIC_PRECISION    = convert(smallint,
  18325.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18326.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18327.         NUMERIC_SCALE        = convert(smallint, 
  18328.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18329.         DESCRIPTION        = convert(varchar(1),null),
  18330.         TYPE_NAME        = d.type_name,
  18331.         LOCAL_TYPE_NAME        = d.local_type_name
  18332.     
  18333.     from
  18334.         syscolumns c,
  18335.         sysobjects o,
  18336.         master.dbo.spt_provider_types d,
  18337.         systypes t
  18338.     where
  18339.         o.type = 'P'                            /* Just Procedures */
  18340.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18341.     and    o.id = c.id
  18342.     and    c.type = d.ss_dtype
  18343.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18344.     and    c.usertype = t.usertype
  18345.     and    (t.usertype != 80 or d.type_name='timestamp')
  18346.     and    (@parameter_name is null or @parameter_name = c.name)
  18347.     UNION ALL
  18348.     SELECT           /* return value row*/
  18349.         PROCEDURE_CATALOG     = db_name(),
  18350.         PROCEDURE_SCHEMA     = user_name(),
  18351.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18352.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18353.         ORDINAL_POSITION     = convert(smallint,0),
  18354.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18355.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18356.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18357.         IS_NULLABLE        = convert(bit,0),
  18358.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18359.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18360.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18361.         NUMERIC_PRECISION    = convert(smallint,10),
  18362.         NUMERIC_SCALE        = convert(smallint,null),
  18363.         DESCRIPTION        = convert(varchar(1),null),
  18364.         TYPE_NAME        = convert(sysname,'int'),
  18365.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18366.     from
  18367.         syscomments c,
  18368.         sysobjects o
  18369.     where
  18370.         o.type = 'P'                        /* Just Procedures */
  18371.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18372.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18373.     and    c.id = o.id
  18374.     and     c.colid = 1
  18375.     order by 2, 3, 5
  18376.     END
  18377. go
  18378. dump tran master with no_log
  18379. go
  18380. create procedure sp_procedure_params_rowset;2
  18381.     (
  18382.       @handle            int output,
  18383.        @scrollopt        int output,
  18384.     @ccopt            int output,
  18385.     @rows            int output,
  18386.        @procedure_name        varchar(255) = null,
  18387.     @group_number        int = null,
  18388.     @procedure_schema     varchar(255) = null,
  18389.     @parameter_name        varchar(255) = null
  18390.     )
  18391. as
  18392.     declare @ret int
  18393.  
  18394.  
  18395. SET NOCOUNT ON
  18396.  
  18397.     create table #spprocparamrowset1
  18398.         (
  18399.         PROCEDURE_CATALOG     sysname not null,
  18400.         PROCEDURE_SCHEMA     sysname not null,
  18401.         PROCEDURE_NAME         varchar(35) not null,
  18402.         PARAMETER_NAME         sysname not null,
  18403.         ORDINAL_POSITION     smallint not null,
  18404.         PARAMETER_TYPE         smallint null,
  18405.         PARAMETER_HASDEFAULT    tinyint null,
  18406.         PARAMETER_DEFAULT    varchar(255) null,
  18407.         IS_NULLABLE        bit not null,
  18408.         DATA_TYPE        smallint null,
  18409.         CHARACTER_MAXIMUM_LENGTH int null,
  18410.         CHARACTER_OCTET_LENGTH    int null,
  18411.         NUMERIC_PRECISION    smallint null,
  18412.         NUMERIC_SCALE        smallint null,
  18413.         DESCRIPTION        varchar(1) null,
  18414.         TYPE_NAME        sysname null,
  18415.         LOCAL_TYPE_NAME        sysname null,
  18416.         )
  18417.  
  18418. IF @procedure_name is not null
  18419.     BEGIN
  18420.     insert into #spprocparamrowset1
  18421.     select
  18422.         PROCEDURE_CATALOG     = db_name(),
  18423.         PROCEDURE_SCHEMA     = user_name(),
  18424.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18425.         PARAMETER_NAME         = c.name,
  18426.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18427.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18428.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18429.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18430.         IS_NULLABLE        = convert(bit,
  18431.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18432.                         then 0 else 1
  18433.                         end),
  18434.         DATA_TYPE        = d.oledb_data_type,
  18435.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18436.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18437.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18438.                         then coalesce(d.column_size,c.length)
  18439.                         else null 
  18440.                         end),
  18441.         CHARACTER_OCTET_LENGTH    = convert(int,
  18442.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18443.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18444.                         then coalesce(d.column_size,c.length)
  18445.                         else null 
  18446.                         end),
  18447.         NUMERIC_PRECISION    = convert(smallint,
  18448.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18449.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18450.         NUMERIC_SCALE        = convert(smallint, 
  18451.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18452.         DESCRIPTION        = convert(varchar(1),null),
  18453.         TYPE_NAME        = d.type_name,
  18454.         LOCAL_TYPE_NAME        = d.local_type_name
  18455.     
  18456.     from
  18457.         syscolumns c,
  18458.         sysobjects o,
  18459.         master.dbo.spt_provider_types d,
  18460.         systypes t
  18461.     where
  18462.         o.name = @procedure_name
  18463.     and    o.type = 'P'                            /* Just Procedures */
  18464.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18465.     and    o.id = c.id
  18466.     and    c.number = @group_number
  18467.     and    c.type = d.ss_dtype
  18468.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18469.     and    c.usertype = t.usertype
  18470.     and    (t.usertype != 80 or d.type_name='timestamp')
  18471.     and    (@parameter_name is null or @parameter_name = c.name)
  18472.     UNION ALL
  18473.     SELECT           /* return value row*/
  18474.         PROCEDURE_CATALOG     = db_name(),
  18475.         PROCEDURE_SCHEMA     = user_name(),
  18476.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18477.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18478.         ORDINAL_POSITION     = convert(smallint,0),
  18479.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18480.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18481.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18482.         IS_NULLABLE        = convert(bit,0),
  18483.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18484.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18485.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18486.         NUMERIC_PRECISION    = convert(smallint,10),
  18487.         NUMERIC_SCALE        = convert(smallint,null),
  18488.         DESCRIPTION        = convert(varchar(1),null),
  18489.         TYPE_NAME        = convert(sysname,'int'),
  18490.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18491.     from
  18492.         syscomments c,
  18493.         sysobjects o
  18494.     where
  18495.         o.name = @procedure_name
  18496.     and    o.type = 'P'                        /* Just Procedures */
  18497.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18498.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18499.     and    c.id = o.id
  18500.     and    c.number = @group_number
  18501.     and c.colid = 1
  18502.     order by 2, 3, 5
  18503.     END
  18504. ELSE
  18505.     BEGIN
  18506.     insert into #spprocparamrowset1
  18507.     select
  18508.         PROCEDURE_CATALOG     = db_name(),
  18509.         PROCEDURE_SCHEMA     = user_name(),
  18510.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18511.         PARAMETER_NAME         = c.name,
  18512.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18513.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18514.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18515.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18516.         IS_NULLABLE        = convert(bit,
  18517.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18518.                         then 0 else 1
  18519.                         end),
  18520.         DATA_TYPE        = d.oledb_data_type,
  18521.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18522.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18523.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18524.                         then coalesce(d.column_size,c.length)
  18525.                         else null 
  18526.                         end),
  18527.         CHARACTER_OCTET_LENGTH    = convert(int,
  18528.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18529.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18530.                         then coalesce(d.column_size,c.length)
  18531.                         else null 
  18532.                         end),
  18533.         NUMERIC_PRECISION    = convert(smallint,
  18534.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18535.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18536.         NUMERIC_SCALE        = convert(smallint, 
  18537.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18538.         DESCRIPTION        = convert(varchar(1),null),
  18539.         TYPE_NAME        = d.type_name,
  18540.         LOCAL_TYPE_NAME        = d.local_type_name
  18541.     
  18542.     from
  18543.         syscolumns c,
  18544.         sysobjects o,
  18545.         master.dbo.spt_provider_types d,
  18546.         systypes t
  18547.     where
  18548.         o.type = 'P'                            /* Just Procedures */
  18549.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18550.     and    o.id = c.id
  18551.     and    c.type = d.ss_dtype
  18552.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18553.     and    c.usertype = t.usertype
  18554.     and    (t.usertype != 80 or d.type_name='timestamp')
  18555.     and    (@parameter_name is null or @parameter_name = c.name)
  18556.     UNION ALL
  18557.     SELECT           /* return value row*/
  18558.         PROCEDURE_CATALOG     = db_name(),
  18559.         PROCEDURE_SCHEMA     = user_name(),
  18560.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18561.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18562.         ORDINAL_POSITION     = convert(smallint,0),
  18563.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18564.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18565.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18566.         IS_NULLABLE        = convert(bit,0),
  18567.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18568.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18569.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18570.         NUMERIC_PRECISION    = convert(smallint,10),
  18571.         NUMERIC_SCALE        = convert(smallint,null),
  18572.         DESCRIPTION        = convert(varchar(1),null),
  18573.         TYPE_NAME        = convert(sysname,'int'),
  18574.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18575.     from
  18576.         syscomments c,
  18577.         sysobjects o
  18578.     where
  18579.         o.type = 'P'                        /* Just Procedures */
  18580.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18581.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18582.     and    c.id = o.id
  18583.     and     c.colid = 1
  18584.     order by 2, 3, 5
  18585.     END
  18586.  
  18587.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  18588.         @scrollopt output, @ccopt output, @rows output
  18589.  
  18590.     drop table #spprocparamrowset1
  18591.        return isnull(@ret,0)
  18592. go
  18593. dump tran master with no_log
  18594. go
  18595. create procedure sp_procedure_params_rowset;3
  18596. as
  18597.     select
  18598.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  18599.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  18600.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  18601.         PARAMETER_NAME         = convert(sysname, ' '),
  18602.         ORDINAL_POSITION     = convert(smallint, 0),
  18603.         PARAMETER_TYPE         = convert(smallint, 0),
  18604.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18605.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18606.         IS_NULLABLE        = convert(bit, 0),
  18607.         DATA_TYPE        = convert(smallint, 0),
  18608.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  18609.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  18610.         NUMERIC_PRECISION    = convert(smallint, 0),
  18611.         NUMERIC_SCALE        = convert(smallint, 0),
  18612.         DESCRIPTION        = convert(varchar(1),null),
  18613.         TYPE_NAME        = convert(sysname,null),
  18614.         LOCAL_TYPE_NAME        = convert(sysname,null)
  18615.     where    1=0
  18616. go
  18617. dump tran master with no_log
  18618. go
  18619.  
  18620.  
  18621. if (charindex('7.00', @@version) > 0)
  18622.     drop procedure sp_procedure_params_rowset
  18623. else
  18624. begin
  18625.     print ''
  18626.     print ''
  18627.     print 'Warning:'
  18628.     print 'you are installing the stored procedures '
  18629.     print 'on a pre 7.0 SQL Server.'
  18630.     print 'Ignore the following errors.'
  18631. end
  18632. go
  18633.  
  18634. /*    Procedure for 7.0 servers */
  18635. create procedure sp_procedure_params_rowset
  18636.     (
  18637.        @procedure_name        sysname,
  18638.     @group_number        int = 1,
  18639.     @procedure_schema     sysname = null,
  18640.     @parameter_name        sysname = null
  18641.     )
  18642. as
  18643.     select
  18644.         PROCEDURE_CATALOG     = db_name(),
  18645.         PROCEDURE_SCHEMA     = user_name(),
  18646.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18647.         PARAMETER_NAME         = c.name,
  18648.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18649.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  18650.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18651.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18652.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  18653.         DATA_TYPE        = d.oledb_data_type,
  18654.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18655.                         case 
  18656.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18657.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18658.                         then coalesce(d.column_size,c.length)
  18659.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18660.                         then coalesce(d.column_size,c.length/2)
  18661.                         else null 
  18662.                         end),
  18663.         CHARACTER_OCTET_LENGTH    = convert(int,
  18664.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18665.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18666.                         then coalesce(d.column_size,c.length)
  18667.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18668.                         then coalesce(d.column_size*2,c.length)
  18669.                         else null 
  18670.                         end),
  18671.         NUMERIC_PRECISION    = convert(smallint,
  18672.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18673.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18674.         NUMERIC_SCALE        = convert(smallint, 
  18675.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18676.         DESCRIPTION        = convert(nvarchar(1),null),
  18677.         TYPE_NAME        = d.type_name,
  18678.         LOCAL_TYPE_NAME        = d.local_type_name
  18679.     
  18680.     from
  18681.         sysobjects o,
  18682.         syscolumns c,
  18683.         master.dbo.spt_provider_types d,
  18684.         systypes t
  18685.     where
  18686.         o.name = @procedure_name
  18687.     and    o.type = 'P'                            /* Just Procedures */
  18688.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18689.     and    o.id = c.id
  18690.     and    c.number = @group_number
  18691.     and    c.xtype = d.ss_dtype
  18692.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18693.     and    c.xusertype = t.xusertype
  18694.     and    (@parameter_name is null or @parameter_name = c.name)
  18695.     UNION ALL
  18696.     SELECT           /* return value row*/
  18697.         PROCEDURE_CATALOG     = db_name(),
  18698.         PROCEDURE_SCHEMA     = user_name(),
  18699.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18700.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18701.         ORDINAL_POSITION     = convert(smallint,0),
  18702.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18703.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18704.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18705.         IS_NULLABLE        = convert(bit,0),
  18706.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18707.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18708.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18709.         NUMERIC_PRECISION    = convert(smallint,10),
  18710.         NUMERIC_SCALE        = convert(smallint,null),
  18711.         DESCRIPTION        = convert(nvarchar(1),null),
  18712.         TYPE_NAME        = convert(sysname,N'int'),
  18713.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  18714.     from
  18715.         sysobjects o,
  18716.         syscomments c
  18717.     where
  18718.         o.name = @procedure_name
  18719.     and    o.id = c.id 
  18720.     and    c.number = @group_number
  18721.     and    c.colid = 1
  18722.     and    o.type = 'P'                        /* Just Procedures */
  18723.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18724.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18725.     order by 2, 3, 5
  18726. go
  18727. dump tran master with no_log
  18728. go
  18729. create procedure sp_procedure_params_rowset;2
  18730.     (
  18731.     @procedure_schema     sysname = null,
  18732.     @parameter_name        sysname = null
  18733.     )
  18734. as
  18735.     select
  18736.         PROCEDURE_CATALOG     = db_name(),
  18737.         PROCEDURE_SCHEMA     = user_name(),
  18738.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18739.         PARAMETER_NAME         = c.name,
  18740.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18741.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  18742.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18743.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18744.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  18745.         DATA_TYPE        = d.oledb_data_type,
  18746.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18747.                         case 
  18748.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18749.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18750.                         then coalesce(d.column_size,c.length)
  18751.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18752.                         then coalesce(d.column_size,c.length/2)
  18753.                         else null 
  18754.                         end),
  18755.         CHARACTER_OCTET_LENGTH    = convert(int,
  18756.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18757.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18758.                         then coalesce(d.column_size,c.length)
  18759.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18760.                         then coalesce(d.column_size*2,c.length)
  18761.                         else null 
  18762.                         end),
  18763.         NUMERIC_PRECISION    = convert(smallint,
  18764.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18765.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18766.         NUMERIC_SCALE        = convert(smallint, 
  18767.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18768.         DESCRIPTION        = convert(nvarchar(1),null),
  18769.         TYPE_NAME        = d.type_name,
  18770.         LOCAL_TYPE_NAME        = d.local_type_name
  18771.     
  18772.     from
  18773.         sysobjects o,
  18774.         syscolumns c,
  18775.         master.dbo.spt_provider_types d,
  18776.         systypes t
  18777.     where
  18778.         o.type = 'P'                            /* Just Procedures */
  18779.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18780.     and    o.id = c.id
  18781.     and    c.xtype = d.ss_dtype
  18782.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18783.     and    c.xusertype = t.xusertype
  18784.     and    (@parameter_name is null or @parameter_name = c.name)
  18785.     UNION ALL
  18786.     SELECT           /* return value row*/
  18787.         PROCEDURE_CATALOG     = db_name(),
  18788.         PROCEDURE_SCHEMA     = user_name(),
  18789.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18790.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18791.         ORDINAL_POSITION     = convert(smallint,0),
  18792.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18793.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18794.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18795.         IS_NULLABLE        = convert(bit,0),
  18796.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18797.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18798.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18799.         NUMERIC_PRECISION    = convert(smallint,10),
  18800.         NUMERIC_SCALE        = convert(smallint,null),
  18801.         DESCRIPTION        = convert(nvarchar(1),null),
  18802.         TYPE_NAME        = convert(sysname,N'int'),
  18803.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  18804.     from
  18805.         sysobjects o,
  18806.         syscomments c
  18807.     where
  18808.         o.type = 'P'        /* Just Procedures */
  18809.     and    o.id = c.id 
  18810.     and    c.colid = 1
  18811.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18812.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18813.     order by 2, 3, 5
  18814. go
  18815.  
  18816.  
  18817. grant execute on sp_procedure_params_rowset to public
  18818. go
  18819.  
  18820. dump tran master with no_log
  18821. go
  18822. if (charindex('6.00', @@version) > 0)
  18823.     begin
  18824.     if (exists (select * from sysobjects
  18825.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  18826.         begin
  18827.         drop procedure sp_procedure_params_rowset
  18828.         dump tran master with no_log
  18829.         end
  18830.     end
  18831. go
  18832.  
  18833.  
  18834. print ''
  18835. print 'creating sp_procedures_rowset'
  18836. go
  18837.  
  18838.  
  18839. /* pre 7.00 version */
  18840. create procedure sp_procedures_rowset
  18841.     (
  18842.     @procedure_name        varchar(255), 
  18843.     @group_number        int = 1,
  18844.     @procedure_schema    varchar(255) = null
  18845.     )        
  18846. as
  18847.     select
  18848.         PROCEDURE_CATALOG    = db_name(),
  18849.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18850.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18851.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18852.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18853.         DESCRIPTION        = convert(varchar(1),null),
  18854.         DATE_CREATED        = o.crdate,
  18855.         DATE_MODIFIED        = convert(datetime,null)
  18856.     from     
  18857.         sysobjects o, 
  18858.         syscomments p,
  18859.         sysusers u
  18860.     where
  18861.             o.name = @procedure_name
  18862.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18863.     and     o.type = 'P'        /* Object type of Procedure */
  18864.     and     p.colid = 1
  18865.     and     p.id = o.id
  18866.     and    p.number = @group_number
  18867.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18868.     and     (    suser_id() = 1     /* User is the System Administrator */
  18869.         or     o.uid = user_id()    /* User created the object */
  18870.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18871.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18872.              from sysprotects p
  18873.              where p.id = o.id
  18874.                  /*  get rows for public,current user,user's group */
  18875.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18876.                  /* check for SELECT,EXECUTE privilege */
  18877.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18878.             ) = 1     /* final magic...compare Grants    */
  18879.         )
  18880.     order by 2, 3
  18881. go
  18882. dump tran master with no_log
  18883. go
  18884. create procedure sp_procedures_rowset;2
  18885.     (
  18886.     @procedure_schema    varchar(255) = null
  18887.     )        
  18888. as
  18889.     select
  18890.         PROCEDURE_CATALOG    = db_name(),
  18891.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18892.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18893.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18894.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18895.         DESCRIPTION        = convert(varchar(1),null),
  18896.         DATE_CREATED        = o.crdate,
  18897.         DATE_MODIFIED        = convert(datetime,null)
  18898.     from     
  18899.         sysobjects o, 
  18900.         syscomments p,
  18901.         sysusers u
  18902.     where
  18903.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18904.     and     o.type = 'P'        /* Object type of Procedure */
  18905.     and     p.colid = 1
  18906.     and     p.id = o.id
  18907.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18908.     and     (    suser_id() = 1     /* User is the System Administrator */
  18909.         or     o.uid = user_id()    /* User created the object */
  18910.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18911.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18912.              from sysprotects p
  18913.              where p.id = o.id
  18914.                  /*  get rows for public,current user,user's group */
  18915.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18916.                  /* check for SELECT,EXECUTE privilege */
  18917.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18918.             ) = 1     /* final magic...compare Grants    */
  18919.         )
  18920.     order by 2, 3
  18921. go
  18922. dump tran master with no_log
  18923. go
  18924.  
  18925. if (charindex('7.00', @@version) > 0)
  18926.     drop procedure sp_procedures_rowset
  18927. else
  18928. begin
  18929.     print ''
  18930.     print ''
  18931.     print 'Warning:'
  18932.     print 'you are installing the stored procedures '
  18933.     print 'on a pre 7.0 SQL Server.'
  18934.     print 'Ignore the following errors.'
  18935. end
  18936. go
  18937.  
  18938. /* 7.00 version */
  18939. create procedure sp_procedures_rowset
  18940.     (
  18941.     @procedure_name        sysname, 
  18942.     @group_number        int = 1,
  18943.     @procedure_schema    sysname = null
  18944.     )        
  18945. as
  18946.     select
  18947.         PROCEDURE_CATALOG    = db_name(),
  18948.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18949.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  18950.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18951.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  18952.         DESCRIPTION        = convert(nvarchar(1),null),
  18953.         DATE_CREATED        = o.crdate,
  18954.         DATE_MODIFIED        = convert(datetime,null)
  18955.     from     
  18956.         sysobjects o, 
  18957.         syscomments p
  18958.     where
  18959.         permissions(o.id) <> 0
  18960.     and    o.name = @procedure_name
  18961.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18962.     and     o.type = 'P'        /* Object type of Procedure */
  18963.     and     p.colid = 1
  18964.     and     p.id = o.id
  18965.     and    p.number = @group_number
  18966.     order by 2, 3
  18967. go
  18968. dump tran master with no_log
  18969. go
  18970. create procedure sp_procedures_rowset;2
  18971.     (
  18972.     @procedure_schema    sysname = null
  18973.     )        
  18974. as
  18975.     select
  18976.         PROCEDURE_CATALOG    = db_name(),
  18977.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18978.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  18979.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18980.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  18981.         DESCRIPTION        = convert(nvarchar(1),null),
  18982.         DATE_CREATED        = o.crdate,
  18983.         DATE_MODIFIED        = convert(datetime,null)
  18984.     from     sysobjects o, syscomments p
  18985.     where
  18986.         permissions(o.id) <> 0
  18987.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18988.     and     o.type = 'P'        /* Object type of Procedure */
  18989.     and p.colid = 1
  18990.     and p.id = o.id
  18991.     order by 2, 3
  18992. go
  18993.  
  18994. grant execute on sp_procedures_rowset to public
  18995. go
  18996.  
  18997. dump tran master with no_log
  18998. go
  18999. if (charindex('6.00', @@version) > 0)
  19000.     begin
  19001.     if (exists (select * from sysobjects
  19002.             where name = 'sp_procedures_rowset' and type = 'P '))
  19003.         begin
  19004.         drop procedure sp_procedures_rowset
  19005.         dump tran master with no_log
  19006.         end
  19007.     end
  19008. go
  19009.  
  19010.  
  19011. print ''
  19012. print 'creating sp_schemata_rowset'
  19013. go
  19014.  
  19015. /*    6.0 and 6.5 version */
  19016. create procedure sp_schemata_rowset
  19017.     (
  19018.     @schema_name    varchar(90) = null, 
  19019.     @schema_owner    varchar(90) = null
  19020.     )        
  19021. as
  19022.     select    distinct
  19023.         CATALOG_NAME            = db_name(),
  19024.         SCHEMA_NAME            = user_name(o.uid),    
  19025.         SCHEMA_OWNER            = user_name(o.uid),    
  19026.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  19027.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  19028.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  19029.     from    
  19030.         sysobjects o,
  19031.         master.dbo.sysconfigures    cfg,
  19032.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  19033.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  19034.     where   
  19035.         (@schema_name is null or @schema_name = user_name(o.uid))
  19036.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  19037.     and    a_cha.type = 1001 /* type is charset */
  19038.     and     b_cha.type = 2001 /* type is sortorder */
  19039.     and     a_cha.id = b_cha.csid
  19040.     and     b_cha.id = cfg.value
  19041.     order by 2
  19042. go
  19043. dump tran master with no_log
  19044. go
  19045.  
  19046. if (charindex('7.00', @@version) > 0)
  19047.     drop procedure sp_schemata_rowset
  19048. else
  19049. begin
  19050.     print ''
  19051.     print ''
  19052.     print 'Warning:'
  19053.     print 'you are installing the stored procedures '
  19054.     print 'on a pre 7.0 SQL Server.'
  19055.     print 'Ignore the following errors.'
  19056. end
  19057. go
  19058.  
  19059. /* 7.00 version */
  19060. create procedure sp_schemata_rowset
  19061.     (
  19062.     @schema_name    sysname = null, 
  19063.     @schema_owner    sysname = null
  19064.     )        
  19065. as
  19066.     select    distinct
  19067.         CATALOG_NAME            = db_name(),
  19068.         SCHEMA_NAME            = user_name(o.uid),    
  19069.         SCHEMA_OWNER            = user_name(o.uid),    
  19070.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  19071.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  19072.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  19073.     from    
  19074.         sysobjects o,
  19075.         master.dbo.sysconfigures    cfg,
  19076.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  19077.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  19078.     where   
  19079.         (@schema_name is null or @schema_name = user_name(o.uid))
  19080.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  19081.     and    a_cha.type = 1001 /* type is charset */
  19082.     and     b_cha.type = 2001 /* type is sortorder */
  19083.     and     a_cha.id = b_cha.csid
  19084.     and     b_cha.id = cfg.value
  19085.     order by 2
  19086. go
  19087. dump tran master with no_log
  19088. go
  19089. go
  19090. /* The following stored procedure is used for Sphinx and Hydra */
  19091. create procedure sp_schemata_rowset;3
  19092. as
  19093.     select
  19094.         CATALOG_NAME            = convert(sysname,' '),
  19095.         SCHEMA_NAME            = convert(sysname,' '),    
  19096.         SCHEMA_OWNER            = convert(sysname,' '),    
  19097.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  19098.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  19099.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  19100.     where    1=0
  19101. go
  19102. grant execute on sp_schemata_rowset to public
  19103. go
  19104.  
  19105. dump tran master with no_log
  19106. go
  19107. if (charindex('6.00', @@version) > 0)
  19108.     begin
  19109.     if (exists (select * from sysobjects
  19110.             where name = 'sp_schemata_rowset' and type = 'P '))
  19111.         begin
  19112.         drop procedure sp_schemata_rowset
  19113.         dump tran master with no_log
  19114.         end
  19115.     end
  19116. go
  19117.  
  19118.  
  19119. print ''
  19120. print 'creating sp_statistics_rowset'
  19121. go
  19122.  
  19123.  
  19124. /*    6.0 and 6.5 version */
  19125. create procedure sp_statistics_rowset
  19126.     (
  19127.     @table_name    varchar(255),
  19128.     @table_schema    varchar(255) = null     
  19129.     )
  19130. as
  19131.     select    db_name()                as TABLE_CATALOG,        
  19132.         user_name(o.uid)            as TABLE_SCHEMA,
  19133.         o.name                    as TABLE_NAME,
  19134.         x.rows                    as CARDINALITY
  19135.     from    sysobjects o, sysindexes x, sysusers u
  19136.     where    o.type in ('U')
  19137.     and     o.name = @table_name
  19138.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19139.     and     x.id = o.id
  19140.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19141.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19142.     and     (
  19143.         suser_id() = 1     /* User is the System Administrator */
  19144.         or o.uid = user_id()     /* User created the object */
  19145.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19146.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19147.             from sysprotects p
  19148.             where p.id = o.id
  19149.             /* get rows for public,current user,user's group */
  19150.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19151.             /* check for SELECT,EXECUTE privilege */
  19152.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19153.             ) = 1    /* final magic...compare Grants      */
  19154.            )
  19155.     order by 2, 3
  19156. go
  19157. dump tran master with no_log
  19158. go
  19159. create procedure sp_statistics_rowset;2
  19160.     (
  19161.     @table_schema    varchar(255) = null     
  19162.     )
  19163. as
  19164.     select    db_name()                as TABLE_CATALOG,        
  19165.         user_name(o.uid)            as TABLE_SCHEMA,
  19166.         o.name                    as TABLE_NAME,
  19167.         x.rows                    as CARDINALITY
  19168.     from    sysobjects o, sysindexes x, sysusers u
  19169.     where    o.type in ('U')
  19170.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19171.     and     x.id = o.id
  19172.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19173.     and     (
  19174.         suser_id() = 1     /* User is the System Administrator */
  19175.         or o.uid = user_id()     /* User created the object */
  19176.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19177.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19178.             from sysprotects p
  19179.             where p.id = o.id
  19180.             /* get rows for public,current user,user's group */
  19181.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19182.             /* check for SELECT,EXECUTE privilege */
  19183.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19184.             ) = 1    /* final magic...compare Grants      */
  19185.            )        
  19186.     order by 2, 3
  19187. go
  19188. dump tran master with no_log
  19189. go
  19190.  
  19191. if (charindex('7.00', @@version) > 0)
  19192.     drop procedure sp_statistics_rowset
  19193. else
  19194. begin
  19195.     print ''
  19196.     print ''
  19197.     print 'Warning:'
  19198.     print 'you are installing the stored procedures '
  19199.     print 'on a pre 7.0 SQL Server.'
  19200.     print 'Ignore the following errors.'
  19201. end
  19202. go
  19203.  
  19204. /*     7.0 version */
  19205. create procedure sp_statistics_rowset
  19206.     (
  19207.     @table_name    sysname,
  19208.     @table_schema    sysname = null     
  19209.     )
  19210. as
  19211.     select    db_name()                as TABLE_CATALOG,        
  19212.         user_name(o.uid)            as TABLE_SCHEMA,
  19213.         o.name                    as TABLE_NAME,
  19214.         x.rows                    as CARDINALITY
  19215.     from    sysobjects o, sysindexes x
  19216.     where    o.type in ('U')
  19217.     and     o.name = @table_name
  19218.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19219.     and     x.id = o.id
  19220.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19221.     and     permissions(o.id) <> 0
  19222.     order by 2, 3
  19223. go
  19224. dump tran master with no_log
  19225. go
  19226. create procedure sp_statistics_rowset;2
  19227.     (
  19228.     @table_schema    sysname = null     
  19229.     )
  19230. as
  19231.     select    db_name()                as TABLE_CATALOG,        
  19232.         user_name(o.uid)            as TABLE_SCHEMA,
  19233.         o.name                    as TABLE_NAME,
  19234.         x.rows                    as CARDINALITY
  19235.     from    sysobjects o, sysindexes x
  19236.     where    o.type in ('U')
  19237.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19238.     and     x.id = o.id
  19239.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19240.     and     permissions(o.id) <> 0
  19241.     order by 2, 3
  19242. go
  19243.  
  19244. grant execute on sp_statistics_rowset to public
  19245. go
  19246.  
  19247. dump tran master with no_log
  19248. go
  19249. if (charindex('6.00', @@version) > 0)
  19250.     begin
  19251.     if (exists (select * from sysobjects
  19252.             where name = 'sp_statistics_rowset' and type = 'P '))
  19253.         begin
  19254.         drop procedure sp_statistics_rowset
  19255.         dump tran master with no_log
  19256.         end
  19257.     end
  19258. go
  19259.  
  19260.  
  19261. print ''
  19262. print 'creating sp_tables_rowset'
  19263. go
  19264.  
  19265.  
  19266. /*    Procedure for 6.50 and earlier servers */
  19267. create procedure sp_tables_rowset
  19268.     (
  19269.     @table_name    varchar(255), 
  19270.     @table_schema    varchar(255) = null,    
  19271.     @table_type    varchar(255) = null 
  19272.     )
  19273. as
  19274.     select    TABLE_CATALOG    = db_name(),
  19275.         TABLE_SCHEMA    = user_name(o.uid),
  19276.         TABLE_NAME    = o.name,
  19277.         TABLE_TYPE    = convert(varchar(30),
  19278.                     case o.type 
  19279.                     when 'U' then 'TABLE'
  19280.                     when 'V' then 'VIEW'
  19281.                     when 'S' then 'SYSTEM TABLE'
  19282.                     end),
  19283.         TABLE_GUID    = convert(binary(16), null),
  19284.         DESCRIPTION    = convert(varchar(1), null),
  19285.         TABLE_PROPID    = convert(int,null),
  19286.         DATE_CREATED    = o.crdate,
  19287.         DATE_MODIFIED    = convert(datetime,null)
  19288.     from    sysusers u, sysobjects o
  19289.     where    o.type in ('U','V','S')
  19290.     and     o.name = @table_name
  19291.     and     (    @table_schema is null
  19292.         or    @table_schema = user_name(o.uid)
  19293.         )
  19294.     and     (
  19295.             @table_type is null
  19296.         or    @table_type = case o.type 
  19297.                     when 'U' then 'TABLE'
  19298.                     when 'V' then 'VIEW'
  19299.                     when 'S' then 'SYSTEM TABLE'
  19300.                     end
  19301.         )
  19302.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19303.     and     (
  19304.         suser_id() = 1     /* User is the System Administrator */
  19305.         or o.uid = user_id()     /* User created the object */
  19306.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19307.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19308.             from sysprotects p
  19309.             /* join to correlate with all rows in sysobjects */
  19310.             where p.id = o.id
  19311.             /* get rows for public,current user,user's group */
  19312.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19313.             /* check for SELECT,EXECUTE privilege */
  19314.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19315.             ) = 1    /* final magic...compare Grants      */
  19316.         )        
  19317.     order by 4, 2, 3
  19318. go
  19319. dump tran master with no_log
  19320. go
  19321. create procedure sp_tables_rowset;2
  19322.     (
  19323.     @table_schema    varchar(255) = null,    
  19324.     @table_type    varchar(255) = null 
  19325.     )
  19326. as
  19327.     select    TABLE_CATALOG    = db_name(),
  19328.         TABLE_SCHEMA    = user_name(o.uid),
  19329.         TABLE_NAME    = o.name,
  19330.         TABLE_TYPE    = convert(varchar(30),
  19331.                     case o.type 
  19332.                     when 'U' then 'TABLE'
  19333.                     when 'V' then 'VIEW'
  19334.                     when 'S' then 'SYSTEM TABLE'
  19335.                     end),
  19336.         TABLE_GUID    = convert(binary(16), null),
  19337.         DESCRIPTION    = convert(varchar(1), null),
  19338.         TABLE_PROPID    = convert(int,null),
  19339.         DATE_CREATED    = o.crdate,
  19340.         DATE_MODIFIED    = convert(datetime,null)
  19341.     from    sysusers u, sysobjects o
  19342.     where    o.type in ('U','V','S')
  19343.     and     (    @table_schema is null
  19344.         or    @table_schema = user_name(o.uid)
  19345.         )
  19346.     and     (
  19347.             @table_type is null
  19348.         or    @table_type = case o.type 
  19349.                     when 'U' then 'TABLE'
  19350.                     when 'V' then 'VIEW'
  19351.                     when 'S' then 'SYSTEM TABLE'
  19352.                     end
  19353.         )
  19354.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19355.     and     (
  19356.         suser_id() = 1     /* User is the System Administrator */
  19357.         or o.uid = user_id()     /* User created the object */
  19358.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19359.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19360.             from sysprotects p
  19361.             /* join to correlate with all rows in sysobjects */
  19362.             where p.id = o.id
  19363.             /* get rows for public,current user,user's group */
  19364.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19365.             /* check for SELECT,EXECUTE privilege */
  19366.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19367.             ) = 1    /* final magic...compare Grants      */
  19368.         )        
  19369.     order by 4, 2, 3
  19370. go
  19371. dump tran master with no_log
  19372. go
  19373.  
  19374. if (charindex('7.00', @@version) > 0)
  19375.     drop procedure sp_tables_rowset
  19376. else
  19377. begin
  19378.     print ''
  19379.     print ''
  19380.     print 'Warning:'
  19381.     print 'you are installing the stored procedures '
  19382.     print 'on a pre 7.0 SQL Server.'
  19383.     print 'Ignore the following errors.'
  19384. end
  19385. go
  19386.  
  19387. /*    Procedure for 7.00 server */
  19388. create procedure sp_tables_rowset
  19389.     (
  19390.     @table_name    sysname, 
  19391.     @table_schema    sysname = null,    
  19392.     @table_type    nvarchar(255) = null 
  19393.     )
  19394. as
  19395.     select    *
  19396.     from    (select    TABLE_CATALOG    = db_name(),
  19397.             TABLE_SCHEMA    = user_name(uid),
  19398.             TABLE_NAME    = name,
  19399.             TABLE_TYPE    = convert(nvarchar(30),
  19400.                         case type 
  19401.                         when 'U' then 
  19402.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19403.                             then N'TABLE' else N'SYSTEM TABLE' end
  19404.                         when 'S' then N'SYSTEM TABLE'
  19405.                         when 'V' then 
  19406.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19407.                             then N'VIEW' else N'SYSTEM VIEW' end
  19408.                         end),
  19409.             TABLE_GUID    = convert(uniqueidentifier, null),
  19410.             DESCRIPTION    = convert(nvarchar(1), null),
  19411.             TABLE_PROPID    = convert(int,null),
  19412.             DATE_CREATED    = crdate,
  19413.             DATE_MODIFIED    = convert(datetime,null)
  19414.         from    sysobjects
  19415.         where    name = @table_name
  19416.         and    type in ('U','V','S')
  19417.         and    permissions(id) <> 0
  19418.         ) as o
  19419.  
  19420.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19421.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  19422.     order by 4, 2, 3
  19423. go
  19424. dump tran master with no_log
  19425. go
  19426. create procedure sp_tables_rowset;2
  19427.     (
  19428.     @table_schema    sysname = null,    
  19429.     @table_type    nvarchar(255) = null 
  19430.     )
  19431. as
  19432.     select    *
  19433.     from    (select    TABLE_CATALOG    = db_name(),
  19434.             TABLE_SCHEMA    = user_name(uid),
  19435.             TABLE_NAME    = name,
  19436.             TABLE_TYPE    = convert(nvarchar(30),
  19437.                         case type 
  19438.                         when 'U' then 
  19439.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19440.                             then N'TABLE' else N'SYSTEM TABLE' end
  19441.                         when 'S' then N'SYSTEM TABLE'
  19442.                         when 'V' then 
  19443.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19444.                             then N'VIEW' else N'SYSTEM VIEW' end
  19445.                         end),
  19446.             TABLE_GUID    = convert(uniqueidentifier, null),
  19447.             DESCRIPTION    = convert(nvarchar(1), null),
  19448.             TABLE_PROPID    = convert(int,null),
  19449.             DATE_CREATED    = crdate,
  19450.             DATE_MODIFIED    = convert(datetime,null)
  19451.         from    sysobjects
  19452.         where    type in ('U','V','S')
  19453.         and    permissions(id) <> 0
  19454.         ) as o
  19455.  
  19456.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19457.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  19458.     order by 4, 2, 3
  19459. go
  19460. dump tran master with no_log
  19461. go
  19462. create procedure sp_tables_rowset;5
  19463.     (
  19464.     @table_server        sysname,
  19465.     @table_catalog        sysname = null,
  19466.     @table_name        sysname = null,
  19467.     @table_schema        sysname = null,
  19468.     @table_type        sysname = null
  19469.     )
  19470. as
  19471.     select
  19472.         TABLE_CATALOG,
  19473.         TABLE_SCHEMA,
  19474.         TABLE_NAME,
  19475.         TABLE_TYPE,
  19476.         TABLE_GUID,
  19477.         DESCRIPTION
  19478.     --    TABLE_PROPID,
  19479.     --    DATE_CREATED,
  19480.     --    DATE_MODIFIED
  19481.     from master.dbo.SYSREMOTE_TABLES <
  19482.                 @table_server,
  19483.                 @table_catalog,
  19484.                 @table_schema,
  19485.                 @table_name,
  19486.                 @table_type >
  19487.     order by 4,1,2,3
  19488. go
  19489.  
  19490. grant execute on sp_tables_rowset to public
  19491. go
  19492.  
  19493. dump tran master with no_log
  19494. go
  19495. if (charindex('6.00', @@version) > 0)
  19496.     begin
  19497.     if (exists (select * from sysobjects
  19498.             where name = 'sp_tables_rowset' and type = 'P '))
  19499.         begin
  19500.         drop procedure sp_tables_rowset
  19501.         dump tran master with no_log
  19502.         end
  19503.     end
  19504. go
  19505.  
  19506.  
  19507.  
  19508. print ''
  19509. print 'creating sp_tables_info_rowset'
  19510. go
  19511.  
  19512.  
  19513. /*    Procedure for 6.50 and earlier servers */
  19514. create procedure sp_tables_info_rowset
  19515.     (
  19516.     @table_name    varchar(255), 
  19517.     @table_schema    varchar(255) = null,    
  19518.     @table_type    varchar(255) = null 
  19519.     )
  19520. as
  19521.     select    TABLE_CATALOG        = db_name(),
  19522.         TABLE_SCHEMA        = user_name(o.uid),
  19523.         TABLE_NAME        = o.name,
  19524.         TABLE_TYPE        = convert(varchar(30),
  19525.                         case o.type 
  19526.                         when 'U' then 'TABLE'
  19527.                         when 'V' then 'VIEW'
  19528.                         when 'S' then 'SYSTEM TABLE'
  19529.                         end),
  19530.         TABLE_GUID        = convert(binary(16), null),
  19531.         BOOKMARKS        = convert(bit, 1),
  19532.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19533.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19534.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19535.         BOOKMARK_INFORMATION    = convert(int, 0),
  19536.         TABLE_VERSION        = convert(int, o.schema_ver),
  19537.         CARDINALITY        = x.rows,
  19538.         DESCRIPTION        = convert(varchar(1), null),
  19539.         TABLE_PROPID        = convert(int, null)
  19540.  
  19541.     from    sysusers u, 
  19542.         sysobjects o,
  19543.         sysindexes x
  19544.  
  19545.     where    o.type in ('U','V','S')
  19546.     and     o.name = @table_name
  19547.     and     (    @table_schema is null
  19548.         or    @table_schema = user_name(o.uid)
  19549.         )
  19550.     and     (
  19551.             @table_type is null
  19552.         or    @table_type = case o.type 
  19553.                     when 'U' then 'TABLE'
  19554.                     when 'V' then 'VIEW'
  19555.                     when 'S' then 'SYSTEM TABLE'
  19556.                     end
  19557.         )
  19558.     and    o.id *= x.id
  19559.     and    x.indid in (0,1)
  19560.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19561.     and     (
  19562.         suser_id() = 1     /* User is the System Administrator */
  19563.         or o.uid = user_id()     /* User created the object */
  19564.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19565.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19566.             from sysprotects p
  19567.             /* join to correlate with all rows in sysobjects */
  19568.             where p.id = o.id
  19569.             /* get rows for public,current user,user's group */
  19570.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19571.             /* check for SELECT,EXECUTE privilege */
  19572.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19573.             ) = 1    /* final magic...compare Grants      */
  19574.         )        
  19575.     order by 4, 2, 3
  19576. go
  19577. dump tran master with no_log
  19578. go
  19579. create procedure sp_tables_info_rowset;2
  19580.     (
  19581.     @table_schema    varchar(255) = null,    
  19582.     @table_type    varchar(255) = null 
  19583.     )
  19584. as
  19585.     select    TABLE_CATALOG        = db_name(),
  19586.         TABLE_SCHEMA        = user_name(o.uid),
  19587.         TABLE_NAME        = o.name,
  19588.         TABLE_TYPE        = convert(varchar(30),
  19589.                         case o.type 
  19590.                         when 'U' then 'TABLE'
  19591.                         when 'V' then 'VIEW'
  19592.                         when 'S' then 'SYSTEM TABLE'
  19593.                         end),
  19594.         TABLE_GUID        = convert(binary(16), null),
  19595.         BOOKMARKS        = convert(bit, 1),
  19596.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19597.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19598.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19599.         BOOKMARK_INFORMATION    = convert(int, 0),
  19600.         TABLE_VERSION        = convert(int, o.schema_ver),
  19601.         CARDINALITY        = x.rows,
  19602.         DESCRIPTION        = convert(varchar(1), null),
  19603.         TABLE_PROPID        = convert(int, null)
  19604.  
  19605.     from    sysusers u, 
  19606.         sysobjects o,
  19607.         sysindexes x
  19608.  
  19609.     where    o.type in ('U','V','S')
  19610.     and     (    @table_schema is null
  19611.         or    @table_schema = user_name(o.uid)
  19612.         )
  19613.     and     (
  19614.             @table_type is null
  19615.         or    @table_type = case o.type 
  19616.                     when 'U' then 'TABLE'
  19617.                     when 'V' then 'VIEW'
  19618.                     when 'S' then 'SYSTEM TABLE'
  19619.                     end
  19620.         )
  19621.     and    o.id *= x.id
  19622.     and    x.indid in (0,1)
  19623.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19624.     and     (
  19625.         suser_id() = 1     /* User is the System Administrator */
  19626.         or o.uid = user_id()     /* User created the object */
  19627.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19628.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19629.             from sysprotects p
  19630.             /* join to correlate with all rows in sysobjects */
  19631.             where p.id = o.id
  19632.             /* get rows for public,current user,user's group */
  19633.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19634.             /* check for SELECT,EXECUTE privilege */
  19635.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19636.             ) = 1    /* final magic...compare Grants      */
  19637.         )        
  19638.     order by 4, 2, 3
  19639. go
  19640. dump tran master with no_log
  19641. go
  19642.  
  19643. if (charindex('7.00', @@version) > 0)
  19644.     drop procedure sp_tables_info_rowset
  19645. else
  19646. begin
  19647.     print ''
  19648.     print ''
  19649.     print 'Warning:'
  19650.     print 'you are installing the stored procedures '
  19651.     print 'on a pre 7.0 SQL Server.'
  19652.     print 'Ignore the following errors.'
  19653. end
  19654. go
  19655.  
  19656. /*    Procedure for 7.00 server */
  19657. create procedure sp_tables_info_rowset
  19658.     (
  19659.     @table_name    sysname, 
  19660.     @table_schema    sysname = null,    
  19661.     @table_type    nvarchar(255) = null 
  19662.     )
  19663. as
  19664.     select    *
  19665.     from    (select    TABLE_CATALOG        = db_name(),
  19666.             TABLE_SCHEMA        = user_name(o.uid),
  19667.             TABLE_NAME        = o.name,
  19668.             TABLE_TYPE        = convert(nvarchar(30),
  19669.                             case o.type 
  19670.                             when 'U' then 
  19671.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19672.                                 then N'TABLE' else N'SYSTEM TABLE' end
  19673.                             when 'S' then N'SYSTEM TABLE'
  19674.                             when 'V' then 
  19675.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19676.                                 then N'VIEW' else N'SYSTEM VIEW' end
  19677.                             end),
  19678.             TABLE_GUID        = convert(uniqueidentifier, null),
  19679.             BOOKMARKS        = convert(bit, 1),
  19680.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19681.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19682.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19683.             BOOKMARK_INFORMATION    = convert(int, 0),
  19684.             TABLE_VERSION        = convert(int, o.schema_ver),
  19685.             CARDINALITY        = x.rows,
  19686.             DESCRIPTION        = convert(nvarchar(1), null),
  19687.             TABLE_PROPID        = convert(int, null) 
  19688.     
  19689.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  19690.         where    o.name = @table_name
  19691.         and     o.type in ('U','V','S')
  19692.         and    permissions(o.id) <> 0) as t
  19693.  
  19694.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19695.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  19696.     order by 4, 2, 3
  19697. go
  19698. dump tran master with no_log
  19699. go
  19700. create procedure sp_tables_info_rowset;2
  19701.     (
  19702.     @table_schema    sysname = null,    
  19703.     @table_type    nvarchar(255) = null 
  19704.     )
  19705. as
  19706.     select    *
  19707.     from    (select    TABLE_CATALOG        = db_name(),
  19708.             TABLE_SCHEMA        = user_name(o.uid),
  19709.             TABLE_NAME        = o.name,
  19710.             TABLE_TYPE        = convert(nvarchar(30),
  19711.                             case o.type 
  19712.                             when 'U' then 
  19713.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19714.                                 then N'TABLE' else N'SYSTEM TABLE' end
  19715.                             when 'S' then N'SYSTEM TABLE'
  19716.                             when 'V' then 
  19717.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19718.                                 then N'VIEW' else N'SYSTEM VIEW' end
  19719.                             end),
  19720.             TABLE_GUID        = convert(uniqueidentifier, null),
  19721.             BOOKMARKS        = convert(bit, 1),
  19722.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19723.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19724.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19725.             BOOKMARK_INFORMATION    = convert(int, 0),
  19726.             TABLE_VERSION        = convert(int, o.schema_ver),
  19727.             CARDINALITY        = x.rows,
  19728.             DESCRIPTION        = convert(nvarchar(1), null),
  19729.             TABLE_PROPID        = convert(int, null) 
  19730.     
  19731.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  19732.         where     o.type in ('U','V','S')
  19733.         and    permissions(o.id) <> 0) as t
  19734.  
  19735.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19736.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  19737.     order by 4, 2, 3
  19738. go
  19739.  
  19740. grant execute on sp_tables_info_rowset to public
  19741. go
  19742.  
  19743. dump tran master with no_log
  19744. go
  19745. if (charindex('6.00', @@version) > 0)
  19746.     begin
  19747.     if (exists (select * from sysobjects
  19748.         where name = 'sp_tables_info_rowset' and type = 'P '))
  19749.         begin
  19750.         drop procedure sp_tables_info_rowset
  19751.         dump tran master with no_log
  19752.         end
  19753.     end
  19754. go
  19755.  
  19756. print ''
  19757. print 'creating sp_table_constraints_rowset'
  19758. go
  19759.  
  19760.  
  19761. /*    Procedure for 6.50 and earlier servers */
  19762. create procedure sp_table_constraints_rowset
  19763.     (
  19764.     @table_name            varchar(255), 
  19765.     @table_schema        varchar(255) = null,
  19766.     @table_catalog        varchar(255) = null,
  19767.     @constraint_name    varchar(255) = null,
  19768.     @constraint_schema    varchar(255) = null,
  19769.     @constraint_catalog    varchar(255) = null,
  19770.     @constraint_type    varchar(255) = null 
  19771.     )
  19772. as
  19773.     select
  19774.         CONSTRAINT_CATALOG    = db_name(),
  19775.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19776.         CONSTRAINT_NAME        = c_obj.name,
  19777.         TABLE_CATALOG        = db_name(),
  19778.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19779.         TABLE_NAME            = t_obj.name,
  19780.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19781.                                 when 1 then 'PRIMARY KEY'
  19782.                                 when 2 then    'UNIQUE'
  19783.                                 when 3 then    'FOREIGN KEY'
  19784.                                 when 4 then    'CHECK'
  19785.                                  end, 
  19786.         IS_DEFERRABLE        = convert(bit, 0),
  19787.         INITIALLY_DEFERRED    = convert(bit, 0),
  19788.         DESCRIPTION            = convert(varchar(1), null)
  19789.  
  19790.     from 
  19791.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19792.     where
  19793.             t_obj.name    = @table_name
  19794.         and t_obj.type in ('U','S')
  19795.         and (@table_catalog is null or @table_catalog = db_name())
  19796.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19797.         and c.id = t_obj.id
  19798.         and (c.status & 0xf) between 1 and 4
  19799.         and c_obj.id    = c.constid
  19800.         and    c_obj.uid    = user_id()
  19801.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19802.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19803.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19804.         and (@constraint_type is null
  19805.             or (c.status & 0xf)
  19806.                 = case @constraint_type
  19807.                     when 'PRIMARY KEY' then 1
  19808.                     when 'UNIQUE' then 2
  19809.                     when 'FOREIGN KEY' then 3
  19810.                     when 'CHECK' then 4
  19811.                     end)
  19812. order by 2,3,5,6,7 
  19813. go
  19814. dump tran master with no_log
  19815. go
  19816. create procedure sp_table_constraints_rowset;2
  19817.     (
  19818.     @table_schema        varchar(255) = null,
  19819.     @table_catalog        varchar(255) = null,
  19820.     @constraint_name    varchar(255) = null,
  19821.     @constraint_schema    varchar(255) = null,
  19822.     @constraint_catalog    varchar(255) = null,
  19823.     @constraint_type    varchar(255) = null 
  19824.     )
  19825. as
  19826.     select
  19827.         CONSTRAINT_CATALOG    = db_name(),
  19828.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19829.         CONSTRAINT_NAME        = c_obj.name,
  19830.         TABLE_CATALOG        = db_name(),
  19831.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19832.         TABLE_NAME            = t_obj.name,
  19833.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19834.                                 when 1 then 'PRIMARY KEY'
  19835.                                 when 2 then    'UNIQUE'
  19836.                                 when 3 then    'FOREIGN KEY'
  19837.                                 when 4 then    'CHECK'
  19838.                                  end, 
  19839.         IS_DEFERRABLE        = convert(bit, 0),
  19840.         INITIALLY_DEFERRED    = convert(bit, 0),
  19841.         DESCRIPTION            = convert(varchar(1), null)
  19842.  
  19843.     from 
  19844.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19845.     where
  19846.             t_obj.type in ('U','S')
  19847.         and (@table_catalog is null or @table_catalog = db_name())
  19848.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19849.         and c.id = t_obj.id
  19850.         and (c.status & 0xf) between 1 and 4
  19851.         and c_obj.id    = c.constid
  19852.         and    c_obj.uid    = user_id()
  19853.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19854.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19855.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19856.         and (@constraint_type is null
  19857.             or (c.status & 0xf)
  19858.                 = case @constraint_type
  19859.                     when 'PRIMARY KEY' then 1
  19860.                     when 'UNIQUE' then 2
  19861.                     when 'FOREIGN KEY' then 3
  19862.                     when 'CHECK' then 4
  19863.                     end)
  19864. order by 2,3,5,6,7 
  19865. go
  19866. dump tran master with no_log
  19867. go
  19868.  
  19869.  
  19870. if (charindex('7.00', @@version) > 0)
  19871.     drop procedure sp_table_constraints_rowset
  19872. else
  19873. begin
  19874.     print ''
  19875.     print ''
  19876.     print 'Warning:'
  19877.     print 'you are installing the stored procedures '
  19878.     print 'on a pre 7.0 SQL Server.'
  19879.     print 'Ignore the following errors.'
  19880. end
  19881. go
  19882.  
  19883. /*    Procedure for 7.00 server */
  19884. create procedure sp_table_constraints_rowset
  19885.     (
  19886.     @table_name            sysname, 
  19887.     @table_schema        sysname = null,
  19888.     @table_catalog        sysname = null,
  19889.     @constraint_name    sysname = null,
  19890.     @constraint_schema    sysname = null,
  19891.     @constraint_catalog    sysname = null,
  19892.     @constraint_type    nvarchar(255) = null 
  19893.     )
  19894. as
  19895.     select
  19896.         CONSTRAINT_CATALOG    = db_name(),
  19897.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19898.         CONSTRAINT_NAME        = c_obj.name,
  19899.         TABLE_CATALOG        = db_name(),
  19900.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19901.         TABLE_NAME            = t_obj.name,
  19902.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19903.                                 when 1 then N'PRIMARY KEY'
  19904.                                 when 2 then    N'UNIQUE'
  19905.                                 when 3 then    N'FOREIGN KEY'
  19906.                                 when 4 then    N'CHECK'
  19907.                                  end, 
  19908.         IS_DEFERRABLE        = convert(bit, 0),
  19909.         INITIALLY_DEFERRED    = convert(bit, 0),
  19910.         DESCRIPTION            = convert(nvarchar(1), null)
  19911.  
  19912.     from 
  19913.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19914.     where
  19915.             t_obj.name    = @table_name
  19916.         and t_obj.type in ('U','S')
  19917.         and (@table_catalog is null or @table_catalog = db_name())
  19918.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19919.         and c.id = t_obj.id
  19920.         and (c.status & 0xf) between 1 and 4
  19921.         and c_obj.id    = c.constid
  19922.         and    c_obj.uid    = user_id()
  19923.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19924.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19925.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19926.         and (@constraint_type is null
  19927.             or (c.status & 0xf)
  19928.                 = case @constraint_type
  19929.                     when N'PRIMARY KEY' then 1
  19930.                     when N'UNIQUE' then 2
  19931.                     when N'FOREIGN KEY' then 3
  19932.                     when N'CHECK' then 4
  19933.                     end)
  19934. order by 2,3,5,6,7 
  19935. go
  19936. dump tran master with no_log
  19937. go
  19938. create procedure sp_table_constraints_rowset;2
  19939.     (
  19940.     @table_schema        sysname = null,
  19941.     @table_catalog        sysname = null,
  19942.     @constraint_name    sysname = null,
  19943.     @constraint_schema    sysname = null,
  19944.     @constraint_catalog    sysname = null,
  19945.     @constraint_type    nvarchar(255) = null 
  19946.     )
  19947. as
  19948.     select
  19949.         CONSTRAINT_CATALOG    = db_name(),
  19950.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19951.         CONSTRAINT_NAME        = c_obj.name,
  19952.         TABLE_CATALOG        = db_name(),
  19953.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19954.         TABLE_NAME            = t_obj.name,
  19955.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19956.                                 when 1 then N'PRIMARY KEY'
  19957.                                 when 2 then    N'UNIQUE'
  19958.                                 when 3 then    N'FOREIGN KEY'
  19959.                                 when 4 then    N'CHECK'
  19960.                                  end, 
  19961.         IS_DEFERRABLE        = convert(bit, 0),
  19962.         INITIALLY_DEFERRED    = convert(bit, 0),
  19963.         DESCRIPTION            = convert(nvarchar(1), null)
  19964.  
  19965.     from 
  19966.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19967.     where
  19968.             t_obj.type in ('U','S')
  19969.         and (@table_catalog is null or @table_catalog = db_name())
  19970.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19971.         and c.id = t_obj.id
  19972.         and (c.status & 0xf) between 1 and 4
  19973.         and c_obj.id    = c.constid
  19974.         and    c_obj.uid    = user_id()
  19975.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19976.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19977.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19978.         and (@constraint_type is null
  19979.             or (c.status & 0xf)
  19980.                 = case @constraint_type
  19981.                     when N'PRIMARY KEY' then 1
  19982.                     when N'UNIQUE' then 2
  19983.                     when N'FOREIGN KEY' then 3
  19984.                     when N'CHECK' then 4
  19985.                     end)
  19986. order by 2,3,5,6,7 
  19987. go
  19988. dump tran master with no_log
  19989.  
  19990.  
  19991. grant execute on sp_table_constraints_rowset to public
  19992. go
  19993.  
  19994. dump tran master with no_log
  19995. go
  19996. if (charindex('6.00', @@version) > 0)
  19997.     begin
  19998.     if (exists (select * from sysobjects
  19999.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  20000.         begin
  20001.         drop procedure sp_table_constraints_rowset
  20002.         dump tran master with no_log
  20003.         end
  20004.     end
  20005. go
  20006.  
  20007. print ''
  20008. print 'creating sp_table_privileges_rowset'
  20009. go
  20010.  
  20011. /*    Procedure for 6.0 and 6.5 server */
  20012. CREATE PROCEDURE sp_table_privileges_rowset
  20013.     (
  20014.     @table_name    varchar(255) = null,
  20015.     @table_schema    varchar(255) = null,
  20016.     @grantor    varchar(255) = null,
  20017.     @grantee    varchar(255) = null
  20018.     )
  20019. as
  20020. IF @table_name is not null
  20021.     BEGIN
  20022.     select
  20023.         GRANTOR        = user_name(p.grantor),
  20024.         GRANTEE        = user_name(u.uid),
  20025.         TABLE_CATALOG    = db_name(),
  20026.         TABLE_SCHEMA    = user_name(o.uid),
  20027.         TABLE_NAME    = o.name,
  20028.         PRIVILEGE_TYPE    = convert(varchar(30),
  20029.                     case p.action
  20030.                     when 193 then 'SELECT'
  20031.                     when 195 then 'INSERT'
  20032.                     when 196 then 'DELETE'
  20033.                     when 197 then 'UPDATE'
  20034.                     else 'REFERENCES'
  20035.                     end),
  20036.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20037.     from 
  20038.         sysprotects p, sysobjects o, sysusers u
  20039.     where
  20040.         o.name = @table_name
  20041.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20042.     and    (@grantee is null or @grantee = user_name(u.uid))
  20043.     and     o.type in ('U','V','S')
  20044.     and    p.id = o.id
  20045.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20046.             /* expand groups */
  20047.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  20048.     and     p.protecttype <> 206    /* only grant rows */
  20049.     and     p.action in (26,193,195,196,197)
  20050.     and     o.uid <> u.uid            /* no rows for owner */
  20051.     and     not exists (            /* exclude revoke'd privileges */
  20052.             select     *
  20053.             from     sysprotects p1
  20054.             where    p1.protecttype = 206
  20055.             and     p1.action = p.action
  20056.             and     p1.id = p.id
  20057.             and     p1.uid = u.uid)
  20058.     union all
  20059.     select    /*    Add rows for table owner */
  20060.         GRANTOR        = user_name(u.uid),
  20061.         GRANTEE        = user_name(o.uid),
  20062.         TABLE_CATALOG    = db_name(),
  20063.         TABLE_SCHEMA    = user_name(o.uid),
  20064.         TABLE_NAME    = o.name,
  20065.         PRIVILEGE_TYPE    = convert(varchar(30),
  20066.                     case v.number
  20067.                     when 193 then 'SELECT'
  20068.                     when 195 then 'INSERT'
  20069.                     when 196 then 'DELETE'
  20070.                     when 197 then 'UPDATE'
  20071.                     else 'REFERENCES'
  20072.                     end),
  20073.         IS_GRANTABLE    = convert(bit,1)    
  20074.     from 
  20075.         sysobjects o, master.dbo.spt_values v, sysusers u
  20076.     where
  20077.         o.name = @table_name
  20078.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20079.     and    (@grantee is null or @grantee = user_name(u.uid))
  20080.     and     o.type in ('U','V','S')
  20081.     and     u.suid = 1        /* grantor is dbo of database */
  20082.     and    (@grantor is null or @grantor = user_name(u.uid))
  20083.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20084.     and     v.number in (26,193,195,196,197)
  20085.     and     not exists (    /* exclude revoke'd privileges */
  20086.             select     *
  20087.             from     sysprotects p1
  20088.             where    p1.protecttype = 206
  20089.             and     p1.action = v.number
  20090.             and     p1.id = o.id
  20091.             and     p1.uid = o.uid)
  20092.     order by 4,5,6,1,2
  20093.     END
  20094. ELSE
  20095.     BEGIN
  20096.     select
  20097.         GRANTOR        = user_name(p.grantor),
  20098.         GRANTEE        = user_name(u.uid),
  20099.         TABLE_CATALOG    = db_name(),
  20100.         TABLE_SCHEMA    = user_name(o.uid),
  20101.         TABLE_NAME    = o.name,
  20102.         PRIVILEGE_TYPE    = convert(varchar(30),
  20103.                     case p.action
  20104.                     when 193 then 'SELECT'
  20105.                     when 195 then 'INSERT'
  20106.                     when 196 then 'DELETE'
  20107.                     when 197 then 'UPDATE'
  20108.                     else 'REFERENCES'
  20109.                     end),
  20110.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20111.     from 
  20112.         sysprotects p, sysobjects o, sysusers u
  20113.     where
  20114.         o.type in ('U','V','S')
  20115.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20116.     and    (@grantee is null or @grantee = user_name(u.uid))
  20117.     and    p.id = o.id
  20118.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20119.             /* expand groups */
  20120.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  20121.     and     p.protecttype <> 206    /* only grant rows */
  20122.     and     p.action in (26,193,195,196,197)
  20123.     and     o.uid <> u.uid            /* no rows for owner */
  20124.     and     not exists (            /* exclude revoke'd privileges */
  20125.             select     *
  20126.             from     sysprotects p1
  20127.             where    p1.protecttype = 206
  20128.             and     p1.action = p.action
  20129.             and     p1.id = p.id
  20130.             and     p1.uid = u.uid)
  20131.     union all
  20132.     select    /*    Add rows for table owner */
  20133.         GRANTOR        = user_name(u.uid),
  20134.         GRANTEE        = user_name(o.uid),
  20135.         TABLE_CATALOG    = db_name(),
  20136.         TABLE_SCHEMA    = user_name(o.uid),
  20137.         TABLE_NAME    = o.name,
  20138.         PRIVILEGE_TYPE    = convert(varchar(30),
  20139.                     case v.number
  20140.                     when 193 then 'SELECT'
  20141.                     when 195 then 'INSERT'
  20142.                     when 196 then 'DELETE'
  20143.                     when 197 then 'UPDATE'
  20144.                     else 'REFERENCES'
  20145.                     end),
  20146.         IS_GRANTABLE    = convert(bit,1)    
  20147.     from 
  20148.         sysobjects o, master.dbo.spt_values v, sysusers u
  20149.     where
  20150.         o.type in ('U','V','S')
  20151.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20152.     and    (@grantee is null or @grantee = user_name(u.uid))
  20153.     and     u.suid = 1        /* grantor is dbo of database */
  20154.     and    (@grantor is null or @grantor = user_name(u.uid))
  20155.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20156.     and     v.number in (26,193,195,196,197)
  20157.     and     not exists (    /* exclude revoke'd privileges */
  20158.             select     *
  20159.             from     sysprotects p1
  20160.             where    p1.protecttype = 206
  20161.             and     p1.action = v.number
  20162.             and     p1.id = o.id
  20163.             and     p1.uid = o.uid)
  20164.     order by 4,5,6,1,2
  20165.     END
  20166. go
  20167. dump tran master with no_log
  20168. go
  20169. CREATE PROCEDURE sp_table_privileges_rowset;2
  20170.     (
  20171.        @handle        int output,
  20172.        @scrollopt    int output,
  20173.     @ccopt        int output,
  20174.     @rows        int output,
  20175.     @table_name     varchar(255) = null,
  20176.     @table_schema    varchar(255) = null,
  20177.     @grantor    varchar(255) = null,
  20178.     @grantee    varchar(255) = null
  20179.     )
  20180. as
  20181. declare @ret int
  20182. SET NOCOUNT ON
  20183. create table #sptprivsrowset1
  20184.     (
  20185.     GRANTOR        sysname not null,
  20186.     GRANTEE        sysname not null,
  20187.     TABLE_CATALOG    sysname not null,
  20188.     TABLE_SCHEMA    sysname not null,
  20189.     TABLE_NAME    sysname not null,
  20190.     PRIVILEGE_TYPE    sysname not null,
  20191.     IS_GRANTABLE    bit not null
  20192.     )
  20193.     
  20194. IF @table_name is not null
  20195.     BEGIN
  20196.     insert into #sptprivsrowset1
  20197.     select
  20198.         GRANTOR        = user_name(p.grantor),
  20199.         GRANTEE        = user_name(u.uid),
  20200.         TABLE_CATALOG    = db_name(),
  20201.         TABLE_SCHEMA    = user_name(o.uid),
  20202.         TABLE_NAME    = o.name,
  20203.         PRIVILEGE_TYPE    = convert(varchar(30),
  20204.                     case p.action
  20205.                     when 193 then 'SELECT'
  20206.                     when 195 then 'INSERT'
  20207.                     when 196 then 'DELETE'
  20208.                     when 197 then 'UPDATE'
  20209.                     else 'REFERENCES'
  20210.                     end),
  20211.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20212.     from 
  20213.         sysprotects p, sysobjects o, sysusers u
  20214.     where
  20215.         o.name = @table_name
  20216.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20217.     and    (@grantee is null or @grantee = user_name(u.uid))
  20218.     and     o.type in ('U','V','S')
  20219.     and    p.id = o.id
  20220.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20221.             /* expand groups */
  20222.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  20223.     and     p.protecttype <> 206    /* only grant rows */
  20224.     and     p.action in (26,193,195,196,197)
  20225.     and     o.uid <> u.uid            /* no rows for owner */
  20226.     and     not exists (            /* exclude revoke'd privileges */
  20227.             select     *
  20228.             from     sysprotects p1
  20229.             where    p1.protecttype = 206
  20230.             and     p1.action = p.action
  20231.             and     p1.id = p.id
  20232.             and     p1.uid = u.uid)
  20233.     union all
  20234.     select    /*    Add rows for table owner */
  20235.         GRANTOR        = user_name(u.uid),
  20236.         GRANTEE        = user_name(o.uid),
  20237.         TABLE_CATALOG    = db_name(),
  20238.         TABLE_SCHEMA    = user_name(o.uid),
  20239.         TABLE_NAME    = o.name,
  20240.         PRIVILEGE_TYPE    = convert(varchar(30),
  20241.                     case v.number
  20242.                     when 193 then 'SELECT'
  20243.                     when 195 then 'INSERT'
  20244.                     when 196 then 'DELETE'
  20245.                     when 197 then 'UPDATE'
  20246.                     else 'REFERENCES'
  20247.                     end),
  20248.         IS_GRANTABLE    = convert(bit,1)    
  20249.     from 
  20250.         sysobjects o, master.dbo.spt_values v, sysusers u
  20251.     where
  20252.         o.name = @table_name
  20253.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20254.     and    (@grantee is null or @grantee = user_name(u.uid))
  20255.     and     o.type in ('U','V','S')
  20256.     and     u.suid = 1        /* grantor is dbo of database */
  20257.     and    (@grantor is null or @grantor = user_name(u.uid))
  20258.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20259.     and     v.number in (26,193,195,196,197)
  20260.     and     not exists (    /* exclude revoke'd privileges */
  20261.             select     *
  20262.             from     sysprotects p1
  20263.             where    p1.protecttype = 206
  20264.             and     p1.action = v.number
  20265.             and     p1.id = o.id
  20266.             and     p1.uid = o.uid)
  20267.     order by 4,5,6,1,2
  20268.     END
  20269. ELSE
  20270.     BEGIN
  20271.     insert into #sptprivsrowset1
  20272.     select
  20273.         GRANTOR        = user_name(p.grantor),
  20274.         GRANTEE        = user_name(u.uid),
  20275.         TABLE_CATALOG    = db_name(),
  20276.         TABLE_SCHEMA    = user_name(o.uid),
  20277.         TABLE_NAME    = o.name,
  20278.         PRIVILEGE_TYPE    = convert(varchar(30),
  20279.                     case p.action
  20280.                     when 193 then 'SELECT'
  20281.                     when 195 then 'INSERT'
  20282.                     when 196 then 'DELETE'
  20283.                     when 197 then 'UPDATE'
  20284.                     else 'REFERENCES'
  20285.                     end),
  20286.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20287.     from 
  20288.         sysprotects p, sysobjects o, sysusers u
  20289.     where
  20290.         o.type in ('U','V','S')
  20291.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20292.     and    (@grantee is null or @grantee = user_name(u.uid))
  20293.     and    p.id = o.id
  20294.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20295.             /* expand groups */
  20296.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  20297.     and     p.protecttype <> 206    /* only grant rows */
  20298.     and     p.action in (26,193,195,196,197)
  20299.     and     o.uid <> u.uid            /* no rows for owner */
  20300.     and     not exists (            /* exclude revoke'd privileges */
  20301.             select     *
  20302.             from     sysprotects p1
  20303.             where    p1.protecttype = 206
  20304.             and     p1.action = p.action
  20305.             and     p1.id = p.id
  20306.             and     p1.uid = u.uid)
  20307.     union all
  20308.     select    /*    Add rows for table owner */
  20309.         GRANTOR        = user_name(u.uid),
  20310.         GRANTEE        = user_name(o.uid),
  20311.         TABLE_CATALOG    = db_name(),
  20312.         TABLE_SCHEMA    = user_name(o.uid),
  20313.         TABLE_NAME    = o.name,
  20314.         PRIVILEGE_TYPE    = convert(varchar(30),
  20315.                     case v.number
  20316.                     when 193 then 'SELECT'
  20317.                     when 195 then 'INSERT'
  20318.                     when 196 then 'DELETE'
  20319.                     when 197 then 'UPDATE'
  20320.                     else 'REFERENCES'
  20321.                     end),
  20322.         IS_GRANTABLE    = convert(bit,1)    
  20323.     from 
  20324.         sysobjects o, master.dbo.spt_values v, sysusers u
  20325.     where
  20326.         o.type in ('U','V','S')
  20327.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20328.     and    (@grantee is null or @grantee = user_name(u.uid))
  20329.     and     u.suid = 1        /* grantor is dbo of database */
  20330.     and    (@grantor is null or @grantor = user_name(u.uid))
  20331.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20332.     and     v.number in (26,193,195,196,197)
  20333.     and     not exists (    /* exclude revoke'd privileges */
  20334.             select     *
  20335.             from     sysprotects p1
  20336.             where    p1.protecttype = 206
  20337.             and     p1.action = v.number
  20338.             and     p1.id = o.id
  20339.             and     p1.uid = o.uid)
  20340.     order by 4,5,6,1,2
  20341.     END
  20342.  
  20343. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  20344.     @scrollopt output, @ccopt output, @rows output
  20345.  
  20346. drop table #sptprivsrowset1
  20347. return isnull(@ret,0)
  20348. go
  20349. dump tran master with no_log
  20350. go
  20351. CREATE PROCEDURE sp_table_privileges_rowset;3
  20352. as
  20353.     select
  20354.         GRANTOR        = convert(sysname, ' '),
  20355.         GRANTEE        = convert(sysname, ' '),
  20356.         TABLE_CATALOG    = convert(sysname, ' '),
  20357.         TABLE_SCHEMA    = convert(sysname, ' '),
  20358.         TABLE_NAME    = convert(sysname, ' '),
  20359.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  20360.         IS_GRANTABLE    = convert(bit, 0)
  20361.     where    1=0
  20362. go
  20363.  
  20364. if (charindex('7.00', @@version) = 0)
  20365. begin
  20366.     print ''
  20367.     print ''
  20368.     print 'Warning:'
  20369.     print 'you are installing the stored procedures '
  20370.     print 'on a pre 7.0 SQL Server.'
  20371.     print 'Ignore the following errors.'
  20372. end
  20373. else
  20374.     drop proc sp_table_privileges_rowset
  20375. go
  20376.  
  20377.  
  20378. /*    Procedure for 7.0 server */
  20379. CREATE PROCEDURE sp_table_privileges_rowset
  20380.     (
  20381.     @table_name         sysname,
  20382.     @table_schema        sysname = null,
  20383.     @grantor        sysname = null,
  20384.     @grantee        sysname = null
  20385.     )
  20386. as
  20387.     select
  20388.         GRANTOR        = user_name(p.grantor),
  20389.         GRANTEE        = user_name(u.uid),
  20390.         TABLE_CATALOG    = db_name(),
  20391.         TABLE_SCHEMA    = user_name(o.uid),
  20392.         TABLE_NAME    = o.name,
  20393.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20394.                     case p.action
  20395.                     when 193 then N'SELECT'
  20396.                     when 195 then N'INSERT'
  20397.                     when 196 then N'DELETE'
  20398.                     when 197 then N'UPDATE'
  20399.                     else N'REFERENCES'
  20400.                     end),
  20401.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20402.     from 
  20403.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20404.     where
  20405.         o.name = @table_name
  20406.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20407.     and    (@grantee is null or @grantee = user_name(u.uid))
  20408.     and     o.type in ('U','V','S')
  20409.     and    p.id = o.id
  20410.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20411.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20412.     and     (u.uid > 0 and u.uid < 16384)
  20413.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20414.     and     p.protecttype <> 206    /* only grant rows */
  20415.     and     p.action in (26,193,195,196,197)
  20416.     and     o.uid <> u.uid            /* no rows for owner */
  20417.     and     not exists (            /* exclude revoke'd privileges */
  20418.             select     *
  20419.             from     sysprotects p1
  20420.             where    p1.protecttype = 206
  20421.             and     p1.action = p.action
  20422.             and     p1.id = p.id
  20423.             and     p1.uid = u.uid)
  20424.     union all
  20425.     select    /*    Add rows for table owner */
  20426.         GRANTOR        = user_name(u.uid),
  20427.         GRANTEE        = user_name(o.uid),
  20428.         TABLE_CATALOG    = db_name(),
  20429.         TABLE_SCHEMA    = user_name(o.uid),
  20430.         TABLE_NAME    = o.name,
  20431.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20432.                     case v.number
  20433.                     when 193 then N'SELECT'
  20434.                     when 195 then N'INSERT'
  20435.                     when 196 then N'DELETE'
  20436.                     when 197 then N'UPDATE'
  20437.                     else N'REFERENCES'
  20438.                     end),
  20439.         IS_GRANTABLE    = convert(bit,1)    
  20440.     from 
  20441.         sysobjects o, master.dbo.spt_values v, sysusers u
  20442.     where
  20443.         o.name = @table_name
  20444.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20445.     and    (@grantee is null or @grantee = user_name(u.uid))
  20446.     and     o.type in ('U','V','S')
  20447.     and     u.suid = 1        /* grantor is dbo of database */
  20448.     and    (@grantor is null or @grantor = user_name(u.uid))
  20449.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20450.     and     v.number in (26,193,195,196,197)
  20451.     and     not exists (    /* exclude revoke'd privileges */
  20452.             select     *
  20453.             from     sysprotects p1
  20454.             where    p1.protecttype = 206
  20455.             and     p1.action = v.number
  20456.             and     p1.id = o.id
  20457.             and     p1.uid = o.uid)
  20458.     order by 4,5,6,1,2
  20459. go
  20460. dump tran master with no_log
  20461. go
  20462. CREATE PROCEDURE sp_table_privileges_rowset;2
  20463.     (
  20464.     @table_schema        sysname = null,
  20465.     @grantor        sysname = null,
  20466.     @grantee        sysname = null
  20467.     )
  20468. as
  20469.     select
  20470.         GRANTOR        = user_name(p.grantor),
  20471.         GRANTEE        = user_name(u.uid),
  20472.         TABLE_CATALOG    = db_name(),
  20473.         TABLE_SCHEMA    = user_name(o.uid),
  20474.         TABLE_NAME    = o.name,
  20475.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20476.                     case p.action
  20477.                     when 193 then N'SELECT'
  20478.                     when 195 then N'INSERT'
  20479.                     when 196 then N'DELETE'
  20480.                     when 197 then N'UPDATE'
  20481.                     else N'REFERENCES'
  20482.                     end),
  20483.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20484.     from 
  20485.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20486.     where
  20487.         o.type in ('U','V','S')
  20488.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20489.     and    (@grantee is null or @grantee = user_name(u.uid))
  20490.     and    p.id = o.id
  20491.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20492.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20493.     and     (u.uid > 0 and u.uid < 16384)
  20494.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20495.     and     p.protecttype <> 206    /* only grant rows */
  20496.     and     p.action in (26,193,195,196,197)
  20497.     and     o.uid <> u.uid            /* no rows for owner */
  20498.     and     not exists (            /* exclude revoke'd privileges */
  20499.             select     *
  20500.             from     sysprotects p1
  20501.             where    p1.protecttype = 206
  20502.             and     p1.action = p.action
  20503.             and     p1.id = p.id
  20504.             and     p1.uid = u.uid)
  20505.     union all
  20506.     select    /*    Add rows for table owner */
  20507.         GRANTOR        = user_name(u.uid),
  20508.         GRANTEE        = user_name(o.uid),
  20509.         TABLE_CATALOG    = db_name(),
  20510.         TABLE_SCHEMA    = user_name(o.uid),
  20511.         TABLE_NAME    = o.name,
  20512.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20513.                     case v.number
  20514.                     when 193 then N'SELECT'
  20515.                     when 195 then N'INSERT'
  20516.                     when 196 then N'DELETE'
  20517.                     when 197 then N'UPDATE'
  20518.                     else N'REFERENCES'
  20519.                     end),
  20520.         IS_GRANTABLE    = convert(bit,1)    
  20521.     from 
  20522.         sysobjects o, master.dbo.spt_values v, sysusers u
  20523.     where
  20524.         o.type in ('U','V','S')
  20525.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20526.     and    (@grantee is null or @grantee = user_name(u.uid))
  20527.     and     u.suid = 1        /* grantor is dbo of database */
  20528.     and    (@grantor is null or @grantor = user_name(u.uid))
  20529.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20530.     and     v.number in (26,193,195,196,197)
  20531.     and     not exists (    /* exclude revoke'd privileges */
  20532.             select     *
  20533.             from     sysprotects p1
  20534.             where    p1.protecttype = 206
  20535.             and     p1.action = v.number
  20536.             and     p1.id = o.id
  20537.             and     p1.uid = o.uid)
  20538.     order by 4,5,6,1,2
  20539. go
  20540. dump tran master with no_log
  20541. go
  20542. create procedure sp_table_privileges_rowset;5
  20543.     (
  20544.     @table_server        sysname,
  20545.     @table_catalog        sysname = null,
  20546.     @table_name        sysname = null,
  20547.     @table_schema        sysname = null,
  20548.     @grantor        sysname = null,
  20549.     @grantee        sysname = null
  20550.     )
  20551. as
  20552.     select
  20553.         GRANTOR,        
  20554.         GRANTEE,        
  20555.         TABLE_CATALOG,    
  20556.         TABLE_SCHEMA,    
  20557.         TABLE_NAME,    
  20558.         PRIVILEGE_TYPE,    
  20559.         IS_GRANTABLE    
  20560.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  20561.                 @table_server,
  20562.                 @table_catalog,
  20563.                 @table_schema,
  20564.                 @table_name,
  20565.                 @grantor,
  20566.                 @grantee >
  20567.     order by 3,4,5,6,1,2
  20568. go
  20569.  
  20570. grant execute on sp_table_privileges_rowset to public
  20571. go
  20572. dump tran master with no_log
  20573. go
  20574. if (charindex('6.00', @@version) > 0)
  20575.     begin
  20576.     if (exists (select * from sysobjects
  20577.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  20578.         begin
  20579.         drop proc sp_table_privileges_rowset
  20580.         dump tran master with no_log
  20581.         end
  20582.     end
  20583. go
  20584.  
  20585.  
  20586. print ''
  20587. print 'creating sp_linkedservers_rowset'
  20588. go
  20589. if (charindex('7.00', @@version) = 0)
  20590. begin
  20591.     print ''
  20592.     print ''
  20593.     print 'Warning:'
  20594.     print 'you are installing the stored procedures '
  20595.     print 'on a pre 7.0 SQL Server.'
  20596.     print 'Ignore the following errors.'
  20597. end
  20598. go
  20599. /*    Procedure for 7.0 server */
  20600. create proc sp_linkedservers_rowset
  20601.     (
  20602.     @srvname     sysname
  20603.     )
  20604. as
  20605.     select
  20606.         SVR_NAME         = srvname,
  20607.         SVR_PRODUCT        = srvproduct,
  20608.         SVR_PROVIDERNAME    = providername,
  20609.         SVR_DATASOURCE        = datasource,
  20610.         SVR_PROVIDERSTRING    = providerstring,
  20611.         SVR_LOCATION        = location,
  20612.         SVR_CATALOG        = catalog
  20613.     from master.dbo.sysservers
  20614.     where srvname = @srvname and (srvstatus & 128) = 128
  20615.     order by 1
  20616. go
  20617. dump tran master with no_log
  20618. go
  20619. create proc sp_linkedservers_rowset;2
  20620. as
  20621.     select
  20622.         SVR_NAME         = srvname,
  20623.         SVR_PRODUCT        = srvproduct,
  20624.         SVR_PROVIDERNAME    = providername,
  20625.         SVR_DATASOURCE        = datasource,
  20626.         SVR_PROVIDERSTRING    = providerstring,
  20627.         SVR_LOCATION        = location,
  20628.         SVR_CATALOG        = catalog
  20629.     from master.dbo.sysservers
  20630.     where (srvstatus & 128) = 128
  20631.     order by 1
  20632. go
  20633.  
  20634. grant execute on sp_linkedservers_rowset to public
  20635. go
  20636.  
  20637. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  20638.  
  20639.  
  20640. dump tran master with no_log
  20641. go
  20642.  
  20643.  
  20644.  
  20645.  
  20646. if (exists (select * from sysobjects
  20647.         where name = 'sp_configure' and type = 'P '))
  20648.     begin
  20649.         exec sp_configure 'allow updates',0
  20650.         reconfigure with override
  20651.     end
  20652. go
  20653.  
  20654. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  20655.  
  20656. go
  20657.  
  20658. if exists (select * from sysobjects where name = 'sp_check_objects'
  20659.             and type = 'P ')
  20660.     begin
  20661.         /* Only supported on 6.0 servers */
  20662.         print ''
  20663.         print 'Checking objects created by instcat.sql.'
  20664.  
  20665.         exec sp_check_objects 'catalog'
  20666.     end
  20667. go
  20668.  
  20669. print ''
  20670. print 'instcat.sql completed successfully.'
  20671. go
  20672.  
  20673. set quoted_identifier off
  20674. go
  20675.  
  20676. dump tran master with no_log
  20677. go
  20678. checkpoint
  20679. go
  20680. /**/
  20681.