home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2001 Ek…a Alt Til Din Hjemmeside / K-CD_2001_Ekstra_Alt_til_din_hjemmeside.iso / SYSTEM / INSTCAT.SQL < prev    next >
Text File  |  1998-11-12  |  591KB  |  19,637 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. **    Copyright 1992-1998, Microsoft Corp.    All rights reserved.
  6. */
  7.  
  8. /*
  9. NOTE:  you MUST change the last row inserted into spt_server_info
  10. to be version number of this file.    the convention is j.nn.bbb, where
  11. j is the major version number ('7' now), nn is the minor version number
  12. ('00' now), and bbb is the build number.
  13. insert into spt_server_info
  14.     values (500, 'SYS_SPROC_VERSION', '7.00.bbb')
  15. */
  16.  
  17. /****************************************************************************/
  18. /* This portion sets up the ability to perform all the functions in this    */
  19. /* script                                                                    */
  20. /****************************************************************************/
  21. go
  22. use master
  23. go
  24. dump tran master with no_log
  25. go
  26.  
  27. set quoted_identifier on
  28. go
  29.  
  30. if (exists (select * from sysobjects
  31.         where name = 'sp_configure' and type = 'P '))
  32. begin
  33.     execute sp_configure 'update',1
  34. end
  35. reconfigure with override
  36. go
  37.  
  38. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  39.  
  40. go
  41.  
  42. /*
  43. ** If old versions of tables exist, drop them.
  44. */
  45. if (exists (select * from sysobjects
  46.         where name = 'MSdatatype_info' and type = 'U '))
  47.     drop table MSdatatype_info
  48. go
  49. if (exists (select * from sysobjects
  50.         where name = 'MSdatatype_info_ext' and type = 'U '))
  51.     drop table MSdatatype_info_ext
  52. go
  53. if (exists (select * from sysobjects
  54.         where name = 'MStable_types' and type = 'U '))
  55.     drop table MStable_types
  56. go
  57. if (exists (select * from sysobjects
  58.         where name = 'MSserver_info' and type = 'U '))
  59.     drop table MSserver_info
  60. go
  61. if (exists (select * from sysobjects
  62.         where name = 'spt_table_types' and type = 'U '))
  63.     drop table spt_table_types
  64. go
  65. /*
  66. ** If tables or procs already exist, drop them.
  67. */
  68.  
  69. if (exists (select * from sysobjects
  70.         where name = 'spt_datatype_info' and type = 'U '))
  71.     drop table spt_datatype_info
  72. go
  73. if (exists (select * from sysobjects
  74.         where name = 'spt_datatype_info_ext' and type = 'U '))
  75.     drop table spt_datatype_info_ext
  76. go
  77. if (exists (select * from sysobjects
  78.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  79.     drop proc sp_add_server_sortinfo
  80. go
  81. if (exists (select * from sysobjects
  82.         where name = 'spt_server_info' and type = 'U '))
  83.     drop table spt_server_info
  84. go
  85. if (exists (select * from sysobjects
  86.         where name = 'sp_tables' and type = 'P '))
  87.     drop proc sp_tables
  88. go
  89. if (exists (select * from sysobjects
  90.         where name = 'sp_statistics' and type = 'P '))
  91.     drop proc sp_statistics
  92. go
  93. if (exists (select * from sysobjects
  94.         where name = 'sp_columns' and type = 'P '))
  95.     drop proc sp_columns
  96. go
  97. if (exists (select * from sysobjects
  98.         where name = 'sp_fkeys' and type = 'P '))
  99.     drop proc sp_fkeys
  100. go
  101. if (exists (select * from sysobjects
  102.         where name = 'sp_pkeys' and type = 'P '))
  103.     drop proc sp_pkeys
  104. dump tran master with no_log
  105. go
  106.  
  107. go
  108. if (exists (select * from sysobjects
  109.         where name = 'sp_stored_procedures' and type = 'P '))
  110.     drop proc sp_stored_procedures
  111. go
  112. if (exists (select * from sysobjects
  113.         where name = 'sp_sproc_columns' and type = 'P '))
  114.     drop proc sp_sproc_columns
  115. go
  116. if (exists (select * from sysobjects
  117.         where name = 'sp_table_privileges' and type = 'P '))
  118.     drop proc sp_table_privileges
  119. go
  120. if (exists (select * from sysobjects
  121.         where name = 'sp_column_privileges' and type = 'P '))
  122.     drop proc sp_column_privileges
  123. go
  124. dump tran master with no_log
  125. go
  126. if (exists (select * from sysobjects
  127.         where name = 'sp_server_info' and type = 'P '))
  128.     drop proc sp_server_info
  129. go
  130. if (exists (select * from sysobjects
  131.         where name = 'sp_datatype_info' and type = 'P '))
  132.     drop proc sp_datatype_info
  133. go
  134. if (exists (select * from sysobjects
  135.         where name = 'sp_special_columns' and type = 'P '))
  136.     drop proc sp_special_columns
  137. go
  138. if (exists (select * from sysobjects
  139.         where name = 'sp_databases' and type = 'P '))
  140.     drop proc sp_databases
  141. go
  142. dump tran master with no_log
  143. go
  144. if (exists (select * from sysobjects
  145.         where name = 'sp_ddopen' and type = 'P '))
  146.     drop proc sp_ddopen
  147. go
  148. if (exists (select * from sysobjects
  149.         where name = 'sp_tableswc' and type = 'P '))
  150.     drop proc sp_tableswc
  151. go
  152.  
  153. dump tran master with no_log
  154. go
  155.  
  156.  
  157. if (exists (select * from sysobjects
  158.         where name = 'spt_provider_types' and type = 'U '))
  159.     begin
  160.     drop table spt_provider_types
  161.     dump tran master with no_log
  162.     end
  163. go
  164. if (exists (select * from sysobjects
  165.         where name = 'sp_catalogs_rowset' and type = 'P '))
  166.     begin
  167.     drop procedure sp_catalogs_rowset
  168.     dump tran master with no_log
  169.     end
  170. go
  171. if (exists (select * from sysobjects
  172.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  173.     begin
  174.     drop procedure sp_column_privileges_rowset
  175.     dump tran master with no_log
  176.     end
  177. go
  178. if (exists (select * from sysobjects
  179.         where name = 'sp_columns_rowset' and type = 'P '))
  180.     begin
  181.     drop procedure sp_columns_rowset
  182.     dump tran master with no_log
  183.     end
  184. go
  185. if (exists (select * from sysobjects
  186.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  187.     begin
  188.     drop procedure sp_foreign_keys_rowset
  189.     dump tran master with no_log
  190.     end
  191. go
  192. if (exists (select * from sysobjects
  193.         where name = 'sp_indexes_rowset' and type = 'P '))
  194.     begin
  195.     drop procedure sp_indexes_rowset
  196.     dump tran master with no_log
  197.     end
  198. go
  199. if (exists (select * from sysobjects
  200.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  201.     begin
  202.     drop procedure sp_primary_keys_rowset
  203.     dump tran master with no_log
  204.     end
  205. go
  206. if (exists (select * from sysobjects
  207.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  208.     begin
  209.     drop procedure sp_procedure_params_rowset
  210.     dump tran master with no_log
  211.     end
  212. go
  213. if (exists (select * from sysobjects
  214.         where name = 'sp_procedures_rowset' and type = 'P '))
  215.     begin
  216.     drop procedure sp_procedures_rowset
  217.     dump tran master with no_log
  218.     end
  219. go
  220. if (exists (select * from sysobjects
  221.         where name = 'sp_provider_types_rowset' and type = 'P '))
  222.     begin
  223.     drop procedure sp_provider_types_rowset
  224.     dump tran master with no_log
  225.     end
  226. go
  227. if (exists (select * from sysobjects
  228.         where name = 'sp_schemata_rowset' and type = 'P '))
  229.     begin
  230.     drop procedure sp_schemata_rowset
  231.     dump tran master with no_log
  232.     end
  233. go
  234. if (exists (select * from sysobjects
  235.         where name = 'sp_statistics_rowset' and type = 'P '))
  236.     begin
  237.     drop procedure sp_statistics_rowset
  238.     dump tran master with no_log
  239.     end
  240. go
  241. if (exists (select * from sysobjects
  242.         where name = 'sp_tables_rowset' and type = 'P '))
  243.     begin
  244.     drop procedure sp_tables_rowset
  245.     dump tran master with no_log
  246.     end
  247. go
  248. if (exists (select * from sysobjects
  249.         where name = 'sp_tables_info_rowset' and type = 'P '))
  250.     begin
  251.     drop procedure sp_tables_info_rowset
  252.     dump tran master with no_log
  253.     end
  254. go
  255. if (exists (select * from sysobjects
  256.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  257.     begin
  258.     drop proc sp_table_constraints_rowset
  259.     dump tran master with no_log
  260.     end
  261. go
  262. if (exists (select * from sysobjects
  263.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  264.     begin
  265.     drop proc sp_table_privileges_rowset
  266.     dump tran master with no_log
  267.     end
  268. go
  269. if (exists (select * from sysobjects
  270.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  271.     begin
  272.     drop proc sp_linkedservers_rowset
  273.     dump tran master with no_log
  274.     end
  275. go
  276.  
  277.  
  278. print 'creating table spt_datatype_info_ext'
  279. go
  280.  
  281. if (charindex('6.00', @@version) = 0 and
  282.     charindex('6.50', @@version) = 0 and
  283.     charindex('7.00', @@version) = 0)
  284. begin    /*    Pre 6.0 server */
  285.     print ''
  286.     print ''
  287.     print 'Warning:'
  288.     print 'you are installing the stored procedures '
  289.     print 'on a pre 6.0 SQL Server.'
  290.     print 'Ignore the following error.'
  291.     create table spt_datatype_info_ext (
  292.                 user_type        smallint    not null,
  293.                 CREATE_PARAMS    varchar(32) null,
  294.                 AUTO_INCREMENT    smallint null,
  295.                 typename        varchar(32))
  296. end
  297. go
  298. if (charindex('6.00', @@version) > 0 or
  299.     charindex('6.50', @@version) > 0 or
  300.     charindex('7.00', @@version) > 0)
  301. begin    /* 6.0 or later server */
  302.     create table spt_datatype_info_ext (
  303.                 user_type        smallint    not null,
  304.                 CREATE_PARAMS    varchar(32) null,
  305.                 AUTO_INCREMENT    smallint null,
  306.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  307. end
  308. go
  309.  
  310. grant select on spt_datatype_info_ext to public
  311. go
  312.  
  313.  
  314. insert into spt_datatype_info_ext
  315.     /* CHAR      user_type, create_params, auto_increment */
  316.     values             (1,    'length' ,0, 'char')
  317.  
  318. insert into spt_datatype_info_ext
  319.     /* VARCHAR     user_type, create_params, auto_increment */
  320.     values             (2,    'max length' ,0, 'varchar')
  321.  
  322. insert into spt_datatype_info_ext
  323.     /* BINARY     user_type, create_params, auto_increment */
  324.     values             (3,    'length' ,0, 'binary')
  325.  
  326. insert into spt_datatype_info_ext
  327.     /* VARBINARY user_type, create_params, auto_increment */
  328.     values             (4,    'max length' ,0, 'varbinary')
  329.  
  330. if    (charindex('6.00', @@version) > 0 or
  331.      charindex('6.50', @@version) > 0 or
  332.      charindex('7.00', @@version) > 0)
  333. begin    /*    Add 6.0 data types */
  334.     insert into spt_datatype_info_ext
  335.         /* DECIMAL user_type, create_params, auto_increment */
  336.         values             (26,    'precision,scale' ,0, 'decimal')
  337.  
  338.     insert into spt_datatype_info_ext
  339.         /* NUMERIC user_type, create_params, auto_increment */
  340.         values             (25,    'precision,scale' ,0, 'numeric')
  341.  
  342.     insert into spt_datatype_info_ext
  343.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  344.         values             (26,    'precision' ,1, 'decimal')
  345.  
  346.     insert into spt_datatype_info_ext
  347.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  348.         values             (25,    'precision' ,1, 'numeric')
  349.  
  350. end
  351. else    /*    Pre 6.0 server, add SYSNAME create param */
  352.     begin
  353.         insert into spt_datatype_info_ext
  354.             /* SYSNAME     user_type, create_param, auto_increments */
  355.             values             (18,    'max length' ,0, 'sysname')
  356.  
  357.     end
  358. go
  359.  
  360. if (charindex('7.00', @@version) = 0)
  361. begin
  362.     print ''
  363.     print ''
  364.     print 'Warning:'
  365.     print 'you are installing the stored procedures '
  366.     print 'on a pre 7.0 SQL Server.'
  367.     print 'Ignore the following errors.'
  368. end
  369. go
  370. if (charindex('7.00', @@version) > 0)
  371. begin    /*    Update usertypes for 7.00 server */
  372.     begin tran
  373.     insert into spt_datatype_info_ext
  374.         /* NCHAR     user_type, create_params, auto_increment */
  375.         values             (0,    'length' ,0, 'nchar')
  376.  
  377.     insert into spt_datatype_info_ext
  378.         /* NVARCHAR     user_type, create_params, auto_increment */
  379.         values             (0,    'max length' ,0, 'nvarchar')
  380.  
  381.     /* SET user_type TO SPHINX VALUES */
  382.     update spt_datatype_info_ext set user_type = xusertype
  383.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  384.  
  385.     commit tran
  386. end
  387. go
  388.  
  389. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  390. go
  391.  
  392. print 'creating table spt_datatype_info'
  393. go
  394. if (charindex('6.00', @@version) = 0 and
  395.     charindex('6.50', @@version) = 0 and
  396.     charindex('7.00', @@version) = 0)
  397. begin    /*    Pre 6.0 server */
  398.     print ''
  399.     print ''
  400.     print 'Warning:'
  401.     print 'you are installing the stored procedures '
  402.     print 'on a pre 6.0 SQL Server.'
  403.     print 'Ignore the following error.'
  404.     create table spt_datatype_info (
  405.         ss_dtype            tinyint     not null,
  406.         fixlen                int         null,        /* datatype len for variable, else null */
  407.         ODBCVer             tinyint     null,        /* version if needed, else null */
  408.         TYPE_NAME            varchar(32)    not null,
  409.         DATA_TYPE            smallint    not null,
  410.         data_precision        int         null,
  411.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  412.         RADIX                smallint    null,
  413.         length                int         null,
  414.         LITERAL_PREFIX        varchar(32) null,
  415.         LITERAL_SUFFIX        varchar(32) null,
  416.         CREATE_PARAMS        varchar(32) null,
  417.         NULLABLE            smallint    not null,
  418.         CASE_SENSITIVE        smallint    not null,
  419.         SEARCHABLE            smallint    not null,
  420.         UNSIGNED_ATTRIBUTE    smallint    null,
  421.         MONEY                smallint    not null,
  422.         AUTO_INCREMENT        smallint    null,
  423.         LOCAL_TYPE_NAME     varchar(32)    null,
  424.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  425.         SQL_DATA_TYPE        smallint    not null,
  426.         SQL_DATETIME_SUB    smallint    null)
  427. end
  428. go
  429. if (charindex('6.00', @@version) > 0 or
  430.     charindex('6.50', @@version) > 0 or
  431.     charindex('7.00', @@version) > 0)
  432. begin    /* 6.0 or later server */
  433.     create table spt_datatype_info (
  434.         ss_dtype            tinyint     not null,
  435.         fixlen                int         null,        /* datatype len for variable, else null */
  436.         ODBCVer             tinyint     null,        /* version if needed, else null */
  437.         TYPE_NAME            sysname     not null,
  438.         DATA_TYPE            smallint    not null,
  439.         data_precision        int         null,
  440.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  441.         RADIX                smallint    null,
  442.         length                int         null,
  443.         LITERAL_PREFIX        varchar(32) null,
  444.         LITERAL_SUFFIX        varchar(32) null,
  445.         CREATE_PARAMS        varchar(32) null,
  446.         NULLABLE            smallint    not null,
  447.         CASE_SENSITIVE        smallint    not null,
  448.         SEARCHABLE            smallint    not null,
  449.         UNSIGNED_ATTRIBUTE    smallint    null,
  450.         MONEY                smallint    not null,
  451.         AUTO_INCREMENT        smallint    null,
  452.         LOCAL_TYPE_NAME     sysname     null,
  453.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  454.         SQL_DATA_TYPE        smallint    not null,
  455.         SQL_DATETIME_SUB    smallint    null)
  456. end
  457. go
  458.  
  459. grant select on spt_datatype_info to public
  460. go
  461.  
  462. /* Get case sensitivity */
  463. if 'A' = 'A' /* create dummy begin block */
  464. begin
  465.     declare @case smallint
  466.  
  467.     begin tran
  468.     select @case = 0
  469.     select @case = 1 where 'a' <> 'A'
  470.  
  471.     /* Local Binary */
  472.     insert into spt_datatype_info values
  473.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  474.  
  475.     /* Local Bit */
  476.     insert into spt_datatype_info values
  477.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  478.  
  479.     /* Local Char */
  480.     insert into spt_datatype_info values
  481.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  482.  
  483.     /* Local Datetime */
  484.     insert into spt_datatype_info values
  485.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  486.     insert into spt_datatype_info values
  487.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  488.  
  489.     /* Local Smalldatetime */
  490.     insert into spt_datatype_info values
  491.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  492.     insert into spt_datatype_info values
  493.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  494.  
  495.     /* Local Float */
  496.     insert into spt_datatype_info values
  497.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  498.     insert into spt_datatype_info values
  499.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  500.  
  501.     /* Local Real */
  502.     insert into spt_datatype_info values
  503.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  504.     insert into spt_datatype_info values
  505.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  506.  
  507.     /* Local Smallmoney */
  508.     insert into spt_datatype_info values
  509.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  510.  
  511.     /* Local Money */
  512.     insert into spt_datatype_info values
  513.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  514.  
  515.     /* Local Int */
  516.     insert into spt_datatype_info values
  517.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  518.  
  519.     commit tran
  520. end
  521. go
  522.  
  523. if 'A' = 'A' /* create dummy begin block */
  524. begin
  525.     declare @case smallint
  526.  
  527.     begin tran
  528.     select @case = 0
  529.     select @case = 1 where 'a' <> 'A'
  530.  
  531.  
  532.     /* Local Smallint */
  533.     insert into spt_datatype_info values
  534.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  535.     insert into spt_datatype_info values
  536.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  537.  
  538.     /* Local Tinyint */
  539.     insert into spt_datatype_info values
  540.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  541.  
  542.     /* Local Text */
  543.     insert into spt_datatype_info values
  544.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  545.  
  546.     /* Local Varbinary */
  547.     insert into spt_datatype_info values
  548.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  549.  
  550.     /* Local Varchar */
  551.     insert into spt_datatype_info values
  552.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  553.  
  554.     /* Local Image */
  555.     insert into spt_datatype_info values
  556.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  557.  
  558.     if (charindex('6.00', @@version) > 0 or
  559.         charindex('6.50', @@version) > 0 or
  560.         charindex('7.00', @@version) > 0)
  561.     begin    /*    Add 6.0 data types */
  562.         /* Local Decimal */
  563.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  564.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  565.  
  566.         /* Local Numeric */
  567.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  568.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  569.  
  570.         /* Identity attribute data types */
  571.  
  572.         /* Identity Int */
  573.         insert into spt_datatype_info values
  574.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  575.  
  576.         /* Identity Smallint */
  577.         insert into spt_datatype_info values
  578.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  579.  
  580.         /* Identity Tinyint */
  581.         insert into spt_datatype_info values
  582.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  583.  
  584.         /* Identity Numeric */
  585.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  586.         (55,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  587.  
  588.         /* Identity Numeric */
  589.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  590.         (63,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  591.  
  592.     end
  593.  
  594.     if (charindex('7.00', @@version) = 0)
  595.     begin    /*    Add nullable type for non-Sphinx server */
  596.         /* Local Datetimn */
  597.         insert into spt_datatype_info values
  598.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  599.         insert into spt_datatype_info values
  600.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  601.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  602.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  603.         insert into spt_datatype_info values
  604.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  605.  
  606.         /* Local Floatn */
  607.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  608.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  609.         insert into spt_datatype_info values
  610.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  611.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  612.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  613.         insert into spt_datatype_info values
  614.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  615.  
  616.         /* Local Moneyn */
  617.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  618.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  619.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  620.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  621.  
  622.         /* Local Intn */
  623.         insert into spt_datatype_info values /* sql server type is 'intn' */
  624.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  625.         insert into spt_datatype_info values /* sql server type is 'intn' */
  626.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  627.         insert into spt_datatype_info values
  628.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  629.  
  630.         if (charindex('6.00', @@version) > 0 or
  631.             charindex('6.50', @@version) > 0 or
  632.             charindex('7.00', @@version) > 0)
  633.         begin    /*    Add 6.0 data types */
  634.             /* Local Decimaln */
  635.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  636.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  637.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  638.             (106,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  639.  
  640.             /* Local Numericn */
  641.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  642.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  643.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  644.             (108,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  645.         end
  646.     end
  647.  
  648.     commit tran
  649. end
  650. go
  651.  
  652. if (charindex('7.00', @@version) = 0)
  653. begin
  654.     print ''
  655.     print ''
  656.     print 'Warning:'
  657.     print 'you are installing the stored procedures '
  658.     print 'on a pre 7.0 SQL Server.'
  659.     print 'Ignore the following errors.'
  660. end
  661. go
  662. if (charindex('7.00', @@version) > 0)
  663.     begin
  664.         declare @ncase smallint
  665.  
  666.         select @ncase = 0
  667.         select @ncase = 1 where N'a' <> N'A'
  668.  
  669.         /* Local Timestamp */
  670.         insert into spt_datatype_info values
  671.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  672.  
  673.         /* Local GUID */
  674.         insert into spt_datatype_info values
  675.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  676.  
  677.         /* Local NChar */
  678.         insert into spt_datatype_info values
  679.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  680.  
  681.         /* Local NVarchar */
  682.         insert into spt_datatype_info values
  683.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  684.  
  685.         /* Local NText */
  686.         insert into spt_datatype_info values
  687.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  688.  
  689.         update spt_datatype_info set NULLABLE = 1
  690.             where TYPE_NAME = 'bit'
  691.  
  692.         update spt_datatype_info set ss_dtype = (select xtype from systypes
  693.             where TYPE_NAME like name+'%')
  694.     end
  695. go
  696.  
  697. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  698. go
  699.  
  700. dump tran master with no_log
  701. go
  702.  
  703. print 'creating table spt_server_info'
  704. go
  705. create table spt_server_info (
  706.               attribute_id        int NOT NULL,
  707.               attribute_name    varchar(60) NOT NULL,
  708.               attribute_value    varchar(255) NOT NULL)
  709. go
  710.  
  711. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  712. go
  713.  
  714. if (charindex('7.00', @@version) = 0)
  715.     begin
  716.         drop procedure sp_add_server_sortinfo    /* not used by older servers */
  717.         dump tran master with no_log
  718.     end
  719. go
  720.  
  721.  
  722. insert into spt_server_info
  723.     values (1,'DBMS_NAME','Microsoft SQL Server')
  724. insert into spt_server_info
  725.     values (2,'DBMS_VER',@@version)
  726. insert into spt_server_info
  727.     values (10,'OWNER_TERM','owner')
  728. insert into spt_server_info
  729.     values (11,'TABLE_TERM','table')
  730. insert into spt_server_info
  731.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  732. insert into spt_server_info
  733.     values (13,'TABLE_LENGTH','30')
  734. insert into spt_server_info
  735.     values (14,'MAX_QUAL_LENGTH','30')
  736. insert into spt_server_info
  737.     values (15,'COLUMN_LENGTH','30')
  738. if 'A' = 'a' /* If not case sensitive server */
  739. begin
  740.     insert into spt_server_info
  741.         values (16,'IDENTIFIER_CASE','MIXED')
  742. end
  743. else
  744. begin
  745.     insert into spt_server_info
  746.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  747. end
  748. insert into spt_server_info
  749.     values (17,'TX_ISOLATION','2')
  750. if (charindex('6.00', @@version) > 0 or
  751.     charindex('6.50', @@version) > 0 or
  752.     charindex('7.00', @@version) > 0)
  753. begin    /*    Add 6.0 collation sequence */
  754.     insert into spt_server_info
  755.         select 18,'COLLATION_SEQ',
  756.             'charset='+t2.name+' sort_order='+t1.name
  757.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  758.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  759.         from syscharsets t1, syscharsets t2, sysconfigures t3
  760.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  761. end
  762. else
  763. begin    /*    Add 4.2x collation sequence */
  764.     insert into spt_server_info
  765.         select 18,'COLLATION_SEQ',
  766.             'charset='+t2.name+' sort_order='+t1.name
  767.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  768.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  769.         from syscharsets t1, syscharsets t2, sysconfigures t3
  770.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  771. end
  772. insert into spt_server_info
  773.     values (19,'SAVEPOINT_SUPPORT','Y')
  774. insert into spt_server_info
  775.     values (20,'MULTI_RESULT_SETS','Y')
  776. insert into spt_server_info
  777.     values (22,'ACCESSIBLE_TABLES','Y')
  778. go
  779.  
  780. insert into spt_server_info
  781.     values (100,'USERID_LENGTH','30')
  782. insert into spt_server_info
  783.     values (101,'QUALIFIER_TERM','database')
  784. insert into spt_server_info
  785.     values (102,'NAMED_TRANSACTIONS','Y')
  786. insert into spt_server_info
  787.     values (103,'SPROC_AS_LANGUAGE','Y')
  788. insert into spt_server_info
  789.     values (104,'ACCESSIBLE_SPROC','Y')
  790. insert into spt_server_info
  791.     values (105,'MAX_INDEX_COLS','16')
  792. insert into spt_server_info
  793.     values (106,'RENAME_TABLE','Y')
  794. insert into spt_server_info
  795.     values (107,'RENAME_COLUMN','Y')
  796. insert into spt_server_info
  797.     values (108,'DROP_COLUMN','N')
  798. insert into spt_server_info
  799.     values (109,'INCREASE_COLUMN_LENGTH','N')
  800. if (charindex('6.50', @@version) = 0 and
  801.     charindex('7.00', @@version) = 0)
  802. begin
  803.     insert into spt_server_info
  804.         values (110,'DDL_IN_TRANSACTION','N')
  805. end
  806. else
  807. begin
  808.     insert into spt_server_info
  809.         values (110,'DDL_IN_TRANSACTION','Y')
  810. end
  811. insert into spt_server_info
  812.     values (111,'DESCENDING_INDEXES','N')
  813. insert into spt_server_info
  814.     values (112,'SP_RENAME','Y')
  815. insert into spt_server_info
  816.     values (113,'REMOTE_SPROC','Y')
  817. insert into spt_server_info
  818.     values (500,'SYS_SPROC_VERSION','7.00.621')
  819. go
  820.  
  821. if (charindex('7.00', @@version) > 0)
  822. begin    /*    Update values for 7.00 server */
  823.     update spt_server_info set attribute_value = '128'
  824.         where attribute_id in (12,13,14,15,100)
  825. end
  826. go
  827.  
  828. grant select on spt_server_info to public
  829. go
  830.  
  831. print 'creating sp_column_privileges'
  832. go
  833.  
  834. /*    Procedure for pre 6.50 server */
  835. CREATE PROCEDURE sp_column_privileges (
  836.             @table_name         varchar(32),
  837.             @table_owner        varchar(32) = null,
  838.             @table_qualifier    varchar(32) = null,
  839.             @column_name        varchar(96) = null)    /* 3*32 */
  840. as
  841.  
  842.     set nocount on
  843.     declare @table_id     int
  844.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  845.     declare @low smallint                     /* range of userids to check */
  846.     declare @high smallint
  847.     declare @owner_uid smallint
  848.  
  849.     select @low = 0, @high = 32767
  850.  
  851.     if @column_name is null /*    If column name not supplied, match all */
  852.         select @column_name = '%'
  853.  
  854.     if @table_qualifier is not null
  855.     begin
  856.         if db_name() <> @table_qualifier
  857.         begin    /* If qualifier doesn't match current database */
  858.             raiserror 20001 '~~Rush_5~~'
  859.             return
  860.         end
  861.     end
  862.     if @table_owner is null
  863.     begin    /* If unqualified table name */
  864.         SELECT @full_table_name = @table_name
  865.     end
  866.     else
  867.     begin    /* Qualified table name */
  868.         SELECT @full_table_name = @table_owner + '.' + @table_name
  869.     end
  870.     /*    Get Object ID */
  871.     select @table_id = object_id(@full_table_name)
  872.  
  873.     if (@@trancount <> 0)
  874.     begin    /* If inside a transaction */
  875.         raiserror 20003 '~~Rush_6~~'
  876.         return
  877.     end
  878.  
  879.     /*
  880.     ** We need to create a table which will contain a row for every row to
  881.     ** be returned to the client.
  882.     */
  883.  
  884.     create table #column_priv1(
  885.         COLUMN_NAME             varchar(32) NOT NULL,
  886.         grantor                 smallint NOT NULL,
  887.         grantee                 smallint NOT NULL,
  888.         select_privilege        bit,
  889.         select_grantable        bit,
  890.         insert_privilege        bit,
  891.         insert_grantable        bit,
  892.         update_privilege        bit,
  893.         update_grantable        bit,
  894.         references_privilege    bit,
  895.         references_grantable    bit,
  896.         uid                     smallint NOT NULL,
  897.         gid                     smallint NOT NULL)
  898.  
  899. /*
  900. ** insert a row for the table owner (who has all permissions)
  901. */
  902.     select @owner_uid = (
  903.         select uid
  904.         from sysobjects
  905.         where id = @table_id)
  906.  
  907.     if (charindex('6.00', @@version) > 0)
  908.     begin
  909.         insert into #column_priv1
  910.             select
  911.                 c.name,
  912.                 u.uid,
  913.                 @owner_uid,
  914.                 0,
  915.                 1,
  916.                 0,
  917.                 1,
  918.                 0,
  919.                 1,
  920.                 0,
  921.                 1,
  922.                 @owner_uid,
  923.                 0
  924.             from syscolumns c, sysusers u
  925.             where id = @table_id
  926.                 and c.number = 0
  927.                 and u.suid = 1        /* grantor is dbo of database */
  928.     end
  929.     else
  930.     begin
  931.         insert into #column_priv1
  932.             select
  933.                 c.name,
  934.                 u.uid,
  935.                 @owner_uid,
  936.                 0,
  937.                 1,
  938.                 0,
  939.                 1,
  940.                 0,
  941.                 1,
  942.                 0,
  943.                 0,
  944.                 @owner_uid,
  945.                 0
  946.             from syscolumns c, sysusers u
  947.             where id = @table_id
  948.                 and c.number = 0
  949.                 and u.suid = 1        /* grantor is dbo of database */
  950.     end
  951. /*
  952. ** now stick in a row for every column for every user in the database
  953. ** we will need to weed out those who have no permissions later
  954. ** (and yes this is a cartesion product: the uid field in sysprotects
  955. ** can also have a group id, in which case we need to extend those
  956. ** privileges to all group members).
  957. */
  958.  
  959.     insert into #column_priv1
  960.         select distinct
  961.             c.name,
  962.             o.uid,
  963.             u.uid,
  964.             0,
  965.             0,
  966.             0,
  967.             0,
  968.             0,
  969.             0,
  970.             0,
  971.             0,
  972.             u.uid,
  973.             u.gid
  974.         from sysusers u, syscolumns c, sysobjects o
  975.         where o.id = @table_id
  976.             and c.id = o.id
  977.             and c.number = 0
  978.             and u.gid <> u.uid
  979.             and u.uid <> @owner_uid
  980.  
  981.     /*
  982.     ** we need to create another temporary table to contain all the various
  983.     ** protection information for the table in question
  984.     */
  985.     create table #protects (
  986.                 uid         smallint NOT NULL,
  987.                 grantor        smallint NOT NULL,
  988.                 action        tinyint NOT NULL,
  989.                 protecttype tinyint NOT NULL,
  990.                 name        varchar(32) NOT NULL)
  991.  
  992.     insert into #protects
  993.         select
  994.             p.uid,
  995.             p.uid,
  996.             p.action,
  997.             p.protecttype,
  998.             isnull(col_name(id, c.number), '~All')
  999.             from
  1000.                 sysprotects p,
  1001.                 master.dbo.spt_values c,
  1002.                 master.dbo.spt_values a,
  1003.                 master.dbo.spt_values b
  1004.             where
  1005.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1006.                     & c.high <> 0
  1007.                     and c.number <= (
  1008.                         select count(*)
  1009.                         from syscolumns
  1010.                         where id = @table_id)
  1011.                 and c.type = 'P'
  1012.                 and a.type = 'T'
  1013.                 and a.number = p.action
  1014.                 and p.action in (193,195,197,26)
  1015.                 and b.type = 'T'
  1016.                 and b.number = p.protecttype
  1017.                 and p.id = @table_id
  1018.                 and p.uid between @low and @high
  1019.  
  1020.  
  1021.     update #column_priv1
  1022.     set select_privilege = 1
  1023.         from #protects p
  1024.     where
  1025.         p.protecttype = 205
  1026.         and p.action = 193
  1027.         and (p.name = #column_priv1.COLUMN_NAME
  1028.             or name = '~All')
  1029.         and (p.uid = 0
  1030.             or p.uid = #column_priv1.gid
  1031.             or p.uid = #column_priv1.uid)
  1032.         and not exists (
  1033.             select * from #protects
  1034.             where
  1035.                 protecttype = 206
  1036.                 and action = 193
  1037.                 and (name = #column_priv1.COLUMN_NAME
  1038.                     or name = '~All')
  1039.                 and ( uid = 0
  1040.                     or uid = #column_priv1.gid
  1041.                     or uid = #column_priv1.uid))
  1042.  
  1043.     update #column_priv1
  1044.     set insert_privilege = 1
  1045.         from #protects p
  1046.     where
  1047.         p.protecttype = 205
  1048.         and p.action = 195
  1049.         and (p.name = #column_priv1.COLUMN_NAME
  1050.             or name = '~All')
  1051.         and (p.uid = 0
  1052.             or p.uid = #column_priv1.gid
  1053.             or p.uid = #column_priv1.uid)
  1054.         and not exists (
  1055.             select * from #protects
  1056.             where
  1057.                 protecttype = 206
  1058.                 and action = 195
  1059.                 and (name = #column_priv1.COLUMN_NAME
  1060.                        or name = '~All')
  1061.                 and (uid = 0
  1062.                     or uid = #column_priv1.gid
  1063.                     or uid = #column_priv1.uid))
  1064.  
  1065.     update #column_priv1
  1066.     set update_privilege = 1
  1067.         from #protects p
  1068.     where
  1069.         p.protecttype = 205
  1070.         and p.action = 197
  1071.         and (p.name = #column_priv1.COLUMN_NAME
  1072.             or name = '~All')
  1073.         and (p.uid = 0
  1074.             or p.uid = #column_priv1.gid
  1075.             or p.uid = #column_priv1.uid)
  1076.         and not exists (
  1077.             select * from #protects
  1078.                 where protecttype = 206
  1079.                 and action = 197
  1080.                 and (name = #column_priv1.COLUMN_NAME
  1081.                     or name = '~All')
  1082.                 and (uid = 0
  1083.                     or uid = #column_priv1.gid
  1084.                     or uid = #column_priv1.uid))
  1085.  
  1086.     update #column_priv1
  1087.     set references_privilege = 1
  1088.         from #protects p
  1089.     where
  1090.         p.protecttype = 205
  1091.         and p.action = 26
  1092.         and (p.name = #column_priv1.COLUMN_NAME
  1093.             or name = '~All')
  1094.         and (p.uid = 0
  1095.             or p.uid = #column_priv1.gid
  1096.             or p.uid = #column_priv1.uid)
  1097.         and not exists (
  1098.             select * from #protects
  1099.                 where protecttype = 206
  1100.                 and action = 26
  1101.                 and (name = #column_priv1.COLUMN_NAME
  1102.                     or name = '~All')
  1103.                 and (uid = 0
  1104.                     or uid = #column_priv1.gid
  1105.                     or uid = #column_priv1.uid))
  1106.  
  1107.     update #column_priv1
  1108.     set select_grantable = 1
  1109.         from #protects p
  1110.     where
  1111.         p.protecttype = 204
  1112.         and p.action = 193
  1113.         and (p.name = #column_priv1.COLUMN_NAME
  1114.             or name = '~All')
  1115.         and (p.uid = 0
  1116.             or p.uid = #column_priv1.gid
  1117.             or p.uid = #column_priv1.uid)
  1118.         and not exists (
  1119.             select * from #protects
  1120.             where
  1121.                 protecttype = 206
  1122.                 and action = 193
  1123.                 and (name = #column_priv1.COLUMN_NAME
  1124.                     or name = '~All')
  1125.                 and ( uid = 0
  1126.                     or uid = #column_priv1.gid
  1127.                     or uid = #column_priv1.uid))
  1128.  
  1129.     update #column_priv1
  1130.     set insert_grantable = 1
  1131.         from #protects p
  1132.     where
  1133.         p.protecttype = 204
  1134.         and p.action = 195
  1135.         and (p.name = #column_priv1.COLUMN_NAME
  1136.             or name = '~All')
  1137.         and (p.uid = 0
  1138.             or p.uid = #column_priv1.gid
  1139.             or p.uid = #column_priv1.uid)
  1140.         and not exists (
  1141.             select * from #protects
  1142.             where
  1143.                 protecttype = 206
  1144.                 and action = 195
  1145.                 and (name = #column_priv1.COLUMN_NAME
  1146.                     or name = '~All')
  1147.                 and ( uid = 0
  1148.                     or uid = #column_priv1.gid
  1149.                     or uid = #column_priv1.uid))
  1150.  
  1151.     update #column_priv1
  1152.     set update_grantable = 1
  1153.         from #protects p
  1154.     where
  1155.         p.protecttype = 204
  1156.         and p.action = 197
  1157.         and (p.name = #column_priv1.COLUMN_NAME
  1158.             or name = '~All')
  1159.         and (p.uid = 0
  1160.             or p.uid = #column_priv1.gid
  1161.             or p.uid = #column_priv1.uid)
  1162.         and not exists (
  1163.             select * from #protects
  1164.             where
  1165.                 protecttype = 206
  1166.                 and action = 197
  1167.                 and (name = #column_priv1.COLUMN_NAME
  1168.                     or name = '~All')
  1169.                 and ( uid = 0
  1170.                     or uid = #column_priv1.gid
  1171.                     or uid = #column_priv1.uid))
  1172.  
  1173.     update #column_priv1
  1174.     set references_grantable = 1
  1175.         from #protects p
  1176.     where
  1177.         p.protecttype = 204
  1178.         and p.action = 26
  1179.         and (p.name = #column_priv1.COLUMN_NAME
  1180.             or name = '~All')
  1181.         and (p.uid = 0
  1182.             or p.uid = #column_priv1.gid
  1183.             or p.uid = #column_priv1.uid)
  1184.         and not exists (
  1185.             select * from #protects
  1186.             where
  1187.                 protecttype = 206
  1188.                 and action = 26
  1189.                 and (name = #column_priv1.COLUMN_NAME
  1190.                     or name = '~All')
  1191.                 and ( uid = 0
  1192.                     or uid = #column_priv1.gid
  1193.                     or uid = #column_priv1.uid))
  1194.  
  1195.     create table #column_priv2(
  1196.         COLUMN_NAME     varchar(32) NOT NULL,
  1197.         grantor         smallint NULL,
  1198.         grantee         smallint NOT NULL,
  1199.         PRIVILEGE        varchar(32) NOT NULL,
  1200.         IS_GRANTABLE    varchar(3) NULL)
  1201.  
  1202.     insert into #column_priv2
  1203.         select
  1204.             COLUMN_NAME,
  1205.             grantor,
  1206.             grantee,
  1207.             'SELECT',
  1208.             'NO'
  1209.         from #column_priv1
  1210.         where select_privilege = 1 and select_grantable    = 0
  1211.  
  1212.     insert into #column_priv2
  1213.         select
  1214.             COLUMN_NAME,
  1215.             grantor,
  1216.             grantee,
  1217.             'INSERT',
  1218.             'NO'
  1219.         from #column_priv1
  1220.         where insert_privilege = 1 and insert_grantable = 0
  1221.  
  1222.     insert into #column_priv2
  1223.         select
  1224.             COLUMN_NAME,
  1225.             grantor,
  1226.             grantee,
  1227.             'UPDATE',
  1228.             'NO'
  1229.         from #column_priv1
  1230.         where update_privilege = 1 and update_grantable = 0
  1231.  
  1232.     insert into #column_priv2
  1233.         select
  1234.             COLUMN_NAME,
  1235.             grantor,
  1236.             grantee,
  1237.             'REFERENCES',
  1238.             'NO'
  1239.         from #column_priv1
  1240.         where references_privilege = 1 and references_grantable = 0
  1241.  
  1242.     insert into #column_priv2
  1243.         select
  1244.             COLUMN_NAME,
  1245.             grantor,
  1246.             grantee,
  1247.             'SELECT',
  1248.             'YES'
  1249.         from #column_priv1
  1250.         where select_grantable = 1
  1251.  
  1252.     insert into #column_priv2
  1253.         select
  1254.             COLUMN_NAME,
  1255.             grantor,
  1256.             grantee,
  1257.             'INSERT',
  1258.             'YES'
  1259.         from #column_priv1
  1260.         where insert_grantable = 1
  1261.  
  1262.     insert into #column_priv2
  1263.         select
  1264.             COLUMN_NAME,
  1265.             grantor,
  1266.             grantee,
  1267.             'UPDATE',
  1268.             'YES'
  1269.         from #column_priv1
  1270.         where update_grantable = 1
  1271.  
  1272.     insert into #column_priv2
  1273.         select
  1274.             COLUMN_NAME,
  1275.             grantor,
  1276.             grantee,
  1277.             'REFERENCES',
  1278.             'YES'
  1279.         from #column_priv1
  1280.         where references_grantable = 1
  1281.  
  1282.     select
  1283.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1284.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1285.         @table_name TABLE_NAME,
  1286.         COLUMN_NAME,
  1287.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1288.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1289.         PRIVILEGE,
  1290.         IS_GRANTABLE
  1291.     from #column_priv2
  1292.     where COLUMN_NAME like @column_name
  1293.     order by 4, 7
  1294. go
  1295.  
  1296. if (charindex('6.50', @@version) = 0 and
  1297.     charindex('7.00', @@version) = 0)
  1298. begin
  1299.     print ''
  1300.     print ''
  1301.     print 'Warning:'
  1302.     print 'you are installing the stored procedures '
  1303.     print 'on a pre 6.50 SQL Server.'
  1304.     print 'Ignore the following errors.'
  1305. end
  1306. else
  1307.     drop proc sp_column_privileges
  1308. go
  1309.  
  1310. /*    Procedure for 6.50 server */
  1311. CREATE PROCEDURE sp_column_privileges (
  1312.             @table_name         varchar(32),
  1313.             @table_owner        varchar(32) = null,
  1314.             @table_qualifier    varchar(32) = null,
  1315.             @column_name        varchar(96) = null)    /* 3*32 */
  1316. as
  1317.  
  1318.     declare @table_id     int
  1319.  
  1320.     if @column_name is null /*    If column name not supplied, match all */
  1321.         select @column_name = '%'
  1322.  
  1323.     if @table_qualifier is not null
  1324.     begin
  1325.         if db_name() <> @table_qualifier
  1326.         begin    /* If qualifier doesn't match current database */
  1327.             raiserror (15250, -1,-1)
  1328.             return
  1329.         end
  1330.     end
  1331.     if @table_owner is null
  1332.     begin    /* If unqualified table name */
  1333.         select @table_id = object_id(@table_name)
  1334.     end
  1335.     else
  1336.     begin    /* Qualified table name */
  1337.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1338.     end
  1339.  
  1340.     select
  1341.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1342.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1343.         @table_name TABLE_NAME,
  1344.         convert(varchar(32),c.name) COLUMN_NAME,
  1345.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1346.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1347.         convert (varchar(32),case p.action
  1348.              when 193 then 'SELECT'
  1349.              when 195 then 'INSERT'
  1350.              when 197 then 'UPDATE'
  1351.              else 'REFERENCES'
  1352.         end) PRIVILEGE,
  1353.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1354.             else 'YES'
  1355.         end) IS_GRANTABLE
  1356.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1357.     where
  1358.         c.id = @table_id
  1359.         and c.name like @column_name
  1360.         and c.id = p.id
  1361.         and c.id = o.id
  1362.         and case substring(p.columns, 1, 1) & 1
  1363.                 when NULL then 255    /* all columns have permission */
  1364.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1365.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1366.             end
  1367.             & v.high <> 0            /* permission applies to this column */
  1368.         and v.number <= (select count(*) from syscolumns
  1369.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1370.         and v.type = 'P'
  1371.         and v.number = c.colid
  1372.             /* expand groups */
  1373.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1374.              (p.uid = u.gid and u.uid <> u.gid))
  1375.         and p.protecttype <> 206    /* only grant rows */
  1376.         and p.action in (26,193,195,197)
  1377.         and o.uid <> u.uid            /* no rows for owner */
  1378.         and not exists (            /* exclude revoke'd privileges */
  1379.             select *
  1380.             from sysprotects p1
  1381.             where
  1382.                 p1.protecttype = 206
  1383.                 and p1.action = p.action
  1384.                 and p1.id = p.id
  1385.                 and p1.uid = u.uid
  1386.                 and case substring(p1.columns, 1, 1) & 1
  1387.                         when NULL then 255    /* all columns have permission */
  1388.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1389.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1390.                     end
  1391.                     & v.high <> 0)            /* permission applies to this column */
  1392.     union all
  1393.     select    /*    Add rows for table owner */
  1394.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1395.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1396.         @table_name TABLE_NAME,
  1397.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1398.         convert(varchar(32),user_name(u.uid)) grantor,
  1399.         convert(varchar(32),user_name(o.uid)) grantee,
  1400.         convert (varchar(32),case v.number
  1401.             when 193 then 'SELECT'
  1402.             when 195 then 'INSERT'
  1403.             when 197 then 'UPDATE'
  1404.             else 'REFERENCES'
  1405.         end) PRIVILEGE,
  1406.         convert(varchar(3),'YES') IS_GRANTABLE
  1407.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1408.     where
  1409.         c.id = @table_id
  1410.         and c.name like @column_name
  1411.         and c.id = o.id
  1412.         and u.suid = 1        /* grantor is dbo of database */
  1413.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1414.         and v.number in (26,193,195,197)
  1415.         and not exists (    /* exclude revoke'd privileges */
  1416.             select *
  1417.             from sysprotects p1
  1418.             where
  1419.                 p1.protecttype = 206
  1420.                 and p1.action = v.number
  1421.                 and p1.id = o.id
  1422.                 and p1.uid = o.uid)
  1423.     order by 4, 7
  1424. go
  1425.  
  1426.  
  1427.  
  1428. if (charindex('7.00', @@version) = 0)
  1429. begin
  1430.     print ''
  1431.     print ''
  1432.     print 'Warning:'
  1433.     print 'you are installing the stored procedures '
  1434.     print 'on a pre 7.0 SQL Server.'
  1435.     print 'Ignore the following errors.'
  1436. end
  1437. else
  1438.     drop proc sp_column_privileges
  1439. go
  1440.  
  1441. /*    Procedure for 7.00 server */
  1442. CREATE PROCEDURE sp_column_privileges (
  1443.             @table_name         sysname,
  1444.             @table_owner        sysname = null,
  1445.             @table_qualifier    sysname = null,
  1446.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1447. as
  1448.  
  1449.     declare @table_id     int
  1450.  
  1451.     if @column_name is null /*    If column name not supplied, match all */
  1452.         select @column_name = '%'
  1453.  
  1454.     if @table_qualifier is not null
  1455.     begin
  1456.         if db_name() <> @table_qualifier
  1457.         begin    /* If qualifier doesn't match current database */
  1458.             raiserror (15250, -1,-1)
  1459.             return
  1460.         end
  1461.     end
  1462.     if @table_owner is null
  1463.     begin    /* If unqualified table name */
  1464.         select @table_id = object_id(quotename(@table_name))
  1465.     end
  1466.     else
  1467.     begin    /* Qualified table name */
  1468.         if @table_owner = N''
  1469.         begin    /* If empty owner name */
  1470.             select @table_id = 0
  1471.         end
  1472.         else
  1473.         begin
  1474.             select @table_id = object_id(quotename(@table_owner) +
  1475.                 '.' + quotename(@table_name))
  1476.         end
  1477.     end
  1478.  
  1479.     select
  1480.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1481.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1482.         @table_name TABLE_NAME,
  1483.         convert(sysname,c.name) COLUMN_NAME,
  1484.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1485.         convert(sysname,user_name(u.uid)) GRANTEE,
  1486.         convert (varchar(32),case p.action
  1487.              when 193 then 'SELECT'
  1488.              when 195 then 'INSERT'
  1489.              when 197 then 'UPDATE'
  1490.              else 'REFERENCES'
  1491.         end) PRIVILEGE,
  1492.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1493.             else 'YES'
  1494.         end) IS_GRANTABLE
  1495.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1496.     where
  1497.         c.id = @table_id
  1498.         and c.name like @column_name
  1499.         and c.id = p.id
  1500.         and c.id = o.id
  1501.         and case substring(p.columns, 1, 1) & 1
  1502.                 when NULL then 255    /* all columns have permission */
  1503.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1504.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1505.             end
  1506.             & v.high <> 0            /* permission applies to this column */
  1507.         and v.number <= (select count(*) from syscolumns
  1508.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1509.         and v.type = 'P'
  1510.         and v.number = c.colid
  1511.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1512.         and (u.uid > 0 and u.uid < 16384)
  1513.         and ((p.uid = u.uid) or 
  1514.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1515.         and p.protecttype <> 206    /* only grant rows */
  1516.         and p.action in (26,193,195,197)
  1517.         and o.uid <> u.uid            /* no rows for owner */
  1518.         and not exists (            /* exclude revoke'd privileges */
  1519.             select *
  1520.             from sysprotects p1
  1521.             where
  1522.                 p1.protecttype = 206
  1523.                 and p1.action = p.action
  1524.                 and p1.id = p.id
  1525.                 and p1.uid = u.uid
  1526.                 and case substring(p1.columns, 1, 1) & 1
  1527.                         when NULL then 255    /* all columns have permission */
  1528.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1529.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1530.                     end
  1531.                     & v.high <> 0)            /* permission applies to this column */
  1532.     union all
  1533.     select    /*    Add rows for table owner */
  1534.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1535.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1536.         @table_name TABLE_NAME,
  1537.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1538.         convert(sysname,user_name(u.uid)) grantor,
  1539.         convert(sysname,user_name(o.uid)) grantee,
  1540.         convert (varchar(32),case v.number
  1541.             when 193 then 'SELECT'
  1542.             when 195 then 'INSERT'
  1543.             when 197 then 'UPDATE'
  1544.             else 'REFERENCES'
  1545.         end) PRIVILEGE,
  1546.         convert(varchar(3),'YES') IS_GRANTABLE
  1547.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1548.     where
  1549.         c.id = @table_id
  1550.         and c.name like @column_name
  1551.         and c.id = o.id
  1552.         and u.uid = 1        /* grantor is 'dbo' of database */
  1553.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1554.         and v.number in (26,193,195,197)
  1555.         and not exists (    /* exclude revoke'd privileges */
  1556.             select *
  1557.             from sysprotects p1
  1558.             where
  1559.                 p1.protecttype = 206
  1560.                 and p1.action = v.number
  1561.                 and p1.id = o.id
  1562.                 and p1.uid = o.uid)
  1563.     order by 4, 7
  1564. go
  1565.  
  1566.  
  1567. grant execute on sp_column_privileges to public
  1568. go
  1569.  
  1570. dump tran master with no_log
  1571. go
  1572.  
  1573. print 'creating sp_columns'
  1574. go
  1575.  
  1576. /*    Procedure for pre-6.0 server */
  1577. CREATE PROCEDURE sp_columns (
  1578.                  @table_name        varchar(96),
  1579.                  @table_owner        varchar(96) = null,
  1580.                  @table_qualifier    varchar(32) = null,
  1581.                  @column_name        varchar(96) = null,
  1582.                  @ODBCVer            int = 2)
  1583. AS
  1584.     DECLARE @full_table_name    varchar(193)
  1585.     DECLARE @table_id int
  1586.  
  1587.     if @ODBCVer <> 3
  1588.         select @ODBCVer = 2
  1589.     if @column_name is null /*    If column name not supplied, match all */
  1590.         select @column_name = '%'
  1591.     if @table_qualifier is not null
  1592.     begin
  1593.         if db_name() <> @table_qualifier
  1594.         begin    /* If qualifier doesn't match current database */
  1595.             raiserror 20001 '~~Rush_5~~'
  1596.             return
  1597.         end
  1598.     end
  1599.     if @table_name is null
  1600.     begin    /*    If table name not supplied, match all */
  1601.         select @table_name = '%'
  1602.     end
  1603.     if @table_owner is null
  1604.     begin    /* If unqualified table name */
  1605.         SELECT @full_table_name = @table_name
  1606.     end
  1607.     else
  1608.     begin    /* Qualified table name */
  1609.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1610.     end
  1611.  
  1612.     /*    Get Object ID */
  1613.     SELECT @table_id = object_id(@full_table_name)
  1614.     if ((charindex('%',@full_table_name) = 0) and
  1615.         (charindex('_',@full_table_name) = 0) and
  1616.         @table_id <> 0)
  1617.     begin
  1618.         /* this block is for the case where there is no pattern
  1619.              matching required for the table name */
  1620.         SELECT
  1621.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1622.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1623.             TABLE_NAME = convert(varchar(32),o.name),
  1624.             COLUMN_NAME = convert(varchar(32),c.name),
  1625.             d.DATA_TYPE,
  1626.             TYPE_NAME = t.name,
  1627.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1628.             LENGTH = isnull(d.length, convert(int,c.length)),
  1629.             SCALE = d.numeric_scale,
  1630.             d.RADIX,
  1631.             NULLABLE =    /* set nullability from status flag */
  1632.                 convert(smallint, convert(bit, c.status&8)),
  1633.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1634.             COLUMN_DEF = text,
  1635.             d.SQL_DATA_TYPE,
  1636.             d.SQL_DATETIME_SUB,
  1637.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1638.             ORDINAL_POSITION = convert(int,c.colid),
  1639.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1640.             SS_DATA_TYPE = c.type
  1641.         FROM
  1642.             syscolumns c,
  1643.             sysobjects o,
  1644.             syscomments m,
  1645.             master.dbo.spt_datatype_info d,
  1646.             systypes t
  1647.         WHERE
  1648.             o.id = @table_id
  1649.             AND c.id = o.id
  1650.             AND t.type = d.ss_dtype
  1651.             AND c.length = isnull(d.fixlen, c.length)
  1652.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1653.             AND o.type <> 'P'
  1654.             AND c.usertype = t.usertype
  1655.             AND c.name like @column_name
  1656.             AND c.cdefault *= m.id
  1657.             AND m.colid = 1
  1658.         ORDER BY 17
  1659.     end
  1660.     else
  1661.     begin
  1662.         /* this block is for the case where there IS pattern
  1663.              matching done on the table name */
  1664.         if @table_owner is null /*    If owner not supplied, match all */
  1665.             select @table_owner = '%'
  1666.         SELECT
  1667.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1668.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1669.             TABLE_NAME = convert(varchar(32),o.name),
  1670.             COLUMN_NAME = convert(varchar(32),c.name),
  1671.             d.DATA_TYPE,
  1672.             TYPE_NAME = t.name,
  1673.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1674.             LENGTH = isnull(d.length, convert(int,c.length)),
  1675.             SCALE = d.numeric_scale,
  1676.             d.RADIX,
  1677.             NULLABLE =    /* set nullability from status flag */
  1678.                 convert(smallint, convert(bit, c.status&8)),
  1679.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1680.             COLUMN_DEF = text,
  1681.             d.SQL_DATA_TYPE,
  1682.             d.SQL_DATETIME_SUB,
  1683.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1684.             ORDINAL_POSITION = convert(int,c.colid),
  1685.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1686.             SS_DATA_TYPE = c.type
  1687.         FROM
  1688.             syscolumns c,
  1689.             sysobjects o,
  1690.             syscomments m,
  1691.             master.dbo.spt_datatype_info d,
  1692.             systypes t
  1693.         WHERE
  1694.             o.name like @table_name
  1695.             AND user_name(o.uid) like @table_owner
  1696.             AND o.id = c.id
  1697.             AND t.type = d.ss_dtype
  1698.             AND c.length = isnull(d.fixlen, c.length)
  1699.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1700.             AND o.type <> 'P'
  1701.             AND c.usertype = t.usertype
  1702.             AND c.name like @column_name
  1703.             AND c.cdefault *= m.id
  1704.             AND m.colid = 1
  1705.         ORDER BY 2, 3, 17
  1706.     end
  1707. go
  1708.  
  1709. if (charindex('6.00', @@version) = 0 and
  1710.     charindex('6.50', @@version) = 0 and
  1711.     charindex('7.00', @@version) = 0)
  1712. begin
  1713.     print ''
  1714.     print ''
  1715.     print 'Warning:'
  1716.     print 'you are installing the stored procedures '
  1717.     print 'on a pre 6.0 SQL Server.'
  1718.     print 'Ignore the following error.'
  1719. end
  1720. else
  1721.     drop proc sp_columns
  1722. go
  1723.  
  1724. /*    Procedure for 6.0 and 6.50 server */
  1725. CREATE PROCEDURE sp_columns (
  1726.                  @table_name        varchar(96),
  1727.                  @table_owner        varchar(96) = null,
  1728.                  @table_qualifier    varchar(32) = null,
  1729.                  @column_name        varchar(96) = null,
  1730.                  @ODBCVer            int = 2)
  1731. AS
  1732.     DECLARE @full_table_name    varchar(193)
  1733.     DECLARE @table_id int
  1734.  
  1735.     if @ODBCVer <> 3
  1736.         select @ODBCVer = 2
  1737.     if @column_name is null /*    If column name not supplied, match all */
  1738.         select @column_name = '%'
  1739.     if @table_qualifier is not null
  1740.     begin
  1741.         if db_name() <> @table_qualifier
  1742.         begin    /* If qualifier doesn't match current database */
  1743.             raiserror (15250, -1,-1)
  1744.             return
  1745.         end
  1746.     end
  1747.     if @table_name is null
  1748.     begin    /*    If table name not supplied, match all */
  1749.         select @table_name = '%'
  1750.     end
  1751.     if @table_owner is null
  1752.     begin    /* If unqualified table name */
  1753.         SELECT @full_table_name = @table_name
  1754.     end
  1755.     else
  1756.     begin    /* Qualified table name */
  1757.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1758.     end
  1759.  
  1760.     /*    Get Object ID */
  1761.     SELECT @table_id = object_id(@full_table_name)
  1762.     if ((charindex('%',@full_table_name) = 0) and
  1763.         (charindex('[',@full_table_name) = 0) and
  1764.         (charindex('_',@full_table_name) = 0) and
  1765.         @table_id <> 0)
  1766.     begin
  1767.         /* this block is for the case where there is no pattern
  1768.             matching required for the table name */
  1769.         SELECT
  1770.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1771.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1772.             TABLE_NAME = convert(varchar(32),o.name),
  1773.             COLUMN_NAME = convert(varchar(32),c.name),
  1774.             d.DATA_TYPE,
  1775.             convert(varchar(32),case
  1776.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1777.                 else d.TYPE_NAME
  1778.             end) TYPE_NAME,
  1779.             convert(int,case
  1780.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1781.                 else isnull(convert(int,c.prec), 2147483647)
  1782.             end) "PRECISION",
  1783.             convert(int,case
  1784.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1785.                     c.prec+2
  1786.                 else
  1787.                     isnull(d.length, c.length)
  1788.             end) LENGTH,
  1789.             SCALE = convert(smallint, c.scale),
  1790.             d.RADIX,
  1791.             NULLABLE =    /* set nullability from status flag */
  1792.                 convert(smallint, convert(bit, c.status&8)),
  1793.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1794.             COLUMN_DEF = text,
  1795.             d.SQL_DATA_TYPE,
  1796.             d.SQL_DATETIME_SUB,
  1797.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1798.             ORDINAL_POSITION = convert(int,c.colid),
  1799.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1800.             SS_DATA_TYPE = c.type
  1801.         FROM
  1802.             syscolumns c,
  1803.             sysobjects o,
  1804.             syscomments m,
  1805.             master.dbo.spt_datatype_info d,
  1806.             systypes t
  1807.         WHERE
  1808.             o.id = @table_id
  1809.             AND c.id = o.id
  1810.             AND t.type = d.ss_dtype
  1811.             AND c.length = isnull(d.fixlen, c.length)
  1812.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1813.             AND o.type <> 'P'
  1814.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1815.             AND c.usertype = t.usertype
  1816.             AND c.name like @column_name
  1817.             AND c.cdefault *= m.id
  1818.             AND m.colid = 1
  1819.         ORDER BY 17
  1820.     end
  1821.     else
  1822.     begin
  1823.         /* this block is for the case where there IS pattern
  1824.             matching done on the table name */
  1825.         if @table_owner is null /*    If owner not supplied, match all */
  1826.             select @table_owner = '%'
  1827.         SELECT
  1828.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1829.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1830.             TABLE_NAME = convert(varchar(32),o.name),
  1831.             COLUMN_NAME = convert(varchar(32),c.name),
  1832.             d.DATA_TYPE,
  1833.             convert(varchar(32),case
  1834.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1835.                 else d.TYPE_NAME
  1836.             end) TYPE_NAME,
  1837.             convert(int,case
  1838.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1839.                 else isnull(convert(int,c.prec), 2147483647)
  1840.             end) "PRECISION",
  1841.             convert(int,case
  1842.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1843.                     c.prec+2
  1844.                 else
  1845.                     isnull(d.length, c.length)
  1846.             end) LENGTH,
  1847.             SCALE = convert(smallint, c.scale),
  1848.             d.RADIX,
  1849.             NULLABLE =    /* set nullability from status flag */
  1850.                 convert(smallint, convert(bit, c.status&8)),
  1851.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1852.             COLUMN_DEF = text,
  1853.             d.SQL_DATA_TYPE,
  1854.             d.SQL_DATETIME_SUB,
  1855.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1856.             ORDINAL_POSITION = convert(int,c.colid),
  1857.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1858.             SS_DATA_TYPE = c.type
  1859.         FROM
  1860.             syscolumns c,
  1861.             sysobjects o,
  1862.             syscomments m,
  1863.             master.dbo.spt_datatype_info d,
  1864.             systypes t
  1865.         WHERE
  1866.             o.name like @table_name
  1867.             AND user_name(o.uid) like @table_owner
  1868.             AND o.id = c.id
  1869.             AND t.type = d.ss_dtype
  1870.             AND c.length = isnull(d.fixlen, c.length)
  1871.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1872.             AND o.type <> 'P'
  1873.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1874.             AND c.usertype = t.usertype
  1875.             AND c.name like @column_name
  1876.             AND c.cdefault *= m.id
  1877.             AND m.colid = 1
  1878.         ORDER BY 2, 3, 17
  1879.     end
  1880. go
  1881.  
  1882. if (charindex('7.00', @@version) = 0)
  1883. begin
  1884.     print ''
  1885.     print ''
  1886.     print 'Warning:'
  1887.     print 'you are installing the stored procedures '
  1888.     print 'on a pre 7.0 SQL Server.'
  1889.     print 'Ignore the following errors.'
  1890. end
  1891. else
  1892.     drop proc sp_columns
  1893. go
  1894.  
  1895. /*    Procedure for 7.0 server */
  1896. CREATE PROCEDURE sp_columns (
  1897.                  @table_name        nvarchar(384),
  1898.                  @table_owner        nvarchar(384) = null,
  1899.                  @table_qualifier    sysname = null,
  1900.                  @column_name        nvarchar(384) = null,
  1901.                  @ODBCVer            int = 2)
  1902. AS
  1903.     DECLARE @full_table_name    nvarchar(769)
  1904.     DECLARE @table_id int
  1905.  
  1906.     if @ODBCVer <> 3
  1907.         select @ODBCVer = 2
  1908.     if @column_name is null /*    If column name not supplied, match all */
  1909.         select @column_name = '%'
  1910.     if @table_qualifier is not null
  1911.     begin
  1912.         if db_name() <> @table_qualifier
  1913.         begin    /* If qualifier doesn't match current database */
  1914.             raiserror (15250, -1,-1)
  1915.             return
  1916.         end
  1917.     end
  1918.     if @table_name is null
  1919.     begin    /*    If table name not supplied, match all */
  1920.         select @table_name = '%'
  1921.     end
  1922.     if @table_owner is null
  1923.     begin    /* If unqualified table name */
  1924.         SELECT @full_table_name = quotename(@table_name)
  1925.     end
  1926.     else
  1927.     begin    /* Qualified table name */
  1928.         if @table_owner = ''
  1929.         begin    /* If empty owner name */
  1930.             SELECT @full_table_name = quotename(@table_owner)
  1931.         end
  1932.         else
  1933.         begin
  1934.             SELECT @full_table_name = quotename(@table_owner) +
  1935.                 '.' + quotename(@table_name)
  1936.         end
  1937.     end
  1938.  
  1939.     /*    Get Object ID */
  1940.     SELECT @table_id = object_id(@full_table_name)
  1941.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  1942.         (isnull(charindex('[', @table_name),0) = 0) and
  1943.         (isnull(charindex('[', @table_owner),0) = 0) and
  1944.         (isnull(charindex('_', @full_table_name),0) = 0) and
  1945.         @table_id <> 0)
  1946.     begin
  1947.         /* this block is for the case where there is no pattern
  1948.             matching required for the table name */
  1949.         SELECT
  1950.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  1951.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  1952.             TABLE_NAME = convert(sysname,o.name),
  1953.             COLUMN_NAME = convert(sysname,c.name),
  1954.             d.DATA_TYPE,
  1955.             convert (sysname,case
  1956.                 when t.xusertype > 255 then t.name
  1957.                 else d.TYPE_NAME
  1958.             end) TYPE_NAME,
  1959.             convert(int,case
  1960.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1961.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  1962.             end) "PRECISION",
  1963.             convert(int,case
  1964.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  1965.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  1966.                 else
  1967.                     isnull(d.length, c.length)
  1968.             end) LENGTH,
  1969.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  1970.             d.RADIX,
  1971.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  1972.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1973.             COLUMN_DEF = text,
  1974.             d.SQL_DATA_TYPE,
  1975.             d.SQL_DATETIME_SUB,
  1976.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  1977.             ORDINAL_POSITION = convert(int,c.colid),
  1978.             IS_NULLABLE = convert(varchar(254),
  1979.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  1980.             SS_DATA_TYPE = c.type
  1981.         FROM
  1982.             sysobjects o,
  1983.             master.dbo.spt_datatype_info d,
  1984.             systypes t,
  1985.             syscolumns c
  1986.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  1987.                 AND m.colid = 1
  1988.         WHERE
  1989.             o.id = @table_id
  1990.             AND c.id = o.id
  1991.             AND t.xtype = d.ss_dtype
  1992.             AND c.length = isnull(d.fixlen, c.length)
  1993.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1994.             AND o.type <> 'P'
  1995.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  1996.             AND c.xusertype = t.xusertype
  1997.             AND c.name like @column_name
  1998.         ORDER BY 17
  1999.     end
  2000.     else
  2001.     begin
  2002.         /* this block is for the case where there IS pattern
  2003.             matching done on the table name */
  2004.         if @table_owner is null /*    If owner not supplied, match all */
  2005.             select @table_owner = '%'
  2006.         SELECT
  2007.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2008.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2009.             TABLE_NAME = convert(sysname,o.name),
  2010.             COLUMN_NAME = convert(sysname,c.name),
  2011.             d.DATA_TYPE,
  2012.             convert (sysname,case
  2013.                 when t.xusertype > 255 then t.name
  2014.                 else d.TYPE_NAME
  2015.             end) TYPE_NAME,
  2016.             convert(int,case
  2017.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2018.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2019.             end) "PRECISION",
  2020.             convert(int,case
  2021.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2022.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2023.                 else
  2024.                     isnull(d.length, c.length)
  2025.             end) LENGTH,
  2026.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2027.             d.RADIX,
  2028.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2029.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2030.             COLUMN_DEF = text,
  2031.             d.SQL_DATA_TYPE,
  2032.             d.SQL_DATETIME_SUB,
  2033.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2034.             ORDINAL_POSITION = convert(int,c.colid),
  2035.             IS_NULLABLE = convert(varchar(254),
  2036.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2037.             SS_DATA_TYPE = c.type
  2038.         FROM
  2039.             sysobjects o,
  2040.             master.dbo.spt_datatype_info d,
  2041.             systypes t,
  2042.             syscolumns c
  2043.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2044.                 AND m.colid = 1
  2045.         WHERE
  2046.             o.name like @table_name
  2047.             AND user_name(o.uid) like @table_owner
  2048.             AND o.id = c.id
  2049.             AND t.xtype = d.ss_dtype
  2050.             AND c.length = isnull(d.fixlen, c.length)
  2051.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2052.             AND o.type <> 'P'
  2053.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2054.             AND c.xusertype = t.xusertype
  2055.             AND c.name like @column_name
  2056.         ORDER BY 2, 3, 17
  2057.     end
  2058. go
  2059.  
  2060. grant execute on sp_columns to public
  2061. go
  2062.  
  2063. dump tran master with no_log
  2064. go
  2065.  
  2066. print 'creating sp_databases'
  2067. go
  2068.  
  2069. /*    Procedure for pre-7.0 server */
  2070. create proc sp_databases
  2071. as
  2072.     set nocount on
  2073.     /* Use temporary table to sum up database size w/o using group by */
  2074.     create table #databases (
  2075.                   DATABASE_NAME varchar(32) NOT NULL,
  2076.                   size int NOT NULL)
  2077.  
  2078.     /* Insert row for each database */
  2079.     insert into #databases
  2080.         select
  2081.             name,
  2082.             (select sum(size) from master.dbo.sysusages
  2083.                 where dbid = d.dbid)
  2084.         from master.dbo.sysdatabases d
  2085.  
  2086.     select
  2087.          DATABASE_NAME,
  2088.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2089.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2090.     from #databases
  2091.     order by 1
  2092. go
  2093.  
  2094. if (charindex('7.00', @@version) = 0)
  2095. begin
  2096.     print ''
  2097.     print ''
  2098.     print 'Warning:'
  2099.     print 'you are installing the stored procedures '
  2100.     print 'on a pre 7.0 SQL Server.'
  2101.     print 'Ignore the following errors.'
  2102. end
  2103. else
  2104.     drop proc sp_databases
  2105. go
  2106.  
  2107. /*    Procedure for 7.0 server */
  2108. create proc sp_databases
  2109. as
  2110.     set nocount on
  2111.     declare @name sysname
  2112.     declare @SQL  nvarchar(600)
  2113.  
  2114.     /* Use temporary table to sum up database size w/o using group by */
  2115.     create table #databases (
  2116.                   DATABASE_NAME sysname NOT NULL,
  2117.                   size int NOT NULL)
  2118.  
  2119.     declare c1 cursor for 
  2120.         select name from master.dbo.sysdatabases
  2121.  
  2122.     open c1
  2123.     fetch c1 into @name
  2124.  
  2125.     while @@fetch_status >= 0
  2126.     begin
  2127.         select @SQL = 'insert into #databases
  2128.                 select N'''+ @name + ''', sum(size) from '
  2129.                 + QuoteName(@name) + '.dbo.sysfiles'
  2130.         /* Insert row for each database */
  2131.         execute (@SQL)
  2132.         fetch c1 into @name
  2133.     end
  2134.     deallocate c1
  2135.  
  2136.     select    
  2137.         DATABASE_NAME,
  2138.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2139.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2140.     from #databases
  2141.     order by 1
  2142. go
  2143.  
  2144. grant execute on sp_databases to public
  2145. go
  2146.  
  2147. dump tran master with no_log
  2148. go
  2149.  
  2150. print 'creating sp_datatype_info'
  2151. go
  2152.  
  2153. /*    Procedure for pre-6.0 server */
  2154. create proc sp_datatype_info
  2155.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2156. as
  2157.     if @ODBCVer <> 3
  2158.         select @ODBCVer = 2
  2159.     if @data_type = 0
  2160.         select
  2161.             TYPE_NAME = t.name,
  2162.             d.DATA_TYPE,
  2163.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2164.             d.LITERAL_PREFIX,
  2165.             d.LITERAL_SUFFIX,
  2166.             e.CREATE_PARAMS,
  2167.             d.NULLABLE,
  2168.             d.CASE_SENSITIVE,
  2169.             d.SEARCHABLE,
  2170.             d.UNSIGNED_ATTRIBUTE,
  2171.             d.MONEY,
  2172.             d.AUTO_INCREMENT,
  2173.             LOCAL_TYPE_NAME = t.name,
  2174.             MINIMUM_SCALE = d.numeric_scale,
  2175.             MAXIMUM_SCALE = d.numeric_scale,
  2176.             d.SQL_DATA_TYPE,
  2177.             d.SQL_DATETIME_SUB,
  2178.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2179.             INTERVAL_PRECISION = convert(smallint,NULL),
  2180.             USERTYPE = t.usertype
  2181.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2182.         where
  2183.             d.ss_dtype = t.type
  2184.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2185.             and t.usertype *= e.user_type
  2186.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2187.                 t.usertype > 100)
  2188.         order by 2, 12, 11, t.usertype
  2189.  
  2190.     else
  2191.         select
  2192.             TYPE_NAME = t.name,
  2193.             d.DATA_TYPE,
  2194.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2195.             d.LITERAL_PREFIX,
  2196.             d.LITERAL_SUFFIX,
  2197.             e.CREATE_PARAMS,
  2198.             d.NULLABLE,
  2199.             d.CASE_SENSITIVE,
  2200.             d.SEARCHABLE,
  2201.             d.UNSIGNED_ATTRIBUTE,
  2202.             d.MONEY,
  2203.             d.AUTO_INCREMENT,
  2204.             LOCAL_TYPE_NAME = t.name,
  2205.             MINIMUM_SCALE = d.numeric_scale,
  2206.             MAXIMUM_SCALE = d.numeric_scale,
  2207.             d.SQL_DATA_TYPE,
  2208.             d.SQL_DATETIME_SUB,
  2209.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2210.             INTERVAL_PRECISION = convert(smallint,NULL),
  2211.             USERTYPE = t.usertype
  2212.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2213.         where
  2214.             DATA_TYPE = @data_type
  2215.             and d.ss_dtype = t.type
  2216.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2217.             and t.usertype *= e.user_type
  2218.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2219.                 t.usertype > 100)
  2220.         order by 12, 11, t.usertype
  2221.  
  2222. go
  2223.  
  2224. if (charindex('6.00', @@version) = 0 and
  2225.     charindex('6.50', @@version) = 0 and
  2226.     charindex('7.00', @@version) = 0)
  2227. begin
  2228.     print ''
  2229.     print ''
  2230.     print 'Warning:'
  2231.     print 'you are installing the stored procedures '
  2232.     print 'on a pre 6.0 SQL Server.'
  2233.     print 'Ignore the following errors.'
  2234. end
  2235. else
  2236.     drop proc sp_datatype_info
  2237. go
  2238.  
  2239. /*    Procedure for 6.0 and 6.50 servers */
  2240. create proc sp_datatype_info
  2241.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2242. as
  2243.     if @ODBCVer <> 3
  2244.         select @ODBCVer = 2
  2245.     if @data_type = 0
  2246.         select
  2247.             convert(varchar(32),case
  2248.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2249.                 else d.TYPE_NAME
  2250.             end) TYPE_NAME,
  2251.             d.DATA_TYPE,
  2252.             convert(int,case
  2253.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2254.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2255.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  2256.                 else t.prec
  2257.             end) "PRECISION",
  2258.             d.LITERAL_PREFIX,
  2259.             d.LITERAL_SUFFIX,
  2260.             e.CREATE_PARAMS,
  2261.             convert(smallint,case
  2262.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2263.                 else t.allownulls
  2264.             end) NULLABLE,
  2265.             d.CASE_SENSITIVE,
  2266.             d.SEARCHABLE,
  2267.             d.UNSIGNED_ATTRIBUTE,
  2268.             d.MONEY,
  2269.             d.AUTO_INCREMENT,
  2270.             convert(varchar(32),case
  2271.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2272.                 else d.TYPE_NAME
  2273.             end) LOCAL_TYPE_NAME,
  2274.             MINIMUM_SCALE = d.numeric_scale,
  2275.             convert(smallint,case
  2276.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2277.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2278.                 else t.scale
  2279.             end) MAXIMUM_SCALE,
  2280.             d.SQL_DATA_TYPE,
  2281.             d.SQL_DATETIME_SUB,
  2282.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2283.             INTERVAL_PRECISION = convert(smallint,NULL),
  2284.             USERTYPE = t.usertype
  2285.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2286.         where
  2287.             d.ss_dtype = t.type
  2288.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2289.             and t.usertype *= e.user_type
  2290.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2291.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2292.                 t.usertype > 100)
  2293.             and (t.usertype <= 100 or
  2294.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2295.         order by 2, 12, 11, t.usertype
  2296.  
  2297.     else
  2298.         select
  2299.             convert(varchar(32),case
  2300.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2301.                 else d.TYPE_NAME
  2302.             end) TYPE_NAME,
  2303.             d.DATA_TYPE,
  2304.             convert(int,case
  2305.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2306.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2307.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  2308.                 else t.prec
  2309.             end) "PRECISION",
  2310.             d.LITERAL_PREFIX,
  2311.             d.LITERAL_SUFFIX,
  2312.             e.CREATE_PARAMS,
  2313.             convert(smallint,case
  2314.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2315.                 else t.allownulls
  2316.             end) NULLABLE,
  2317.             d.CASE_SENSITIVE,
  2318.             d.SEARCHABLE,
  2319.             d.UNSIGNED_ATTRIBUTE,
  2320.             d.MONEY,
  2321.             d.AUTO_INCREMENT,
  2322.             convert(varchar(32),case
  2323.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2324.                 else d.TYPE_NAME
  2325.             end) LOCAL_TYPE_NAME,
  2326.             MINIMUM_SCALE = d.numeric_scale,
  2327.             convert(smallint,case
  2328.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2329.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2330.                 else t.scale
  2331.             end) MAXIMUM_SCALE,
  2332.             d.SQL_DATA_TYPE,
  2333.             d.SQL_DATETIME_SUB,
  2334.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2335.             INTERVAL_PRECISION = convert(smallint,NULL),
  2336.             USERTYPE = t.usertype
  2337.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2338.         where
  2339.             d.DATA_TYPE = @data_type
  2340.             and d.ss_dtype = t.type
  2341.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2342.             and t.usertype *= e.user_type
  2343.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2344.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2345.                 t.usertype > 100)
  2346.             and (t.usertype <= 100 or
  2347.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2348.         order by 12, 11, t.usertype
  2349. go
  2350.  
  2351. if (charindex('7.00', @@version) = 0)
  2352. begin
  2353.     print ''
  2354.     print ''
  2355.     print 'Warning:'
  2356.     print 'you are installing the stored procedures '
  2357.     print 'on a pre 7.0 SQL Server.'
  2358.     print 'Ignore the following errors.'
  2359. end
  2360. else
  2361.     drop proc sp_datatype_info
  2362. go
  2363.  
  2364. /*    Procedure for 7.0 server */
  2365. create proc sp_datatype_info
  2366.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2367. as
  2368.     declare @mintype int
  2369.     declare @maxtype int
  2370.  
  2371.     if @ODBCVer <> 3
  2372.         select @ODBCVer = 2
  2373.     if @data_type = 0
  2374.     begin
  2375.         select @mintype = -32768
  2376.         select @maxtype = 32767
  2377.     end
  2378.     else
  2379.     begin
  2380.         select @mintype = @data_type
  2381.         select @maxtype = @data_type
  2382.     end
  2383.  
  2384.     select
  2385.         convert(sysname,case
  2386.             when t.xusertype > 255 then t.name
  2387.             else d.TYPE_NAME
  2388.         end) TYPE_NAME,
  2389.         d.DATA_TYPE,
  2390.         convert(int,case
  2391.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2392.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2393.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2394.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2395.         end) "PRECISION",
  2396.         d.LITERAL_PREFIX,
  2397.         d.LITERAL_SUFFIX,
  2398.         e.CREATE_PARAMS,
  2399.         convert(smallint,case
  2400.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2401.             else TypeProperty (t.name, 'AllowsNull')
  2402.         end) NULLABLE,
  2403.         d.CASE_SENSITIVE,
  2404.         d.SEARCHABLE,
  2405.         d.UNSIGNED_ATTRIBUTE,
  2406.         d.MONEY,
  2407.         d.AUTO_INCREMENT,
  2408.         convert(sysname,case
  2409.             when t.xusertype > 255 then t.name
  2410.             else d.LOCAL_TYPE_NAME
  2411.         end) LOCAL_TYPE_NAME,
  2412.         MINIMUM_SCALE = d.numeric_scale,
  2413.         convert(smallint,case
  2414.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2415.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2416.             else TypeProperty (t.name, 'Scale')
  2417.         end) MAXIMUM_SCALE,
  2418.         d.SQL_DATA_TYPE,
  2419.         d.SQL_DATETIME_SUB,
  2420.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2421.         INTERVAL_PRECISION = convert(smallint,NULL),
  2422.         USERTYPE = t.usertype
  2423.     from master.dbo.spt_datatype_info d
  2424.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2425.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2426.             t.xusertype = e.user_type
  2427.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2428.     where
  2429.         d.DATA_TYPE between @mintype and @maxtype
  2430.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2431.         and (t.xusertype <= 255 or
  2432.             isnull(d.AUTO_INCREMENT,0) = 0)
  2433.     order by 2, 12, 11,
  2434.     case
  2435.         when t.usertype=18 then 255
  2436.         else t.usertype
  2437.     end
  2438.  
  2439. go
  2440.  
  2441. grant execute on sp_datatype_info to public
  2442. go
  2443.  
  2444. dump tran master with no_log
  2445. go
  2446.  
  2447. print 'creating sp_fkeys'
  2448. go
  2449.  
  2450. /*    Procedure for pre-6.0 server */
  2451. CREATE PROCEDURE sp_fkeys(
  2452.                @pktable_name        varchar(32) = null,
  2453.                @pktable_owner        varchar(32) = null,
  2454.                @pktable_qualifier    varchar(32) = null,
  2455.                @fktable_name        varchar(32) = null,
  2456.                @fktable_owner        varchar(32) = null,
  2457.                @fktable_qualifier    varchar(32) = null )
  2458. as
  2459.     set nocount on
  2460.     declare    @order_by_pk int
  2461.  
  2462.     select  @order_by_pk = 0
  2463.  
  2464.     if (@pktable_name is null) and (@fktable_name is null)
  2465.     begin    /* If neither primary key nor foreign key table names given */
  2466.         raiserror 20004 '~~Rush_25~~'
  2467.         return
  2468.     end
  2469.     if @fktable_qualifier is not null
  2470.     begin
  2471.         if db_name() <> @fktable_qualifier
  2472.         begin    /* If qualifier doesn't match current database */
  2473.             raiserror 20001 '~~Rush_26~~'
  2474.             return
  2475.         end
  2476.     end
  2477.     if @pktable_qualifier is not null
  2478.     begin
  2479.         if db_name() <> @pktable_qualifier
  2480.         begin    /* If qualifier doesn't match current database */
  2481.             raiserror 20001 '~~Rush_27~~'
  2482.             return
  2483.         end
  2484.     end
  2485.  
  2486.     if @pktable_name is null
  2487.     begin /*  If table name not supplied, match all */
  2488.         select @pktable_name = '%'
  2489.         select @order_by_pk = 1
  2490.     end
  2491.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  2492.         select @pktable_owner = '%'
  2493.     if @fktable_name is null    /*    If table name not supplied, match all */
  2494.         select @fktable_name = '%'
  2495.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  2496.         select @fktable_owner = '%'
  2497.  
  2498.     if @@trancount <> 0
  2499.     begin    /* If inside a transaction */
  2500.         raiserror 20003 '~~Rush_28~~'
  2501.         return
  2502.     end
  2503.     create table #fkeys(
  2504.              PKTABLE_QUALIFIER    varchar(32) NULL,
  2505.              PKTABLE_OWNER        varchar(32) NULL,
  2506.              PKTABLE_NAME        varchar(32) NOT NULL,
  2507.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  2508.              FKTABLE_QUALIFIER    varchar(32) NULL,
  2509.              FKTABLE_OWNER        varchar(32) NULL,
  2510.              FKTABLE_NAME        varchar(32) NOT NULL,
  2511.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  2512.              KEY_SEQ            smallint NOT NULL)
  2513.  
  2514.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2515.     /*    Process syskeys for each relationship */
  2516.     /*    The inserts below adds a row to the temp table for each of the
  2517.         8 possible relationships */
  2518.     insert into #fkeys
  2519.         select
  2520.             db_name(),
  2521.             user_name(o1.uid),
  2522.             object_name(k.depid),
  2523.             c2.name,
  2524.             db_name(),
  2525.             user_name(o2.uid),
  2526.             object_name(k.id),
  2527.             c1.name,
  2528.             1
  2529.         from
  2530.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2531.         where
  2532.             c1.id = k.id
  2533.             and k.type = 2    /* Foreign type key */
  2534.             and c1.colid = k.key1
  2535.             and c2.id = k.depid
  2536.             and c2.colid = k.depkey1
  2537.             and o1.id = k.depid
  2538.             and o2.id = k.id
  2539.     union all
  2540.         select
  2541.             db_name(),
  2542.             user_name(o1.uid),
  2543.             object_name(k.depid),
  2544.             c2.name,
  2545.             db_name(),
  2546.             user_name(o2.uid),
  2547.             object_name(k.id),
  2548.             c1.name,
  2549.             2
  2550.         from
  2551.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2552.         where
  2553.             c1.id = k.id
  2554.             and k.type = 2    /* Foreign type key */
  2555.             and c1.colid = k.key2
  2556.             and c2.id = k.depid
  2557.             and c2.colid = k.depkey2
  2558.             and o1.id = k.depid
  2559.             and o2.id = k.id
  2560.     union all
  2561.         select
  2562.             db_name(),
  2563.             user_name(o1.uid),
  2564.             object_name(k.depid),
  2565.             c2.name,
  2566.             db_name(),
  2567.             user_name(o2.uid),
  2568.             object_name(k.id),
  2569.             c1.name,
  2570.             3
  2571.         from
  2572.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2573.         where
  2574.             c1.id = k.id
  2575.             and k.type = 2    /* Foreign type key */
  2576.             and c1.colid = k.key3
  2577.             and c2.id = k.depid
  2578.             and c2.colid = k.depkey3
  2579.             and o1.id = k.depid
  2580.             and o2.id = k.id
  2581.     union all
  2582.         select
  2583.             db_name(),
  2584.             user_name(o1.uid),
  2585.             object_name(k.depid),
  2586.             c2.name,
  2587.             db_name(),
  2588.             user_name(o2.uid),
  2589.             object_name(k.id),
  2590.             c1.name,
  2591.             4
  2592.         from
  2593.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2594.         where
  2595.             c1.id = k.id
  2596.             and k.type = 2    /* Foreign type key */
  2597.             and c1.colid = k.key4
  2598.             and c2.id = k.depid
  2599.             and c2.colid = k.depkey4
  2600.             and o1.id = k.depid
  2601.             and o2.id = k.id
  2602.     union all
  2603.         select
  2604.             db_name(),
  2605.             user_name(o1.uid),
  2606.             object_name(k.depid),
  2607.             c2.name,
  2608.             db_name(),
  2609.             user_name(o2.uid),
  2610.             object_name(k.id),
  2611.             c1.name,
  2612.             5
  2613.         from
  2614.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2615.         where
  2616.             c1.id = k.id
  2617.             and k.type = 2    /* Foreign type key */
  2618.             and c1.colid = k.key5
  2619.             and c2.id = k.depid
  2620.             and c2.colid = k.depkey5
  2621.             and o1.id = k.depid
  2622.             and o2.id = k.id
  2623.     union all
  2624.         select
  2625.             db_name(),
  2626.             user_name(o1.uid),
  2627.             object_name(k.depid),
  2628.             c2.name,
  2629.             db_name(),
  2630.             user_name(o2.uid),
  2631.             object_name(k.id),
  2632.             c1.name,
  2633.             6
  2634.         from
  2635.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2636.         where
  2637.             c1.id = k.id
  2638.             and k.type = 2    /* Foreign type key */
  2639.             and c1.colid = k.key6
  2640.             and c2.id = k.depid
  2641.             and c2.colid = k.depkey6
  2642.             and o1.id = k.depid
  2643.             and o2.id = k.id
  2644.     union all
  2645.         select
  2646.             db_name(),
  2647.             user_name(o1.uid),
  2648.             object_name(k.depid),
  2649.             c2.name,
  2650.             db_name(),
  2651.             user_name(o2.uid),
  2652.             object_name(k.id),
  2653.             c1.name,
  2654.             7
  2655.         from
  2656.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2657.         where
  2658.             c1.id = k.id
  2659.             and k.type = 2    /* Foreign type key */
  2660.             and c1.colid = k.key7
  2661.             and c2.id = k.depid
  2662.             and c2.colid = k.depkey7
  2663.             and o1.id = k.depid
  2664.             and o2.id = k.id
  2665.     union all
  2666.         select
  2667.             db_name(),
  2668.             user_name(o1.uid),
  2669.             object_name(k.depid),
  2670.             c2.name,
  2671.             db_name(),
  2672.             user_name(o2.uid),
  2673.             object_name(k.id),
  2674.             c1.name,
  2675.             8
  2676.         from
  2677.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2678.         where
  2679.             c1.id = k.id
  2680.             and k.type = 2    /* Foreign type key */
  2681.             and c1.colid = k.key8
  2682.             and c2.id = k.depid
  2683.             and c2.colid = k.depkey8
  2684.             and o1.id = k.depid
  2685.             and o2.id = k.id
  2686.  
  2687.     if @order_by_pk = 1 /*    If order by PK fields */
  2688.         select
  2689.             PKTABLE_QUALIFIER,
  2690.             PKTABLE_OWNER,
  2691.             PKTABLE_NAME,
  2692.             PKCOLUMN_NAME,
  2693.             FKTABLE_QUALIFIER,
  2694.             FKTABLE_OWNER,
  2695.             FKTABLE_NAME,
  2696.             FKCOLUMN_NAME,
  2697.             KEY_SEQ,
  2698.             UPDATE_RULE = convert(smallint, null),
  2699.             DELETE_RULE = convert(smallint,null),
  2700.             FK_NAME = convert(varchar(32),null),
  2701.             PK_NAME = convert(varchar(32),null),
  2702.             DEFERRABILITY = convert(smallint,7)
  2703.         from #fkeys
  2704.         where FKTABLE_NAME like @fktable_name
  2705.             and FKTABLE_OWNER like @fktable_owner
  2706.             and PKTABLE_NAME  like @pktable_name
  2707.             and PKTABLE_OWNER like @pktable_owner
  2708.         order by 1, 2, 3, 9
  2709.     else        /*    Order by FK fields */
  2710.         select
  2711.             PKTABLE_QUALIFIER,
  2712.             PKTABLE_OWNER,
  2713.             PKTABLE_NAME,
  2714.             PKCOLUMN_NAME,
  2715.             FKTABLE_QUALIFIER,
  2716.             FKTABLE_OWNER,
  2717.             FKTABLE_NAME,
  2718.             FKCOLUMN_NAME,
  2719.             KEY_SEQ,
  2720.             UPDATE_RULE = convert(smallint,null),
  2721.             DELETE_RULE = convert(smallint,null),
  2722.             FK_NAME = convert(varchar(32),null),
  2723.             PK_NAME = convert(varchar(32),null),
  2724.             DEFERRABILITY = convert(smallint,7)
  2725.         from #fkeys
  2726.         where FKTABLE_NAME like @fktable_name
  2727.             and FKTABLE_OWNER like @fktable_owner
  2728.             and PKTABLE_NAME  like @pktable_name
  2729.             and PKTABLE_OWNER like @pktable_owner
  2730.         order by 5, 6, 7, 9
  2731. go
  2732.  
  2733. if (charindex('6.00', @@version) = 0 and
  2734.     charindex('6.50', @@version) = 0 and
  2735.     charindex('7.00', @@version) = 0)
  2736. begin
  2737.     print ''
  2738.     print ''
  2739.     print 'Warning:'
  2740.     print 'you are installing the stored procedures '
  2741.     print 'on a pre 6.0 SQL Server.'
  2742.     print 'Ignore the following errors.'
  2743. end
  2744. else
  2745.     drop proc sp_fkeys
  2746. go
  2747.  
  2748. /*    Procedure for 6.0 and 6.50 servers */
  2749. CREATE PROCEDURE sp_fkeys(
  2750.                @pktable_name        varchar(32) = null,
  2751.                @pktable_owner        varchar(32) = null,
  2752.                @pktable_qualifier    varchar(32) = null,
  2753.                @fktable_name        varchar(32) = null,
  2754.                @fktable_owner        varchar(32) = null,
  2755.                @fktable_qualifier    varchar(32) = null )
  2756. as
  2757.     set nocount on
  2758.     DECLARE @pktable_id            int
  2759.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  2760.     DECLARE @fktable_id            int
  2761.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  2762.     declare    @order_by_pk        int
  2763.  
  2764.     select  @order_by_pk = 0
  2765.  
  2766.     if (@pktable_name is null) and (@fktable_name is null)
  2767.     begin    /* If neither primary key nor foreign key table names given */
  2768.         raiserror (15252,-1,-1)
  2769.         return
  2770.     end
  2771.     if @fktable_qualifier is not null
  2772.     begin
  2773.         if db_name() <> @fktable_qualifier
  2774.         begin    /* If qualifier doesn't match current database */
  2775.             raiserror (15250, -1,-1)
  2776.             return
  2777.         end
  2778.     end
  2779.     if @pktable_qualifier is not null
  2780.     begin
  2781.         if db_name() <> @pktable_qualifier
  2782.         begin    /* If qualifier doesn't match current database */
  2783.             raiserror (15250, -1,-1)
  2784.             return
  2785.         end
  2786.     end
  2787.  
  2788.     if @pktable_owner is null
  2789.     begin    /* If unqualified primary key table name */
  2790.         SELECT @pkfull_table_name = @pktable_name
  2791.     end
  2792.     else
  2793.     begin    /* Qualified primary key table name */
  2794.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  2795.     end
  2796.     /*    Get Object ID */
  2797.     SELECT @pktable_id = object_id(@pkfull_table_name)
  2798.  
  2799.     if @fktable_owner is null
  2800.     begin    /* If unqualified foreign key table name */
  2801.         SELECT @fkfull_table_name = @fktable_name
  2802.     end
  2803.     else
  2804.     begin    /* Qualified foreign key table name */
  2805.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  2806.     end
  2807.     /*    Get Object ID */
  2808.     SELECT @fktable_id = object_id(@fkfull_table_name)
  2809.  
  2810.     if @fktable_name is not null
  2811.     begin
  2812.         if @fktable_id is null
  2813.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  2814.     end
  2815.  
  2816.     if @pktable_name is null
  2817.     begin /*  If table name not supplied, match all */
  2818.         select @order_by_pk = 1
  2819.     end
  2820.     else
  2821.     begin
  2822.         if @pktable_id is null
  2823.         begin
  2824.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  2825.         end
  2826.     end
  2827.  
  2828.     if (@@trancount <> 0 and
  2829.         charindex('6.50', @@version) = 0)
  2830.     begin    /* If inside a transaction */
  2831.         raiserror (15002,-1,-1,'sp_fkeys')
  2832.         return
  2833.     end
  2834.  
  2835.     create table #fkeys(
  2836.              pkdb_id        int NOT NULL,
  2837.              pktable_id     int NOT NULL,
  2838.              pkcolid        int NOT NULL,
  2839.              fkdb_id        int NOT NULL,
  2840.              fktable_id        int NOT NULL,
  2841.              fkcolid        int NOT NULL,
  2842.              KEY_SEQ        smallint NOT NULL,
  2843.              fk_id            int NOT NULL,
  2844.              pk_id            int NOT NULL)
  2845.  
  2846.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  2847.     /*    Process syskeys for each relationship */
  2848.     /*    The inserts below adds a row to the temp table for each of the
  2849.         16 possible relationships */
  2850.     insert into #fkeys
  2851.         select
  2852.             r.rkeydbid,
  2853.             r.rkeyid,
  2854.             r.rkey1,
  2855.             r.fkeydbid,
  2856.             r.fkeyid,
  2857.             r.fkey1,
  2858.             1,
  2859.             r.constid,
  2860.             s.constid
  2861.         from
  2862.             sysreferences r, sysconstraints s
  2863.         where    r.rkeyid = s.id
  2864.             AND (s.status & 0xf) = 1
  2865.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2866.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2867.       union all
  2868.         select
  2869.             r.rkeydbid,
  2870.             r.rkeyid,
  2871.             r.rkey2,
  2872.             r.fkeydbid,
  2873.             r.fkeyid,
  2874.             r.fkey2,
  2875.             2,
  2876.             r.constid,
  2877.             s.constid
  2878.         from
  2879.             sysreferences r, sysconstraints s
  2880.         where    r.rkeyid = s.id
  2881.             AND (s.status & 0xf) = 1
  2882.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2883.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2884.       union all
  2885.         select
  2886.             r.rkeydbid,
  2887.             r.rkeyid,
  2888.             r.rkey3,
  2889.             r.fkeydbid,
  2890.             r.fkeyid,
  2891.             r.fkey3,
  2892.             3,
  2893.             r.constid,
  2894.             s.constid
  2895.         from
  2896.             sysreferences r, sysconstraints s
  2897.         where    r.rkeyid = s.id
  2898.             AND (s.status & 0xf) = 1
  2899.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2900.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2901.       union all
  2902.         select
  2903.             r.rkeydbid,
  2904.             r.rkeyid,
  2905.             r.rkey4,
  2906.             r.fkeydbid,
  2907.             r.fkeyid,
  2908.             r.fkey4,
  2909.             4,
  2910.             r.constid,
  2911.             s.constid
  2912.         from
  2913.             sysreferences r, sysconstraints s
  2914.         where    r.rkeyid = s.id
  2915.             AND (s.status & 0xf) = 1
  2916.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2917.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2918.       union all
  2919.         select
  2920.             r.rkeydbid,
  2921.             r.rkeyid,
  2922.             r.rkey5,
  2923.             r.fkeydbid,
  2924.             r.fkeyid,
  2925.             r.fkey5,
  2926.             5,
  2927.             r.constid,
  2928.             s.constid
  2929.         from
  2930.             sysreferences r, sysconstraints s
  2931.         where    r.rkeyid = s.id
  2932.             AND (s.status & 0xf) = 1
  2933.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2934.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2935.       union all
  2936.         select
  2937.             r.rkeydbid,
  2938.             r.rkeyid,
  2939.             r.rkey6,
  2940.             r.fkeydbid,
  2941.             r.fkeyid,
  2942.             r.fkey6,
  2943.             6,
  2944.             r.constid,
  2945.             s.constid
  2946.         from
  2947.             sysreferences r, sysconstraints s
  2948.         where    r.rkeyid = s.id
  2949.             AND (s.status & 0xf) = 1
  2950.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2951.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2952.       union all
  2953.         select
  2954.             r.rkeydbid,
  2955.             r.rkeyid,
  2956.             r.rkey7,
  2957.             r.fkeydbid,
  2958.             r.fkeyid,
  2959.             r.fkey7,
  2960.             7,
  2961.             r.constid,
  2962.             s.constid
  2963.         from
  2964.             sysreferences r, sysconstraints s
  2965.         where    r.rkeyid = s.id
  2966.             AND (s.status & 0xf) = 1
  2967.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2968.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2969.       union all
  2970.         select
  2971.             r.rkeydbid,
  2972.             r.rkeyid,
  2973.             r.rkey8,
  2974.             r.fkeydbid,
  2975.             r.fkeyid,
  2976.             r.fkey8,
  2977.             8,
  2978.             r.constid,
  2979.             s.constid
  2980.         from
  2981.             sysreferences r, sysconstraints s
  2982.         where    r.rkeyid = s.id
  2983.             AND (s.status & 0xf) = 1
  2984.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2985.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2986.       union all
  2987.         select
  2988.             r.rkeydbid,
  2989.             r.rkeyid,
  2990.             r.rkey9,
  2991.             r.fkeydbid,
  2992.             r.fkeyid,
  2993.             r.fkey9,
  2994.             9,
  2995.             r.constid,
  2996.             s.constid
  2997.         from
  2998.             sysreferences r, sysconstraints s
  2999.         where    r.rkeyid = s.id
  3000.             AND (s.status & 0xf) = 1
  3001.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3002.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3003.       union all
  3004.         select
  3005.             r.rkeydbid,
  3006.             r.rkeyid,
  3007.             r.rkey10,
  3008.             r.fkeydbid,
  3009.             r.fkeyid,
  3010.             r.fkey10,
  3011.             10,
  3012.             r.constid,
  3013.             s.constid
  3014.         from
  3015.             sysreferences r, sysconstraints s
  3016.         where    r.rkeyid = s.id
  3017.             AND (s.status & 0xf) = 1
  3018.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3019.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3020.       union all
  3021.         select
  3022.             r.rkeydbid,
  3023.             r.rkeyid,
  3024.             r.rkey11,
  3025.             r.fkeydbid,
  3026.             r.fkeyid,
  3027.             r.fkey11,
  3028.             11,
  3029.             r.constid,
  3030.             s.constid
  3031.         from
  3032.             sysreferences r, sysconstraints s
  3033.         where    r.rkeyid = s.id
  3034.             AND (s.status & 0xf) = 1
  3035.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3036.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3037.       union all
  3038.         select
  3039.             r.rkeydbid,
  3040.             r.rkeyid,
  3041.             r.rkey12,
  3042.             r.fkeydbid,
  3043.             r.fkeyid,
  3044.             r.fkey12,
  3045.             12,
  3046.             r.constid,
  3047.             s.constid
  3048.         from
  3049.             sysreferences r, sysconstraints s
  3050.         where    r.rkeyid = s.id
  3051.             AND (s.status & 0xf) = 1
  3052.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3053.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3054.       union all
  3055.         select
  3056.             r.rkeydbid,
  3057.             r.rkeyid,
  3058.             r.rkey13,
  3059.             r.fkeydbid,
  3060.             r.fkeyid,
  3061.             r.fkey13,
  3062.             13,
  3063.             r.constid,
  3064.             s.constid
  3065.         from
  3066.             sysreferences r, sysconstraints s
  3067.         where    r.rkeyid = s.id
  3068.             AND (s.status & 0xf) = 1
  3069.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3070.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3071.       union all
  3072.         select
  3073.             r.rkeydbid,
  3074.             r.rkeyid,
  3075.             r.rkey14,
  3076.             r.fkeydbid,
  3077.             r.fkeyid,
  3078.             r.fkey14,
  3079.             14,
  3080.             r.constid,
  3081.             s.constid
  3082.         from
  3083.             sysreferences r, sysconstraints s
  3084.         where    r.rkeyid = s.id
  3085.             AND (s.status & 0xf) = 1
  3086.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3087.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3088.       union all
  3089.         select
  3090.             r.rkeydbid,
  3091.             r.rkeyid,
  3092.             r.rkey15,
  3093.             r.fkeydbid,
  3094.             r.fkeyid,
  3095.             r.fkey15,
  3096.             15,
  3097.             r.constid,
  3098.             s.constid
  3099.         from
  3100.             sysreferences r, sysconstraints s
  3101.         where    r.rkeyid = s.id
  3102.             AND (s.status & 0xf) = 1
  3103.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3104.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3105.       union all
  3106.         select
  3107.             r.rkeydbid,
  3108.             r.rkeyid,
  3109.             r.rkey16,
  3110.             r.fkeydbid,
  3111.             r.fkeyid,
  3112.             r.fkey16,
  3113.             16,
  3114.             r.constid,
  3115.             s.constid
  3116.         from
  3117.             sysreferences r, sysconstraints s
  3118.         where    r.rkeyid = s.id
  3119.             AND (s.status & 0xf) = 1
  3120.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3121.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3122.  
  3123.     if @order_by_pk = 1 /*    If order by PK fields */
  3124.         select
  3125.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3126.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3127.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3128.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3129.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3130.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3131.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3132.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3133.             KEY_SEQ,
  3134.             UPDATE_RULE = convert(smallint,1),
  3135.             DELETE_RULE = convert(smallint,1),
  3136.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3137.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3138.             DEFERRABILITY = convert(smallint,7)
  3139.         from #fkeys f,
  3140.             sysobjects o1, sysobjects o2,
  3141.             syscolumns c1, syscolumns c2
  3142.         where    o1.id = f.pktable_id
  3143.             AND o2.id = f.fktable_id
  3144.             AND c1.id = f.pktable_id
  3145.             AND c2.id = f.fktable_id
  3146.             AND c1.colid = f.pkcolid
  3147.             AND c2.colid = f.fkcolid
  3148.         order by 1,2,3,9
  3149.     else        /*    Order by FK fields */
  3150.         select
  3151.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3152.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3153.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3154.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3155.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3156.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3157.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3158.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3159.             KEY_SEQ,
  3160.             UPDATE_RULE = convert(smallint,1),
  3161.             DELETE_RULE = convert(smallint,1),
  3162.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3163.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3164.             DEFERRABILITY = convert(smallint,7)
  3165.         from #fkeys f,
  3166.             sysobjects o1, sysobjects o2,
  3167.             syscolumns c1, syscolumns c2
  3168.         where    o1.id = f.pktable_id
  3169.             AND o2.id = f.fktable_id
  3170.             AND c1.id = f.pktable_id
  3171.             AND c2.id = f.fktable_id
  3172.             AND c1.colid = f.pkcolid
  3173.             AND c2.colid = f.fkcolid
  3174.         order by 5,6,7,9
  3175. go
  3176.  
  3177. if (charindex('7.00', @@version) = 0)
  3178. begin
  3179.     print ''
  3180.     print ''
  3181.     print 'Warning:'
  3182.     print 'you are installing the stored procedures '
  3183.     print 'on a pre 7.0 SQL Server.'
  3184.     print 'Ignore the following errors.'
  3185. end
  3186. else
  3187.     drop proc sp_fkeys
  3188. go
  3189.  
  3190. /*    Procedure for 7.0 and later servers */
  3191. CREATE PROCEDURE sp_fkeys(
  3192.                @pktable_name        sysname = null,
  3193.                @pktable_owner        sysname = null,
  3194.                @pktable_qualifier    sysname = null,
  3195.                @fktable_name        sysname = null,
  3196.                @fktable_owner        sysname = null,
  3197.                @fktable_qualifier    sysname = null )
  3198. as
  3199.     set nocount on
  3200.     DECLARE @pktable_id            int
  3201.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3202.     DECLARE @fktable_id            int
  3203.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3204.     declare    @order_by_pk        int
  3205.  
  3206.     /* select 'XXX starting table creation' */
  3207.  
  3208.     create table #fkeysall(
  3209.             rkeyid int NOT NULL,
  3210.             rkey1 int NOT NULL,
  3211.                 rkey2 int NOT NULL,
  3212.                 rkey3 int NOT NULL,
  3213.                 rkey4 int NOT NULL,
  3214.                 rkey5 int NOT NULL,
  3215.                 rkey6 int NOT NULL,
  3216.                 rkey7 int NOT NULL,
  3217.                 rkey8 int NOT NULL,
  3218.                 rkey9 int NOT NULL,
  3219.                 rkey10 int NOT NULL,
  3220.                 rkey11 int NOT NULL,
  3221.                 rkey12 int NOT NULL,
  3222.                 rkey13 int NOT NULL,
  3223.                 rkey14 int NOT NULL,
  3224.                 rkey15 int NOT NULL,
  3225.                 rkey16 int NOT NULL,
  3226.             fkeyid int NOT NULL,
  3227.             fkey1 int NOT NULL,
  3228.                 fkey2 int NOT NULL,
  3229.                 fkey3 int NOT NULL,
  3230.                 fkey4 int NOT NULL,
  3231.                 fkey5 int NOT NULL,
  3232.                 fkey6 int NOT NULL,
  3233.                 fkey7 int NOT NULL,
  3234.                 fkey8 int NOT NULL,
  3235.                 fkey9 int NOT NULL,
  3236.                 fkey10 int NOT NULL,
  3237.                 fkey11 int NOT NULL,
  3238.                 fkey12 int NOT NULL,
  3239.                 fkey13 int NOT NULL,
  3240.                 fkey14 int NOT NULL,
  3241.                 fkey15 int NOT NULL,
  3242.                 fkey16 int NOT NULL,
  3243.             constid int NOT NULL,
  3244.             name sysname NOT NULL)
  3245.  
  3246.     create table #fkeys(
  3247.             pktable_id        int NOT NULL,
  3248.             pkcolid         int NOT NULL,
  3249.             fktable_id        int NOT NULL,
  3250.             fkcolid         int NOT NULL,
  3251.             KEY_SEQ         smallint NOT NULL,
  3252.             fk_id            int NOT NULL,
  3253.             PK_NAME            sysname NOT NULL)
  3254.  
  3255.     create table #fkeysout(
  3256.             PKTABLE_QUALIFIER sysname NULL,
  3257.             PKTABLE_OWNER sysname NULL,
  3258.             PKTABLE_NAME sysname NOT NULL,
  3259.             PKCOLUMN_NAME sysname NOT NULL,
  3260.             FKTABLE_QUALIFIER sysname NULL,
  3261.             FKTABLE_OWNER sysname NULL,
  3262.             FKTABLE_NAME sysname NOT NULL,
  3263.             FKCOLUMN_NAME sysname NOT NULL,
  3264.             KEY_SEQ smallint NOT NULL,
  3265.             UPDATE_RULE smallint NULL,
  3266.             DELETE_RULE smallint NULL,
  3267.             FK_NAME sysname NULL,
  3268.             PK_NAME sysname NULL,
  3269.             DEFERRABILITY smallint null)
  3270.  
  3271.     /* select 'XXX starting parameter analysis' */
  3272.  
  3273.     select  @order_by_pk = 0
  3274.  
  3275.     if (@pktable_name is null) and (@fktable_name is null)
  3276.     begin    /* If neither primary key nor foreign key table names given */
  3277.         raiserror (15252,-1,-1)
  3278.         return
  3279.     end
  3280.     if @fktable_qualifier is not null
  3281.     begin
  3282.         if db_name() <> @fktable_qualifier
  3283.         begin    /* If qualifier doesn't match current database */
  3284.             raiserror (15250, -1,-1)
  3285.             return
  3286.         end
  3287.     end
  3288.     if @pktable_qualifier is not null
  3289.     begin
  3290.         if db_name() <> @pktable_qualifier
  3291.         begin    /* If qualifier doesn't match current database */
  3292.             raiserror (15250, -1,-1)
  3293.             return
  3294.         end
  3295.     end
  3296.  
  3297.     if @pktable_owner is null
  3298.     begin    /* If unqualified primary key table name */
  3299.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3300.     end
  3301.     else
  3302.     begin    /* Qualified primary key table name */
  3303.         if @pktable_owner = ''
  3304.         begin    /* If empty owner name */
  3305.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3306.         end
  3307.         else
  3308.         begin
  3309.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3310.                 '.' + quotename(@pktable_name)
  3311.         end
  3312.     end
  3313.     /*    Get Object ID */
  3314.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3315.  
  3316.     if @fktable_owner is null
  3317.     begin    /* If unqualified foreign key table name */
  3318.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3319.     end
  3320.     else
  3321.     begin    /* Qualified foreign key table name */
  3322.         if @fktable_owner = ''
  3323.         begin    /* If empty owner name */
  3324.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3325.         end
  3326.         else
  3327.         begin
  3328.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3329.                 '.' + quotename(@fktable_name)
  3330.         end
  3331.     end
  3332.     /*    Get Object ID */
  3333.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3334.  
  3335.     if @fktable_name is not null
  3336.     begin
  3337.         if @fktable_id is null
  3338.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3339.     end
  3340.  
  3341.     if @pktable_name is null
  3342.     begin /*  If table name not supplied, match all */
  3343.         select @order_by_pk = 1
  3344.     end
  3345.     else
  3346.     begin
  3347.         if @pktable_id is null
  3348.         begin
  3349.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3350.         end
  3351.     end
  3352.  
  3353.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3354.     /*    Process syskeys for each relationship */
  3355.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3356.         them out with a 16-way "insert select ... union select ..." */
  3357.  
  3358.     /* select 'XXX starting data analysis' */
  3359.  
  3360.     insert into #fkeysall
  3361.         select
  3362.             r.rkeyid,
  3363.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3364.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3365.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3366.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3367.             r.fkeyid,
  3368.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3369.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3370.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3371.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3372.             r.constid,
  3373.             i.name
  3374.         from
  3375.             sysreferences r, sysobjects o, sysindexes i
  3376.         where    r.constid = o.id
  3377.             AND o.xtype = 'F'
  3378.             AND r.rkeyindid = i.indid
  3379.             AND r.rkeyid = i.id
  3380.             AND r.rkeyid between isnull(@pktable_id, 0)
  3381.                             and isnull(@pktable_id, 0x7fffffff)
  3382.             AND r.fkeyid between isnull(@fktable_id, 0)
  3383.                             and isnull(@fktable_id, 0x7fffffff)
  3384.  
  3385.     /* select count (*) as 'XXX countall' from #fkeysall */
  3386.  
  3387.     insert into #fkeys
  3388.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3389.             from #fkeysall
  3390.         union all
  3391.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3392.             from #fkeysall
  3393.         union all
  3394.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3395.             from #fkeysall
  3396.         union all
  3397.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3398.             from #fkeysall
  3399.         union all
  3400.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3401.             from #fkeysall
  3402.         union all
  3403.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3404.             from #fkeysall
  3405.         union all
  3406.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3407.             from #fkeysall
  3408.         union all
  3409.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3410.             from #fkeysall
  3411.         union all
  3412.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3413.             from #fkeysall
  3414.         union all
  3415.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3416.             from #fkeysall
  3417.         union all
  3418.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3419.             from #fkeysall
  3420.         union all
  3421.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3422.             from #fkeysall
  3423.         union all
  3424.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3425.             from #fkeysall
  3426.         union all
  3427.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3428.             from #fkeysall
  3429.         union all
  3430.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3431.             from #fkeysall
  3432.         union all
  3433.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3434.             from #fkeysall
  3435.  
  3436.     /* select count (*) as 'XXX count' from #fkeys */
  3437.  
  3438.     insert into #fkeysout
  3439.         select
  3440.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3441.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3442.             PKTABLE_NAME = convert(sysname,o1.name),
  3443.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3444.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3445.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3446.             FKTABLE_NAME = convert(sysname,o2.name),
  3447.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3448.             KEY_SEQ,
  3449.             UPDATE_RULE = convert(smallint,1),
  3450.             DELETE_RULE = convert(smallint,1),
  3451.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3452.             PK_NAME,
  3453.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  3454.         from #fkeys f,
  3455.             sysobjects o1, sysobjects o2,
  3456.             syscolumns c1, syscolumns c2
  3457.         where    o1.id = f.pktable_id
  3458.             AND o2.id = f.fktable_id
  3459.             AND c1.id = f.pktable_id
  3460.             AND c2.id = f.fktable_id
  3461.             AND c1.colid = f.pkcolid
  3462.             AND c2.colid = f.fkcolid
  3463.  
  3464.     /* select count (*) as 'XXX countout' from #fkeysout */
  3465.  
  3466.     if @order_by_pk = 1 /*    If order by PK fields */
  3467.         select
  3468.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3469.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3470.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3471.         from #fkeysout
  3472.         order by 1,2,3,9
  3473.     else        /*    Order by FK fields */
  3474.         select
  3475.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3476.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3477.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3478.         from #fkeysout
  3479.         order by 5,6,7,9
  3480. go
  3481.  
  3482. grant execute on sp_fkeys to public
  3483. go
  3484.  
  3485. dump tran master with no_log
  3486. go
  3487.  
  3488. print 'creating sp_pkeys'
  3489. go
  3490.  
  3491. /*    Procedure for pre-6.0 server */
  3492. CREATE PROCEDURE sp_pkeys(
  3493.                @table_name        varchar(32),
  3494.                @table_owner     varchar(32) = null,
  3495.                @table_qualifier varchar(32) = null )
  3496. as
  3497.     set nocount on
  3498.     if @table_qualifier is not null
  3499.     begin
  3500.         if db_name() <> @table_qualifier
  3501.         begin    /* If qualifier doesn't match current database */
  3502.             raiserror 20001 '~~Rush_5~~'
  3503.             return
  3504.         end
  3505.     end
  3506.     if @table_owner is null /*    If owner not supplied, match all */
  3507.         select @table_owner = '%'
  3508.     if @@trancount <> 0
  3509.     begin    /* If inside a transaction */
  3510.         raiserror 20003 '~~Rush_35~~'
  3511.         return
  3512.     end
  3513.  
  3514.     create table #pkeys(
  3515.              TABLE_QUALIFIER varchar(32) NULL,
  3516.              TABLE_OWNER     varchar(32) NULL,
  3517.              TABLE_NAME      varchar(32) NOT NULL,
  3518.              COLUMN_NAME     varchar(32) NOT NULL,
  3519.              KEY_SEQ         smallint NOT NULL)
  3520.  
  3521.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3522.     /*    Process syskeys for each relationship */
  3523.     /*    The inserts below adds a row to the temp table for each of the
  3524.         8 possible relationships */
  3525.     insert into #pkeys
  3526.         select
  3527.             db_name(),
  3528.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3529.             object_name(k.id),
  3530.             c.name,
  3531.             1
  3532.         from
  3533.             syskeys k, syscolumns c
  3534.         where
  3535.             c.id = k.id
  3536.             and k.type = 1    /* Primary type key */
  3537.             and c.colid = k.key1
  3538.     if (@@rowcount = 0)
  3539.         goto done
  3540.  
  3541.     insert into #pkeys
  3542.         select
  3543.             db_name(),
  3544.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3545.             object_name(k.id),
  3546.             c.name,
  3547.             2
  3548.         from
  3549.             syskeys k, syscolumns c
  3550.         where
  3551.             c.id = k.id
  3552.             and k.type = 1    /* Primary type key */
  3553.             and c.colid = key2
  3554.     if (@@rowcount = 0)
  3555.         goto done
  3556.  
  3557.     insert into #pkeys
  3558.         select
  3559.             db_name(),
  3560.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3561.             object_name(k.id),
  3562.             c.name,
  3563.             3
  3564.         from
  3565.             syskeys k, syscolumns c
  3566.         where
  3567.             c.id = k.id
  3568.             and k.type = 1    /* Primary type key */
  3569.             and c.colid = key3
  3570.     if (@@rowcount = 0)
  3571.         goto done
  3572.  
  3573.     insert into #pkeys
  3574.         select
  3575.             db_name(),
  3576.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3577.             object_name(k.id),
  3578.             c.name,
  3579.             4
  3580.         from
  3581.             syskeys k, syscolumns c
  3582.         where
  3583.             c.id = k.id
  3584.             and k.type = 1    /* Primary type key */
  3585.             and c.colid = key4
  3586.     if (@@rowcount = 0)
  3587.         goto done
  3588.  
  3589.     insert into #pkeys
  3590.         select
  3591.             db_name(),
  3592.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3593.             object_name(k.id),
  3594.             c.name,
  3595.             5
  3596.         from
  3597.             syskeys k, syscolumns c
  3598.         where
  3599.             c.id = k.id
  3600.             and k.type = 1    /* Primary type key */
  3601.             and c.colid = key5
  3602.     if (@@rowcount = 0)
  3603.         goto done
  3604.  
  3605.     insert into #pkeys
  3606.         select
  3607.             db_name(),
  3608.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3609.             object_name(k.id),
  3610.             c.name,
  3611.             6
  3612.         from
  3613.             syskeys k, syscolumns c
  3614.         where
  3615.             c.id = k.id
  3616.             and k.type = 1    /* Primary type key */
  3617.             and c.colid = key6
  3618.     if (@@rowcount = 0)
  3619.         goto done
  3620.  
  3621.     insert into #pkeys
  3622.         select
  3623.             db_name(),
  3624.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3625.             object_name(k.id),
  3626.             c.name,
  3627.             7
  3628.         from
  3629.             syskeys k, syscolumns c
  3630.         where
  3631.             c.id = k.id
  3632.             and k.type = 1    /* Primary type key */
  3633.             and c.colid = key7
  3634.     if (@@rowcount = 0)
  3635.         goto done
  3636.  
  3637.     insert into #pkeys
  3638.          select
  3639.              db_name(),
  3640.              (select user_name(uid) from sysobjects o where o.id = k.id),
  3641.              object_name(k.id),
  3642.              c.name,
  3643.              8
  3644.          from
  3645.              syskeys k, syscolumns c
  3646.          where
  3647.              c.id = k.id
  3648.              and k.type = 1 /* Primary type key */
  3649.              and c.colid = key8
  3650.  
  3651.     done:
  3652.     select
  3653.         TABLE_QUALIFIER,
  3654.         TABLE_OWNER,
  3655.         TABLE_NAME,
  3656.         COLUMN_NAME,
  3657.         KEY_SEQ,
  3658.         PK_NAME = convert(varchar(32),null)
  3659.     from #pkeys
  3660.     where TABLE_NAME = @table_name
  3661.         and TABLE_OWNER like @table_owner
  3662.     order by 1, 2, 3, 5
  3663. go
  3664.  
  3665. if (charindex('6.00', @@version) = 0 and
  3666.     charindex('6.50', @@version) = 0 and
  3667.     charindex('7.00', @@version) = 0)
  3668. begin
  3669.     print ''
  3670.     print ''
  3671.     print 'Warning:'
  3672.     print 'you are installing the stored procedures '
  3673.     print 'on a pre 6.0 SQL Server.'
  3674.     print 'Ignore the following error.'
  3675. end
  3676. else
  3677.     drop proc sp_pkeys
  3678. go
  3679.  
  3680. /*    Procedure for 6.0 and 6.50 servers */
  3681. CREATE PROCEDURE sp_pkeys(
  3682.                @table_name        sysname,
  3683.                @table_owner     sysname = null,
  3684.                @table_qualifier sysname = null )
  3685. as
  3686.     DECLARE @table_id            int
  3687.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  3688.  
  3689.     if @table_qualifier is not null
  3690.     begin
  3691.         if db_name() <> @table_qualifier
  3692.         begin    /* If qualifier doesn't match current database */
  3693.             raiserror (15250, -1,-1)
  3694.             return
  3695.         end
  3696.     end
  3697.     if @table_owner is null
  3698.     begin    /* If unqualified table name */
  3699.         SELECT @full_table_name = @table_name
  3700.     end
  3701.     else
  3702.     begin    /* Qualified table name */
  3703.         if @table_owner = ''
  3704.         begin    /* If empty owner name */
  3705.             SELECT @full_table_name = @table_owner
  3706.         end
  3707.         else
  3708.         begin
  3709.             SELECT @full_table_name = @table_owner + '.' + @table_name
  3710.         end
  3711.     end
  3712.     /*    Get Object ID */
  3713.     SELECT @table_id = object_id(@full_table_name)
  3714.  
  3715.     select
  3716.         TABLE_QUALIFIER = convert(sysname,db_name()),
  3717.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  3718.         TABLE_NAME = convert(sysname,o.name),
  3719.         COLUMN_NAME = convert(sysname,c.name),
  3720.         KEY_SEQ = convert(smallint,c1.colid),
  3721.         PK_NAME = convert(sysname,i.name)
  3722.     from
  3723.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  3724.     where
  3725.         o.id = @table_id
  3726.         and o.id = c.id
  3727.         and o.id = i.id
  3728.         and (i.status & 0x800) = 0x800
  3729.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  3730.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  3731.         and c1.id = @table_id
  3732.     order by 1, 2, 3, 5
  3733. go
  3734.  
  3735. if (charindex('7.00', @@version) = 0)
  3736. begin
  3737.     print ''
  3738.     print ''
  3739.     print 'Warning:'
  3740.     print 'you are installing the stored procedures '
  3741.     print 'on a pre 7.0 SQL Server.'
  3742.     print 'Ignore the following errors.'
  3743. end
  3744. else
  3745.     drop proc sp_pkeys
  3746. go
  3747.  
  3748. /*    Procedure for 7.0 and later servers */
  3749. CREATE PROCEDURE sp_pkeys(
  3750.                @table_name        sysname,
  3751.                @table_owner     sysname = null,
  3752.                @table_qualifier sysname = null )
  3753. as
  3754.     DECLARE @table_id            int
  3755.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  3756.  
  3757.     if @table_qualifier is not null
  3758.     begin
  3759.         if db_name() <> @table_qualifier
  3760.         begin    /* If qualifier doesn't match current database */
  3761.             raiserror (15250, -1,-1)
  3762.             return
  3763.         end
  3764.     end
  3765.     if @table_owner is null
  3766.     begin    /* If unqualified table name */
  3767.         SELECT @full_table_name = quotename(@table_name)
  3768.     end
  3769.     else
  3770.     begin    /* Qualified table name */
  3771.         if @table_owner = ''
  3772.         begin    /* If empty owner name */
  3773.             SELECT @full_table_name = quotename(@table_owner)
  3774.         end
  3775.         else
  3776.         begin
  3777.             SELECT @full_table_name = quotename(@table_owner) +
  3778.                 '.' + quotename(@table_name)
  3779.         end
  3780.     end
  3781.     /*    Get Object ID */
  3782.     SELECT @table_id = object_id(@full_table_name)
  3783.  
  3784.     select
  3785.         TABLE_QUALIFIER = convert(sysname,db_name()),
  3786.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  3787.         TABLE_NAME = convert(sysname,o.name),
  3788.         COLUMN_NAME = convert(sysname,c.name),
  3789.         KEY_SEQ = convert(smallint,c1.colid),
  3790.         PK_NAME = convert(sysname,i.name)
  3791.     from
  3792.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  3793.     where
  3794.         o.id = @table_id
  3795.         and o.id = c.id
  3796.         and o.id = i.id
  3797.         and (i.status & 0x800) = 0x800
  3798.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  3799.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  3800.         and c1.id = @table_id
  3801.     order by 1, 2, 3, 5
  3802. go
  3803.  
  3804. grant execute on sp_pkeys to public
  3805. go
  3806.  
  3807. dump tran master with no_log
  3808. go
  3809.  
  3810. print 'creating sp_server_info'
  3811. go
  3812.  
  3813. create proc sp_server_info (
  3814.             @attribute_id  int = null)
  3815. as
  3816.     if @attribute_id is not null
  3817.         select *
  3818.         from master.dbo.spt_server_info
  3819.         where attribute_id = @attribute_id
  3820.     else
  3821.         select *
  3822.         from master.dbo.spt_server_info
  3823.         order by attribute_id
  3824. go
  3825.  
  3826. grant execute on sp_server_info to public
  3827. go
  3828.  
  3829. dump tran master with no_log
  3830. go
  3831.  
  3832. print 'creating sp_special_columns'
  3833. go
  3834.  
  3835. /*    Procedure for pre-6.0 server */
  3836. CREATE PROCEDURE sp_special_columns (
  3837.                  @table_name        varchar(32),
  3838.                  @table_owner        varchar(32) = null,
  3839.                  @table_qualifier    varchar(32) = null,
  3840.                  @col_type            char(1) = 'R',
  3841.                  @scope                char(1) = 'T',
  3842.                  @nullable            char(1) = 'U',
  3843.                  @ODBCVer            int = 2)
  3844. AS
  3845.     DECLARE @indid                int
  3846.     DECLARE @table_id            int
  3847.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  3848.     DECLARE @scopeout            smallint
  3849.  
  3850.     if @col_type not in ('R','V')
  3851.     begin
  3852.         raiserror 20002 '~~Rush_42~~'
  3853.         return
  3854.     end
  3855.  
  3856.     if @scope = 'C'
  3857.         select @scopeout = 0
  3858.     else if @scope = 'T'
  3859.         select @scopeout = 1
  3860.     else
  3861.     begin
  3862.         raiserror 20002 '~~Rush_43~~'
  3863.         return
  3864.     end
  3865.  
  3866.     if @nullable not in ('U','O')
  3867.     begin
  3868.         raiserror 20002 '~~Rush_44~~'
  3869.         return
  3870.     end
  3871.  
  3872.     if @table_qualifier is not null
  3873.     begin
  3874.         if db_name() <> @table_qualifier
  3875.         begin /* If qualifier doesn't match current database */
  3876.             raiserror 20001 '~~Rush_5~~'
  3877.             return
  3878.         end
  3879.     end
  3880.     if @table_owner is null
  3881.     begin     /* If unqualified table name */
  3882.         SELECT @full_table_name = @table_name
  3883.     end
  3884.     else
  3885.     begin    /* Qualified table name */
  3886.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3887.     end
  3888.     /*    Get Object ID */
  3889.     SELECT @table_id = object_id(@full_table_name)
  3890.  
  3891.     if @col_type = 'V'
  3892.     BEGIN /* if ROWVER, just run that query */
  3893.         SELECT
  3894.             SCOPE = convert(smallint,NULL),
  3895.             COLUMN_NAME = convert(varchar(32),c.name),
  3896.             DATA_TYPE = convert(smallint, -3),
  3897.             TYPE_NAME = t.name,
  3898.             "PRECISION" = convert(int,8),
  3899.             LENGTH = convert(int,8),
  3900.             SCALE = convert(smallint, NULL),
  3901.             PSEUDO_COLUMN = convert(smallint,1)
  3902.         FROM
  3903.             systypes t, syscolumns c
  3904.         WHERE
  3905.             c.id = @table_id
  3906.             AND c.usertype = 80 /*    TIMESTAMP */
  3907.             AND t.usertype = 80 /*    TIMESTAMP */
  3908.         RETURN
  3909.     END
  3910.  
  3911.     /* ROWID, now find the id of the 'best' index for this table */
  3912.  
  3913.     IF @nullable = 'O'    /* Don't include any indexes that contain
  3914.                            nullable columns. */
  3915.  
  3916.             SELECT @indid = MIN(indid)
  3917.                 FROM sysindexes i,syscolumns c,syscolumns c2
  3918.                 WHERE
  3919.                     i.status&2 = 2        /*    If Unique Index */
  3920.                      AND c.id = i.id
  3921.                      AND c2.id = c.id
  3922.                      AND c2.colid < i.keycnt + (i.status&16)/16
  3923.                     AND i.id = @table_id
  3924.                     AND indid > 0        /*    Eliminate Table Row */
  3925.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  3926.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  3927.  
  3928.     ELSE    /* Include indexes that are partially nullable. */
  3929.  
  3930.         SELECT @indid = MIN(indid)
  3931.             FROM sysindexes i
  3932.             WHERE
  3933.                 status&2 = 2        /*    If Unique Index */
  3934.                 AND id = @table_id
  3935.                 AND indid > 0        /*    Eliminate Table Row */
  3936.  
  3937.     SELECT
  3938.         SCOPE = @scopeout,
  3939.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  3940.         d.DATA_TYPE,
  3941.         TYPE_NAME = t.name,
  3942.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  3943.         LENGTH = isnull(d.length, convert(int,c.length)),
  3944.         SCALE = d.numeric_scale,
  3945.         PSEUDO_COLUMN = convert(smallint,1)
  3946.     FROM
  3947.         sysindexes x,
  3948.         syscolumns c,
  3949.         master.dbo.spt_datatype_info d,
  3950.         systypes t,
  3951.         syscolumns c2    /* Self-join to generate list of index columns and */
  3952.                         /* to extract datatype names */
  3953.     WHERE
  3954.         x.id = @table_id
  3955.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  3956.         AND c.id = x.id
  3957.         AND c2.id = x.id
  3958.         AND c2.colid < keycnt+(x.status&16)/16
  3959.         AND x.indid = @indid
  3960.         AND t.type = d.ss_dtype
  3961.         AND c.length = d.fixlen
  3962.         AND c.usertype = t.usertype
  3963.  
  3964. go
  3965.  
  3966. if (charindex('6.00', @@version) = 0 and
  3967.     charindex('6.50', @@version) = 0 and
  3968.     charindex('7.00', @@version) = 0)
  3969. begin
  3970.     print ''
  3971.     print ''
  3972.     print 'Warning:'
  3973.     print 'you are installing the stored procedures '
  3974.     print 'on a pre 6.0 SQL Server.'
  3975.     print 'Ignore the following errors.'
  3976. end
  3977. else
  3978.     drop proc sp_special_columns
  3979. go
  3980.  
  3981. /*    Procedure for 6.0 and 6.50 servers */
  3982. CREATE PROCEDURE sp_special_columns (
  3983.                  @table_name        varchar(32),
  3984.                  @table_owner        varchar(32) = null,
  3985.                  @table_qualifier    varchar(32) = null,
  3986.                  @col_type            char(1) = 'R',
  3987.                  @scope                char(1) = 'T',
  3988.                  @nullable            char(1) = 'U',
  3989.                  @ODBCVer            int = 2)
  3990. AS
  3991.     DECLARE @indid                int
  3992.     DECLARE @table_id            int
  3993.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  3994.     DECLARE @scopeout            smallint
  3995.  
  3996.     if @col_type not in ('R','V')
  3997.     begin
  3998.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  3999.         return
  4000.     end
  4001.  
  4002.     if @scope = 'C'
  4003.         select @scopeout = 0
  4004.     else if @scope = 'T'
  4005.         select @scopeout = 1
  4006.     else
  4007.     begin
  4008.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4009.         return
  4010.     end
  4011.  
  4012.     if @nullable not in ('U','O')
  4013.     begin
  4014.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4015.         return
  4016.     end
  4017.  
  4018.     if @table_qualifier is not null
  4019.     begin
  4020.         if db_name() <> @table_qualifier
  4021.         begin /* If qualifier doesn't match current database */
  4022.             raiserror (15250, -1,-1)
  4023.             return
  4024.         end
  4025.     end
  4026.     if @table_owner is null
  4027.     begin     /* If unqualified table name */
  4028.         SELECT @full_table_name = @table_name
  4029.     end
  4030.     else
  4031.     begin     /* Qualified table name */
  4032.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4033.     end
  4034.     /*    Get Object ID */
  4035.     SELECT @table_id = object_id(@full_table_name)
  4036.  
  4037.     if @col_type = 'V'
  4038.     BEGIN /* if ROWVER, just run that query */
  4039.         SELECT
  4040.             SCOPE = convert(smallint,NULL),
  4041.             COLUMN_NAME = convert(varchar(32),c.name),
  4042.             DATA_TYPE = convert(smallint, -2),
  4043.             TYPE_NAME = t.name,
  4044.             "PRECISION" = convert(int,8),
  4045.             LENGTH = convert(int,8),
  4046.             SCALE = convert(smallint, NULL),
  4047.             PSEUDO_COLUMN = convert(smallint,1)
  4048.         FROM
  4049.             systypes t, syscolumns c
  4050.         WHERE
  4051.             c.id = @table_id
  4052.             AND c.usertype = 80 /*    TIMESTAMP */
  4053.             AND t.usertype = 80 /*    TIMESTAMP */
  4054.         RETURN
  4055.     END
  4056.  
  4057.     /* ROWID, now find the id of the 'best' index for this table */
  4058.  
  4059.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4060.                            nullable columns. */
  4061.  
  4062.         SELECT @indid = MIN(indid)
  4063.             FROM sysindexes i,syscolumns c,syscolumns c2
  4064.             WHERE
  4065.                 i.status&2 = 2        /*    If Unique Index */
  4066.                 AND c.id = i.id
  4067.                 AND c2.id = c.id
  4068.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4069.                 AND i.id = @table_id
  4070.                 AND indid > 0        /*    Eliminate Table Row */
  4071.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4072.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4073.  
  4074.     ELSE    /* Include indexes that are partially nullable. */
  4075.  
  4076.         SELECT @indid = MIN(indid)
  4077.             FROM sysindexes i
  4078.             WHERE
  4079.                 status&2 = 2        /*    If Unique Index */
  4080.                 AND id = @table_id
  4081.                 AND indid > 0        /*    Eliminate Table Row */
  4082.  
  4083.     SELECT
  4084.         SCOPE = @scopeout,
  4085.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4086.         d.DATA_TYPE,
  4087.         convert(varchar(32),case
  4088.             when (t.usertype > 100 or t.usertype in (18,80))
  4089.                 then t.name
  4090.             else d.TYPE_NAME
  4091.         end) TYPE_NAME,
  4092.         convert(int,case
  4093.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4094.             else isnull(convert(int,c.prec), 2147483647)
  4095.         end) "PRECISION",
  4096.         convert(int,case
  4097.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4098.                 convert(int,c.prec+2)
  4099.             else
  4100.                 isnull(d.length, c.length)
  4101.         end) LENGTH,
  4102.         SCALE = convert(smallint, c.scale),
  4103.         PSEUDO_COLUMN = convert(smallint,1)
  4104.     FROM
  4105.         sysindexes x,
  4106.         syscolumns c,
  4107.         master.dbo.spt_datatype_info d,
  4108.         systypes t,
  4109.         syscolumns c2    /* Self-join to generate list of index columns and */
  4110.                         /* to extract datatype names */
  4111.     WHERE
  4112.         x.id = @table_id
  4113.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4114.         AND c.id = x.id
  4115.         AND c2.id = x.id
  4116.         AND c2.colid < x.keycnt+(x.status&16)/16
  4117.         AND x.indid = @indid
  4118.         AND t.type = d.ss_dtype
  4119.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4120.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  4121.         AND c.usertype = t.usertype
  4122. go
  4123.  
  4124. if (charindex('7.00', @@version) = 0)
  4125. begin
  4126.     print ''
  4127.     print ''
  4128.     print 'Warning:'
  4129.     print 'you are installing the stored procedures '
  4130.     print 'on a pre 7.0 SQL Server.'
  4131.     print 'Ignore the following errors.'
  4132. end
  4133. else
  4134.     drop proc sp_special_columns
  4135. go
  4136.  
  4137. /*    Procedure for 7.0 and later servers */
  4138. CREATE PROCEDURE sp_special_columns (
  4139.                  @table_name        sysname,
  4140.                  @table_owner        sysname = null,
  4141.                  @table_qualifier    sysname = null,
  4142.                  @col_type            char(1) = 'R',
  4143.                  @scope                char(1) = 'T',
  4144.                  @nullable            char(1) = 'U',
  4145.                  @ODBCVer            int = 2)
  4146. AS
  4147.     DECLARE @indid                int
  4148.     DECLARE @table_id            int
  4149.     DECLARE @full_table_name    nvarchar(257)
  4150.     DECLARE @scopeout            smallint
  4151.  
  4152.     if @col_type not in ('R','V')
  4153.     begin
  4154.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4155.         return
  4156.     end
  4157.  
  4158.     if @scope = 'C'
  4159.         select @scopeout = 0
  4160.     else if @scope = 'T'
  4161.         select @scopeout = 1
  4162.     else
  4163.     begin
  4164.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4165.         return
  4166.     end
  4167.  
  4168.     if @nullable not in ('U','O')
  4169.     begin
  4170.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4171.         return
  4172.     end
  4173.  
  4174.     if @table_qualifier is not null
  4175.     begin
  4176.         if db_name() <> @table_qualifier
  4177.         begin /* If qualifier doesn't match current database */
  4178.             raiserror (15250, -1,-1)
  4179.             return
  4180.         end
  4181.     end
  4182.     if @table_owner is null
  4183.     begin     /* If unqualified table name */
  4184.         SELECT @full_table_name = quotename(@table_name)
  4185.     end
  4186.     else
  4187.     begin    /* Qualified table name */
  4188.         if @table_owner = ''
  4189.         begin    /* If empty owner name */
  4190.             SELECT @full_table_name = quotename(@table_owner)
  4191.         end
  4192.         else
  4193.         begin
  4194.             SELECT @full_table_name = quotename(@table_owner) +
  4195.                 '.' + quotename(@table_name)
  4196.         end
  4197.     end
  4198.     /*    Get Object ID */
  4199.     SELECT @table_id = object_id(@full_table_name)
  4200.  
  4201.     if @col_type = 'V'
  4202.     BEGIN /* if ROWVER, just run that query */
  4203.         SELECT
  4204.             SCOPE = convert(smallint,NULL),
  4205.             COLUMN_NAME = convert(sysname,c.name),
  4206.             DATA_TYPE = convert(smallint, -2),
  4207.             TYPE_NAME = t.name,
  4208.             "PRECISION" = convert(int,8),
  4209.             LENGTH = convert(int,8),
  4210.             SCALE = convert(smallint, NULL),
  4211.             PSEUDO_COLUMN = convert(smallint,1)
  4212.         FROM
  4213.             systypes t, syscolumns c
  4214.         WHERE
  4215.             not (@table_id is null)
  4216.             AND c.id = @table_id
  4217.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  4218.             AND t.xtype = c.xtype        
  4219.             AND t.xusertype = c.xusertype    
  4220.         RETURN
  4221.     END
  4222.  
  4223.     /* ROWID, now find the id of the 'best' index for this table */
  4224.  
  4225.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4226.                            nullable columns. */
  4227.  
  4228.         SELECT @indid = MIN(indid)
  4229.             FROM sysindexes x, syscolumns c, syscolumns c2
  4230.             WHERE
  4231.                 not (@table_id is null)
  4232.                 AND x.status&2 = 2        /*    If Unique Index */
  4233.                 AND c.id = x.id
  4234.                 AND c2.id = c.id
  4235.                 AND c2.colid < x.keycnt + (x.status&16)/16
  4236.                 AND x.id = @table_id
  4237.                 AND indid > 0        /*    Eliminate Table Row */
  4238.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  4239.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4240.  
  4241.     ELSE    /* Include indexes that are partially nullable. */
  4242.  
  4243.         SELECT @indid = MIN(indid)
  4244.             FROM sysindexes x
  4245.             WHERE
  4246.                 not (@table_id is null)
  4247.                 AND status&2 = 2        /*    If Unique Index */
  4248.                 AND id = @table_id
  4249.                 AND indid > 0        /*    Eliminate Table Row */
  4250.  
  4251.     SELECT
  4252.         SCOPE = @scopeout,
  4253.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  4254.         d.DATA_TYPE,
  4255.         convert(sysname,case
  4256.             when t.xusertype > 255 then t.name
  4257.             else d.TYPE_NAME
  4258.         end) TYPE_NAME,
  4259.         convert(int,case
  4260.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4261.             else OdbcPrec(c.xtype,c.length,c.xprec)
  4262.         end) "PRECISION",
  4263.         convert(int,case
  4264.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4265.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  4266.             else isnull(d.length, c.length)
  4267.         end) LENGTH,
  4268.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4269.         PSEUDO_COLUMN = convert(smallint,1)
  4270.     FROM
  4271.         sysindexes x,
  4272.         syscolumns c,
  4273.         master.dbo.spt_datatype_info d,
  4274.         systypes t,
  4275.         syscolumns c2    /* Self-join to generate list of index columns and */
  4276.                         /* to extract datatype names */
  4277.     WHERE
  4278.         not (@table_id is null)
  4279.         AND x.id = @table_id
  4280.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4281.         AND c.id = x.id
  4282.         AND c2.id = x.id
  4283.         AND c2.colid < x.keycnt + (x.status&16)/16
  4284.         AND x.indid = @indid
  4285.         AND t.xtype = d.ss_dtype
  4286.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4287.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  4288.         AND c.xusertype = t.xusertype
  4289. go
  4290.  
  4291. grant execute on sp_special_columns to public
  4292. go
  4293.  
  4294. dump tran master with no_log
  4295. go
  4296.  
  4297. print 'creating sp_sproc_columns'
  4298. go
  4299.  
  4300. /*    Procedure for pre-6.0 server */
  4301. CREATE PROCEDURE sp_sproc_columns (
  4302.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  4303.                  @procedure_owner        varchar(96) = null,
  4304.                  @procedure_qualifier    varchar(32) = null,
  4305.                  @column_name            varchar(96) = null,
  4306.                  @ODBCVer                int = 2)
  4307. AS
  4308.     DECLARE @group_num_lower smallint
  4309.     DECLARE @group_num_upper smallint
  4310.     DECLARE @semi_position int
  4311.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  4312.     DECLARE @procedure_id int
  4313.  
  4314.     if @column_name is null /*    If column name not supplied, match all */
  4315.         select @column_name = '%'
  4316.     if @procedure_qualifier is not null
  4317.     begin
  4318.         if db_name() <> @procedure_qualifier
  4319.         begin
  4320.             if @procedure_qualifier = ''
  4321.             begin
  4322.                 /* in this case, we need to return an empty result set */
  4323.                 /* because the user has requested a database with an empty name */
  4324.                 select @procedure_name = ''
  4325.                 select @procedure_owner = ''
  4326.             end
  4327.             else
  4328.             begin    /* If qualifier doesn't match current database */
  4329.                 raiserror 20001 '~~Rush_51~~'
  4330.                 return
  4331.             end
  4332.         end
  4333.     end
  4334.  
  4335.     if @procedure_name is null
  4336.     begin    /*    If procedure name not supplied, match all */
  4337.         select @procedure_name = '%'
  4338.     end
  4339.  
  4340.     /* first we need to extract the procedure group number, if one exists */
  4341.     select @semi_position = charindex(';',@procedure_name)
  4342.     if (@semi_position > 0)
  4343.     begin    /* If group number separator (;) found */
  4344.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4345.         select @group_num_upper = @group_num_lower
  4346.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4347.     end
  4348.     else
  4349.     begin    /* No group separator, so default to group number of 1 */
  4350.         select @group_num_lower = 1
  4351.         select @group_num_upper = 32767            
  4352.     end
  4353.  
  4354.     if @procedure_owner is null
  4355.     begin    /* If unqualified procedure name */
  4356.         SELECT @full_procedure_name = @procedure_name
  4357.     end
  4358.     else
  4359.     begin    /* Qualified procedure name */
  4360.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4361.     end
  4362.  
  4363.     /*    Get Object ID */
  4364.     SELECT @procedure_id = object_id(@full_procedure_name)
  4365.     if ((charindex('%',@full_procedure_name) = 0) and
  4366.         (charindex('_',@full_procedure_name) = 0) and
  4367.         @procedure_id <> 0)
  4368.     begin
  4369.         /* this block is for the case where there is no pattern
  4370.             matching required for the procedure name */
  4371.         SELECT
  4372.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4373.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4374.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4375.             COLUMN_NAME = convert(varchar(32),c.name),
  4376.             COLUMN_TYPE = convert(smallint, 0),
  4377.             d.DATA_TYPE,
  4378.             TYPE_NAME = t.name,
  4379.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4380.             LENGTH = isnull(d.length, convert(int,c.length)),
  4381.             SCALE = d.numeric_scale,
  4382.             d.RADIX,
  4383.             d.NULLABLE,
  4384.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4385.             COLUMN_DEF = convert(varchar(255),null),
  4386.             d.SQL_DATA_TYPE,
  4387.             d.SQL_DATETIME_SUB,
  4388.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4389.             ORDINAL_POSITION = convert(int,c.colid),
  4390.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4391.             SS_DATA_TYPE = c.type
  4392.         FROM
  4393.             syscolumns c,
  4394.             sysobjects o,
  4395.             master.dbo.spt_datatype_info d,
  4396.             systypes t
  4397.         WHERE
  4398.             o.id = @procedure_id
  4399.             AND c.id = o.id
  4400.             AND t.type = d.ss_dtype
  4401.             AND c.length = isnull(d.fixlen, c.length)
  4402.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4403.             AND c.usertype = t.usertype
  4404.             AND c.name like @column_name
  4405.             AND c.number between @group_num_lower and @group_num_upper
  4406.         UNION ALL
  4407.         SELECT           /* return value row*/
  4408.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4409.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4410.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4411.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4412.             COLUMN_TYPE = convert(smallint, 5),
  4413.             DATA_TYPE = convert(smallint, 4),
  4414.             TYPE_NAME = convert(varchar(32),'int'),
  4415.             "PRECISION" = convert(int,10),
  4416.             LENGTH = convert(int,4),
  4417.             SCALE = convert(smallint,0),
  4418.             RADIX = convert(smallint,10),
  4419.             NULLABLE = convert(smallint,0),
  4420.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4421.             COLUMN_DEF = convert(varchar(255),NULL),
  4422.             SQL_DATA_TYPE = convert(smallint, 4),
  4423.             SQL_DATETIME_SUB = convert(smallint,null),
  4424.             CHAR_OCTET_LENGTH = convert(int,null),
  4425.             ORDINAL_POSITION = convert(int,0),
  4426.             IS_NULLABLE = convert(varchar(254),'NO'),
  4427.             SS_DATA_TYPE = convert(tinyint,56)
  4428.         FROM
  4429.             syscomments c, sysobjects o
  4430.         WHERE
  4431.             o.id = @procedure_id
  4432.             AND c.id = o.id
  4433.             AND c.colid = 1
  4434.             AND o.type = 'P'                        /* Just Procedures */
  4435.             AND 'RETURN_VALUE' like @column_name
  4436.             AND c.number between @group_num_lower and @group_num_upper        
  4437.         ORDER BY 1, 2, 3, 18
  4438.     end
  4439.     else
  4440.     begin
  4441.         /* this block is for the case where there IS pattern
  4442.             matching done on the procedure name */
  4443.         if @procedure_owner is null
  4444.             select @procedure_owner = '%'
  4445.         SELECT
  4446.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4447.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4448.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4449.             COLUMN_NAME = convert(varchar(32),c.name),
  4450.             COLUMN_TYPE = convert(smallint, 0),
  4451.             d.DATA_TYPE,
  4452.             TYPE_NAME = t.name,
  4453.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4454.             LENGTH = isnull(d.length, convert(int,c.length)),
  4455.             SCALE = d.numeric_scale,
  4456.             d.RADIX,
  4457.             d.NULLABLE,
  4458.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4459.             COLUMN_DEF = convert(varchar(255),null),
  4460.             d.SQL_DATA_TYPE,
  4461.             d.SQL_DATETIME_SUB,
  4462.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4463.             ORDINAL_POSITION = convert(int,c.colid),
  4464.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4465.             SS_DATA_TYPE = c.type
  4466.         FROM
  4467.             syscolumns c,
  4468.             sysobjects o,
  4469.             master.dbo.spt_datatype_info d,
  4470.             systypes t
  4471.         WHERE
  4472.             o.name like @procedure_name
  4473.             AND user_name(o.uid) like @procedure_owner
  4474.             AND o.id = c.id
  4475.             AND t.type = d.ss_dtype
  4476.             AND c.length = isnull(d.fixlen, c.length)
  4477.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4478.             AND c.usertype = t.usertype
  4479.             AND o.type = 'P'                            /* Just Procedures */
  4480.             AND c.name like @column_name
  4481.             AND c.number between @group_num_lower and @group_num_upper
  4482.         UNION ALL
  4483.         SELECT           /* return value row*/
  4484.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4485.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4486.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4487.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4488.             COLUMN_TYPE = convert(smallint, 5),
  4489.             DATA_TYPE = convert(smallint, 4),
  4490.             TYPE_NAME = convert(varchar(32),'int'),
  4491.             "PRECISION" = convert(int,10),
  4492.             LENGTH = convert(int,4),
  4493.             SCALE = convert(smallint,0),
  4494.             RADIX = convert(smallint,10),
  4495.             NULLABLE = convert(smallint,0),
  4496.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4497.             COLUMN_DEF = convert(varchar(255),NULL),
  4498.             SQL_DATA_TYPE = convert(smallint, 4),
  4499.             SQL_DATETIME_SUB = convert(smallint,null),
  4500.             CHAR_OCTET_LENGTH = convert(int,null),
  4501.             ORDINAL_POSITION = convert(int,0),
  4502.             IS_NULLABLE = convert(varchar(254),'NO'),
  4503.             SS_DATA_TYPE = convert(tinyint,56)
  4504.         FROM
  4505.             syscomments c, sysobjects o
  4506.         WHERE
  4507.             o.name like @procedure_name
  4508.             AND user_name(o.uid) like @procedure_owner
  4509.             AND c.id = o.id
  4510.             AND c.colid = 1
  4511.             AND o.type = 'P'                        /* Just Procedures */
  4512.             AND 'RETURN_VALUE' like @column_name
  4513.             AND c.number between @group_num_lower and @group_num_upper
  4514.         ORDER BY 1, 2, 3, 18
  4515.     end
  4516. go
  4517.  
  4518. if (charindex('6.00', @@version) = 0 and
  4519.     charindex('6.50', @@version) = 0 and
  4520.     charindex('7.00', @@version) = 0)
  4521. begin
  4522.     print ''
  4523.     print ''
  4524.     print 'Warning:'
  4525.     print 'you are installing the stored procedures '
  4526.     print 'on a pre 6.0 SQL Server.'
  4527.     print 'Ignore the following error.'
  4528. end
  4529. else
  4530.     drop proc sp_sproc_columns
  4531. go
  4532.  
  4533. /*    Procedure for 6.0 and 6.50 servers */
  4534. CREATE PROCEDURE sp_sproc_columns (
  4535.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  4536.                  @procedure_owner        varchar(96) = null,
  4537.                  @procedure_qualifier    varchar(32) = null,
  4538.                  @column_name            varchar(96) = null,
  4539.                  @ODBCVer                int = 2)
  4540. AS
  4541.     DECLARE @group_num_lower smallint
  4542.     DECLARE @group_num_upper smallint
  4543.     DECLARE @semi_position int
  4544.     DECLARE @full_procedure_name    varchar(205)
  4545.     DECLARE @procedure_id int
  4546.  
  4547.     if @column_name is null /*    If column name not supplied, match all */
  4548.         select @column_name = '%'
  4549.     if @procedure_qualifier is not null
  4550.     begin
  4551.         if db_name() <> @procedure_qualifier
  4552.         begin
  4553.             if @procedure_qualifier = ''
  4554.             begin
  4555.                 /* in this case, we need to return an empty result set */
  4556.                 /* because the user has requested a database with an empty name */
  4557.                 select @procedure_name = ''
  4558.                 select @procedure_owner = ''
  4559.             end
  4560.             else
  4561.             begin    /* If qualifier doesn't match current database */
  4562.                 raiserror (15250, -1,-1)
  4563.                 return
  4564.             end
  4565.         end
  4566.     end
  4567.  
  4568.     if @procedure_name is null
  4569.     begin    /*    If procedure name not supplied, match all */
  4570.         select @procedure_name = '%'
  4571.     end
  4572.  
  4573.     /* first we need to extract the procedure group number, if one exists */
  4574.     select @semi_position = charindex(';',@procedure_name)
  4575.     if (@semi_position > 0)
  4576.     begin    /* If group number separator (;) found */
  4577.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4578.         select @group_num_upper = @group_num_lower
  4579.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4580.     end
  4581.     else
  4582.     begin    /* No group separator, so default to group number of 1 */
  4583.         select @group_num_lower = 1
  4584.         select @group_num_upper = 32767            
  4585.     end
  4586.  
  4587.     if @procedure_owner is null
  4588.     begin    /* If unqualified procedure name */
  4589.         SELECT @full_procedure_name = @procedure_name
  4590.     end
  4591.     else
  4592.     begin    /* Qualified procedure name */
  4593.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4594.     end
  4595.  
  4596.     /*    Get Object ID */
  4597.     SELECT @procedure_id = object_id(@full_procedure_name)
  4598.     if ((charindex('%',@full_procedure_name) = 0) and
  4599.         (charindex('[',@full_procedure_name) = 0) and
  4600.         (charindex('_',@full_procedure_name) = 0) and
  4601.         @procedure_id <> 0)
  4602.     begin
  4603.         /* this block is for the case where there is no pattern
  4604.             matching required for the procedure name */
  4605.         SELECT
  4606.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4607.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4608.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4609.             COLUMN_NAME = convert(varchar(32),c.name),
  4610.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4611.             d.DATA_TYPE,
  4612.             TYPE_NAME = t.name,
  4613.             convert(int,case
  4614.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4615.                 else isnull(convert(int,c.prec), 2147483647)
  4616.             end) "PRECISION",
  4617.             convert(int,case
  4618.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4619.                     c.prec+2
  4620.                 else
  4621.                     isnull(d.length, c.length)
  4622.             end) LENGTH,
  4623.             SCALE = convert(smallint, c.scale),
  4624.             d.RADIX,
  4625.             d.NULLABLE,
  4626.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4627.             COLUMN_DEF = convert(varchar(255),NULL),
  4628.             d.SQL_DATA_TYPE,
  4629.             d.SQL_DATETIME_SUB,
  4630.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4631.             ORDINAL_POSITION = convert(int,c.colid),
  4632.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4633.             SS_DATA_TYPE = c.type
  4634.         FROM
  4635.             syscolumns c,
  4636.             sysobjects o,
  4637.             master.dbo.spt_datatype_info d,
  4638.             systypes t
  4639.         WHERE
  4640.             o.id = @procedure_id
  4641.             AND c.id = o.id
  4642.             AND c.type = d.ss_dtype
  4643.             AND c.length = isnull(d.fixlen, c.length)
  4644.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4645.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4646.             AND c.usertype = t.usertype
  4647.             AND c.name like @column_name
  4648.             AND c.number between @group_num_lower and @group_num_upper
  4649.         UNION ALL
  4650.         SELECT           /* return value row*/
  4651.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4652.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4653.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4654.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4655.             COLUMN_TYPE = convert(smallint, 5),
  4656.             DATA_TYPE = convert(smallint, 4),
  4657.             TYPE_NAME = convert(varchar(32),'int'),
  4658.             "PRECISION" = convert(int,10),
  4659.             LENGTH = convert(int,4),
  4660.             SCALE = convert(smallint,0),
  4661.             RADIX = convert(smallint,10),
  4662.             NULLABLE = convert(smallint,0),
  4663.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4664.             COLUMN_DEF = convert(varchar(255),NULL),
  4665.             SQL_DATA_TYPE = convert(smallint, 4),
  4666.             SQL_DATETIME_SUB = convert(smallint,null),
  4667.             CHAR_OCTET_LENGTH = convert(int,null),
  4668.             ORDINAL_POSITION = convert(int,0),
  4669.             IS_NULLABLE = convert(varchar(254),'NO'),
  4670.             SS_DATA_TYPE = convert(tinyint,56)
  4671.         FROM
  4672.             syscomments c, sysobjects o
  4673.         WHERE
  4674.             o.id = @procedure_id
  4675.             AND c.id = o.id
  4676.             AND c.colid = 1
  4677.             AND o.type = 'P'                        /* Just Procedures */
  4678.             AND 'RETURN_VALUE' like @column_name
  4679.             AND c.number between @group_num_lower and @group_num_upper
  4680.         ORDER BY 1, 2, 3, 18
  4681.     end
  4682.     else
  4683.     begin
  4684.         /* this block is for the case where there IS pattern
  4685.             matching done on the procedure name */
  4686.         if @procedure_owner is null
  4687.             select @procedure_owner = '%'
  4688.         SELECT
  4689.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4690.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4691.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4692.             COLUMN_NAME = convert(varchar(32),c.name),
  4693.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4694.             d.DATA_TYPE,
  4695.             TYPE_NAME = t.name,
  4696.             convert(int,case
  4697.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4698.                 else isnull(convert(int,c.prec), 2147483647)
  4699.             end) "PRECISION",
  4700.             convert(int,case
  4701.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4702.                     c.prec+2
  4703.                 else
  4704.                     isnull(d.length, c.length)
  4705.             end) LENGTH,
  4706.             SCALE = convert(smallint, c.scale),
  4707.             d.RADIX,
  4708.             d.NULLABLE,
  4709.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4710.             COLUMN_DEF = convert(varchar(255),NULL),
  4711.             d.SQL_DATA_TYPE,
  4712.             d.SQL_DATETIME_SUB,
  4713.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4714.             ORDINAL_POSITION = convert(int,c.colid),
  4715.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4716.             SS_DATA_TYPE = c.type
  4717.         FROM
  4718.             syscolumns c,
  4719.             sysobjects o,
  4720.             master.dbo.spt_datatype_info d,
  4721.             systypes t
  4722.         WHERE
  4723.             o.name like @procedure_name
  4724.             AND user_name(o.uid) like @procedure_owner
  4725.             AND o.id = c.id
  4726.             AND c.type = d.ss_dtype
  4727.             AND c.length = isnull(d.fixlen, c.length)
  4728.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4729.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4730.             AND c.usertype = t.usertype
  4731.             AND o.type = 'P'                            /* Just Procedures */
  4732.             AND c.name like @column_name
  4733.             AND c.number between @group_num_lower and @group_num_upper
  4734.         UNION ALL
  4735.         SELECT           /* return value row*/
  4736.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4737.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4738.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4739.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4740.             COLUMN_TYPE = convert(smallint, 5),
  4741.             DATA_TYPE = convert(smallint, 4),
  4742.             TYPE_NAME = convert(varchar(32),'int'),
  4743.             "PRECISION" = convert(int,10),
  4744.             LENGTH = convert(int,4),
  4745.             SCALE = convert(smallint,0),
  4746.             RADIX = convert(smallint,10),
  4747.             NULLABLE = convert(smallint,0),
  4748.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4749.             COLUMN_DEF = convert(varchar(255),NULL),
  4750.             SQL_DATA_TYPE = convert(smallint, 4),
  4751.             SQL_DATETIME_SUB = convert(smallint,null),
  4752.             CHAR_OCTET_LENGTH = convert(int,null),
  4753.             ORDINAL_POSITION = convert(int,0),
  4754.             IS_NULLABLE = convert(varchar(254),'NO'),
  4755.             SS_DATA_TYPE = convert(tinyint,56)
  4756.         FROM
  4757.             syscomments c, sysobjects o
  4758.         WHERE
  4759.             o.name like @procedure_name
  4760.             AND user_name(o.uid) like @procedure_owner
  4761.             AND c.id = o.id
  4762.             AND c.colid = 1
  4763.             AND o.type = 'P'                        /* Just Procedures */
  4764.             AND 'RETURN_VALUE' like @column_name
  4765.             AND c.number between @group_num_lower and @group_num_upper
  4766.         ORDER BY 1, 2, 3, 18
  4767.     end
  4768. go
  4769.  
  4770. if (charindex('7.00', @@version) = 0)
  4771. begin
  4772.     print ''
  4773.     print ''
  4774.     print 'Warning:'
  4775.     print 'you are installing the stored procedures '
  4776.     print 'on a pre 7.0 SQL Server.'
  4777.     print 'Ignore the following errors.'
  4778. end
  4779. else
  4780.     drop proc sp_sproc_columns
  4781. go
  4782.  
  4783. /*    Procedure for 7.0 server */
  4784. CREATE PROCEDURE sp_sproc_columns (
  4785.                  @procedure_name        nvarchar(390) = '%',
  4786.                  @procedure_owner        nvarchar(384) = null,
  4787.                  @procedure_qualifier    sysname = null,
  4788.                  @column_name            nvarchar(384) = null,
  4789.                  @ODBCVer                int = 2)
  4790. AS
  4791.     DECLARE @group_num_lower smallint
  4792.     DECLARE @group_num_upper smallint
  4793.     DECLARE @semi_position int
  4794.     DECLARE @full_procedure_name    nvarchar(774)
  4795.     DECLARE @procedure_id int
  4796.  
  4797.     if @column_name is null /*    If column name not supplied, match all */
  4798.         select @column_name = '%'
  4799.     if @procedure_qualifier is not null
  4800.     begin
  4801.         if db_name() <> @procedure_qualifier
  4802.         begin
  4803.             if @procedure_qualifier = ''
  4804.             begin
  4805.                 /* in this case, we need to return an empty result set */
  4806.                 /* because the user has requested a database with an empty name */
  4807.                 select @procedure_name = ''
  4808.                 select @procedure_owner = ''
  4809.             end
  4810.             else
  4811.             begin    /* If qualifier doesn't match current database */
  4812.                 raiserror (15250, -1,-1)
  4813.                 return
  4814.             end
  4815.         end
  4816.     end
  4817.  
  4818.     if @procedure_name is null
  4819.     begin    /*    If procedure name not supplied, match all */
  4820.         select @procedure_name = '%'
  4821.     end
  4822.  
  4823.     /* first we need to extract the procedure group number, if one exists */
  4824.     select @semi_position = charindex(';',@procedure_name)
  4825.     if (@semi_position > 0)
  4826.     begin    /* If group number separator (;) found */
  4827.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4828.         select @group_num_upper = @group_num_lower
  4829.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4830.     end
  4831.     else
  4832.     begin    /* No group separator, so default to all groups */
  4833.         select @group_num_lower = 1
  4834.         select @group_num_upper = 32767            
  4835.     end
  4836.  
  4837.     if @procedure_owner is null
  4838.     begin    /* If unqualified procedure name */
  4839.         SELECT @full_procedure_name = quotename(@procedure_name)
  4840.     end
  4841.     else
  4842.     begin    /* Qualified procedure name */
  4843.         if @procedure_owner = ''
  4844.         begin    /* If empty owner name */
  4845.             SELECT @full_procedure_name = quotename(@procedure_owner)
  4846.         end
  4847.         else
  4848.         begin
  4849.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  4850.                 '.' + quotename(@procedure_name)
  4851.         end
  4852.     end
  4853.  
  4854.     /*    Get Object ID */
  4855.     SELECT @procedure_id = object_id(@full_procedure_name)
  4856.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  4857.         (isnull(charindex('[', @procedure_name),0) = 0) and
  4858.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  4859.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  4860.         not (@procedure_id is null))
  4861.     begin
  4862.         /* this block is for the case where there is no pattern
  4863.             matching required for the procedure name */
  4864.         SELECT
  4865.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4866.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4867.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4868.             COLUMN_NAME = convert(sysname,c.name),
  4869.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  4870.             d.DATA_TYPE,
  4871.             TYPE_NAME = t.name,
  4872.             convert(int,case
  4873.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4874.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  4875.             end) "PRECISION",
  4876.             convert(int,case
  4877.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4878.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  4879.                 else
  4880.                     isnull(d.length, c.length)
  4881.             end) LENGTH,
  4882.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4883.             d.RADIX,
  4884.             d.NULLABLE,
  4885.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4886.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4887.             d.SQL_DATA_TYPE,
  4888.             d.SQL_DATETIME_SUB,
  4889.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  4890.             ORDINAL_POSITION = convert(int,c.colid),
  4891.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4892.             SS_DATA_TYPE = c.type
  4893.         FROM
  4894.             syscolumns c,
  4895.             sysobjects o,
  4896.             master.dbo.spt_datatype_info d,
  4897.             systypes t
  4898.         WHERE
  4899.             o.id = @procedure_id
  4900.             AND c.id = o.id
  4901.             AND c.xtype = d.ss_dtype
  4902.             AND c.length = isnull(d.fixlen, c.length)
  4903.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4904.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4905.             AND c.xusertype = t.xusertype
  4906.             AND c.name like @column_name
  4907.             AND c.number between @group_num_lower and @group_num_upper
  4908.         UNION ALL
  4909.         SELECT           /* return value row*/
  4910.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4911.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4912.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4913.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  4914.             COLUMN_TYPE = convert(smallint, 5),
  4915.             DATA_TYPE = convert(smallint, 4),
  4916.             TYPE_NAME = convert(sysname,'int'),
  4917.             "PRECISION" = convert(int,10),
  4918.             LENGTH = convert(int,4),
  4919.             SCALE = convert(smallint,0),
  4920.             RADIX = convert(smallint,10),
  4921.             NULLABLE = convert(smallint,0),
  4922.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4923.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4924.             SQL_DATA_TYPE = convert(smallint, 4),
  4925.             SQL_DATETIME_SUB = convert(smallint,null),
  4926.             CHAR_OCTET_LENGTH = convert(int,null),
  4927.             ORDINAL_POSITION = convert(int,0),
  4928.             IS_NULLABLE = convert(varchar(254),'NO'),
  4929.             SS_DATA_TYPE = convert(tinyint,56)
  4930.         FROM
  4931.             syscomments c, sysobjects o
  4932.         WHERE
  4933.             o.id = @procedure_id
  4934.             AND c.id = o.id
  4935.             AND c.colid = 1
  4936.             AND o.type = 'P'                        /* Just Procedures */
  4937.             AND 'RETURN_VALUE' like @column_name
  4938.             AND c.number between @group_num_lower and @group_num_upper        
  4939.         ORDER BY 1, 2, 3, 18
  4940.     end
  4941.     else
  4942.     begin
  4943.         /* this block is for the case where there IS pattern
  4944.             matching done on the procedure name */
  4945.         if @procedure_owner is null
  4946.             select @procedure_owner = '%'
  4947.         SELECT
  4948.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4949.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4950.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4951.             COLUMN_NAME = convert(sysname,c.name),
  4952.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  4953.             d.DATA_TYPE,
  4954.             TYPE_NAME = t.name,
  4955.             convert(int,case
  4956.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4957.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  4958.             end) "PRECISION",
  4959.             convert(int,case
  4960.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4961.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  4962.                 else
  4963.                     isnull(d.length, c.length)
  4964.             end) LENGTH,
  4965.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4966.             d.RADIX,
  4967.             d.NULLABLE,
  4968.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4969.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4970.             d.SQL_DATA_TYPE,
  4971.             d.SQL_DATETIME_SUB,
  4972.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  4973.             ORDINAL_POSITION = convert(int,c.colid),
  4974.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4975.             SS_DATA_TYPE = c.type
  4976.         FROM
  4977.             syscolumns c,
  4978.             sysobjects o,
  4979.             master.dbo.spt_datatype_info d,
  4980.             systypes t
  4981.         WHERE
  4982.             o.name like @procedure_name
  4983.             AND user_name(o.uid) like @procedure_owner
  4984.             AND o.id = c.id
  4985.             AND c.xtype = d.ss_dtype
  4986.             AND c.length = isnull(d.fixlen, c.length)
  4987.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4988.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4989.             AND c.xusertype = t.xusertype
  4990.             AND o.type = 'P'                            /* Just Procedures */
  4991.             AND c.name like @column_name
  4992.             AND c.number between @group_num_lower and @group_num_upper
  4993.         UNION ALL
  4994.         SELECT           /* return value row*/
  4995.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4996.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4997.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4998.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  4999.             COLUMN_TYPE = convert(smallint, 5),
  5000.             DATA_TYPE = convert(smallint, 4),
  5001.             TYPE_NAME = convert(sysname,'int'),
  5002.             "PRECISION" = convert(int,10),
  5003.             LENGTH = convert(int,4),
  5004.             SCALE = convert(smallint,0),
  5005.             RADIX = convert(smallint,10),
  5006.             NULLABLE = convert(smallint,0),
  5007.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5008.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5009.             SQL_DATA_TYPE = convert(smallint, 4),
  5010.             SQL_DATETIME_SUB = convert(smallint,null),
  5011.             CHAR_OCTET_LENGTH = convert(int,null),
  5012.             ORDINAL_POSITION = convert(int,0),
  5013.             IS_NULLABLE = convert(varchar(254),'NO'),
  5014.             SS_DATA_TYPE = convert(tinyint,56)
  5015.         FROM
  5016.             syscomments c, sysobjects o
  5017.         WHERE
  5018.             o.name like @procedure_name
  5019.             AND c.id = o.id
  5020.             AND user_name(o.uid) like @procedure_owner
  5021.             AND c.colid = 1
  5022.             AND o.type = 'P'                        /* Just Procedures */
  5023.             AND 'RETURN_VALUE' like @column_name
  5024.             AND c.number between @group_num_lower and @group_num_upper
  5025.         ORDER BY 1, 2, 3, 18
  5026.     end
  5027. go
  5028.  
  5029. grant execute on sp_sproc_columns to public
  5030. go
  5031.  
  5032. dump tran master with no_log
  5033. go
  5034.  
  5035. print 'creating sp_statistics'
  5036. go
  5037.  
  5038. /*    Procedure for pre-7.0 server */
  5039. CREATE PROCEDURE sp_statistics (
  5040.                  @table_name        varchar(32),
  5041.                  @table_owner        varchar(32) = null,
  5042.                  @table_qualifier    varchar(32) = null,
  5043.                  @index_name        varchar(32) = '%',
  5044.                  @is_unique         char(1) = 'N',
  5045.                  @accuracy            char(1) = 'Q')
  5046. AS
  5047.     set nocount on
  5048.     DECLARE @indid                int
  5049.     DECLARE @lastindid            int
  5050.     DECLARE @table_id            int
  5051.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  5052.  
  5053.     create table #TmpIndex(
  5054.         TABLE_QUALIFIER varchar(32) NULL,
  5055.         TABLE_OWNER     varchar(32) NULL,
  5056.         TABLE_NAME        varchar(32) NOT NULL,
  5057.         INDEX_QUALIFIER varchar(32) null,
  5058.         INDEX_NAME        varchar(32) null,
  5059.         NON_UNIQUE        smallint null,
  5060.         TYPE            smallint NOT NULL,
  5061.         SEQ_IN_INDEX    smallint null,
  5062.         COLUMN_NAME     varchar(32) null,
  5063.         COLLATION        char(1) null,
  5064.         index_id        int null,
  5065.         CARDINALITY     int null,
  5066.         PAGES            int null,
  5067.         status            smallint NOT NULL)
  5068.  
  5069.     if @table_qualifier is not null
  5070.     begin
  5071.         if db_name() <> @table_qualifier
  5072.         begin    /* If qualifier doesn't match current database */
  5073.             raiserror 20001 '~~Rush_5~~'
  5074.             return
  5075.         end
  5076.     end
  5077.  
  5078.     if @accuracy not in ('Q','E')
  5079.         begin
  5080.             raiserror 20002 '~~Rush_58~~'
  5081.             return
  5082.         end
  5083.  
  5084.     if (@@trancount <> 0 and
  5085.         charindex('6.50', @@version) = 0 and
  5086.         charindex('7.00', @@version) = 0)
  5087.     begin    /* If inside a transaction */
  5088.         raiserror 20003 '~~Rush_59~~'
  5089.         return
  5090.     end
  5091.  
  5092.     if @table_owner is null
  5093.     begin    /* If unqualified table name */
  5094.         SELECT @full_table_name = @table_name
  5095.     end
  5096.     else
  5097.     begin    /* Qualified table name */
  5098.         if @table_owner = ''
  5099.         begin    /* If empty owner name */
  5100.             SELECT @full_table_name = @table_owner
  5101.         end
  5102.         else
  5103.         begin
  5104.             SELECT @full_table_name = @table_owner + '.' + @table_name
  5105.         end
  5106.     end
  5107.     /*    Get Object ID */
  5108.     SELECT @table_id = object_id(@full_table_name)
  5109.  
  5110.     /*    Start at lowest index id */
  5111.     SELECT @indid = min(indid)
  5112.     FROM sysindexes
  5113.     WHERE id = @table_id
  5114.         AND indid > 0
  5115.         AND indid < 255
  5116.  
  5117.     WHILE @indid is not NULL
  5118.     BEGIN
  5119.         INSERT #TmpIndex    /* Add all columns that are in index */
  5120.             SELECT
  5121.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5122.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5123.                 o.name,                                 /* TABLE_NAME       */
  5124.                 o.name,                                 /* INDEX_QUALIFIER */
  5125.                 x.name,                                 /* INDEX_NAME       */
  5126.                 0,                                        /* NON_UNIQUE       */
  5127.                 1,                                        /* SQL_INDEX_CLUSTERED */
  5128.                 colid,                                    /* SEQ_IN_INDEX    */
  5129.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5130.                 'A',                                    /* COLLATION       */
  5131.                 @indid,                                 /* index_id        */
  5132.                 x.rows,                                 /* CARDINALITY       */
  5133.                 x.dpages,                                /* PAGES           */
  5134.                 x.status                                /* status            */
  5135.             FROM sysindexes x, syscolumns c, sysobjects o
  5136.             WHERE
  5137.                 x.id = @table_id
  5138.                 AND x.id = o.id
  5139.                 AND x.id = c.id
  5140.                 AND c.colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5141.                 AND x.indid = @indid
  5142.         /*
  5143.         **      Now move @indid to the next index.
  5144.         */
  5145.         SELECT @lastindid = @indid
  5146.         SELECT @indid = NULL
  5147.  
  5148.         SELECT @indid = min(indid)
  5149.         FROM sysindexes
  5150.         WHERE id = @table_id
  5151.             AND indid > @lastindid
  5152.             AND indid < 255
  5153.     END
  5154.  
  5155.     UPDATE #TmpIndex
  5156.         SET NON_UNIQUE = 1
  5157.         WHERE status&2 <> 2 /* If non-unique index */
  5158.     UPDATE #TmpIndex
  5159.         SET
  5160.             TYPE = 3,            /* SQL_INDEX_OTHER */
  5161.             CARDINALITY = NULL,
  5162.             PAGES = NULL
  5163.         WHERE index_id > 1    /* If non-clustered index */
  5164.  
  5165.     /* now add row for table statistics */
  5166.     INSERT #TmpIndex
  5167.         SELECT
  5168.             DB_NAME(),                /* TABLE_QUALIFIER */
  5169.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5170.             o.name,                 /* TABLE_NAME       */
  5171.             null,                    /* INDEX_QUALIFIER */
  5172.             null,                    /* INDEX_NAME       */
  5173.             null,                    /* NON_UNIQUE       */
  5174.             0,                        /* SQL_TABLE_STAT  */
  5175.             null,                    /* SEQ_IN_INDEX    */
  5176.             null,                    /* COLUMN_NAME       */
  5177.             null,                    /* COLLATION       */
  5178.             0,                        /* index_id        */
  5179.             x.rows,                 /* CARDINALITY       */
  5180.             x.dpages,                /* PAGES           */
  5181.             0                        /* status           */
  5182.         FROM sysindexes x, sysobjects o
  5183.         WHERE o.id = @table_id
  5184.             AND x.id = o.id
  5185.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5186.                                                 /*    then table stats are in */
  5187.                                                 /*    a row with indid =0        */
  5188.  
  5189.     if @is_unique <> 'Y'    /* If all indexes desired */
  5190.         SELECT
  5191.             TABLE_QUALIFIER,
  5192.             TABLE_OWNER,
  5193.             TABLE_NAME,
  5194.             NON_UNIQUE,
  5195.             INDEX_QUALIFIER,
  5196.             INDEX_NAME,
  5197.             TYPE,
  5198.             SEQ_IN_INDEX,
  5199.             COLUMN_NAME,
  5200.             COLLATION,
  5201.             CARDINALITY,
  5202.             PAGES,
  5203.             FILTER_CONDITION = convert(varchar(128),null)
  5204.         FROM #TmpIndex
  5205.         WHERE
  5206.             INDEX_NAME like @index_name /* If matching name */
  5207.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5208.         ORDER BY 4, 7, 6, 8
  5209.     else                    /* If only unique indexes desired */
  5210.         SELECT
  5211.             TABLE_QUALIFIER,
  5212.             TABLE_OWNER,
  5213.             TABLE_NAME,
  5214.             NON_UNIQUE,
  5215.             INDEX_QUALIFIER,
  5216.             INDEX_NAME,
  5217.             TYPE,
  5218.             SEQ_IN_INDEX,
  5219.             COLUMN_NAME,
  5220.             COLLATION,
  5221.             CARDINALITY,
  5222.             PAGES,
  5223.             FILTER_CONDITION = convert(varchar(128),null)
  5224.         FROM #TmpIndex
  5225.         WHERE
  5226.             (NON_UNIQUE = 0             /* If unique */
  5227.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  5228.             and (INDEX_NAME like @index_name    /* If matching name */
  5229.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  5230.         ORDER BY 4, 7, 6, 8
  5231.  
  5232.     DROP TABLE #TmpIndex
  5233. go
  5234.  
  5235. if (charindex('7.00', @@version) = 0)
  5236. begin
  5237.     print ''
  5238.     print ''
  5239.     print 'Warning:'
  5240.     print 'you are installing the stored procedures '
  5241.     print 'on a pre 7.0 SQL Server.'
  5242.     print 'Ignore the following errors.'
  5243. end
  5244. else
  5245.     drop proc sp_statistics
  5246. go
  5247.  
  5248. /*    Procedure for 7.0 server */
  5249. CREATE PROCEDURE sp_statistics (
  5250.                  @table_name        sysname,
  5251.                  @table_owner        sysname = null,
  5252.                  @table_qualifier    sysname = null,
  5253.                  @index_name        sysname = '%',
  5254.                  @is_unique         char(1) = 'N',
  5255.                  @accuracy            char(1) = 'Q')
  5256. AS
  5257.     set nocount on
  5258.     DECLARE @indid                int
  5259.     DECLARE @lastindid            int
  5260.     DECLARE @table_id            int
  5261.     DECLARE @full_table_name    nvarchar(257)
  5262.  
  5263.     create table #TmpIndex(
  5264.         TABLE_QUALIFIER sysname NULL,
  5265.         TABLE_OWNER     sysname NULL,
  5266.         TABLE_NAME        sysname NOT NULL,
  5267.         INDEX_QUALIFIER sysname null,
  5268.         INDEX_NAME        sysname null,
  5269.         NON_UNIQUE        smallint null,
  5270.         TYPE            smallint NOT NULL,
  5271.         SEQ_IN_INDEX    smallint null,
  5272.         COLUMN_NAME     sysname null,
  5273.         COLLATION        char(1) null,
  5274.         index_id        int null,
  5275.         CARDINALITY     int null,
  5276.         PAGES            int null,
  5277.         status            int NOT NULL)
  5278.  
  5279.     if @table_qualifier is not null
  5280.     begin
  5281.         if db_name() <> @table_qualifier
  5282.         begin    /* If qualifier doesn't match current database */
  5283.             raiserror (15250, -1,-1)
  5284.             return
  5285.         end
  5286.     end
  5287.  
  5288.     if @accuracy not in ('Q','E')
  5289.         begin
  5290.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  5291.             return
  5292.         end
  5293.  
  5294.     if @table_owner is null
  5295.     begin    /* If unqualified table name */
  5296.         SELECT @full_table_name = quotename(@table_name)
  5297.     end
  5298.     else
  5299.     begin    /* Qualified table name */
  5300.         if @table_owner = ''
  5301.         begin    /* If empty owner name */
  5302.             SELECT @full_table_name = quotename(@table_owner)
  5303.         end
  5304.         else
  5305.         begin
  5306.             SELECT @full_table_name = quotename(@table_owner) +
  5307.                 '.' + quotename(@table_name)
  5308.         end
  5309.     end
  5310.     /*    Get Object ID */
  5311.     SELECT @table_id = object_id(@full_table_name)
  5312.  
  5313.     /*    Start at lowest index id */
  5314.     SELECT @indid = min(indid)
  5315.     FROM sysindexes
  5316.     WHERE not (@table_id is null)
  5317.         AND id = @table_id
  5318.         AND indid > 0
  5319.         AND indid < 255
  5320.  
  5321.     WHILE @indid is not NULL
  5322.     BEGIN
  5323.         INSERT #TmpIndex    /* Add all columns that are in index */
  5324.             SELECT
  5325.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5326.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5327.                 o.name,                                 /* TABLE_NAME       */
  5328.                 o.name,                                 /* INDEX_QUALIFIER */
  5329.                 x.name,                                 /* INDEX_NAME       */
  5330.                 case                                    /* NON_UNIQUE       */
  5331.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  5332.                     else 0                                /* Unique index    */
  5333.                 end,
  5334.                 case                                    /* TYPE            */
  5335.                     when @indid > 1 then 3                /* Non-Clustered   */
  5336.                     else 1                                /* Clustered index */
  5337.                 end,
  5338.                 colid,                                    /* SEQ_IN_INDEX    */
  5339.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5340.                 'A',                                    /* COLLATION       */
  5341.                 @indid,                                 /* index_id        */
  5342.                 case                                    /* CARDINALITY       */
  5343.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5344.                     else x.rows                         /* Clustered index */
  5345.                 end,
  5346.                 case                                    /* PAGES           */
  5347.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5348.                     else x.dpages                        /* Clustered index */
  5349.                 end,
  5350.                 x.status                                /* status            */
  5351.             FROM sysindexes x, syscolumns c, sysobjects o
  5352.             WHERE
  5353.                 not (@table_id is null)
  5354.                 AND x.id = @table_id
  5355.                 AND x.id = o.id
  5356.                 AND x.id = c.id
  5357.                 AND colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5358.                 AND INDEX_COL(@full_table_name,indid,colid) IS NOT NULL
  5359.                 AND indid = @indid
  5360.                 AND (x.status&2 = 2
  5361.                     OR @is_unique <> 'Y')
  5362.                 AND (x.status&32) = 0
  5363.         /*
  5364.         **      Now move @indid to the next index.
  5365.         */
  5366.         SELECT @lastindid = @indid
  5367.         SELECT @indid = NULL
  5368.  
  5369.         SELECT @indid = min(indid)
  5370.         FROM sysindexes
  5371.         WHERE not (@table_id is null)
  5372.             AND id = @table_id
  5373.             AND indid > @lastindid
  5374.             AND indid < 255
  5375.     END
  5376.  
  5377.     /* now add row for table statistics */
  5378.     INSERT #TmpIndex
  5379.         SELECT
  5380.             DB_NAME(),                /* TABLE_QUALIFIER */
  5381.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5382.             o.name,                 /* TABLE_NAME       */
  5383.             null,                    /* INDEX_QUALIFIER */
  5384.             null,                    /* INDEX_NAME       */
  5385.             null,                    /* NON_UNIQUE       */
  5386.             0,                        /* SQL_TABLE_STAT  */
  5387.             null,                    /* SEQ_IN_INDEX    */
  5388.             null,                    /* COLUMN_NAME       */
  5389.             null,                    /* COLLATION       */
  5390.             0,                        /* index_id        */
  5391.             x.rows,                 /* CARDINALITY       */
  5392.             x.dpages,                /* PAGES           */
  5393.             0                        /* status           */
  5394.         FROM sysindexes x, sysobjects o
  5395.         WHERE not (@table_id is null)
  5396.             AND o.id = @table_id
  5397.             AND x.id = o.id
  5398.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5399.                                                 /*    then table stats are in */
  5400.                                                 /*    a row with indid =0        */
  5401.  
  5402.     SELECT
  5403.         TABLE_QUALIFIER,
  5404.         TABLE_OWNER,
  5405.         TABLE_NAME,
  5406.         NON_UNIQUE,
  5407.         INDEX_QUALIFIER,
  5408.         INDEX_NAME,
  5409.         TYPE,
  5410.         SEQ_IN_INDEX,
  5411.         COLUMN_NAME,
  5412.         COLLATION,
  5413.         CARDINALITY,
  5414.         PAGES,
  5415.         FILTER_CONDITION = convert(varchar(128),null)
  5416.     FROM #TmpIndex
  5417.     WHERE
  5418.         INDEX_NAME like @index_name /* If matching name */
  5419.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5420.     ORDER BY 4, 7, 6, 8
  5421.  
  5422.     DROP TABLE #TmpIndex
  5423. go
  5424.  
  5425. grant execute on sp_statistics to public
  5426. go
  5427.  
  5428. dump tran master with no_log
  5429. go
  5430.  
  5431. print 'creating sp_stored_procedures'
  5432. go
  5433.  
  5434.  
  5435. /* pre 7.00 version */
  5436. create procedure sp_stored_procedures(
  5437.                         @sp_name        varchar(102) = null,
  5438.                         @sp_owner        varchar(96) = null,
  5439.                         @sp_qualifier    varchar(32) = null)
  5440. as
  5441.     declare @proc_type smallint
  5442.  
  5443.     if @sp_qualifier is not null
  5444.     begin
  5445.         if db_name() <> @sp_qualifier
  5446.         begin
  5447.             if @sp_qualifier = ''
  5448.             begin
  5449.                 /* in this case, we need to return an empty result set */
  5450.                 /* because the user has requested a database with an empty name */
  5451.                 select @sp_name = ''
  5452.                 select @sp_owner = ''
  5453.             end else
  5454.             begin    /* If qualifier doesn't match current database */
  5455.                 raiserror 20001 '~~Rush_51~~'
  5456.                 return
  5457.             end
  5458.         end
  5459.     end
  5460.  
  5461.     if @sp_name is null
  5462.     begin  /*  If procedure name not supplied, match all */
  5463.         select @sp_name = '%'
  5464.     end
  5465.     else begin
  5466.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5467.         begin
  5468.             if exists (select * from sysobjects
  5469.                 where uid = user_id()
  5470.                     and name = @sp_name
  5471.                     and type = 'P') /* Object type of Procedure */
  5472.             begin
  5473.                 select @sp_owner = user_name()
  5474.             end
  5475.         end
  5476.     end
  5477.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5478.         select @sp_owner = '%'
  5479.  
  5480.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5481.  
  5482.     select
  5483.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  5484.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  5485.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5486.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5487.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5488.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5489.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5490.         PROCEDURE_TYPE = @proc_type
  5491.     from
  5492.         sysobjects o,syscomments c,sysusers u
  5493.     where
  5494.         o.name like @sp_name
  5495.         and c.colid = 1
  5496.         and user_name(o.uid) like @sp_owner
  5497.         and o.type = 'P'        /* Object type of Procedure */
  5498.         and c.id = o.id
  5499.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  5500.         and (suser_id() = 1     /* User is the System Administrator */
  5501.             or o.uid = user_id()    /* User created the object */
  5502.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  5503.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  5504.              from sysprotects p
  5505.              /* outer join to correlate with all rows in sysobjects */
  5506.              where p.id =* o.id
  5507.                  /*  get rows for public,current user,user's group */
  5508.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  5509.                  /* check for SELECT,EXECUTE privilege */
  5510.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  5511.             ) = 1     /* final magic...compare Grants    */
  5512.         )
  5513.     order by 1, 2, 3
  5514. go
  5515.  
  5516. grant execute on sp_stored_procedures to public
  5517. go
  5518.  
  5519.  
  5520. if (charindex('7.00', @@version) > 0)
  5521.     drop procedure sp_stored_procedures
  5522. else
  5523. begin
  5524.     print ''
  5525.     print ''
  5526.     print 'Warning:'
  5527.     print 'you are installing the stored procedures '
  5528.     print 'on a pre 7.0 SQL Server.'
  5529.     print 'Ignore the following errors.'
  5530. end
  5531. go
  5532.  
  5533.  
  5534. /* 7.00 version */
  5535. create procedure sp_stored_procedures(
  5536.                         @sp_name        nvarchar(390) = null,
  5537.                         @sp_owner        nvarchar(384) = null,
  5538.                         @sp_qualifier    sysname = null)
  5539. as
  5540.     declare @proc_type smallint
  5541.  
  5542.     if @sp_qualifier is not null
  5543.     begin
  5544.         if db_name() <> @sp_qualifier
  5545.         begin
  5546.             if @sp_qualifier = ''
  5547.             begin
  5548.                 /* in this case, we need to return an empty result set */
  5549.                 /* because the user has requested a database with an empty name */
  5550.                 select @sp_name = ''
  5551.                 select @sp_owner = ''
  5552.             end else
  5553.             begin    /* If qualifier doesn't match current database */
  5554.                 raiserror (15250, -1,-1)
  5555.                 return
  5556.             end
  5557.         end
  5558.     end
  5559.  
  5560.     if @sp_name is null
  5561.     begin  /*  If procedure name not supplied, match all */
  5562.         select @sp_name = '%'
  5563.     end
  5564.     else begin
  5565.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5566.         begin
  5567.             if exists (select * from sysobjects
  5568.                 where uid = user_id()
  5569.                     and name = @sp_name
  5570.                     and type = 'P') /* Object type of Procedure */
  5571.             begin
  5572.                 select @sp_owner = user_name()
  5573.             end
  5574.         end
  5575.     end
  5576.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5577.         select @sp_owner = '%'
  5578.  
  5579.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5580.  
  5581.     select
  5582.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  5583.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  5584.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5585.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5586.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5587.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5588.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5589.         PROCEDURE_TYPE = @proc_type
  5590.     from
  5591.         sysobjects o,syscomments c
  5592.     where
  5593.         o.name like @sp_name
  5594.         and c.colid = 1
  5595.         and user_name(o.uid) like @sp_owner
  5596.         and o.type = 'P'        /* Object type of Procedure */
  5597.         and c.id = o.id
  5598.         and permissions (o.id)&32 <> 0
  5599.     order by 1, 2, 3
  5600. go
  5601.  
  5602. grant execute on sp_stored_procedures to public
  5603. go
  5604.  
  5605. dump tran master with no_log
  5606. go
  5607.  
  5608.  
  5609. print 'creating sp_table_privileges'
  5610. go
  5611.  
  5612. /*    Procedure for pre 6.50 server */
  5613. CREATE PROCEDURE sp_table_privileges (
  5614.             @table_name         varchar(90),
  5615.             @table_owner        varchar(90) = null,
  5616.             @table_qualifier    varchar(32) = null)
  5617. as
  5618.     set nocount on
  5619.  
  5620.     declare @table_id    int,
  5621.             @owner_id     int,
  5622.             @full_table_name char(181)
  5623.     declare @refconst int
  5624.  
  5625.     select @refconst = 1
  5626.     if    (charindex('6.00', @@version) = 0)
  5627.         select @refconst = NULL
  5628.  
  5629.     if @table_qualifier is not null
  5630.     begin
  5631.         if db_name() != @table_qualifier
  5632.         begin    /* If qualifier doesn't match current database */
  5633.             raiserror 20001 'Table qualifier must be name of current database'
  5634.             return
  5635.         end
  5636.     end
  5637.     if @table_owner is null
  5638.     begin    /* If unqualified table name */
  5639.         SELECT @full_table_name = @table_name
  5640.     end
  5641.     else
  5642.     begin    /* Qualified table name */
  5643.         SELECT @full_table_name = @table_owner + '.' + @table_name
  5644.     end
  5645.     /*    Get Object ID */
  5646.     SELECT @table_id = object_id(@full_table_name)
  5647.  
  5648.     if @@trancount != 0
  5649.     begin    /* If inside a transaction */
  5650.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  5651.         return
  5652.     end
  5653.     create table #table_priv1(
  5654.         table_qualifier            varchar(32) NOT NULL,
  5655.         table_owner             varchar(32) NOT NULL,
  5656.         table_name                varchar(32) NOT NULL,
  5657.         grantor                 varchar(32) NOT NULL,
  5658.         grantee                 varchar(32) NOT NULL,
  5659.         select_privilege        int NOT NULL,
  5660.         insert_privilege        int NOT NULL,
  5661.         update_privilege        int NOT NULL,
  5662.         delete_privilege        int NOT NULL,
  5663.         references_privilege    int NULL,
  5664.         is_grantable            varchar(3) NOT NULL,
  5665.         uid                     int NOT NULL,
  5666.         gid                     int NOT NULL)
  5667.  
  5668.     insert into #table_priv1
  5669.         select distinct
  5670.             db_name(),
  5671.             user_name(o.uid),
  5672.             o.name,
  5673.             user_name(o.uid),
  5674.             u.name,
  5675.             0,
  5676.             0,
  5677.             0,
  5678.             0,
  5679.             @refconst,
  5680.             'no',
  5681.             u.uid,
  5682.             u.gid
  5683.         from sysusers u, sysobjects o
  5684.         where o.id = @table_id and u.uid != u.gid
  5685.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  5686.                                            ** user tables, and views. */
  5687.  
  5688.     /*
  5689.     ** now add row for table owner
  5690.     */
  5691.     if exists (
  5692.         select *
  5693.             from #table_priv1
  5694.             where grantor = grantee)
  5695.     begin
  5696.         update #table_priv1
  5697.         set
  5698.             select_privilege = 1,
  5699.             update_privilege = 1,
  5700.             insert_privilege = 1,
  5701.             delete_privilege = 1,
  5702.             references_privilege = 1,
  5703.             is_grantable = 'yes'
  5704.         where grantor = grantee
  5705.     end
  5706.     else
  5707.     begin
  5708.         insert into #table_priv1
  5709.             select    db_name(),
  5710.                 user_name(o.uid),
  5711.                 o.name,
  5712.                 user_name(o.uid),
  5713.                 user_name(o.uid),
  5714.                 1,
  5715.                 1,
  5716.                 1,
  5717.                 1,
  5718.                 @refconst,
  5719.                 'yes',
  5720.                 o.uid,
  5721.                 u.gid
  5722.             from sysobjects o, sysusers u
  5723.             where o.id = @table_id and u.uid = o.uid
  5724.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  5725.                                            ** user tables, and views. */
  5726.  
  5727.     end
  5728.  
  5729.     update #table_priv1
  5730.     set select_privilege = 1
  5731.     where
  5732.         exists (
  5733.             select * from sysprotects
  5734.             where
  5735.                 id = @table_id
  5736.                 and (#table_priv1.uid = uid
  5737.                     or #table_priv1.gid = uid
  5738.                     or uid = 0)
  5739.                 and protecttype = 205
  5740.                 and action = 193)
  5741.         and not exists (
  5742.             select * from sysprotects
  5743.             where
  5744.                 id = @table_id
  5745.                 and (#table_priv1.uid = uid
  5746.                     or #table_priv1.gid = uid
  5747.                     or uid = 0)
  5748.                 and protecttype = 206
  5749.                 and action = 193)
  5750.  
  5751.     update #table_priv1
  5752.     set insert_privilege = 1
  5753.     where
  5754.         exists (
  5755.             select * from sysprotects
  5756.             where
  5757.                 id = @table_id
  5758.                 and (#table_priv1.uid = uid
  5759.                     or #table_priv1.gid = uid
  5760.                     or uid = 0)
  5761.                 and protecttype = 205
  5762.                 and action = 195)
  5763.         and not exists (
  5764.             select * from sysprotects
  5765.             where
  5766.                 id = @table_id
  5767.                 and (#table_priv1.uid = uid
  5768.                     or #table_priv1.gid = uid
  5769.                     or uid = 0)
  5770.                 and protecttype = 206
  5771.                 and action = 195)
  5772.  
  5773.     update #table_priv1
  5774.     set delete_privilege = 1
  5775.     where
  5776.         exists (
  5777.             select * from sysprotects
  5778.             where
  5779.                 id = @table_id
  5780.                 and (#table_priv1.uid = uid
  5781.                     or #table_priv1.gid = uid
  5782.                     or uid = 0)
  5783.                 and protecttype = 205
  5784.                 and action = 196)
  5785.         and not exists (select * from sysprotects
  5786.             where
  5787.                 id = @table_id
  5788.                 and (#table_priv1.uid = uid
  5789.                     or #table_priv1.gid = uid
  5790.                     or uid = 0)
  5791.                 and protecttype = 206
  5792.                 and action = 196)
  5793.  
  5794.     update #table_priv1
  5795.     set update_privilege = 1
  5796.     where
  5797.         exists (
  5798.             select * from sysprotects
  5799.             where
  5800.                 id = @table_id
  5801.                 and (#table_priv1.uid = uid
  5802.                     or #table_priv1.gid = uid
  5803.                     or uid = 0)
  5804.                 and protecttype = 205
  5805.                 and action = 197)
  5806.         and not exists (
  5807.             select * from sysprotects
  5808.             where
  5809.                 id = @table_id
  5810.                 and (#table_priv1.uid = uid
  5811.                     or #table_priv1.gid = uid
  5812.                     or uid = 0)
  5813.                 and protecttype = 206
  5814.                 and action = 197)
  5815.  
  5816.     update #table_priv1
  5817.     set references_privilege = 1
  5818.     where
  5819.         exists (
  5820.             select * from sysprotects
  5821.             where
  5822.                 id = @table_id
  5823.                 and (#table_priv1.uid = uid
  5824.                     or #table_priv1.gid = uid
  5825.                     or uid = 0)
  5826.                 and protecttype = 205
  5827.                 and action = 26)
  5828.         and not exists (
  5829.             select * from sysprotects
  5830.             where
  5831.                 id = @table_id
  5832.                 and (#table_priv1.uid = uid
  5833.                     or #table_priv1.gid = uid
  5834.                     or uid = 0)
  5835.                 and protecttype = 206
  5836.                 and action = 26)
  5837.  
  5838.     create table #table_priv2(
  5839.         table_qualifier varchar(32) NULL,
  5840.         table_owner     varchar(32) NULL,
  5841.         table_name        varchar(32) NOT NULL,
  5842.         grantor         varchar(32) NULL,
  5843.         grantee         varchar(32) NOT NULL,
  5844.         privilege        varchar(32) NOT NULL,
  5845.         is_grantable    varchar(3) NULL)
  5846.  
  5847.     insert into #table_priv2
  5848.         select
  5849.             table_qualifier,
  5850.             table_owner,
  5851.             table_name,
  5852.             grantor,
  5853.             grantee,
  5854.             'SELECT',
  5855.             is_grantable
  5856.         from #table_priv1
  5857.         where select_privilege = 1
  5858.  
  5859.  
  5860.     insert into #table_priv2
  5861.         select
  5862.             table_qualifier,
  5863.             table_owner,
  5864.             table_name,
  5865.             grantor,
  5866.             grantee,
  5867.             'INSERT',
  5868.             is_grantable
  5869.         from #table_priv1
  5870.         where insert_privilege = 1
  5871.  
  5872.  
  5873.     insert into #table_priv2
  5874.         select
  5875.             table_qualifier,
  5876.             table_owner,
  5877.             table_name,
  5878.             grantor,
  5879.             grantee,
  5880.             'DELETE',
  5881.             is_grantable
  5882.         from #table_priv1
  5883.         where delete_privilege = 1
  5884.  
  5885.  
  5886.     insert into #table_priv2
  5887.         select
  5888.             table_qualifier,
  5889.             table_owner,
  5890.             table_name,
  5891.             grantor,
  5892.             grantee,
  5893.             'UPDATE',
  5894.             is_grantable
  5895.         from #table_priv1
  5896.         where update_privilege = 1
  5897.  
  5898.     insert into #table_priv2
  5899.         select
  5900.             table_qualifier,
  5901.             table_owner,
  5902.             table_name,
  5903.             grantor,
  5904.             grantee,
  5905.             'REFERENCES',
  5906.             is_grantable
  5907.         from #table_priv1
  5908.         where references_privilege = 1
  5909.  
  5910.  
  5911.     select * from #table_priv2
  5912.     order by table_owner,table_name,privilege,grantee
  5913. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  5914. go
  5915.  
  5916. if (charindex('6.50', @@version) = 0 and
  5917.     charindex('7.00', @@version) = 0)
  5918. begin
  5919.     print ''
  5920.     print ''
  5921.     print 'Warning:'
  5922.     print 'you are installing the stored procedures '
  5923.     print 'on a pre 6.50 SQL Server.'
  5924.     print 'Ignore the following errors.'
  5925. end
  5926. else
  5927.     drop proc sp_table_privileges
  5928. go
  5929.  
  5930.  
  5931. /*    Procedure for 6.50 server */
  5932. CREATE PROCEDURE sp_table_privileges (
  5933.             @table_name         varchar(96),
  5934.             @table_owner        varchar(96) = null,
  5935.             @table_qualifier    varchar(32) = null)
  5936. as
  5937.  
  5938.     if @table_qualifier is not null
  5939.     begin
  5940.         if db_name() <> @table_qualifier
  5941.         begin    /* If qualifier doesn't match current database */
  5942.             raiserror (15250, -1,-1)
  5943.             return
  5944.         end
  5945.     end
  5946.     if @table_name is null
  5947.         select @table_name = '%'
  5948.     if @table_owner is null /* If no owner supplied, force wildcard */
  5949.         select @table_owner = '%'
  5950.  
  5951.     select
  5952.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5953.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5954.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5955.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  5956.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  5957.         convert(varchar(32),case p.action
  5958.              when 193 then 'SELECT'
  5959.              when 195 then 'INSERT'
  5960.              when 196 then 'DELETE'
  5961.              when 197 then 'UPDATE'
  5962.              else 'REFERENCES'
  5963.         end) PRIVILEGE,
  5964.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  5965.             else 'YES'
  5966.         end) IS_GRANTABLE
  5967.     from sysprotects p, sysobjects o, sysusers u
  5968.     where
  5969.         p.id = o.id
  5970.         and o.type in ('U','V','S')
  5971.         and object_name(o.id) like @table_name
  5972.         and user_name(o.uid) like @table_owner
  5973.             /* expand groups */
  5974.         and ((p.uid = u.uid and u.uid <> u.gid) or
  5975.              (p.uid = u.gid and u.uid <> u.gid))
  5976.         and p.protecttype <> 206    /* only grant rows */
  5977.         and p.action in (26,193,195,196,197)
  5978.         and o.uid <> u.uid            /* no rows for owner */
  5979.         and not exists (            /* exclude revoke'd privileges */
  5980.             select *
  5981.             from sysprotects p1
  5982.             where
  5983.                 p1.protecttype = 206
  5984.                 and p1.action = p.action
  5985.                 and p1.id = p.id
  5986.                 and p1.uid = u.uid)
  5987.     union all
  5988.     select    /*    Add rows for table owner */
  5989.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5990.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5991.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5992.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  5993.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  5994.         convert(varchar(32),case v.number
  5995.             when 193 then 'SELECT'
  5996.             when 195 then 'INSERT'
  5997.             when 196 then 'DELETE'
  5998.             when 197 then 'UPDATE'
  5999.             else 'REFERENCES'
  6000.         end) PRIVILEGE,
  6001.         convert(varchar(3),'YES') IS_GRANTABLE
  6002.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6003.     where
  6004.         object_name(o.id) like @table_name
  6005.         and o.type in ('U','V','S')
  6006.         and user_name(o.uid) like @table_owner
  6007.         and u.suid = 1        /* grantor is dbo of database */
  6008.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6009.         and v.number in (26,193,195,196,197)
  6010.         and not exists (    /* exclude revoke'd privileges */
  6011.             select *
  6012.             from sysprotects p1
  6013.             where
  6014.                 p1.protecttype = 206
  6015.                 and p1.action = v.number
  6016.                 and p1.id = o.id
  6017.                 and p1.uid = o.uid)
  6018.     order by 2,3,6,5
  6019. go
  6020.  
  6021.  
  6022. if (charindex('7.00', @@version) > 0)
  6023.     drop proc sp_table_privileges
  6024. else
  6025. begin
  6026.     print ''
  6027.     print ''
  6028.     print 'Warning:'
  6029.     print 'you are installing the stored procedures '
  6030.     print 'on a pre 7.0 SQL Server.'
  6031.     print 'Ignore the following errors.'
  6032. end
  6033. go
  6034.  
  6035. /*    Procedure for 7.00 server */
  6036. CREATE PROCEDURE sp_table_privileges (
  6037.             @table_name         nvarchar(384),
  6038.             @table_owner        nvarchar(384) = null,
  6039.             @table_qualifier    sysname = null)
  6040. as
  6041.  
  6042.     if @table_qualifier is not null
  6043.     begin
  6044.         if db_name() <> @table_qualifier
  6045.         begin    /* If qualifier doesn't match current database */
  6046.             raiserror (15250, -1,-1)
  6047.             return
  6048.         end
  6049.     end
  6050.     if @table_name is null
  6051.         select @table_name = '%'
  6052.     if @table_owner is null /* If no owner supplied, force wildcard */
  6053.         select @table_owner = '%'
  6054.  
  6055.     select
  6056.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6057.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6058.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6059.         convert(sysname,user_name(p.grantor)) GRANTOR,
  6060.         convert(sysname,user_name(u.uid)) GRANTEE,
  6061.         convert(varchar(32),case p.action
  6062.              when 193 then 'SELECT'
  6063.              when 195 then 'INSERT'
  6064.              when 196 then 'DELETE'
  6065.              when 197 then 'UPDATE'
  6066.              else 'REFERENCES'
  6067.         end) PRIVILEGE,
  6068.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  6069.             else 'YES'
  6070.         end) IS_GRANTABLE
  6071.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  6072.     where
  6073.         p.id = o.id
  6074.         and o.type in ('U','V','S')
  6075.         and object_name(o.id) like @table_name
  6076.         and user_name(o.uid) like @table_owner
  6077.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  6078.         and (u.uid > 0 and u.uid < 16384)
  6079.         and ((p.uid = u.uid) or
  6080.              (p.uid = m.groupuid and u.uid = m.memberuid))
  6081.         and p.protecttype <> 206    /* only grant rows */
  6082.         and p.action in (26,193,195,196,197)
  6083.         and o.uid <> u.uid            /* no rows for owner */
  6084.         and not exists (            /* exclude revoke'd privileges */
  6085.             select *
  6086.             from sysprotects p1
  6087.             where
  6088.                 p1.protecttype = 206
  6089.                 and p1.action = p.action
  6090.                 and p1.id = p.id
  6091.                 and p1.uid = u.uid)
  6092.     union all
  6093.     select    /*    Add rows for table owner */
  6094.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6095.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6096.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6097.         convert(sysname,user_name(u.uid)) GRANTOR,
  6098.         convert(sysname,user_name(o.uid)) GRANTEE,
  6099.         convert(varchar(32),case v.number
  6100.             when 193 then 'SELECT'
  6101.             when 195 then 'INSERT'
  6102.             when 196 then 'DELETE'
  6103.             when 197 then 'UPDATE'
  6104.             else 'REFERENCES'
  6105.         end) PRIVILEGE,
  6106.         convert(varchar(3),'YES') IS_GRANTABLE
  6107.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6108.     where
  6109.         object_name(o.id) like @table_name
  6110.         and o.type in ('U','V','S')
  6111.         and user_name(o.uid) like @table_owner
  6112.         and u.uid = 1        /* grantor is 'dbo' of database */
  6113.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6114.         and v.number in (26,193,195,196,197)
  6115.         and not exists (    /* exclude revoke'd privileges */
  6116.             select *
  6117.             from sysprotects p1
  6118.             where
  6119.                 p1.protecttype = 206
  6120.                 and p1.action = v.number
  6121.                 and p1.id = o.id
  6122.                 and p1.uid = o.uid)
  6123.     order by 2,3,6,5
  6124. go
  6125.  
  6126. grant execute on sp_table_privileges to public
  6127. go
  6128.  
  6129. dump tran master with no_log
  6130. go
  6131.  
  6132. print 'creating sp_tables'
  6133. go
  6134.  
  6135. /*    Procedure for 6.50 and earlier servers */
  6136. create procedure sp_tables(
  6137.                @table_name        varchar(96)    = null,
  6138.                @table_owner     varchar(96)    = null,
  6139.                @table_qualifier varchar(32)    = null,
  6140.                @table_type        varchar(100) = null)
  6141. as
  6142.     declare @type1 varchar(3)
  6143.     declare @tableindex int
  6144.  
  6145.  
  6146.     /* Special feature #1:    enumerate databases when owner and name
  6147.          are blank but qualifier is explicitly '%'.  */
  6148.     if @table_qualifier = '%' and
  6149.         @table_owner = '' and
  6150.         @table_name = ''
  6151.     begin    /* If enumerating databases */
  6152.         select
  6153.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  6154.             TABLE_OWNER = convert(varchar(32),null),
  6155.             TABLE_NAME = convert(varchar(32),null),
  6156.             TABLE_TYPE = convert(varchar(32),null),
  6157.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6158.         from master.dbo.sysdatabases d
  6159.         where d.name <> 'model'    /* eliminate MODEL database */
  6160.         order by 1
  6161.     end
  6162.  
  6163.     /* Special feature #2:    enumerate owners when qualifier and name
  6164.          are blank but owner is explicitly '%'.  */
  6165.     else if @table_qualifier = '' and
  6166.         @table_owner = '%' and
  6167.         @table_name = ''
  6168.     begin    /* If enumerating owners */
  6169.         select distinct
  6170.             TABLE_QUALIFIER = convert(varchar(32),null),
  6171.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  6172.             TABLE_NAME = convert(varchar(32),null),
  6173.             TABLE_TYPE = convert(varchar(32),null),
  6174.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6175.         from sysobjects
  6176.         order by 2
  6177.     end
  6178.  
  6179.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6180.          name are blank but table type is explicitly '%'.    */
  6181.     else if @table_qualifier = '' and
  6182.         @table_owner = '' and
  6183.         @table_name = '' and
  6184.         @table_type = '%'
  6185.     begin    /* If enumerating table types */
  6186.         select
  6187.             TABLE_QUALIFIER = convert(varchar(32),null),
  6188.             TABLE_OWNER = convert(varchar(32),null),
  6189.             TABLE_NAME = convert(varchar(32),null),
  6190.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6191.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6192.         from sysobjects o, syscolumns c
  6193.         where o.id=c.id and o.name='sysusers' and colid<=3
  6194.     end
  6195.  
  6196.     else
  6197.     begin /* end of special features - do normal processing */
  6198.         if @table_qualifier is not null
  6199.         begin
  6200.             if db_name() <> @table_qualifier
  6201.             begin
  6202.                 if @table_qualifier = ''
  6203.                 begin  /* If empty qualifier supplied */
  6204.                     /* Force an empty result set */
  6205.                     select @table_name = ''
  6206.                     select @table_owner = ''
  6207.                 end
  6208.                 else
  6209.                 begin    /* If qualifier doesn't match current database */
  6210.                     raiserror 20001 '~~Rush_5~~'
  6211.                     return
  6212.                 end
  6213.             end
  6214.         end
  6215.         if @table_type is null
  6216.         begin    /* Select all ODBC supported table types */
  6217.             select @type1 = 'SUV'
  6218.         end
  6219.         else
  6220.         begin
  6221.             /*    TableType is case sensitive if CS server */
  6222.             select @type1 = null
  6223.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6224.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6225.             if (charindex('''TABLE''',@table_type) <> 0)
  6226.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6227.             if (charindex('''VIEW''',@table_type) <> 0)
  6228.                 select @type1 = @type1 + 'V'    /* Add Views */
  6229.         end
  6230.         if @table_name is null
  6231.         begin    /*    If table name not supplied, match all */
  6232.             select @table_name = '%'
  6233.         end
  6234.         else
  6235.         begin
  6236.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6237.             begin    /* If owner not specified and table is specified */
  6238.                 if exists (select * from sysobjects
  6239.                     where uid = user_id()
  6240.                     and name = @table_name
  6241.                     and (type = 'U' or type = 'V' or type = 'S'))
  6242.                 begin    /* Override supplied owner w/owner of table */
  6243.                     select @table_owner = user_name()
  6244.                 end
  6245.             end
  6246.         end
  6247.         if @table_owner is null /* If no owner supplied, force wildcard */
  6248.             select @table_owner = '%'
  6249.         select
  6250.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  6251.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  6252.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  6253.             TABLE_TYPE = convert(varchar(32),rtrim(
  6254.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6255.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6256.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6257.         from sysusers u, sysobjects o
  6258.         where
  6259.             o.name like @table_name
  6260.             and user_name(o.uid) like @table_owner
  6261.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6262.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  6263.             and (
  6264.                 suser_id() = 1     /* User is the System Administrator */
  6265.                 or o.uid = user_id()     /* User created the object */
  6266.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6267.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6268.                     from sysprotects p
  6269.                     /* outer join to correlate with all rows in sysobjects */
  6270.                     where p.id =* o.id
  6271.                         /* get rows for public,current user,user's group */
  6272.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6273.                         /* check for SELECT,EXECUTE privilege */
  6274.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  6275.                     ) = 1    /* final magic...compare Grants      */
  6276.             )
  6277.         order by 4, 1, 2, 3
  6278.     end
  6279. go
  6280.  
  6281. if (charindex('7.00', @@version) > 0)
  6282.     drop procedure sp_tables
  6283. else
  6284. begin
  6285.     print ''
  6286.     print ''
  6287.     print 'Warning:'
  6288.     print 'you are installing the stored procedures '
  6289.     print 'on a pre 7.0 SQL Server.'
  6290.     print 'Ignore the following errors.'
  6291. end
  6292. go
  6293.  
  6294. /*    Procedure for 7.00 server */
  6295. create procedure sp_tables(
  6296.                @table_name        nvarchar(384)    = null,
  6297.                @table_owner     nvarchar(384)    = null,
  6298.                @table_qualifier sysname    = null,
  6299.                @table_type        varchar(100) = null)
  6300. as
  6301.     declare @type1 varchar(3)
  6302.     declare @tableindex int
  6303.  
  6304.  
  6305.     /* Special feature #1:    enumerate databases when owner and name
  6306.          are blank but qualifier is explicitly '%'.  */
  6307.     if @table_qualifier = '%' and
  6308.         @table_owner = '' and
  6309.         @table_name = ''
  6310.     begin    /* If enumerating databases */
  6311.         select
  6312.             TABLE_QUALIFIER = convert(sysname,d.name),
  6313.             TABLE_OWNER = convert(sysname,null),
  6314.             TABLE_NAME = convert(sysname,null),
  6315.             TABLE_TYPE = convert(varchar(32),null),
  6316.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6317.         from master.dbo.sysdatabases d
  6318.         where d.name <> 'model'    /* eliminate MODEL database */
  6319.         order by 1
  6320.     end
  6321.  
  6322.     /* Special feature #2:    enumerate owners when qualifier and name
  6323.          are blank but owner is explicitly '%'.  */
  6324.     else if @table_qualifier = '' and
  6325.         @table_owner = '%' and
  6326.         @table_name = ''
  6327.     begin    /* If enumerating owners */
  6328.         select distinct
  6329.             TABLE_QUALIFIER = convert(sysname,null),
  6330.             TABLE_OWNER = convert(sysname,user_name(uid)),
  6331.             TABLE_NAME = convert(sysname,null),
  6332.             TABLE_TYPE = convert(varchar(32),null),
  6333.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6334.         from sysobjects
  6335.         order by 2
  6336.     end
  6337.  
  6338.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6339.          name are blank but table type is explicitly '%'.    */
  6340.     else if @table_qualifier = '' and
  6341.         @table_owner = '' and
  6342.         @table_name = '' and
  6343.         @table_type = '%'
  6344.     begin    /* If enumerating table types */
  6345.         select
  6346.             TABLE_QUALIFIER = convert(sysname,null),
  6347.             TABLE_OWNER = convert(sysname,null),
  6348.             TABLE_NAME = convert(sysname,null),
  6349.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6350.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6351.         from sysobjects o, syscolumns c
  6352.         where o.id=c.id and o.name='sysusers' and colid<=3
  6353.     end
  6354.  
  6355.     else
  6356.     begin /* end of special features - do normal processing */
  6357.         if @table_qualifier is not null
  6358.         begin
  6359.             if db_name() <> @table_qualifier
  6360.             begin
  6361.                 if @table_qualifier = ''
  6362.                 begin  /* If empty qualifier supplied */
  6363.                     /* Force an empty result set */
  6364.                     select @table_name = ''
  6365.                     select @table_owner = ''
  6366.                 end
  6367.                 else
  6368.                 begin    /* If qualifier doesn't match current database */
  6369.                     raiserror (15250, -1,-1)
  6370.                     return
  6371.                 end
  6372.             end
  6373.         end
  6374.         if @table_type is null
  6375.         begin    /* Select all ODBC supported table types */
  6376.             select @type1 = 'SUV'
  6377.         end
  6378.         else
  6379.         begin
  6380.             /*    TableType is case sensitive if CS server */
  6381.             select @type1 = ''
  6382.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6383.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6384.             if (charindex('''TABLE''',@table_type) <> 0)
  6385.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6386.             if (charindex('''VIEW''',@table_type) <> 0)
  6387.                 select @type1 = @type1 + 'V'    /* Add Views */
  6388.         end
  6389.         if @table_name is null
  6390.         begin    /*    If table name not supplied, match all */
  6391.             select @table_name = '%'
  6392.         end
  6393.         else
  6394.         begin
  6395.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6396.             begin    /* If owner not specified and table is specified */
  6397.                 if exists (select * from sysobjects
  6398.                     where uid = user_id()
  6399.                     and name = @table_name
  6400.                     and (type = 'U' or type = 'V' or type = 'S'))
  6401.                 begin    /* Override supplied owner w/owner of table */
  6402.                     select @table_owner = user_name()
  6403.                 end
  6404.             end
  6405.         end
  6406.         if @table_owner is null /* If no owner supplied, force wildcard */
  6407.             select @table_owner = '%'
  6408.         select
  6409.             TABLE_QUALIFIER = convert(sysname,db_name()),
  6410.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  6411.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  6412.             TABLE_TYPE = convert(varchar(32),rtrim(
  6413.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6414.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6415.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6416.         from sysobjects o
  6417.         where
  6418.             o.name like @table_name
  6419.             and user_name(o.uid) like @table_owner
  6420.             and o.type in ('U','V','S')
  6421.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6422.             and permissions (o.id)&4096 <> 0
  6423.         order by 4, 1, 2, 3
  6424.     end
  6425. go
  6426.  
  6427.  
  6428. grant execute on sp_tables to public
  6429. go
  6430.  
  6431. dump tran master with no_log
  6432. go
  6433.  
  6434. /*-----------------------------------------------------------------------------*/
  6435. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  6436. /*-----------------------------------------------------------------------------*/
  6437.  
  6438. if object_id('sp_linkedservers', 'P') is not null
  6439.     drop proc sp_linkedservers
  6440. go
  6441. raiserror(15339,-1,-1,'sp_linkedservers')
  6442. go
  6443. create proc sp_linkedservers as
  6444.     select
  6445.         SRV_NAME = srvname,
  6446.         SRV_PROVIDERNAME = providername,
  6447.         SRV_PRODUCT = srvproduct,
  6448.         SRV_DATASOURCE = datasource,
  6449.         SRV_PROVIDERSTRING = providerstring,
  6450.         SRV_LOCATION = location,
  6451.         SRV_CAT = catalog
  6452.     from master.dbo.sysservers
  6453.     order by 1
  6454. go
  6455. grant execute on sp_linkedservers to public
  6456. go
  6457.  
  6458.  
  6459. if object_id('sp_catalogs', 'P') is not null
  6460.     drop proc sp_catalogs
  6461. go
  6462. raiserror(15339,-1,-1,'sp_catalogs')
  6463. go
  6464. create procedure sp_catalogs(
  6465.     @server_name        sysname)
  6466. as
  6467.     select
  6468.         CATALOG_NAME,
  6469.         convert (nvarchar(255),DESCRIPTION)
  6470.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  6471.     order by CATALOG_NAME
  6472. go
  6473. grant execute on sp_catalogs to public
  6474. go
  6475.  
  6476.  
  6477. if object_id('sp_tables_ex', 'P') is not null
  6478.     drop proc sp_tables_ex
  6479. go
  6480. raiserror(15339,-1,-1,'sp_tables_ex')
  6481. go
  6482. create procedure sp_tables_ex(
  6483.     @table_server        sysname,
  6484.     @table_name            sysname = null,
  6485.     @table_schema        sysname = null,
  6486.     @table_catalog        sysname = null,
  6487.     @table_type            sysname = null)
  6488. as
  6489.     declare @table_catalog_param        sysname
  6490.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  6491.         (isnull(charindex('[', @table_catalog),0) = 0) and
  6492.         (isnull(charindex('_', @table_catalog),0) = 0))
  6493.         select @table_catalog_param = @table_catalog
  6494.     else
  6495.         select @table_catalog_param = null
  6496.  
  6497.     if ((isnull(charindex('%', @table_name),0) = 0) and
  6498.         (isnull(charindex('[', @table_name),0) = 0) and
  6499.         (isnull(charindex('_', @table_name),0) = 0))
  6500.     begin    /*    If no wild carding */
  6501.         select
  6502.             TABLE_CAT = TABLE_CATALOG,
  6503.             TABLE_SCHEM = TABLE_SCHEMA,
  6504.             TABLE_NAME = TABLE_NAME,
  6505.             TABLE_TYPE = TABLE_TYPE,
  6506.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  6507.         from master.dbo.SYSREMOTE_TABLES <
  6508.                     @table_server,
  6509.                     @table_catalog_param,
  6510.                     NULL,
  6511.                     @table_name,
  6512.                     NULL >
  6513.         where (TABLE_SCHEMA like @table_schema
  6514.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6515.             and (TABLE_CATALOG like @table_catalog
  6516.             or     @table_catalog is NULL)
  6517.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  6518.             or     @table_type is NULL)
  6519.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  6520.     end
  6521.     else
  6522.     begin    /*    If wild carding */
  6523.         select
  6524.             TABLE_CAT = TABLE_CATALOG,
  6525.             TABLE_SCHEM = TABLE_SCHEMA,
  6526.             TABLE_NAME = TABLE_NAME,
  6527.             TABLE_TYPE = TABLE_TYPE,
  6528.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  6529.         from master.dbo.SYSREMOTE_TABLES <
  6530.                     @table_server,
  6531.                     @table_catalog_param,
  6532.                     NULL,
  6533.                     NULL,
  6534.                     NULL >
  6535.         where (TABLE_SCHEMA like @table_schema
  6536.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6537.             and (TABLE_CATALOG like @table_catalog
  6538.             or     @table_catalog is NULL)
  6539.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  6540.             or     @table_type is NULL)
  6541.             and TABLE_NAME like @table_name
  6542.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  6543.     end
  6544. go
  6545. grant execute on sp_tables_ex to public
  6546. go
  6547.  
  6548.  
  6549. if object_id('sp_columns_ex', 'P') is not null
  6550.     drop proc sp_columns_ex
  6551. go
  6552. raiserror(15339,-1,-1,'sp_columns_ex')
  6553. go
  6554. create procedure sp_columns_ex(
  6555.     @table_server        sysname,
  6556.     @table_name            sysname = null,
  6557.     @table_schema        sysname = null,
  6558.     @table_catalog        sysname = null,
  6559.     @column_name        sysname = null,
  6560.     @ODBCVer            int = 2)
  6561. as
  6562.  
  6563.     set nocount on
  6564.     declare
  6565.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  6566.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  6567.  
  6568.     select
  6569.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  6570.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  6571.  
  6572.     declare    
  6573.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  6574.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  6575.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  6576.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  6577.     @DBTYPE_VARIANT smallint,        @DBTYPE_DECIMAL smallint,
  6578.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  6579.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  6580.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  6581.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  6582.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  6583.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  6584.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  6585.     @DBTYPE_DBTIMESTAMP smallint
  6586.  
  6587.     select
  6588.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  6589.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  6590.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  6591.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  6592.     @DBTYPE_VARIANT        = 12,    @DBTYPE_DECIMAL        = 14,
  6593.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  6594.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  6595.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  6596.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  6597.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  6598.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  6599.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  6600.     @DBTYPE_DBTIMESTAMP    = 135
  6601.  
  6602.     declare    
  6603.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  6604.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  6605.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  6606.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  6607.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  6608.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  6609.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  6610.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  6611.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  6612.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  6613.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  6614.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  6615.  
  6616.     select     
  6617.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  6618.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  6619.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  6620.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  6621.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  6622.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  6623.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  6624.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  6625.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  6626.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  6627.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  6628.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  6629.  
  6630.     declare 
  6631.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  6632.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  6633.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  6634.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  6635.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  6636.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  6637.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  6638.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  6639.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  6640.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  6641.     @ODBC_DATETIME smallint
  6642.  
  6643.     select
  6644.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  6645.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  6646.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  6647.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  6648.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  6649.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  6650.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  6651.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  6652.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  6653.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  6654.     @ODBC_DATETIME    =    
  6655.     case @ODBCVer
  6656.         when 2 then 11
  6657.         else 93
  6658.     end
  6659.  
  6660.     create table #tmp_columns
  6661.     (    TABLE_CAT sysname NULL,
  6662.         TABLE_SCHEM sysname NULL,
  6663.         TABLE_NAME sysname    NOT NULL,
  6664.         COLUMN_NAME sysname NULL,
  6665.         DATA_TYPE smallint NOT NULL,
  6666.         TYPE_NAME sysname  NULL,
  6667.         COLUMN_SIZE int NULL,
  6668.         BUFFER_LENGTH int NULL,
  6669.         DECIMAL_DIGITS smallint NULL,
  6670.         NUM_PREC_RADIX smallint NULL,
  6671.         NULLABLE smallint NOT NULL,
  6672.         REMARKS nvarchar(255) NULL,
  6673.         COLUMN_DEF nvarchar(127) NULL,
  6674.         SQL_DATA_TYPE smallint null,
  6675.         SQL_DATETIME_SUB smallint NULL,
  6676.         CHAR_OCTET_LENGTH int NULL,
  6677.         ORDINAL_POSITION smallint,
  6678.         IS_NULLABLE varchar(254) NOT NULL,
  6679.         SS_DATA_TYPE tinyint null,
  6680.         COLUMN_FLAGS int NOT NULL)
  6681.  
  6682.     if ((isnull(charindex('%', @table_name),0) = 0) and
  6683.         (isnull(charindex('[', @table_name),0) = 0) and
  6684.         (isnull(charindex('_', @table_name),0) = 0))
  6685.     begin    /*    If no wild carding */
  6686.         insert into #tmp_columns
  6687.         select
  6688.             TABLE_CAT = c.TABLE_CATALOG,
  6689.             TABLE_SCHEM = c.TABLE_SCHEMA,
  6690.             TABLE_NAME = c.TABLE_NAME,
  6691.             COLUMN_NAME = c.COLUMN_NAME,
  6692.             DATA_TYPE =
  6693.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  6694.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  6695.                 when @DBTYPE_I4                then @ODBC_INT
  6696.                 when @DBTYPE_R4                then @ODBC_REAL
  6697.                 when @DBTYPE_R8                then @ODBC_FLOAT
  6698.                 when @DBTYPE_CY                then @ODBC_MONEY
  6699.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  6700.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  6701.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  6702.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  6703.                 when @DBTYPE_BOOL            then @ODBC_BIT
  6704.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  6705.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  6706.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  6707.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  6708.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  6709.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  6710.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  6711.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  6712.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  6713.                 when @DBTYPE_GUID            then @ODBC_GUID
  6714.                 when @DBTYPE_BYTES then
  6715.                     case
  6716.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  6717.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6718.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6719.                                  p.COLUMN_SIZE = 8
  6720.                             then @ODBC_TIMESTAMP
  6721.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6722.                             then @ODBC_BINARY
  6723.                         else @ODBC_VARBINARY
  6724.                     end
  6725.                  when @DBTYPE_STR  then
  6726.                     case
  6727.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  6728.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  6729.                         else @ODBC_VARCHAR
  6730.                     end
  6731.                 when @DBTYPE_WSTR then
  6732.                     case
  6733.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6734.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6735.                         else @ODBC_NVARCHAR
  6736.                     end
  6737.                 when @DBTYPE_BSTR then
  6738.                     case
  6739.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6740.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6741.                         else @ODBC_NVARCHAR
  6742.                     end
  6743.             end,
  6744.             TYPE_NAME = p.TYPE_NAME,
  6745.             COLUMN_SIZE =
  6746.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  6747.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6748.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  6749.             NUM_PREC_RADIX = NULL,
  6750.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  6751.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  6752.             COLUMN_DEF = c.COLUMN_DEFAULT,
  6753.             SQL_DATA_TYPE = NULL,
  6754.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  6755.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6756.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  6757.             IS_NULLABLE =
  6758.             case c.IS_NULLABLE
  6759.                 when 1 then 'YES'
  6760.                 else 'NO'
  6761.             end,
  6762.             SS_DATA_TYPE = NULL,
  6763.             COLUMN_FLAGS = c.COLUMN_FLAGS
  6764.         from master.dbo.SYSREMOTE_COLUMNS <
  6765.                     @table_server,
  6766.                     @table_catalog,
  6767.                     NULL,
  6768.                     @table_name,
  6769.                     NULL > c
  6770.             LEFT OUTER JOIN
  6771.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  6772.                     @table_server > p
  6773.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  6774.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  6775.             where (TABLE_SCHEMA like @table_schema
  6776.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6777.                 and (c.COLUMN_NAME like @column_name
  6778.                 or     @column_name is NULL)
  6779.     end
  6780.     else
  6781.     begin    /*    If wild carding */
  6782.         insert into #tmp_columns
  6783.         select
  6784.             TABLE_CAT = c.TABLE_CATALOG,
  6785.             TABLE_SCHEM = c.TABLE_SCHEMA,
  6786.             TABLE_NAME = c.TABLE_NAME,
  6787.             COLUMN_NAME = c.COLUMN_NAME,
  6788.             DATA_TYPE =
  6789.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  6790.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  6791.                 when @DBTYPE_I4                then @ODBC_INT
  6792.                 when @DBTYPE_R4                then @ODBC_REAL
  6793.                 when @DBTYPE_R8                then @ODBC_FLOAT
  6794.                 when @DBTYPE_CY                then @ODBC_MONEY
  6795.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  6796.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  6797.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  6798.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  6799.                 when @DBTYPE_BOOL            then @ODBC_BIT
  6800.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  6801.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  6802.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  6803.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  6804.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  6805.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  6806.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  6807.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  6808.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  6809.                 when @DBTYPE_GUID            then @ODBC_GUID
  6810.                 when @DBTYPE_BYTES then
  6811.                     case
  6812.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  6813.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6814.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6815.                                  p.COLUMN_SIZE = 8
  6816.                             then @ODBC_TIMESTAMP
  6817.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6818.                             then @ODBC_BINARY
  6819.                         else @ODBC_VARBINARY
  6820.                     end
  6821.                  when @DBTYPE_STR  then
  6822.                     case
  6823.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  6824.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  6825.                         else @ODBC_VARCHAR
  6826.                     end
  6827.                 when @DBTYPE_WSTR then
  6828.                     case
  6829.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6830.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6831.                         else @ODBC_NVARCHAR
  6832.                     end
  6833.                 when @DBTYPE_BSTR then
  6834.                     case
  6835.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6836.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6837.                         else @ODBC_NVARCHAR
  6838.                     end
  6839.             end,
  6840.             TYPE_NAME = p.TYPE_NAME,
  6841.             COLUMN_SIZE =
  6842.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  6843.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6844.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  6845.             NUM_PREC_RADIX = NULL,
  6846.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  6847.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  6848.             COLUMN_DEF = c.COLUMN_DEFAULT,
  6849.             SQL_DATA_TYPE = NULL,
  6850.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  6851.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6852.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  6853.             IS_NULLABLE =
  6854.             case c.IS_NULLABLE
  6855.                 when 1 then 'YES'
  6856.                 else 'NO'
  6857.             end,
  6858.             SS_DATA_TYPE = NULL,
  6859.             COLUMN_FLAGS = c.COLUMN_FLAGS
  6860.         from master.dbo.SYSREMOTE_COLUMNS <
  6861.                     @table_server,
  6862.                     @table_catalog,
  6863.                     NULL,
  6864.                     NULL,
  6865.                     NULL > c
  6866.             LEFT OUTER JOIN
  6867.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  6868.                     @table_server > p
  6869.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  6870.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  6871.             where (TABLE_SCHEMA like @table_schema
  6872.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6873.                 and (c.COLUMN_NAME like @column_name
  6874.                 or     @column_name is NULL)
  6875.                 and c.TABLE_NAME like @table_name
  6876.     end
  6877.  
  6878.     update #tmp_columns
  6879.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  6880.         SQL_DATETIME_SUB =
  6881.         case
  6882.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  6883.             else #tmp_columns.SQL_DATETIME_SUB
  6884.         end,
  6885.         TYPE_NAME = 
  6886.         case 
  6887.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6888.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6889.                     BUFFER_LENGTH = 8
  6890.                 then 'timestamp'
  6891.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  6892.                     #tmp_columns.SQL_DATETIME_SUB = 0
  6893.                 then 'smalldatetime'
  6894.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  6895.                     #tmp_columns.COLUMN_SIZE = 10
  6896.                 then 'smallmoney'
  6897.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  6898.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  6899.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  6900.                 then spt_dt.TYPE_NAME
  6901.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  6902.         end,
  6903.         COLUMN_SIZE =
  6904.         case
  6905.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  6906.                 spt_dt.data_precision
  6907.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  6908.                     #tmp_columns.SQL_DATETIME_SUB = 0
  6909.                 then 16
  6910.             else #tmp_columns.COLUMN_SIZE
  6911.         end,
  6912.         BUFFER_LENGTH =
  6913.         case
  6914.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  6915.                 COLUMN_SIZE+2
  6916.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  6917.             else #tmp_columns.BUFFER_LENGTH
  6918.         end,
  6919.         DECIMAL_DIGITS =
  6920.         case
  6921.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  6922.                 then #tmp_columns.SQL_DATETIME_SUB
  6923.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  6924.         end,
  6925.         NUM_PREC_RADIX = spt_dt.RADIX,
  6926.         SS_DATA_TYPE = 
  6927.         case
  6928.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  6929.                 and #tmp_columns.NULLABLE = 1
  6930.                     then @ODS_INT_NULL
  6931.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  6932.                 then @ODS_TINYINT_NOT_NULL
  6933.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  6934.                 then @ODS_SMALLINT_NOT_NULL
  6935.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  6936.                 then @ODS_INT_NOT_NULL
  6937.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  6938.                 then @ODS_FLOAT_NULL
  6939.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  6940.                 then @ODS_REAL_NOT_NULL
  6941.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  6942.                 then @ODS_FLOAT_NOT_NULL
  6943.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  6944.                 then @ODS_MONEY_NULL
  6945.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  6946.                 then @ODS_MONEY_NOT_NULL
  6947.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  6948.                 then @ODS_DATETIME_NULL
  6949.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  6950.                 then @ODS_DATETIME_NOT_NULL
  6951.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  6952.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  6953.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  6954.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  6955.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  6956.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  6957.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  6958.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6959.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6960.                     BUFFER_LENGTH = 8
  6961.                 then @ODS_TIMESTAMP
  6962.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  6963.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6964.                 then  @ODS_BINARY_NULL
  6965.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  6966.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6967.                 then @ODS_BINARY_NOT_NULL
  6968.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  6969.                 then @ODS_VARBINARY    
  6970.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  6971.                 then @ODS_CHAR_NULL
  6972.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  6973.                 then @ODS_CHAR_NOT_NULL
  6974.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  6975.             else null
  6976.         end
  6977.         from master.dbo.spt_datatype_info spt_dt
  6978.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  6979.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  6980.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  6981.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  6982.  
  6983.     select
  6984.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  6985.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  6986.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  6987.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  6988.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  6989.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  6990.             SS_DATA_TYPE
  6991.         from #tmp_columns
  6992.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  6993.  
  6994.         drop table #tmp_columns
  6995.  
  6996. go
  6997. grant execute on sp_columns_ex to public
  6998. go
  6999.  
  7000.  
  7001. if object_id('sp_table_privileges_ex', 'P') is not null
  7002.     drop proc sp_table_privileges_ex
  7003. go
  7004. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  7005. go
  7006. create procedure sp_table_privileges_ex(
  7007.     @table_server        sysname,
  7008.     @table_name            sysname = null,
  7009.     @table_schema        sysname = null,
  7010.     @table_catalog        sysname = null)
  7011. as
  7012.     select
  7013.         TABLE_CAT = TABLE_CATALOG,
  7014.         TABLE_SCHEM = TABLE_SCHEMA,
  7015.         TABLE_NAME = TABLE_NAME,
  7016.         GRANTOR = GRANTOR,
  7017.         GRANTEE = GRANTEE,
  7018.         PRIVILEGE = PRIVILEGE_TYPE,
  7019.         IS_GRANTABLE = 
  7020.         case IS_GRANTABLE
  7021.             when 1 then 'YES'
  7022.             when 0 then 'NO'
  7023.             else null
  7024.         end
  7025.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  7026.                 @table_server,
  7027.                 @table_catalog,
  7028.                 NULL,
  7029.                 NULL >
  7030.     where (TABLE_SCHEMA like @table_schema
  7031.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7032.         and (TABLE_NAME like @table_name
  7033.         or     @table_name is NULL)
  7034.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  7035. go
  7036. grant execute on sp_table_privileges_ex to public
  7037. go
  7038.  
  7039.  
  7040. if object_id('sp_column_privileges_ex', 'P') is not null
  7041.     drop proc sp_column_privileges_ex
  7042. go
  7043. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  7044. go
  7045. create procedure sp_column_privileges_ex(
  7046.     @table_server        sysname,
  7047.     @table_name            sysname = null,
  7048.     @table_schema        sysname = null,
  7049.     @table_catalog        sysname = null,
  7050.     @column_name        sysname = null)
  7051. as
  7052.     select
  7053.         TABLE_CAT = TABLE_CATALOG,
  7054.         TABLE_SCHEM = TABLE_SCHEMA,
  7055.         TABLE_NAME = TABLE_NAME,
  7056.         COLUMN_NAME = COLUMN_NAME,
  7057.         GRANTOR = GRANTOR,
  7058.         GRANTEE = GRANTEE,
  7059.         PRIVILEGE = PRIVILEGE_TYPE,
  7060.         IS_GRANTABLE = 
  7061.         case IS_GRANTABLE
  7062.             when 1 then 'YES'
  7063.             when 0 then 'NO'
  7064.             else null
  7065.         end
  7066.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  7067.         @table_server,
  7068.         @table_catalog,
  7069.         @table_schema,
  7070.         @table_name,
  7071.         NULL >
  7072.     where (COLUMN_NAME like @column_name
  7073.         or @column_name is NULL)
  7074.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  7075. go
  7076. grant execute on sp_column_privileges_ex to public
  7077. go
  7078.  
  7079.  
  7080. if object_id('sp_indexes', 'P') is not null
  7081.     drop proc sp_indexes
  7082. go
  7083. raiserror(15339,-1,-1,'sp_indexes')
  7084. go
  7085. create procedure sp_indexes(
  7086.     @table_server        sysname,
  7087.     @table_name            sysname = null,
  7088.     @table_schema        sysname = null,
  7089.     @table_catalog        sysname = null,
  7090.     @index_name            sysname = null,
  7091.     @is_unique          bit = null)
  7092. as
  7093.     select
  7094.         TABLE_CAT = TABLE_CATALOG,
  7095.         TABLE_SCHEM = TABLE_SCHEMA,
  7096.         TABLE_NAME = TABLE_NAME,
  7097.         NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  7098.         INDEX_QUALIFIER = TABLE_NAME,
  7099.         INDEX_NAME = INDEX_NAME,
  7100.         TYPE = 
  7101.         case [CLUSTERED]
  7102.             when 1 then 1
  7103.             else 3
  7104.         end,
  7105.         ORDINAL_POSITION = ORDINAL_POSITION,
  7106.         COLUMN_NAME = COLUMN_NAME,
  7107.         ASC_OR_DESC = 
  7108.         case [COLLATION]
  7109.             when 1 then 'A'
  7110.             when 2 then 'D'
  7111.             else null
  7112.         end,
  7113.         CARDINALITY = CARDINALITY,
  7114.         PAGES = 
  7115.         case [CLUSTERED]
  7116.             when 1 then PAGES
  7117.             else NULL
  7118.         end,
  7119.         FILTER_CONDITION = FILTER_CONDITION
  7120.     from master.dbo.SYSREMOTE_INDEXES <
  7121.                 @table_server,
  7122.                 @table_catalog,
  7123.                 @table_schema,
  7124.                 @index_name,
  7125.                 NULL,            /* TYPE (index type) */
  7126.                 @table_name >
  7127.     where @is_unique is null or @is_unique = [UNIQUE]
  7128.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  7129. go
  7130. grant execute on sp_indexes to public
  7131. go
  7132.  
  7133.  
  7134. if object_id('sp_foreignkeys', 'P') is not null
  7135.     drop proc sp_foreignkeys
  7136. go
  7137. raiserror(15339,-1,-1,'sp_foreignkeys')
  7138. go
  7139. create procedure sp_foreignkeys(
  7140.     @table_server        sysname,
  7141.     @pktab_name         sysname = null,
  7142.     @pktab_schema       sysname = null,
  7143.     @pktab_catalog      sysname = null,
  7144.     @fktab_name         sysname = null,
  7145.     @fktab_schema       sysname = null,
  7146.     @fktab_catalog      sysname = null)
  7147. as
  7148.     select
  7149.         PKTABLE_CAT = PK_TABLE_CATALOG,
  7150.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  7151.         PKTABLE_NAME = PK_TABLE_NAME,
  7152.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  7153.         FKTABLE_CAT = FK_TABLE_CATALOG,
  7154.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  7155.         FKTABLE_NAME = FK_TABLE_NAME,
  7156.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  7157.         KEY_SEQ = ORDINAL,
  7158.         UPDATE_RULE = 
  7159.         case UPDATE_RULE
  7160.             when 'CASCADE' then 0
  7161.             when 'NO ACTION' then 1
  7162.             when 'SET NULL' then 2
  7163.             else null
  7164.         end,
  7165.         DELETE_RULE = 
  7166.         case DELETE_RULE
  7167.             when 'CASCADE' then 0
  7168.             when 'NO ACTION' then 1
  7169.             when 'SET NULL' then 2
  7170.             else null
  7171.         end,
  7172.         FK_NAME = convert(sysname, NULL),
  7173.         PK_NAME = convert(sysname, NULL),
  7174.         DEFERRABILITY = convert(smallint, null)
  7175.  
  7176.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  7177.                 @table_server,
  7178.                 @pktab_catalog,
  7179.                 @pktab_schema,
  7180.                 @pktab_name,
  7181.                 @fktab_catalog,
  7182.                 @fktab_schema,
  7183.                 @fktab_name >
  7184.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  7185. go
  7186. grant execute on sp_foreignkeys to public
  7187. go
  7188.  
  7189.  
  7190. if object_id('sp_primarykeys', 'P') is not null
  7191.     drop proc sp_primarykeys
  7192. go
  7193. raiserror(15339,-1,-1,'sp_primarykeys')
  7194. go
  7195. create procedure sp_primarykeys(
  7196.     @table_server        sysname,
  7197.     @table_name         sysname = null,
  7198.     @table_schema       sysname = null,
  7199.     @table_catalog      sysname = null)
  7200. as
  7201.     select
  7202.         TABLE_CAT = TABLE_CATALOG,
  7203.         TABLE_SCHEM = TABLE_SCHEMA,
  7204.         TABLE_NAME = TABLE_NAME,
  7205.         COLUMN_NAME = COLUMN_NAME,
  7206.         KEY_SEQ = ORDINAL,
  7207.         PK_NAME = convert(sysname, NULL)
  7208.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  7209.                 @table_server,
  7210.                 @table_catalog,
  7211.                 @table_schema,
  7212.                 @table_name >
  7213.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  7214. go
  7215. grant execute on sp_primarykeys to public
  7216. go
  7217.  
  7218. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  7219.  
  7220.  
  7221. dump tran master with no_log
  7222. go
  7223.  
  7224. if (charindex('6.50', @@version) = 0 and
  7225.     charindex('7.00', @@version) = 0)
  7226. begin
  7227.     print ''
  7228.     print ''
  7229.     print 'Warning:'
  7230.     print 'you are installing the stored procedures '
  7231.     print 'on a pre 6.50 SQL Server.'
  7232.     print 'Ignore the following errors.'
  7233. end
  7234.  
  7235. print 'creating sp_ddopen'
  7236. go
  7237.  
  7238. /*    Procedure for pre-7.00 server */
  7239. create procedure sp_ddopen(
  7240.                @handle            int output,
  7241.                @procname        sysname,
  7242.                @scrollopt        int output,
  7243.                @ccopt            int output,
  7244.                @rows            int output,
  7245.                @p1                varchar(255) = null,
  7246.                @p2                varchar(255) = null,
  7247.                @p3                varchar(255) = null,
  7248.                @p4                varchar(255) = null,
  7249.                @p5                varchar(255) = null,
  7250.                @p6                varchar(255) = null,
  7251.                @p7                int = null,
  7252.                @ODBCVer         int = 2)
  7253. as
  7254.     set nocount on
  7255.     declare @ret int
  7256.  
  7257.     if @procname = 'sp_column_privileges'
  7258.     begin
  7259.         create table #spcolpriv (
  7260.             TABLE_QUALIFIER varchar(32) null,
  7261.             TABLE_OWNER varchar(32) null,
  7262.             TABLE_NAME varchar(32)    not null,
  7263.             COLUMN_NAME varchar(32) not null,
  7264.             GRANTOR varchar(32) null,
  7265.             GRANTEE varchar(32) not null,
  7266.             PRIVILEGE varchar(32) not null,
  7267.             IS_GRANTABLE varchar(3) null
  7268.             )
  7269.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  7270.         exec @ret = sp_cursoropen @handle output,
  7271.             'select * from #spcolpriv',
  7272.             @scrollopt output, @ccopt output, @rows output
  7273.         drop table #spcolpriv
  7274.     end
  7275.     else if @procname = 'sp_columns'
  7276.     begin
  7277.         create table #spcolumns (
  7278.             TABLE_QUALIFIER varchar(32) null,
  7279.             TABLE_OWNER varchar(32) null,
  7280.             TABLE_NAME varchar(32)    not null,
  7281.             COLUMN_NAME varchar(32) not null,
  7282.             DATA_TYPE smallint not null,
  7283.             TYPE_NAME varchar(32) not null,
  7284.             "PRECISION" int null,
  7285.             LENGTH int null,
  7286.             SCALE smallint null,
  7287.             RADIX smallint null,
  7288.             NULLABLE smallint not null,
  7289.             REMARKS varchar(254) null,
  7290.             COLUMN_DEF varchar(255) null,
  7291.             SQL_DATA_TYPE smallint not null,
  7292.             SQL_DATETIME_SUB smallint null,
  7293.             CHAR_OCTET_LENGTH int null,
  7294.             ORDINAL_POSITION int not null,
  7295.             IS_NULLABLE varchar(254) null,
  7296.             SS_DATA_TYPE tinyint null
  7297.             )
  7298.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  7299.         exec @ret = sp_cursoropen @handle output,
  7300.             'select * from #spcolumns',
  7301.             @scrollopt output, @ccopt output, @rows output
  7302.         drop table #spcolumns
  7303.     end
  7304.     else if @procname = 'sp_datatype_info'
  7305.     begin
  7306.         create table #spdatatypeinfo (
  7307.             TYPE_NAME            varchar(32)  not null,
  7308.             DATA_TYPE            smallint not null,
  7309.             "PRECISION"            int null,
  7310.             LITERAL_PREFIX        varchar(32)    null,
  7311.             LITERAL_SUFFIX        varchar(32)    null,
  7312.             CREATE_PARAMS        varchar(32)    null,
  7313.             NULLABLE            smallint   not null,
  7314.             CASE_SENSITIVE        smallint   not null,
  7315.             SEARCHABLE            smallint   not null,
  7316.             UNSIGNED_ATTRIBUTE    smallint   null,
  7317.             MONEY    smallint    not null,
  7318.             AUTO_INCREMENT        smallint    null,
  7319.             LOCAL_TYPE_NAME     varchar(32) null,
  7320.             MINIMUM_SCALE        smallint     null,
  7321.             MAXIMUM_SCALE        smallint   null,
  7322.             SQL_DATA_TYPE        smallint      not null,
  7323.             SQL_DATETIME_SUB    smallint   null,
  7324.             NUM_PREC_RADIX        int     null,
  7325.             INTERVAL_PRECISION    smallint    NULL,
  7326.             USERTYPE            smallint not null)
  7327.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  7328.         exec @ret = sp_cursoropen @handle output,
  7329.             'select * from #spdatatypeinfo',
  7330.             @scrollopt output, @ccopt output, @rows output
  7331.         drop table #spdatatypeinfo
  7332.     end
  7333.     else if @procname = 'sp_fkeys'
  7334.     begin
  7335.         create table #spfkeys (
  7336.             PKTABLE_QUALIFIER varchar(32)     null,
  7337.             PKTABLE_OWNER varchar(32)    null,
  7338.             PKTABLE_NAME varchar(32)  not null,
  7339.             PKCOLUMN_NAME varchar(32)    not null,
  7340.             FKTABLE_QUALIFIER varchar(32)    null,
  7341.             FKTABLE_OWNER varchar(32)    null,
  7342.             FKTABLE_NAME varchar(32)  not null,
  7343.             FKCOLUMN_NAME varchar(32)    not null,
  7344.             KEY_SEQ smallint not null,
  7345.             UPDATE_RULE smallint null,
  7346.             DELETE_RULE smallint null,
  7347.             FK_NAME varchar(32) null,
  7348.             PK_NAME varchar(32) null,
  7349.             DEFERRABILITY smallint null
  7350.             )
  7351.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  7352.         exec @ret = sp_cursoropen @handle output,
  7353.             'select * from #spfkeys',
  7354.             @scrollopt output, @ccopt output, @rows output
  7355.         drop table #spfkeys
  7356.     end
  7357.     else if @procname = 'sp_pkeys'
  7358.     begin
  7359.         create table #sppkeys (
  7360.             TABLE_QUALIFIER varchar(32)   null,
  7361.             TABLE_OWNER varchar(32)   null,
  7362.             TABLE_NAME varchar(32)    not null,
  7363.             COLUMN_NAME varchar(32)  not null,
  7364.             KEY_SEQ smallint not null,
  7365.             PK_NAME varchar(32) null
  7366.             )
  7367.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  7368.         exec @ret = sp_cursoropen @handle output,
  7369.             'select * from #sppkeys',
  7370.             @scrollopt output, @ccopt output, @rows output
  7371.         drop table #sppkeys
  7372.     end
  7373.     else if @procname = 'sp_special_columns'
  7374.     begin
  7375.         create table #spspeccol (
  7376.             SCOPE smallint null,
  7377.             COLUMN_NAME varchar(32) not null,
  7378.             DATA_TYPE smallint not null,
  7379.             TYPE_NAME varchar(32) not null,
  7380.             "PRECISION" int null,
  7381.             LENGTH int null,
  7382.             SCALE smallint null,
  7383.             PSEUDO_COLUMN smallint null
  7384.             )
  7385.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  7386.         exec @ret = sp_cursoropen @handle output,
  7387.             'select * from #spspeccol',
  7388.             @scrollopt output, @ccopt output, @rows output
  7389.         drop table #spspeccol
  7390.     end
  7391.     else if @procname = 'sp_sproc_columns'
  7392.     begin
  7393.         create table #spproccol (
  7394.             PROCEDURE_QUALIFIER varchar(32)  null,
  7395.             PROCEDURE_OWNER varchar(32)  null,
  7396.             PROCEDURE_NAME varchar(32)    not null,
  7397.             COLUMN_NAME varchar(32) not null,
  7398.             COLUMN_TYPE smallint not null,
  7399.             DATA_TYPE smallint not null,
  7400.             TYPE_NAME varchar(32) not null,
  7401.             "PRECISION" int null,
  7402.             LENGTH int null,
  7403.             SCALE smallint null,
  7404.             RADIX smallint null,
  7405.             NULLABLE smallint not null,
  7406.             REMARKS varchar(254) null,
  7407.             COLUMN_DEF varchar(255) null,
  7408.             SQL_DATA_TYPE smallint not null,
  7409.             SQL_DATETIME_SUB smallint null,
  7410.             CHAR_OCTET_LENGTH int null,
  7411.             ORDINAL_POSITION int not null,
  7412.             IS_NULLABLE varchar(254) null,
  7413.             SS_DATA_TYPE tinyint null
  7414.             )
  7415.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  7416.         exec @ret = sp_cursoropen @handle output,
  7417.             'select * from #spproccol',
  7418.             @scrollopt output, @ccopt output, @rows output
  7419.         drop table #spproccol
  7420.     end
  7421.     else if @procname = 'sp_statistics'
  7422.     begin
  7423.         create table #spstatistics (
  7424.             TABLE_QUALIFIER varchar(32)   null,
  7425.             TABLE_OWNER varchar(32)   null,
  7426.             TABLE_NAME varchar(32)    not null,
  7427.             NON_UNIQUE smallint null,
  7428.             INDEX_QUALIFIER varchar(32) null,
  7429.             INDEX_NAME varchar(32)    null,
  7430.             TYPE smallint not null,
  7431.             SEQ_IN_INDEX smallint null,
  7432.             COLUMN_NAME varchar(32) null,
  7433.             COLLATION char(1) null,
  7434.             CARDINALITY int null,
  7435.             PAGES int null,
  7436.             FILTER_CONDITION varchar(128) null
  7437.             )
  7438.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  7439.         exec @ret = sp_cursoropen @handle output,
  7440.             'select * from #spstatistics',
  7441.             @scrollopt output, @ccopt output, @rows output
  7442.         drop table #spstatistics
  7443.     end
  7444.     else if @procname = 'sp_stored_procedures'
  7445.     begin
  7446.         create table #spprocedures (
  7447.             PROCEDURE_QUALIFIER varchar(32)  null,
  7448.             PROCEDURE_OWNER varchar(32)  null,
  7449.             PROCEDURE_NAME varchar(32)    not null,
  7450.             NUM_INPUT_PARAMS int null,
  7451.             NUM_OUTPUT_PARAMS int null,
  7452.             NUM_RESULT_SETS int null,
  7453.             REMARKS varchar(254) null,
  7454.             PROCEDURE_TYPE smallint null
  7455.             )
  7456.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  7457.         exec @ret = sp_cursoropen @handle output,
  7458.             'select * from #spprocedures',
  7459.             @scrollopt output, @ccopt output, @rows output
  7460.         drop table #spprocedures
  7461.     end
  7462.     else if @procname = 'sp_table_privileges'
  7463.     begin
  7464.         create table #sptabpriv (
  7465.             TABLE_QUALIFIER varchar(32) null,
  7466.             TABLE_OWNER varchar(32) null,
  7467.             TABLE_NAME varchar(32)    not null,
  7468.             GRANTOR varchar(32) null,
  7469.             GRANTEE varchar(32) not null,
  7470.             PRIVILEGE varchar(32) not null,
  7471.             IS_GRANTABLE varchar(3) null
  7472.             )
  7473.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  7474.         exec @ret = sp_cursoropen @handle output,
  7475.             'select * from #sptabpriv',
  7476.             @scrollopt output, @ccopt output, @rows output
  7477.         drop table #sptabpriv
  7478.     end
  7479.     else if @procname = 'sp_tables'
  7480.     begin
  7481.         create table #sptables (
  7482.             TABLE_QUALIFIER varchar(32) null,
  7483.             TABLE_OWNER varchar(32) null,
  7484.             TABLE_NAME varchar(32)    null,
  7485.             TABLE_TYPE     varchar(32) null,
  7486.             REMARKS varchar(254) null)
  7487.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  7488.         exec @ret = sp_cursoropen @handle output,
  7489.             'select * from #sptables',
  7490.             @scrollopt output, @ccopt output, @rows output
  7491.         drop table #sptables
  7492.     end
  7493.     select @ret = isnull(@ret,0)
  7494.     return isnull(@ret,0)
  7495. go
  7496.  
  7497. if (charindex('7.00', @@version) > 0)
  7498.     drop procedure sp_ddopen
  7499. else
  7500. begin
  7501.     print ''
  7502.     print ''
  7503.     print 'Warning:'
  7504.     print 'you are installing the stored procedures '
  7505.     print 'on a pre 7.0 SQL Server.'
  7506.     print 'Ignore the following errors.'
  7507. end
  7508. go
  7509.  
  7510. /*    Procedure for 7.00 server */
  7511. create procedure sp_ddopen; 1(
  7512.                @handle            int output,
  7513.                @procname        sysname,
  7514.                @scrollopt        int output,
  7515.                @ccopt            int output,
  7516.                @rows            int output,
  7517.                @p1                nvarchar(774) = null,
  7518.                @p2                nvarchar(774) = null,
  7519.                @p3                nvarchar(774) = null,
  7520.                @p4                nvarchar(774) = null,
  7521.                @p5                nvarchar(774) = null,
  7522.                @p6                nvarchar(774) = null,
  7523.                @p7                int = null,
  7524.                @ODBCVer         int = 2)
  7525. as
  7526.     set nocount on
  7527.     declare @ret int
  7528.  
  7529.     if @procname = 'sp_column_privileges'
  7530.     begin
  7531.         exec @ret = sp_ddopen;2 @handle output, 
  7532.                             @scrollopt output, 
  7533.                             @ccopt output, 
  7534.                             @rows output, 
  7535.                             @p1,
  7536.                             @p2,
  7537.                             @p3,
  7538.                             @p4
  7539.     end
  7540.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  7541.     begin
  7542.         exec @ret = sp_ddopen;3 @handle output,
  7543.                             @procname,
  7544.                             @scrollopt output, 
  7545.                             @ccopt output, 
  7546.                             @rows output, 
  7547.                             @p1,
  7548.                             @p2,
  7549.                             @p3,
  7550.                             @p4,
  7551.                             @p5,
  7552.                             @ODBCVer
  7553.     end
  7554.     else if @procname = 'sp_datatype_info'
  7555.     begin
  7556.         exec @ret = sp_ddopen;4 @handle output,
  7557.                             @scrollopt output, 
  7558.                             @ccopt output, 
  7559.                             @rows output, 
  7560.                             @p7,
  7561.                             @ODBCVer
  7562.     end
  7563.     else if @procname = 'sp_fkeys'
  7564.     begin
  7565.         exec @ret = sp_ddopen;5 @handle output, 
  7566.                             @scrollopt output, 
  7567.                             @ccopt output, 
  7568.                             @rows output, 
  7569.                             @p1,
  7570.                             @p2,
  7571.                             @p3,
  7572.                             @p4,
  7573.                             @p5,
  7574.                             @p6
  7575.     end
  7576.     else if @procname = 'sp_pkeys'
  7577.     begin
  7578.         exec @ret = sp_ddopen;6 @handle output, 
  7579.                             @scrollopt output, 
  7580.                             @ccopt output, 
  7581.                             @rows output, 
  7582.                             @p1,
  7583.                             @p2,
  7584.                             @p3
  7585.     end
  7586.     else if @procname = 'sp_special_columns'
  7587.     begin
  7588.         exec @ret = sp_ddopen;7 @handle output, 
  7589.                             @scrollopt output, 
  7590.                             @ccopt output, 
  7591.                             @rows output, 
  7592.                             @p1,
  7593.                             @p2,
  7594.                             @p3,
  7595.                             @p4,
  7596.                             @p5,
  7597.                             @p6,
  7598.                             @ODBCVer
  7599.     end
  7600.     else if @procname = 'sp_sproc_columns'
  7601.     begin
  7602.         exec @ret = sp_ddopen;8 @handle output, 
  7603.                             @scrollopt output, 
  7604.                             @ccopt output, 
  7605.                             @rows output, 
  7606.                             @p1,
  7607.                             @p2,
  7608.                             @p3,
  7609.                             @p4,
  7610.                             @ODBCVer
  7611.     end
  7612.     else if @procname = 'sp_statistics'
  7613.     begin
  7614.         exec @ret = sp_ddopen;9 @handle output, 
  7615.                             @scrollopt output, 
  7616.                             @ccopt output, 
  7617.                             @rows output, 
  7618.                             @p1,
  7619.                             @p2,
  7620.                             @p3,
  7621.                             @p4,
  7622.                             @p5,
  7623.                             @p6
  7624.     end
  7625.     else if @procname = 'sp_stored_procedures'
  7626.     begin
  7627.         exec @ret = sp_ddopen;10 @handle output, 
  7628.                              @scrollopt output, 
  7629.                              @ccopt output, 
  7630.                              @rows output, 
  7631.                              @p1,
  7632.                              @p2,
  7633.                              @p3
  7634.     end
  7635.     else if @procname = 'sp_table_privileges'
  7636.     begin
  7637.         exec @ret = sp_ddopen;11 @handle output, 
  7638.                              @scrollopt output, 
  7639.                              @ccopt output, 
  7640.                              @rows output, 
  7641.                              @p1,
  7642.                              @p2,
  7643.                              @p3
  7644.     end
  7645.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  7646.     begin
  7647.         exec @ret = sp_ddopen;12 @handle output,
  7648.                             @procname,
  7649.                             @scrollopt output, 
  7650.                             @ccopt output, 
  7651.                             @rows output, 
  7652.                             @p1,
  7653.                             @p2,
  7654.                             @p3,
  7655.                             @p4,
  7656.                             @p5
  7657.     end
  7658.     else
  7659.         print 'Unknown dd_open procedure'
  7660.     select @ret = isnull(@ret,0)
  7661.     return isnull(@ret,0)
  7662. go
  7663.  
  7664.  
  7665. if (charindex('7.00', @@version) = 0)
  7666. begin
  7667.     print ''
  7668.     print ''
  7669.     print 'Warning:'
  7670.     print 'you are installing the stored procedures '
  7671.     print 'on a pre 7.0 SQL Server.'
  7672.     print 'Ignore the following errors.'
  7673. end
  7674. go
  7675.  
  7676. create procedure sp_ddopen; 2(
  7677.                @handle            int output,
  7678.                @scrollopt        int output,
  7679.                @ccopt            int output,
  7680.                @rows            int output,
  7681.                @p1                nvarchar(774),
  7682.                @p2                nvarchar(774),
  7683.                @p3                nvarchar(774),
  7684.                @p4                nvarchar(774))
  7685. as
  7686.     set nocount on
  7687.     declare @ret int
  7688.  
  7689.     create table #spcolpriv (
  7690.         TABLE_QUALIFIER sysname null,
  7691.         TABLE_OWNER sysname null,
  7692.         TABLE_NAME sysname not null,
  7693.         COLUMN_NAME sysname not null,
  7694.         GRANTOR sysname null,
  7695.         GRANTEE sysname not null,
  7696.         PRIVILEGE varchar(32) not null,
  7697.         IS_GRANTABLE varchar(3) null
  7698.         )
  7699.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  7700.     exec @ret = sp_cursoropen @handle output,
  7701.         'select * from #spcolpriv',
  7702.         @scrollopt output, @ccopt output, @rows output
  7703.     drop table #spcolpriv
  7704.     return @ret
  7705. go
  7706.  
  7707.  
  7708. if (charindex('7.00', @@version) = 0)
  7709. begin
  7710.     print ''
  7711.     print ''
  7712.     print 'Warning:'
  7713.     print 'you are installing the stored procedures '
  7714.     print 'on a pre 7.0 SQL Server.'
  7715.     print 'Ignore the following errors.'
  7716. end
  7717. go
  7718.  
  7719. create procedure sp_ddopen; 3(
  7720.                @handle            int output,
  7721.                @procname        sysname,
  7722.                @scrollopt        int output,
  7723.                @ccopt            int output,
  7724.                @rows            int output,
  7725.                @p1                nvarchar(774),
  7726.                @p2                nvarchar(774),
  7727.                @p3                nvarchar(774),
  7728.                @p4                nvarchar(774),
  7729.                @p5                nvarchar(774),
  7730.                @ODBCVer         int)
  7731. as
  7732.     set nocount on
  7733.     declare @ret int
  7734.  
  7735.     create table #spcolumns (
  7736.         TABLE_QUALIFIER sysname null,
  7737.         TABLE_OWNER sysname null,
  7738.         TABLE_NAME sysname not null,
  7739.         COLUMN_NAME sysname not null,
  7740.         DATA_TYPE smallint not null,
  7741.         TYPE_NAME sysname not null,
  7742.         "PRECISION" int null,
  7743.         LENGTH int null,
  7744.         SCALE smallint null,
  7745.         RADIX smallint null,
  7746.         NULLABLE smallint not null,
  7747.         REMARKS varchar(254) null,
  7748.         COLUMN_DEF nvarchar(3000) null,
  7749.         SQL_DATA_TYPE smallint not null,
  7750.         SQL_DATETIME_SUB smallint null,
  7751.         CHAR_OCTET_LENGTH int null,
  7752.         ORDINAL_POSITION int not null,
  7753.         IS_NULLABLE varchar(254) null,
  7754.         SS_DATA_TYPE tinyint null
  7755.         )
  7756.  
  7757.     if @procname = 'sp_columns'
  7758.     begin
  7759.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  7760.     end
  7761.     else
  7762.     begin
  7763.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  7764.     end
  7765.     exec @ret = sp_cursoropen @handle output,
  7766.         'select * from #spcolumns',
  7767.         @scrollopt output, @ccopt output, @rows output
  7768.     drop table #spcolumns
  7769.     return @ret
  7770. go
  7771.  
  7772.  
  7773.  
  7774. if (charindex('7.00', @@version) = 0)
  7775. begin
  7776.     print ''
  7777.     print ''
  7778.     print 'Warning:'
  7779.     print 'you are installing the stored procedures '
  7780.     print 'on a pre 7.0 SQL Server.'
  7781.     print 'Ignore the following errors.'
  7782. end
  7783. go
  7784.  
  7785. create procedure sp_ddopen; 4(
  7786.                @handle            int output,
  7787.                @scrollopt        int output,
  7788.                @ccopt            int output,
  7789.                @rows            int output,
  7790.                @p7                int,
  7791.                @ODBCVer         int)
  7792. as
  7793.     set nocount on
  7794.     declare @ret int
  7795.  
  7796.     create table #spdatatypeinfo (
  7797.         TYPE_NAME            sysname  not null,
  7798.         DATA_TYPE            smallint not null,
  7799.         "PRECISION"            int null,
  7800.         LITERAL_PREFIX        varchar(32)    null,
  7801.         LITERAL_SUFFIX        varchar(32)    null,
  7802.         CREATE_PARAMS        varchar(32)    null,
  7803.         NULLABLE            smallint   not null,
  7804.         CASE_SENSITIVE        smallint   not null,
  7805.         SEARCHABLE            smallint   not null,
  7806.         UNSIGNED_ATTRIBUTE    smallint   null,
  7807.         MONEY    smallint    not null,
  7808.         AUTO_INCREMENT        smallint    null,
  7809.         LOCAL_TYPE_NAME     sysname null,
  7810.         MINIMUM_SCALE        smallint     null,
  7811.         MAXIMUM_SCALE        smallint   null,
  7812.         SQL_DATA_TYPE        smallint      not null,
  7813.         SQL_DATETIME_SUB    smallint   null,
  7814.         NUM_PREC_RADIX        int     null,
  7815.         INTERVAL_PRECISION    smallint    NULL,
  7816.         USERTYPE            smallint not null)
  7817.  
  7818.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  7819.     exec @ret = sp_cursoropen @handle output,
  7820.         'select * from #spdatatypeinfo',
  7821.         @scrollopt output, @ccopt output, @rows output
  7822.     drop table #spdatatypeinfo
  7823.     return @ret
  7824. go
  7825.  
  7826.  
  7827.  
  7828. if (charindex('7.00', @@version) = 0)
  7829. begin
  7830.     print ''
  7831.     print ''
  7832.     print 'Warning:'
  7833.     print 'you are installing the stored procedures '
  7834.     print 'on a pre 7.0 SQL Server.'
  7835.     print 'Ignore the following errors.'
  7836. end
  7837. go
  7838.  
  7839. create procedure sp_ddopen; 5(
  7840.                @handle            int output,
  7841.                @scrollopt        int output,
  7842.                @ccopt            int output,
  7843.                @rows            int output,
  7844.                @p1                nvarchar(774),
  7845.                @p2                nvarchar(774),
  7846.                @p3                nvarchar(774),
  7847.                @p4                nvarchar(774),
  7848.                @p5                nvarchar(774),
  7849.                @p6                nvarchar(774))
  7850. as
  7851.     set nocount on
  7852.     declare @ret int
  7853.  
  7854.     create table #spfkeys (
  7855.         PKTABLE_QUALIFIER sysname     null,
  7856.         PKTABLE_OWNER sysname    null,
  7857.         PKTABLE_NAME sysname  not null,
  7858.         PKCOLUMN_NAME sysname  not null,
  7859.         FKTABLE_QUALIFIER sysname    null,
  7860.         FKTABLE_OWNER sysname    null,
  7861.         FKTABLE_NAME sysname  not null,
  7862.         FKCOLUMN_NAME sysname  not null,
  7863.         KEY_SEQ smallint not null,
  7864.         UPDATE_RULE smallint null,
  7865.         DELETE_RULE smallint null,
  7866.         FK_NAME sysname null,
  7867.         PK_NAME sysname null,
  7868.         DEFERRABILITY smallint null
  7869.         )
  7870.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  7871.     exec @ret = sp_cursoropen @handle output,
  7872.         'select * from #spfkeys',
  7873.         @scrollopt output, @ccopt output, @rows output
  7874.     drop table #spfkeys
  7875.     return @ret
  7876. go
  7877.  
  7878.  
  7879.  
  7880. if (charindex('7.00', @@version) = 0)
  7881. begin
  7882.     print ''
  7883.     print ''
  7884.     print 'Warning:'
  7885.     print 'you are installing the stored procedures '
  7886.     print 'on a pre 7.0 SQL Server.'
  7887.     print 'Ignore the following errors.'
  7888. end
  7889. go
  7890.  
  7891. create procedure sp_ddopen; 6(
  7892.                @handle            int output,
  7893.                @scrollopt        int output,
  7894.                @ccopt            int output,
  7895.                @rows            int output,
  7896.                @p1                nvarchar(774),
  7897.                @p2                nvarchar(774),
  7898.                @p3                nvarchar(774))
  7899. as
  7900.     set nocount on
  7901.     declare @ret int
  7902.  
  7903.     create table #sppkeys (
  7904.         TABLE_QUALIFIER sysname   null,
  7905.         TABLE_OWNER sysname   null,
  7906.         TABLE_NAME sysname    not null,
  7907.         COLUMN_NAME sysname  not null,
  7908.         KEY_SEQ smallint not null,
  7909.         PK_NAME sysname null
  7910.         )
  7911.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  7912.     exec @ret = sp_cursoropen @handle output,
  7913.         'select * from #sppkeys',
  7914.         @scrollopt output, @ccopt output, @rows output
  7915.     drop table #sppkeys
  7916.     return @ret
  7917. go
  7918.  
  7919.  
  7920.  
  7921. if (charindex('7.00', @@version) = 0)
  7922. begin
  7923.     print ''
  7924.     print ''
  7925.     print 'Warning:'
  7926.     print 'you are installing the stored procedures '
  7927.     print 'on a pre 7.0 SQL Server.'
  7928.     print 'Ignore the following errors.'
  7929. end
  7930. go
  7931.  
  7932. create procedure sp_ddopen; 7(
  7933.                @handle            int output,
  7934.                @scrollopt        int output,
  7935.                @ccopt            int output,
  7936.                @rows            int output,
  7937.                @p1                nvarchar(774),
  7938.                @p2                nvarchar(774),
  7939.                @p3                nvarchar(774),
  7940.                @p4                nvarchar(774),
  7941.                @p5                nvarchar(774),
  7942.                @p6                nvarchar(774),
  7943.                @ODBCVer         int)
  7944. as
  7945.     set nocount on
  7946.     declare @ret int
  7947.  
  7948.     create table #spspeccol (
  7949.         SCOPE smallint null,
  7950.         COLUMN_NAME sysname not null,
  7951.         DATA_TYPE smallint not null,
  7952.         TYPE_NAME sysname not null,
  7953.         "PRECISION" int null,
  7954.         LENGTH int null,
  7955.         SCALE smallint null,
  7956.         PSEUDO_COLUMN smallint null
  7957.         )
  7958.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  7959.     exec @ret = sp_cursoropen @handle output,
  7960.         'select * from #spspeccol',
  7961.         @scrollopt output, @ccopt output, @rows output
  7962.     drop table #spspeccol
  7963.     return @ret
  7964. go
  7965.  
  7966.  
  7967.  
  7968. if (charindex('7.00', @@version) = 0)
  7969. begin
  7970.     print ''
  7971.     print ''
  7972.     print 'Warning:'
  7973.     print 'you are installing the stored procedures '
  7974.     print 'on a pre 7.0 SQL Server.'
  7975.     print 'Ignore the following errors.'
  7976. end
  7977. go
  7978.  
  7979. create procedure sp_ddopen; 8(
  7980.                @handle            int output,
  7981.                @scrollopt        int output,
  7982.                @ccopt            int output,
  7983.                @rows            int output,
  7984.                @p1                nvarchar(774),
  7985.                @p2                nvarchar(774),
  7986.                @p3                nvarchar(774),
  7987.                @p4                nvarchar(774),
  7988.                @ODBCVer         int)
  7989. as
  7990.     set nocount on
  7991.     declare @ret int
  7992.  
  7993.     create table #spproccol (
  7994.         PROCEDURE_QUALIFIER sysname  null,
  7995.         PROCEDURE_OWNER sysname  null,
  7996.         PROCEDURE_NAME sysname not null,
  7997.         COLUMN_NAME sysname not null,
  7998.         COLUMN_TYPE smallint not null,
  7999.         DATA_TYPE smallint not null,
  8000.         TYPE_NAME sysname not null,
  8001.         "PRECISION" int null,
  8002.         LENGTH int null,
  8003.         SCALE smallint null,
  8004.         RADIX smallint null,
  8005.         NULLABLE smallint not null,
  8006.         REMARKS varchar(254) null,
  8007.         COLUMN_DEF nvarchar(3000) null,
  8008.         SQL_DATA_TYPE smallint not null,
  8009.         SQL_DATETIME_SUB smallint null,
  8010.         CHAR_OCTET_LENGTH int null,
  8011.         ORDINAL_POSITION int not null,
  8012.         IS_NULLABLE varchar(254) null,
  8013.         SS_DATA_TYPE tinyint null
  8014.         )
  8015.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  8016.     exec @ret = sp_cursoropen @handle output,
  8017.         'select * from #spproccol',
  8018.         @scrollopt output, @ccopt output, @rows output
  8019.     drop table #spproccol
  8020.     return @ret
  8021. go
  8022.  
  8023.  
  8024.  
  8025. if (charindex('7.00', @@version) = 0)
  8026. begin
  8027.     print ''
  8028.     print ''
  8029.     print 'Warning:'
  8030.     print 'you are installing the stored procedures '
  8031.     print 'on a pre 7.0 SQL Server.'
  8032.     print 'Ignore the following errors.'
  8033. end
  8034. go
  8035.  
  8036. create procedure sp_ddopen; 9(
  8037.                @handle            int output,
  8038.                @scrollopt        int output,
  8039.                @ccopt            int output,
  8040.                @rows            int output,
  8041.                @p1                nvarchar(774),
  8042.                @p2                nvarchar(774),
  8043.                @p3                nvarchar(774),
  8044.                @p4                nvarchar(774),
  8045.                @p5                nvarchar(774),
  8046.                @p6                nvarchar(774))
  8047. as
  8048.     set nocount on
  8049.     declare @ret int
  8050.  
  8051.     create table #spstatistics (
  8052.         TABLE_QUALIFIER sysname   null,
  8053.         TABLE_OWNER sysname   null,
  8054.         TABLE_NAME sysname    not null,
  8055.         NON_UNIQUE smallint null,
  8056.         INDEX_QUALIFIER sysname null,
  8057.         INDEX_NAME sysname null,
  8058.         TYPE smallint not null,
  8059.         SEQ_IN_INDEX smallint null,
  8060.         COLUMN_NAME sysname null,
  8061.         COLLATION char(1) null,
  8062.         CARDINALITY int null,
  8063.         PAGES int null,
  8064.         FILTER_CONDITION varchar(128) null
  8065.         )
  8066.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  8067.     exec @ret = sp_cursoropen @handle output,
  8068.         'select * from #spstatistics',
  8069.         @scrollopt output, @ccopt output, @rows output
  8070.     drop table #spstatistics
  8071.     return @ret
  8072. go
  8073.                
  8074.  
  8075.  
  8076. if (charindex('7.00', @@version) = 0)
  8077. begin
  8078.     print ''
  8079.     print ''
  8080.     print 'Warning:'
  8081.     print 'you are installing the stored procedures '
  8082.     print 'on a pre 7.0 SQL Server.'
  8083.     print 'Ignore the following errors.'
  8084. end
  8085. go
  8086.  
  8087. create procedure sp_ddopen; 10(
  8088.                @handle            int output,
  8089.                @scrollopt        int output,
  8090.                @ccopt            int output,
  8091.                @rows            int output,
  8092.                @p1                nvarchar(774),
  8093.                @p2                nvarchar(774),
  8094.                @p3                nvarchar(774))
  8095. as
  8096.     set nocount on
  8097.     declare @ret int
  8098.  
  8099.     create table #spprocedures (
  8100.         PROCEDURE_QUALIFIER sysname  null,
  8101.         PROCEDURE_OWNER sysname  null,
  8102.         PROCEDURE_NAME sysname not null,
  8103.         NUM_INPUT_PARAMS int null,
  8104.         NUM_OUTPUT_PARAMS int null,
  8105.         NUM_RESULT_SETS int null,
  8106.         REMARKS varchar(254) null,
  8107.         PROCEDURE_TYPE smallint null
  8108.         )
  8109.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  8110.     exec @ret = sp_cursoropen @handle output,
  8111.         'select * from #spprocedures',
  8112.         @scrollopt output, @ccopt output, @rows output
  8113.     drop table #spprocedures
  8114.     return @ret
  8115. go
  8116.  
  8117.  
  8118.  
  8119. if (charindex('7.00', @@version) = 0)
  8120. begin
  8121.     print ''
  8122.     print ''
  8123.     print 'Warning:'
  8124.     print 'you are installing the stored procedures '
  8125.     print 'on a pre 7.0 SQL Server.'
  8126.     print 'Ignore the following errors.'
  8127. end
  8128. go
  8129.  
  8130. create procedure sp_ddopen; 11(
  8131.                @handle            int output,
  8132.                @scrollopt        int output,
  8133.                @ccopt            int output,
  8134.                @rows            int output,
  8135.                @p1                nvarchar(774),
  8136.                @p2                nvarchar(774),
  8137.                @p3                nvarchar(774))
  8138. as
  8139.     set nocount on
  8140.     declare @ret int
  8141.  
  8142.     create table #sptabpriv (
  8143.         TABLE_QUALIFIER sysname null,
  8144.         TABLE_OWNER sysname null,
  8145.         TABLE_NAME sysname not null,
  8146.         GRANTOR sysname null,
  8147.         GRANTEE sysname not null,
  8148.         PRIVILEGE varchar(32) not null,
  8149.         IS_GRANTABLE varchar(3) null
  8150.         )
  8151.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  8152.     exec @ret = sp_cursoropen @handle output,
  8153.         'select * from #sptabpriv',
  8154.         @scrollopt output, @ccopt output, @rows output
  8155.     drop table #sptabpriv
  8156.     return @ret
  8157. go
  8158.  
  8159.  
  8160.  
  8161. if (charindex('7.00', @@version) = 0)
  8162. begin
  8163.     print ''
  8164.     print ''
  8165.     print 'Warning:'
  8166.     print 'you are installing the stored procedures '
  8167.     print 'on a pre 7.0 SQL Server.'
  8168.     print 'Ignore the following errors.'
  8169. end
  8170. go
  8171.  
  8172. create procedure sp_ddopen; 12(
  8173.                @handle            int output,
  8174.                @procname        sysname,
  8175.                @scrollopt        int output,
  8176.                @ccopt            int output,
  8177.                @rows            int output,
  8178.                @p1                nvarchar(774),
  8179.                @p2                nvarchar(774),
  8180.                @p3                nvarchar(774),
  8181.                @p4                nvarchar(774),
  8182.                @p5                nvarchar(774))
  8183. as
  8184.     set nocount on
  8185.     declare @ret int
  8186.  
  8187.     create table #sptables (
  8188.         TABLE_QUALIFIER sysname null,
  8189.         TABLE_OWNER sysname null,
  8190.         TABLE_NAME sysname null,
  8191.         TABLE_TYPE    varchar(32) null,
  8192.         REMARKS varchar(254) null)
  8193.     if @procname = 'sp_tables'
  8194.     begin
  8195.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  8196.     end
  8197.     else
  8198.     begin
  8199.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  8200.     end
  8201.     exec @ret = sp_cursoropen @handle output,
  8202.         'select * from #sptables',
  8203.         @scrollopt output, @ccopt output, @rows output
  8204.     drop table #sptables
  8205.     return @ret
  8206. go
  8207.  
  8208. grant execute on sp_ddopen to public
  8209. go
  8210.  
  8211. print 'creating sp_tableswc'
  8212. go
  8213.  
  8214. if (charindex('7.00', @@version) = 0)
  8215. begin
  8216.     print ''
  8217.     print ''
  8218.     print 'Warning:'
  8219.     print 'you are installing the stored procedures '
  8220.     print 'on a pre 7.0 SQL Server.'
  8221.     print 'Ignore the following errors.'
  8222. end
  8223. go
  8224.  
  8225. /*    Procedure for 7.00 server */
  8226. create procedure sp_tableswc(
  8227.                @table_name        nvarchar(384)    = null,
  8228.                @table_owner     nvarchar(384)    = null,
  8229.                @table_qualifier sysname    = null,
  8230.                @table_type        varchar(100) = null)
  8231. as
  8232.     declare @databasename    sysname
  8233.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  8234.  
  8235.     create table #sptables (
  8236.         TABLE_QUALIFIER sysname null,
  8237.         TABLE_OWNER sysname null,
  8238.         TABLE_NAME sysname null,
  8239.         TABLE_TYPE    varchar(32) null,
  8240.         REMARKS varchar(254) null)
  8241.  
  8242.     declare databases CURSOR FOR
  8243.         select name from master..sysdatabases
  8244.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  8245.         for read only
  8246.  
  8247.     open databases
  8248.     fetch next from databases into @databasename
  8249.     while (@@FETCH_STATUS <> -1)
  8250.     begin
  8251.         if (charindex('%', @databasename) = 0 and
  8252.             charindex('_', @databasename) = 0)
  8253.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  8254.             select @qualprocname = @databasename + '..sp_tables'
  8255.             insert into #sptables exec @qualprocname
  8256.                 @table_name, @table_owner, @databasename, @table_type
  8257.         end
  8258.         fetch next from databases into @databasename
  8259.     end
  8260.     deallocate databases
  8261.     select * from #sptables
  8262.         order by 4, 1, 2, 3
  8263. go
  8264.  
  8265. grant execute on sp_tableswc to public
  8266. go
  8267.  
  8268. dump tran master with no_log
  8269. go
  8270.  
  8271. /*-------------------------------------------------------------------------*/
  8272. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  8273. /*-------------------------------------------------------------------------*/
  8274.  
  8275. print ''
  8276. print 'creating spt_provider_types'
  8277. go
  8278. if (charindex('7.00', @@version) = 0)
  8279. begin /* Pre 7.00 Server */
  8280. create table spt_provider_types
  8281.     (
  8282.     ss_dtype        tinyint     not null,
  8283.     fixlen            int         null,        /* datatype len for variable, else null */
  8284.     type_name        sysname        not null,
  8285.     oledb_data_type        smallint    not null,
  8286.     best_match        bit        not null,
  8287.     is_nullable        tinyint        null,
  8288.     case_sensitive        bit        not null,
  8289.     fixed_prec_scale    bit        not null,
  8290.     is_long            bit        not null,
  8291.     auto_unique_value    tinyint        not null,
  8292.     data_precision        int         null,
  8293.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8294.     column_size        int         null,
  8295.     literal_prefix        varchar(32)    null,
  8296.     literal_suffix        varchar(32)     null,
  8297.     searchable        int        not null,
  8298.     unsigned_attribute    tinyint        null,
  8299.     local_type_name     sysname        null
  8300.     )
  8301. print ''
  8302. print ''
  8303. print 'Warning:'
  8304. print 'you are installing the stored procedures '
  8305. print 'on a pre 7.0 SQL Server.'
  8306. print 'Ignore the following errors.'
  8307. end
  8308. go
  8309. if (charindex('7.00', @@version) > 0)
  8310. begin /* 7.00 server*/
  8311. create table spt_provider_types
  8312.     (
  8313.     ss_dtype        tinyint     not null,
  8314.     fixlen            int         null,        /* datatype len for variable, else null */
  8315.     type_name        sysname        not null,
  8316.     oledb_data_type        smallint    not null,
  8317.     best_match        bit        not null,
  8318.     is_nullable        bit        null,
  8319.     case_sensitive        bit        not null,
  8320.     fixed_prec_scale    bit        not null,
  8321.     is_long            bit        not null,
  8322.     auto_unique_value    tinyint        not null,
  8323.     data_precision        int         null,
  8324.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8325.     column_size        int         null,
  8326.     literal_prefix        nvarchar(32)    null,
  8327.     literal_suffix        nvarchar(32)     null,
  8328.     searchable        int        not null,
  8329.     unsigned_attribute    tinyint        null,
  8330.     local_type_name     sysname        null
  8331.     )
  8332. end
  8333. go
  8334.  
  8335. grant select on spt_provider_types to public
  8336. go
  8337. dump tran master with no_log
  8338. go
  8339.  
  8340. /*
  8341. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  8342. */
  8343. begin tran
  8344.  
  8345. /* Get case sensitivity */
  8346. declare @case_sensitive bit
  8347. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  8348. /* Local Char */
  8349. insert into spt_provider_types values
  8350.     (
  8351.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  8352.     0,                /* fixlen */
  8353.     'char',                /* type_name */
  8354.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8355.     0,                /* best_match */
  8356.     null,                /* is_nullable */
  8357.     @case_sensitive,        /* case_sensitive */
  8358.     0,                /* fixed_prec_scale */
  8359.     0,                /* is_long */
  8360.     0,                /* auto_unique_value */
  8361.     null,                /* data_precision */
  8362.     null,                /* numeric_scale */
  8363.     null,                /* column_size */
  8364.     '''',                /* literal_prefix */
  8365.     '''',                /* literal_suffix */
  8366.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8367.     null,                /* unsigned_attribute */
  8368.     'char'                /* local_type_name */
  8369.     )
  8370.  
  8371. /* Local Varchar */
  8372. insert into spt_provider_types values
  8373.     (
  8374.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  8375.     null,                /* fixlen */
  8376.     'varchar',            /* type_name */
  8377.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8378.     1,                /* best_match */
  8379.     null,                /* is_nullable */
  8380.     @case_sensitive,        /* case_sensitive */
  8381.     0,                /* fixed_prec_scale */
  8382.     0,                /* is_long */
  8383.     0,                /* auto_unique_value */
  8384.     null,                /* data_precision */
  8385.     null,                /* numeric_scale */
  8386.     null,                /* column_size */
  8387.     '''',                /* literal_prefix */
  8388.     '''',                /* literal_suffix */
  8389.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8390.     null,                /* unsigned_attribute */
  8391.     'varchar'            /* local_type_name */
  8392.     )
  8393.  
  8394. /* Local Text */
  8395. insert into spt_provider_types values
  8396.     (
  8397.     35  /*SQLTEXT*/,        /* ss_dtype */
  8398.     null,                /* fixlen */
  8399.     'text',                /* type_name */
  8400.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8401.     0,                /* best_match */
  8402.     null,                /* is_nullable */
  8403.     @case_sensitive,        /* case_sensitive */
  8404.     0,                /* fixed_prec_scale */
  8405.     1,                /* is_long */
  8406.     0,                /* auto_unique_value */
  8407.     null,                /* data_precision */
  8408.     null,                /* numeric_scale */
  8409.     2147483647,            /* column_size */
  8410.     '''',                /* literal_prefix */
  8411.     '''',                /* literal_suffix */
  8412.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  8413.     null,                /* unsigned_attribute */
  8414.     'text'                /* local_type_name */
  8415.     )
  8416. commit tran
  8417. go
  8418. dump tran master with no_log
  8419. go
  8420.  
  8421. /*
  8422. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  8423. */
  8424. begin tran
  8425. /* Local Binary */
  8426. insert into spt_provider_types values
  8427.     (
  8428.     45 /*SQLBINARY*/,        /* ss_dtype */
  8429.     0,                /* fixlen */
  8430.     'binary',            /* type_name */
  8431.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8432.     0,                /* best_match */
  8433.     null,                /* is_nullable */
  8434.     0,                /* case_sensitive */
  8435.     0,                /* fixed_prec_scale */
  8436.     0,                /* is_long */
  8437.     0,                /* auto_unique_value */
  8438.     null,                /* data_precision */
  8439.     null,                /* numeric_scale */
  8440.     null,                /* column_size */
  8441.     '0x',                /* literal_prefix */
  8442.     null,                /* literal_suffix */
  8443.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8444.     null,                /* unsigned_attribute */
  8445.     'binary'            /* local_type_name */
  8446.     )
  8447.  
  8448. /* Local Varbinary */
  8449. insert into spt_provider_types values
  8450.     (
  8451.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  8452.     null,                /* fixlen */
  8453.     'varbinary',            /* type_name */
  8454.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8455.     1,                /* best_match */
  8456.     null,                /* is_nullable */
  8457.     0,                /* case_sensitive */
  8458.     0,                /* fixed_prec_scale */
  8459.     0,                /* is_long */
  8460.     0,                /* auto_unique_value */
  8461.     null,                /* data_precision */
  8462.     null,                /* numeric_scale */
  8463.     null,                /* column_size */
  8464.     '0x',                /* literal_prefix */
  8465.     null,                /* literal_suffix */
  8466.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8467.     null,                /* unsigned_attribute */
  8468.     'varbinary'            /* local_type_name */
  8469.     )
  8470.  
  8471. /* Local Image */
  8472. insert into spt_provider_types values
  8473.     (
  8474.     34 /*SQLIMAGE*/,        /* ss_dtype */
  8475.     null,                /* fixlen */
  8476.     'image',            /* type_name */
  8477.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8478.     0,                /* best_match */
  8479.     null,                /* is_nullable */
  8480.     0,                /* case_sensitive */
  8481.     0,                /* fixed_prec_scale */
  8482.     1,                /* is_long */
  8483.     0,                /* auto_unique_value */
  8484.     null,                /* data_precision */
  8485.     null,                /* numeric_scale */
  8486.     2147483647,            /* column_size */
  8487.     '0x',                /* literal_prefix */
  8488.     null,                /* literal_suffix */
  8489.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  8490.     null,                /* unsigned_attribute */
  8491.     'image'                /* local_type_name */
  8492.     )
  8493.  
  8494. commit tran
  8495. go
  8496. dump tran master with no_log
  8497. go
  8498.  
  8499. /*
  8500. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  8501. */
  8502. begin tran
  8503.  
  8504. /* Local Datetime */
  8505. insert into spt_provider_types values
  8506.     (
  8507.     61 /*SQLDATETIME*/,        /* ss_dtype */
  8508.     8,                /* fixlen */
  8509.     'datetime',            /* type_name */
  8510.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8511.     1,                /* best_match */
  8512.     null,                /* is_nullable */
  8513.     0,                /* case_sensitive */
  8514.     0,                /* fixed_prec_scale */
  8515.     0,                /* is_long */
  8516.     0,                /* auto_unique_value */
  8517.     23,                /* data_precision */
  8518.     null,                /* numeric_scale */
  8519.     null,                /* column_size */
  8520.     '''',                /* literal_prefix */
  8521.     '''',                /* literal_suffix */
  8522.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8523.     null,                /* unsigned_attribute */
  8524.     'datetime'            /* local_type_name */
  8525.     )
  8526.  
  8527. /* Local Smalldatetime */
  8528. insert into spt_provider_types values
  8529.     (
  8530.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  8531.     4,                /* fixlen */
  8532.     'smalldatetime',        /* type_name */
  8533.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8534.     0,                /* best_match */
  8535.     null,                /* is_nullable */
  8536.     0,                /* case_sensitive */
  8537.     0,                /* fixed_prec_scale */
  8538.     0,                /* is_long */
  8539.     0,                /* auto_unique_value */
  8540.     16,                /* data_precision */
  8541.     null,                /* numeric_scale */
  8542.     null,                /* column_size */
  8543.     '''',                /* literal_prefix */
  8544.     '''',                /* literal_suffix */
  8545.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8546.     null,                /* unsigned_attribute */
  8547.     'smalldatetime'            /* local_type_name */
  8548.     )
  8549.  
  8550. if (charindex('7.00', @@version) = 0)
  8551.     begin    /*    Add nullable type for non-Sphinx server */
  8552.     
  8553.     /* Local Datetimn */
  8554.     insert into spt_provider_types values
  8555.         (
  8556.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  8557.         4,                /* fixlen */
  8558.         'smalldatetime',        /* type_name */
  8559.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8560.         0,                /* best_match */
  8561.         null,                /* is_nullable */
  8562.         0,                /* case_sensitive */
  8563.         0,                /* fixed_prec_scale */
  8564.         0,                /* is_long */
  8565.         0,                /* auto_unique_value */
  8566.         16,                /* data_precision */
  8567.         null,                /* numeric_scale */
  8568.         null,                /* column_size */
  8569.         '''',                /* literal_prefix */
  8570.         '''',                /* literal_suffix */
  8571.         4 /*DB_SEARCHABLE*/,        /* searchable */
  8572.         null,                /* unsigned_attribute */
  8573.         'smalldatetime'            /* local_type_name */
  8574.         )    
  8575.     insert into spt_provider_types values
  8576.         (
  8577.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  8578.         8,                /* fixlen */
  8579.         'datetime',            /* type_name */
  8580.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8581.         0,                /* best_match */
  8582.         null,                /* is_nullable */
  8583.         0,                /* case_sensitive */
  8584.         0,                /* fixed_prec_scale */
  8585.         0,                /* is_long */
  8586.         0,                /* auto_unique_value */
  8587.         23,                /* data_precision */
  8588.         null,                /* numeric_scale */
  8589.         null,                /* column_size */
  8590.         '''',                /* literal_prefix */
  8591.         '''',                /* literal_suffix */
  8592.         4 /*DB_SEARCHABLE*/,        /* searchable */
  8593.         null,                /* unsigned_attribute */
  8594.         'datetime'            /* local_type_name */
  8595.         )
  8596.     end
  8597.  
  8598. commit tran
  8599. go
  8600. dump tran master with no_log
  8601. go
  8602.  
  8603. /*
  8604. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  8605. */
  8606. begin tran
  8607.  
  8608. /* Local Smallmoney */
  8609. insert into spt_provider_types values
  8610.     (
  8611.     122 /*SQLMONEY4*/,        /* ss_dtype */
  8612.     4,                /* fixlen */
  8613.     'smallmoney',            /* type_name */
  8614.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  8615.     0,                /* best_match */
  8616.     null,                /* is_nullable */
  8617.     0,                /* case_sensitive */
  8618.     1,                /* fixed_prec_scale */
  8619.     0,                /* is_long */
  8620.     0,                /* auto_unique_value */
  8621.     10,                /* data_precision */
  8622.     null,                /* numeric_scale */
  8623.     null,                /* column_size */
  8624.     '$',                /* literal_prefix */
  8625.     null,                /* literal_suffix */
  8626.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8627.     0,                /* unsigned_attribute */
  8628.     'smallmoney'            /* local_type_name */
  8629.     )    
  8630.     
  8631. /* Local Money */
  8632. insert into spt_provider_types values
  8633.     (
  8634.     60 /*SQLMONEY*/,        /* ss_dtype */
  8635.     8,                /* fixlen */
  8636.     'money',            /* type_name */
  8637.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  8638.     1,                /* best_match */
  8639.     null,                /* is_nullable */
  8640.     0,                /* case_sensitive */
  8641.     1,                /* fixed_prec_scale */
  8642.     0,                /* is_long */
  8643.     0,                /* auto_unique_value */
  8644.     19,                /* data_precision */
  8645.     null,                /* numeric_scale */
  8646.     null,                /* column_size */
  8647.     '$',                /* literal_prefix */
  8648.     null,                /* literal_suffix */
  8649.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8650.     0,                /* unsigned_attribute */
  8651.     'money'                /* local_type_name */
  8652.     )    
  8653.  
  8654. if (charindex('7.00', @@version) = 0)
  8655.     begin    /*    Add nullable type for non-Sphinx server */
  8656.  
  8657.     /* Local Moneyn */
  8658.     insert into spt_provider_types values
  8659.         (
  8660.         110 /*SQLMONEYN*/,    /* ss_dtype */
  8661.         4,            /* fixlen */
  8662.         'smallmoney',        /* type_name */
  8663.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  8664.         0,            /* best_match */
  8665.         null,            /* is_nullable */
  8666.         0,            /* case_sensitive */
  8667.         1,            /* fixed_prec_scale */
  8668.         0,            /* is_long */
  8669.         0,            /* auto_unique_value */
  8670.         10,            /* data_precision */
  8671.         null,            /* numeric_scale */
  8672.         null,            /* column_size */
  8673.         '$',            /* literal_prefix */
  8674.         null,            /* literal_suffix */
  8675.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  8676.         0,            /* unsigned_attribute */
  8677.         'smallmoney'        /* local_type_name */
  8678.         )    
  8679.     insert into spt_provider_types values
  8680.         (
  8681.         110 /*SQLMONEYN*/,    /* ss_dtype */
  8682.         8,            /* fixlen */
  8683.         'money',        /* type_name */
  8684.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  8685.         0,            /* best_match */
  8686.         null,            /* is_nullable */
  8687.         0,            /* case_sensitive */
  8688.         1,            /* fixed_prec_scale */
  8689.         0,            /* is_long */
  8690.         0,            /* auto_unique_value */
  8691.         19,            /* data_precision */
  8692.         null,            /* numeric_scale */
  8693.         null,            /* column_size */
  8694.         '$',            /* literal_prefix */
  8695.         null,            /* literal_suffix */
  8696.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  8697.         null,            /* unsigned_attribute */
  8698.         'money'            /* local_type_name */
  8699.         )    
  8700.     end
  8701.  
  8702. commit tran
  8703. go
  8704. dump tran master with no_log
  8705. go
  8706.  
  8707. /*
  8708. ** Insert the spt_provider_types rows for the numeric data types
  8709. */
  8710. begin tran
  8711.  
  8712. /* Local Float */
  8713. insert into spt_provider_types values
  8714.     (
  8715.     62 /*SQLFLT8*/,            /* ss_dtype */
  8716.     8,                /* fixlen */
  8717.     'float',            /* type_name */
  8718.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  8719.     1,                /* best_match */
  8720.     null,                /* is_nullable */
  8721.     0,                /* case_sensitive */
  8722.     0,                /* fixed_prec_scale */
  8723.     0,                /* is_long */
  8724.     0,                /* auto_unique_value */
  8725.     15,                /* data_precision */
  8726.     null,                /* numeric_scale */
  8727.     null,                /* column_size */
  8728.     null,                /* literal_prefix */
  8729.     null,                /* literal_suffix */
  8730.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8731.     0,                /* unsigned_attribute */
  8732.     'float'                /* local_type_name */
  8733.     )    
  8734.  
  8735. /* Local Real */
  8736. insert into spt_provider_types values
  8737.     (
  8738.     59 /*SQLFLT4*/,            /* ss_dtype */
  8739.     4,                /* fixlen */
  8740.     'real',                /* type_name */
  8741.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  8742.     1,                /* best_match */
  8743.     null,                /* is_nullable */
  8744.     0,                /* case_sensitive */
  8745.     0,                /* fixed_prec_scale */
  8746.     0,                /* is_long */
  8747.     0,                /* auto_unique_value */
  8748.     7,                /* data_precision */
  8749.     null,                /* numeric_scale */
  8750.     null,                /* column_size */
  8751.     null,                /* literal_prefix */
  8752.     null,                /* literal_suffix */
  8753.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8754.     0,                /* unsigned_attribute */
  8755.     'real'                /* local_type_name */
  8756.     )    
  8757.  
  8758. /* Local Int */
  8759. insert into spt_provider_types values
  8760.     (
  8761.     56 /*SQLINT4*/,            /* ss_dtype */
  8762.     4,                /* fixlen */
  8763.     'int',                /* type_name */
  8764.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  8765.     1,                /* best_match */
  8766.     null,                /* is_nullable */
  8767.     0,                /* case_sensitive */
  8768.     1,                /* fixed_prec_scale */
  8769.     0,                /* is_long */
  8770.     1,                /* auto_unique_value */
  8771.     10,                /* data_precision */
  8772.     null,                /* numeric_scale */
  8773.     null,                /* column_size */
  8774.     null,                /* literal_prefix */
  8775.     null,                /* literal_suffix */
  8776.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8777.     0,                /* unsigned_attribute */
  8778.     'int'                /* local_type_name */
  8779.     )    
  8780.  
  8781. /* Local Smallint */
  8782. insert into spt_provider_types values
  8783.     (
  8784.     52 /*SQLINT2*/,            /* ss_dtype */
  8785.     2,                /* fixlen */
  8786.     'smallint',            /* type_name */
  8787.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  8788.     1,                /* best_match */
  8789.     null,                /* is_nullable */
  8790.     0,                /* case_sensitive */
  8791.     1,                /* fixed_prec_scale */
  8792.     0,                /* is_long */
  8793.     1,                /* auto_unique_value */
  8794.     5,                /* data_precision */
  8795.     null,                /* numeric_scale */
  8796.     null,                /* column_size */
  8797.     null,                /* literal_prefix */
  8798.     null,                /* literal_suffix */
  8799.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8800.     0,                /* unsigned_attribute */
  8801.     'smallint'            /* local_type_name */
  8802.     )    
  8803.  
  8804. /* Local Tinyint */
  8805. insert into spt_provider_types values
  8806.     (
  8807.     48 /*SQLINT1*/,            /* ss_dtype */
  8808.     1,                /* fixlen */
  8809.     'tinyint',            /* type_name */
  8810.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  8811.     1,                /* best_match */
  8812.     null,                /* is_nullable */
  8813.     0,                /* case_sensitive */
  8814.     1,                /* fixed_prec_scale */
  8815.     0,                /* is_long */
  8816.     1,                /* auto_unique_value */
  8817.     3,                /* data_precision */
  8818.     null,                /* numeric_scale */
  8819.     null,                /* column_size */
  8820.     null,                /* literal_prefix */
  8821.     null,                /* literal_suffix */
  8822.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8823.     1,                /* unsigned_attribute */
  8824.     'tinyint'            /* local_type_name */
  8825.     )    
  8826. commit tran
  8827. go
  8828. dump tran master with no_log
  8829. go
  8830. begin tran    
  8831. if (charindex('6.00', @@version) > 0 or
  8832.     charindex('6.50', @@version) > 0 or
  8833.     charindex('7.00', @@version) > 0)
  8834.     begin    /*    Add 6.0 data types */
  8835.     
  8836.     /* Local Decimal */
  8837.     insert into spt_provider_types values
  8838.         (
  8839.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  8840.         0,                /* fixlen */
  8841.         'decimal',            /* type_name */
  8842.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  8843.         0,                /* best_match */
  8844.         null,                /* is_nullable */
  8845.         0,                /* case_sensitive */
  8846.         1,                /* fixed_prec_scale */
  8847.         0,                /* is_long */
  8848.         1,                /* auto_unique_value */
  8849.         38,                /* data_precision */
  8850.         null,                /* numeric_scale */
  8851.         null,                /* column_size */
  8852.         null,                /* literal_prefix */
  8853.         null,                /* literal_suffix */
  8854.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8855.         0,                /* unsigned_attribute */
  8856.         'decimal'            /* local_type_name */
  8857.         )    
  8858.  
  8859.     /* Local Numeric */
  8860.     insert into spt_provider_types values
  8861.         (
  8862.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  8863.         0,                /* fixlen */
  8864.         'numeric',            /* type_name */
  8865.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  8866.         1,                /* best_match */
  8867.         null,                /* is_nullable */
  8868.         0,                /* case_sensitive */
  8869.         1,                /* fixed_prec_scale */
  8870.         0,                /* is_long */
  8871.         1,                /* auto_unique_value */
  8872.         38,                /* data_precision */
  8873.         null,                /* numeric_scale */
  8874.         null,                /* column_size */
  8875.         null,                /* literal_prefix */
  8876.         null,                /* literal_suffix */
  8877.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8878.         0,                /* unsigned_attribute */
  8879.         'numeric'            /* local_type_name */
  8880.         )    
  8881.     end
  8882. commit tran
  8883. go
  8884. dump tran master with no_log
  8885. go
  8886. begin tran
  8887. if (charindex('7.00', @@version) = 0)
  8888.     begin    /*    Add nullable type for non-Sphinx server */
  8889.     
  8890.     /* Local Floatn */
  8891.     insert into spt_provider_types values
  8892.         (
  8893.         109 /*SQLFLTN*/,        /* ss_dtype */
  8894.         8,                /* fixlen */
  8895.         'float',            /* type_name */
  8896.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  8897.         0,                /* best_match */
  8898.         null,                /* is_nullable */
  8899.         0,                /* case_sensitive */
  8900.         0,                /* fixed_prec_scale */
  8901.         0,                /* is_long */
  8902.         0,                /* auto_unique_value */
  8903.         15,                /* data_precision */
  8904.         null,                /* numeric_scale */
  8905.         null,                /* column_size */
  8906.         null,                /* literal_prefix */
  8907.         null,                /* literal_suffix */
  8908.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8909.         null,                /* unsigned_attribute */
  8910.         'float'                /* local_type_name */
  8911.         )    
  8912.     insert into spt_provider_types values
  8913.         (
  8914.         109 /*SQLFLT4*/,        /* ss_dtype */
  8915.         4,                /* fixlen */
  8916.         'real',                /* type_name */
  8917.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  8918.         0,                /* best_match */
  8919.         null,                /* is_nullable */
  8920.         0,                /* case_sensitive */
  8921.         0,                /* fixed_prec_scale */
  8922.         0,                /* is_long */
  8923.         0,                /* auto_unique_value */
  8924.         7,                /* data_precision */
  8925.         null,                /* numeric_scale */
  8926.         null,                /* column_size */
  8927.         null,                /* literal_prefix */
  8928.         null,                /* literal_suffix */
  8929.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8930.         null,                /* unsigned_attribute */
  8931.         'real'                /* local_type_name */
  8932.         )    
  8933.  
  8934.     /* Local Intn */
  8935.     insert into spt_provider_types values
  8936.         (
  8937.         38 /*SQLINTN*/,            /* ss_dtype */
  8938.         4,                /* fixlen */
  8939.         'int',                /* type_name */
  8940.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  8941.         1,                /* best_match */
  8942.         null,                /* is_nullable */
  8943.         0,                /* case_sensitive */
  8944.         1,                /* fixed_prec_scale */
  8945.         0,                /* is_long */
  8946.         1,                /* auto_unique_value */
  8947.         10,                /* data_precision */
  8948.         null,                /* numeric_scale */
  8949.         null,                /* column_size */
  8950.         null,                /* literal_prefix */
  8951.         null,                /* literal_suffix */
  8952.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8953.         0,                /* unsigned_attribute */
  8954.         'int'                /* local_type_name */
  8955.         )    
  8956.     insert into spt_provider_types values
  8957.         (
  8958.         38 /*SQLINTN*/,            /* ss_dtype */
  8959.         2,                /* fixlen */
  8960.         'smallint',            /* type_name */
  8961.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  8962.         0,                /* best_match */
  8963.         null,                /* is_nullable */
  8964.         0,                /* case_sensitive */
  8965.         1,                /* fixed_prec_scale */
  8966.         0,                /* is_long */
  8967.         1,                /* auto_unique_value */
  8968.         5,                /* data_precision */
  8969.         null,                /* numeric_scale */
  8970.         null,                /* column_size */
  8971.         null,                /* literal_prefix */
  8972.         null,                /* literal_suffix */
  8973.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8974.         0,                /* unsigned_attribute */
  8975.         'smallint'            /* local_type_name */
  8976.         )    
  8977.     insert into spt_provider_types values
  8978.         (
  8979.         38 /*SQLINTN*/,            /* ss_dtype */
  8980.         1,                /* fixlen */
  8981.         'tinyint',            /* type_name */
  8982.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  8983.         0,                /* best_match */
  8984.         null,                /* is_nullable */
  8985.         0,                /* case_sensitive */
  8986.         1,                /* fixed_prec_scale */
  8987.         0,                /* is_long */
  8988.         1,                /* auto_unique_value */
  8989.         3,                /* data_precision */
  8990.         null,                /* numeric_scale */
  8991.         null,                /* column_size */
  8992.         null,                /* literal_prefix */
  8993.         null,                /* literal_suffix */
  8994.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8995.         1,                /* unsigned_attribute */
  8996.         'tinyint'            /* local_type_name */
  8997.         )    
  8998.  
  8999.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  9000.         begin    /*    Add 6.0 data types */
  9001.         
  9002.         /* Local Decimaln */
  9003.         insert into spt_provider_types values
  9004.             (
  9005.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  9006.             0,                /* fixlen */
  9007.             'decimal',            /* type_name */
  9008.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9009.             0,                /* best_match */
  9010.             null,                /* is_nullable */
  9011.             0,                /* case_sensitive */
  9012.             1,                /* fixed_prec_scale */
  9013.             0,                /* is_long */
  9014.             1,                /* auto_unique_value */
  9015.             38,                /* data_precision */
  9016.             null,                /* numeric_scale */
  9017.             null,                /* column_size */
  9018.             null,                /* literal_prefix */
  9019.             null,                /* literal_suffix */
  9020.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9021.             0,                /* unsigned_attribute */
  9022.             'decimal'            /* local_type_name */
  9023.             )    
  9024.  
  9025.         /* Local Numericn */
  9026.         insert into spt_provider_types values
  9027.             (
  9028.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  9029.             0,                /* fixlen */
  9030.             'numeric',            /* type_name */
  9031.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9032.             0,                /* best_match */
  9033.             null,                /* is_nullable */
  9034.             0,                /* case_sensitive */
  9035.             1,                /* fixed_prec_scale */
  9036.             0,                /* is_long */
  9037.             1,                /* auto_unique_value */
  9038.             38,                /* data_precision */
  9039.             null,                /* numeric_scale */
  9040.             null,                /* column_size */
  9041.             null,                /* literal_prefix */
  9042.             null,                /* literal_suffix */
  9043.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9044.             0,                /* unsigned_attribute */
  9045.             'numeric'            /* local_type_name */
  9046.             )    
  9047.         end
  9048.     end
  9049. commit tran
  9050. go
  9051. dump tran master with no_log
  9052. go
  9053.  
  9054. /*
  9055. **    Remaining data types
  9056. */
  9057. if (charindex('7.00', @@version) = 0)
  9058. begin
  9059.     print ''
  9060.     print ''
  9061.     print 'Warning:'
  9062.     print 'you are installing the stored procedures '
  9063.     print 'on a pre 7.0 SQL Server.'
  9064.     print 'Ignore the following errors.'
  9065. end
  9066. go
  9067. begin tran
  9068.  
  9069. if (charindex('7.00', @@version) > 0)
  9070.     begin
  9071.     /* Local Bit */
  9072.     insert into spt_provider_types values
  9073.         (
  9074.         50 /*SQLBIT*/,            /* ss_dtype */
  9075.         0,                /* fixlen */
  9076.         'bit',                /* type_name */
  9077.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9078.         1,                /* best_match */
  9079.         null,                /* is_nullable */
  9080.         0,                /* case_sensitive */
  9081.         0,                /* fixed_prec_scale */
  9082.         0,                /* is_long */
  9083.         0,                /* auto_unique_value */
  9084.         1,                /* data_precision */
  9085.         null,                /* numeric_scale */
  9086.         null,                /* column_size */
  9087.         null,                /* literal_prefix */
  9088.         null,                /* literal_suffix */
  9089.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9090.         null,                /* unsigned_attribute */
  9091.         'bit'                /* local_type_name */
  9092.         )        
  9093.     
  9094.     /* Local Timestamp */
  9095.     insert into spt_provider_types values
  9096.         (
  9097.         0,                /* ss_dtype */
  9098.         8,                /* fixlen */
  9099.         'timestamp',            /* type_name */
  9100.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9101.         0,                /* best_match */
  9102.         null,                /* is_nullable */
  9103.         0,                /* case_sensitive */
  9104.         0,                /* fixed_prec_scale */
  9105.         0,                /* is_long */
  9106.         0,                /* auto_unique_value */
  9107.         null,                /* data_precision */
  9108.         null,                /* numeric_scale */
  9109.         null,                /* column_size */
  9110.         '0x',                /* literal_prefix */
  9111.         null,                /* literal_suffix */
  9112.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9113.         null,                /* unsigned_attribute */
  9114.         'timestamp'            /* local_type_name */
  9115.         )    
  9116.     /* Local GUID */
  9117.     insert into spt_provider_types values
  9118.         (
  9119.         0,                /* ss_dtype */
  9120.         16,                /* fixlen */
  9121.         'uniqueidentifier',        /* type_name */
  9122.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  9123.         1,                /* best_match */
  9124.         null,                /* is_nullable */
  9125.         0,                /* case_sensitive */
  9126.         0,                /* fixed_prec_scale */
  9127.         0,                /* is_long */
  9128.         0,                /* auto_unique_value */
  9129.         null,                /* data_precision */
  9130.         null,                /* numeric_scale */
  9131.         null,                /* column_size */
  9132.         '''',                /* literal_prefix */
  9133.         '''',                /* literal_suffix */
  9134.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9135.         null,                /* unsigned_attribute */
  9136.         'uniqueidentifier'        /* local_type_name */
  9137.         )    
  9138.     
  9139.     /* Get case sensitivity */
  9140.     declare @ncase_sensitive bit
  9141.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9142.  
  9143.     /* Local NChar */
  9144.     insert into spt_provider_types values
  9145.         (
  9146.         0,                /* ss_dtype */
  9147.         0,                /* fixlen */
  9148.         'nchar',            /* type_name */
  9149.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9150.         0,                /* best_match */
  9151.         null,                /* is_nullable */
  9152.         @ncase_sensitive,        /* case_sensitive */
  9153.         0,                /* fixed_prec_scale */
  9154.         0,                /* is_long */
  9155.         0,                /* auto_unique_value */
  9156.         null,                /* data_precision */
  9157.         null,                /* numeric_scale */
  9158.         null,                /* column_size */
  9159.         'N''',                /* literal_prefix */
  9160.         '''',                /* literal_suffix */
  9161.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9162.         null,                /* unsigned_attribute */
  9163.         'nchar'                /* local_type_name */
  9164.         )    
  9165.  
  9166.     /* Local NVarChar */
  9167.     insert into spt_provider_types values
  9168.         (
  9169.         0,                /* ss_dtype */
  9170.         null,                /* fixlen */
  9171.         'nvarchar',            /* type_name */
  9172.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9173.         1,                /* best_match */
  9174.         null,                /* is_nullable */
  9175.         @ncase_sensitive,        /* case_sensitive */
  9176.         0,                /* fixed_prec_scale */
  9177.         0,                /* is_long */
  9178.         0,                /* auto_unique_value */
  9179.         null,                /* data_precision */
  9180.         null,                /* numeric_scale */
  9181.         null,                /* column_size */
  9182.         'N''',                /* literal_prefix */
  9183.         '''',                /* literal_suffix */
  9184.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9185.         null,                /* unsigned_attribute */
  9186.         'nvarchar'            /* local_type_name */
  9187.         )    
  9188.  
  9189.     /* Local NText */
  9190.     insert into spt_provider_types values
  9191.         (
  9192.         0,                /* ss_dtype */
  9193.         null,                /* fixlen */
  9194.         'ntext',            /* type_name */
  9195.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9196.         0,                /* best_match */
  9197.         null,                /* is_nullable */
  9198.         @ncase_sensitive,        /* case_sensitive */
  9199.         0,                /* fixed_prec_scale */
  9200.         1,                /* is_long */
  9201.         0,                /* auto_unique_value */
  9202.         null,                /* data_precision */
  9203.         null,                /* numeric_scale */
  9204.         1073741823,            /* column_size */
  9205.         'N''',                /* literal_prefix */
  9206.         '''',                /* literal_suffix */
  9207.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  9208.         null,                /* unsigned_attribute */
  9209.         'ntext'                /* local_type_name */
  9210.         )    
  9211.  
  9212.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  9213.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  9214.  
  9215.     end
  9216. commit tran
  9217. go
  9218. dump tran master with no_log
  9219. go
  9220. begin tran
  9221. if (charindex('7.00', @@version) = 0)
  9222.     begin
  9223.     /* Local Bit */
  9224.     insert into spt_provider_types values
  9225.         (
  9226.         50 /*SQLBIT*/,            /* ss_dtype */
  9227.         0,                /* fixlen */
  9228.         'bit',                /* type_name */
  9229.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9230.         1,                /* best_match */
  9231.         0,                /* is_nullable */
  9232.         0,                /* case_sensitive */
  9233.         0,                /* fixed_prec_scale */
  9234.         0,                /* is_long */
  9235.         0,                /* auto_unique_value */
  9236.         1,                /* data_precision */
  9237.         null,                /* numeric_scale */
  9238.         null,                /* column_size */
  9239.         null,                /* literal_prefix */
  9240.         null,                /* literal_suffix */
  9241.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9242.         null,                /* unsigned_attribute */
  9243.         'bit'                /* local_type_name */
  9244.         )    
  9245.  
  9246.     /* Local Timestamp */
  9247.     insert into spt_provider_types values
  9248.         (
  9249.         45 /*SQLBINARY*/,        /* ss_dtype */
  9250.         8,                /* fixlen */
  9251.         'timestamp',            /* type_name */
  9252.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9253.         0,                /* best_match */
  9254.         null,                /* is_nullable */
  9255.         0,                /* case_sensitive */
  9256.         0,                /* fixed_prec_scale */
  9257.         0,                /* is_long */
  9258.         0,                /* auto_unique_value */
  9259.         null,                /* data_precision */
  9260.         null,                /* numeric_scale */
  9261.         null,                /* column_size */
  9262.         '0x',                /* literal_prefix */
  9263.         null,                /* literal_suffix */
  9264.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9265.         null,                /* unsigned_attribute */
  9266.         'timestamp'            /* local_type_name */
  9267.         )        
  9268. end
  9269.  
  9270. commit tran
  9271. go
  9272. dump tran master with no_log
  9273. go
  9274.  
  9275. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  9276. go
  9277.  
  9278. dump tran master with no_log
  9279. go
  9280. if (charindex('6.00', @@version) > 0)
  9281.     begin
  9282.     if (exists (select * from sysobjects
  9283.         where name = 'spt_provider_types' and type = 'U '))
  9284.         begin
  9285.         drop table spt_provider_types
  9286.         dump tran master with no_log
  9287.         end
  9288.     end
  9289. go
  9290.  
  9291. if (charindex('7.00', @@version) = 0)
  9292. begin
  9293.     print ''
  9294.     print ''
  9295.     print 'Warning:'
  9296.     print 'you are installing the stored procedures '
  9297.     print 'on a pre 7.0 SQL Server.'
  9298.     print 'Ignore the following errors.'
  9299. end
  9300. go
  9301.  
  9302. /* 
  9303. ** (leih 2/11/98) 
  9304. ** The following SP is shared by instcat.sql and the server to add information
  9305. ** related to server language collations. The server calls it at the end of
  9306. ** collation change to update the catalog with the new language collations
  9307. ** Everything in this SP should NOT use tempdb.  
  9308. */
  9309. create procedure sp_add_server_sortinfo
  9310. as
  9311. -- spt_server_info has unigue clustered index on attribute_id
  9312. delete spt_server_info where attribute_id in (16,18)
  9313.  
  9314. --insert into spt_server_info
  9315. --    select 18,'COLLATION_SEQ',
  9316. --        'charset='+t2.name+' sort_order='+t1.name
  9317. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  9318. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  9319. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  9320. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  9321. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  9322. declare @case_sensitive bit
  9323. declare @ncase_sensitive bit
  9324. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  9325. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9326. select @sortid = value from sysconfigures where config = 1123
  9327. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  9328. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  9329.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  9330.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  9331.         from syscharsets where id = @csid
  9332.  
  9333. insert into spt_server_info
  9334.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  9335.  
  9336. if @case_sensitive = 1 /* If case sensitive server */
  9337. begin
  9338.     insert into spt_server_info
  9339.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  9340. end
  9341. else
  9342. begin
  9343.     insert into spt_server_info
  9344.         values (16,'IDENTIFIER_CASE','MIXED')
  9345. end
  9346.  
  9347.  
  9348. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  9349.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  9350. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  9351.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  9352. update spt_provider_types set case_sensitive = @case_sensitive 
  9353.     where oledb_data_type = 129 /*DBTYPE_STR*/
  9354. update spt_provider_types set case_sensitive = @ncase_sensitive
  9355.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  9356.  
  9357. go
  9358.  
  9359. if (charindex('7.00', @@version) <> 0)
  9360. begin
  9361.     exec sp_add_server_sortinfo
  9362. end
  9363. go
  9364.  
  9365. print ''
  9366. print 'creating sp_catalogs_rowset'
  9367. go
  9368.  
  9369. create procedure sp_catalogs_rowset
  9370.     (
  9371.     @catalog_name        varchar(255)
  9372.     )        
  9373. as
  9374.     select
  9375.         CATALOG_NAME    = name,
  9376.         DESCRIPTION    = convert(varchar(1),null)
  9377.     from     master.dbo.sysdatabases
  9378.     where    name = @catalog_name
  9379. go
  9380. dump tran master with no_log
  9381. go
  9382. create procedure sp_catalogs_rowset;2
  9383.     (
  9384.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9385.     )
  9386. as
  9387.     select
  9388.         CATALOG_NAME    = name,
  9389.         DESCRIPTION    = convert(varchar(1),null)
  9390.     from     master.dbo.sysdatabases
  9391.     order by 1
  9392. go
  9393. dump tran master with no_log
  9394. go
  9395.  
  9396. if (charindex('7.00', @@version) = 0)
  9397. begin
  9398.     print ''
  9399.     print ''
  9400.     print 'Warning:'
  9401.     print 'you are installing the stored procedures '
  9402.     print 'on a pre 7.0 SQL Server.'
  9403.     print 'Ignore the following errors.'
  9404. end
  9405. else
  9406.     drop proc sp_catalogs_rowset
  9407. go
  9408.  
  9409. /*    Procedure for 7.0 server */
  9410. create procedure sp_catalogs_rowset
  9411.     (
  9412.     @catalog_name        sysname
  9413.     )        
  9414. as
  9415.     select
  9416.         CATALOG_NAME    = name,
  9417.         DESCRIPTION    = convert(nvarchar(1),null)
  9418.     from     master.dbo.sysdatabases
  9419.     where    name = @catalog_name
  9420. go
  9421. dump tran master with no_log
  9422. go
  9423. create procedure sp_catalogs_rowset;2
  9424.     (
  9425.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9426.     )
  9427. as
  9428.     select
  9429.         CATALOG_NAME    = name,
  9430.         DESCRIPTION    = convert(nvarchar(1),null)
  9431.     from     master.dbo.sysdatabases
  9432.     order by 1
  9433. go
  9434. dump tran master with no_log
  9435. go
  9436. create procedure sp_catalogs_rowset;5
  9437.     (
  9438.     @server_name    sysname,
  9439.     @catalog_name    sysname = NULL
  9440.     )
  9441. as
  9442.     select    CATALOG_NAME,
  9443.         DESCRIPTION
  9444.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  9445.     order by 1
  9446. go
  9447.  
  9448. grant execute on sp_catalogs_rowset to public
  9449. go
  9450.  
  9451. dump tran master with no_log
  9452. go
  9453. if (charindex('6.00', @@version) > 0)
  9454.     begin
  9455.     if (exists (select * from sysobjects
  9456.             where name = 'sp_catalogs_rowset' and type = 'P '))
  9457.         begin
  9458.         drop procedure sp_catalogs_rowset
  9459.         dump tran master with no_log
  9460.         end
  9461.     end
  9462. go
  9463.  
  9464.  
  9465. print ''
  9466. print 'creating sp_column_privileges_rowset'
  9467. go
  9468.  
  9469. /*    Procedure for 6.0 and 6.5 server */
  9470. CREATE PROCEDURE sp_column_privileges_rowset
  9471.     (
  9472.     @table_name     varchar(255) = null,
  9473.     @table_schema    varchar(255) = null,
  9474.     @column_name    varchar(255) = null,
  9475.     @grantor    varchar(255) = null,
  9476.     @grantee    varchar(255) = null
  9477.     )
  9478. as
  9479. IF @table_name is not null
  9480.     BEGIN
  9481.     select
  9482.         GRANTOR        = user_name(p.grantor),
  9483.         GRANTEE        = user_name(u.uid),
  9484.         TABLE_CATALOG    = db_name(),
  9485.         TABLE_SCHEMA    = user_name(o.uid),
  9486.         TABLE_NAME    = o.name,
  9487.         COLUMN_NAME    = c.name,
  9488.         COLUMN_GUID    = convert(binary(16),null),
  9489.         COLUMN_PROPID    = convert(int,null),
  9490.         PRIVILEGE_TYPE    = convert(varchar(30),
  9491.                     case p.action
  9492.                     when 193 then 'SELECT'
  9493.                     when 195 then 'INSERT'
  9494.                     when 197 then 'UPDATE'
  9495.                     else 'REFERENCES'
  9496.                     end),
  9497.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9498.     from 
  9499.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9500.     where
  9501.         o.name = @table_name
  9502.     and     o.type in ('U','V','S')
  9503.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9504.     and    c.id = o.id
  9505.     and     (@column_name is null or @column_name = c.name)
  9506.     and     c.id = p.id
  9507.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9508.     and     case 
  9509.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9510.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9511.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9512.             end & v.high <> 0            /* permission applies to this column */
  9513.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9514.     and     v.type = 'P'
  9515.     and     v.number = c.colid
  9516.     and    (@grantee is null or @grantee = user_name(u.uid))
  9517.             /* expand groups */
  9518.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9519.     and     p.protecttype <> 206    /* only grant rows */
  9520.     and     p.action in (26,193,195,197)
  9521.     and     o.uid <> u.uid            /* no rows for owner */
  9522.     and     not exists (            /* exclude revoke'd privileges */
  9523.             select *
  9524.             from sysprotects p1
  9525.             where
  9526.                 p1.protecttype = 206
  9527.             and     p1.action = p.action
  9528.             and     p1.id = p.id
  9529.             and     p1.uid = u.uid
  9530.             and     case 
  9531.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9532.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9533.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9534.                 end & v.high <> 0)            /* permission applies to this column */
  9535.     union all
  9536.     select    /*    Add rows for table owner */
  9537.         GRANTOR        = user_name(u.uid),
  9538.         GRANTEE        = user_name(o.uid),
  9539.         TABLE_CATALOG    = db_name(),
  9540.         TABLE_SCHEMA    = user_name(o.uid),
  9541.         TABLE_NAME    = o.name,
  9542.         COLUMN_NAME    = c.name,
  9543.         COLUMN_GUID    = convert(binary(16),null),
  9544.         COLUMN_PROPID    = convert(int,null),
  9545.         PRIVILEGE_TYPE    = convert(varchar(30),
  9546.                     case v.number
  9547.                     when 193 then 'SELECT'
  9548.                     when 195 then 'INSERT'
  9549.                     when 197 then 'UPDATE'
  9550.                     else 'REFERENCES'
  9551.                     end),
  9552.         IS_GRANTABLE    = convert(bit,1)    
  9553.     from 
  9554.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9555.     where
  9556.         o.name = @table_name
  9557.     and     o.type in ('U','V','S')
  9558.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9559.     and    (@grantee is null or @grantee = user_name(o.uid))
  9560.     and    c.id = o.id
  9561.     and     (@column_name is null or @column_name = c.name)
  9562.     and     u.suid = 1        /* grantor is dbo of database */
  9563.     and    (@grantor is null or @grantor = user_name(u.uid))
  9564.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9565.     and     v.number in (26,193,195,197)
  9566.     and     not exists (        /* exclude revoke'd privileges */
  9567.             select *
  9568.             from     sysprotects p1
  9569.             where    p1.protecttype = 206
  9570.             and     p1.action = v.number
  9571.             and     p1.id = o.id
  9572.             and     p1.uid = o.uid)
  9573.     order by 4, 5, 6, 9, 1, 2
  9574.     END
  9575. ELSE
  9576.     BEGIN
  9577.     select
  9578.         GRANTOR        = user_name(p.grantor),
  9579.         GRANTEE        = user_name(u.uid),
  9580.         TABLE_CATALOG    = db_name(),
  9581.         TABLE_SCHEMA    = user_name(o.uid),
  9582.         TABLE_NAME    = o.name,
  9583.         COLUMN_NAME    = c.name,
  9584.         COLUMN_GUID    = convert(binary(16),null),
  9585.         COLUMN_PROPID    = convert(int,null),
  9586.         PRIVILEGE_TYPE    = convert(varchar(30),
  9587.                     case p.action
  9588.                     when 193 then 'SELECT'
  9589.                     when 195 then 'INSERT'
  9590.                     when 197 then 'UPDATE'
  9591.                     else 'REFERENCES'
  9592.                     end),
  9593.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9594.     from 
  9595.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9596.     where
  9597.         o.type in ('U','V','S')
  9598.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9599.     and    c.id = o.id
  9600.     and     (@column_name is null or @column_name = c.name)
  9601.     and     c.id = p.id
  9602.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9603.     and     case 
  9604.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9605.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9606.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9607.             end & v.high <> 0            /* permission applies to this column */
  9608.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9609.     and     v.type = 'P'
  9610.     and     v.number = c.colid
  9611.     and    (@grantee is null or @grantee = user_name(u.uid))
  9612.             /* expand groups */
  9613.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9614.     and     p.protecttype <> 206    /* only grant rows */
  9615.     and     p.action in (26,193,195,197)
  9616.     and     o.uid <> u.uid            /* no rows for owner */
  9617.     and     not exists (            /* exclude revoke'd privileges */
  9618.             select *
  9619.             from sysprotects p1
  9620.             where
  9621.                 p1.protecttype = 206
  9622.             and     p1.action = p.action
  9623.             and     p1.id = p.id
  9624.             and     p1.uid = u.uid
  9625.             and     case 
  9626.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9627.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9628.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9629.                 end & v.high <> 0)            /* permission applies to this column */
  9630.     union all
  9631.     select    /*    Add rows for table owner */
  9632.         GRANTOR        = user_name(u.uid),
  9633.         GRANTEE        = user_name(o.uid),
  9634.         TABLE_CATALOG    = db_name(),
  9635.         TABLE_SCHEMA    = user_name(o.uid),
  9636.         TABLE_NAME    = o.name,
  9637.         COLUMN_NAME    = c.name,
  9638.         COLUMN_GUID    = convert(binary(16),null),
  9639.         COLUMN_PROPID    = convert(int,null),
  9640.         PRIVILEGE_TYPE    = convert(varchar(30),
  9641.                     case v.number
  9642.                     when 193 then 'SELECT'
  9643.                     when 195 then 'INSERT'
  9644.                     when 197 then 'UPDATE'
  9645.                     else 'REFERENCES'
  9646.                     end),
  9647.         IS_GRANTABLE    = convert(bit,1)    
  9648.     from 
  9649.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9650.     where
  9651.          o.type in ('U','V','S')
  9652.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9653.     and    (@grantee is null or @grantee = user_name(o.uid))
  9654.     and    c.id = o.id
  9655.     and     (@column_name is null or @column_name = c.name)
  9656.     and     u.suid = 1        /* grantor is dbo of database */
  9657.     and    (@grantor is null or @grantor = user_name(u.uid))
  9658.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9659.     and     v.number in (26,193,195,197)
  9660.     and     not exists (        /* exclude revoke'd privileges */
  9661.             select *
  9662.             from     sysprotects p1
  9663.             where    p1.protecttype = 206
  9664.             and     p1.action = v.number
  9665.             and     p1.id = o.id
  9666.             and     p1.uid = o.uid)
  9667.     order by 4, 5, 6, 9, 1, 2
  9668.     END
  9669. go
  9670. dump tran master with no_log
  9671. go
  9672. CREATE PROCEDURE sp_column_privileges_rowset;2
  9673.     (
  9674.        @handle        int output,
  9675.        @scrollopt    int output,
  9676.     @ccopt        int output,
  9677.     @rows        int output,
  9678.     @table_name     varchar(255) = null,
  9679.     @table_schema    varchar(255) = null,
  9680.     @column_name    varchar(255) = null,
  9681.     @grantor    varchar(255) = null,
  9682.     @grantee    varchar(255) = null
  9683.     )
  9684. as
  9685.  
  9686. declare @ret int
  9687.  
  9688. SET NOCOUNT ON
  9689.     
  9690. create table #spcprivsrowset1
  9691.     (
  9692.     GRANTOR        sysname not null,
  9693.     GRANTEE        sysname not null,
  9694.     TABLE_CATALOG    sysname not null,
  9695.     TABLE_SCHEMA    sysname not null,
  9696.     TABLE_NAME    sysname not null,
  9697.     COLUMN_NAME    sysname not null,
  9698.     COLUMN_GUID    binary(16) null,
  9699.     COLUMN_PROPID    int null,
  9700.     PRIVILEGE_TYPE    sysname not null,
  9701.     IS_GRANTABLE    bit not null
  9702.     )
  9703.  
  9704. IF @table_name is not null
  9705.     BEGIN
  9706.     insert into #spcprivsrowset1
  9707.     select
  9708.         GRANTOR        = user_name(p.grantor),
  9709.         GRANTEE        = user_name(u.uid),
  9710.         TABLE_CATALOG    = db_name(),
  9711.         TABLE_SCHEMA    = user_name(o.uid),
  9712.         TABLE_NAME    = o.name,
  9713.         COLUMN_NAME    = c.name,
  9714.         COLUMN_GUID    = convert(binary(16),null),
  9715.         COLUMN_PROPID    = convert(int,null),
  9716.         PRIVILEGE_TYPE    = convert(varchar(30),
  9717.                     case p.action
  9718.                     when 193 then 'SELECT'
  9719.                     when 195 then 'INSERT'
  9720.                     when 197 then 'UPDATE'
  9721.                     else 'REFERENCES'
  9722.                     end),
  9723.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9724.     from 
  9725.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9726.     where
  9727.         o.name = @table_name
  9728.     and     o.type in ('U','V','S')
  9729.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9730.     and    c.id = o.id
  9731.     and     (@column_name is null or @column_name = c.name)
  9732.     and     c.id = p.id
  9733.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9734.     and     case 
  9735.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9736.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9737.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9738.             end & v.high <> 0            /* permission applies to this column */
  9739.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9740.     and     v.type = 'P'
  9741.     and     v.number = c.colid
  9742.     and    (@grantee is null or @grantee = user_name(u.uid))
  9743.             /* expand groups */
  9744.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9745.     and     p.protecttype <> 206    /* only grant rows */
  9746.     and     p.action in (26,193,195,197)
  9747.     and     o.uid <> u.uid            /* no rows for owner */
  9748.     and     not exists (            /* exclude revoke'd privileges */
  9749.             select *
  9750.             from sysprotects p1
  9751.             where
  9752.                 p1.protecttype = 206
  9753.             and     p1.action = p.action
  9754.             and     p1.id = p.id
  9755.             and     p1.uid = u.uid
  9756.             and     case 
  9757.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9758.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9759.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9760.                 end & v.high <> 0)            /* permission applies to this column */
  9761.     union all
  9762.     select    /*    Add rows for table owner */
  9763.         GRANTOR        = user_name(u.uid),
  9764.         GRANTEE        = user_name(o.uid),
  9765.         TABLE_CATALOG    = db_name(),
  9766.         TABLE_SCHEMA    = user_name(o.uid),
  9767.         TABLE_NAME    = o.name,
  9768.         COLUMN_NAME    = c.name,
  9769.         COLUMN_GUID    = convert(binary(16),null),
  9770.         COLUMN_PROPID    = convert(int,null),
  9771.         PRIVILEGE_TYPE    = convert(varchar(30),
  9772.                     case v.number
  9773.                     when 193 then 'SELECT'
  9774.                     when 195 then 'INSERT'
  9775.                     when 197 then 'UPDATE'
  9776.                     else 'REFERENCES'
  9777.                     end),
  9778.         IS_GRANTABLE    = convert(bit,1)    
  9779.     from 
  9780.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9781.     where
  9782.         o.name = @table_name
  9783.     and     o.type in ('U','V','S')
  9784.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9785.     and    (@grantee is null or @grantee = user_name(o.uid))
  9786.     and    c.id = o.id
  9787.     and     (@column_name is null or @column_name = c.name)
  9788.     and     u.suid = 1        /* grantor is dbo of database */
  9789.     and    (@grantor is null or @grantor = user_name(u.uid))
  9790.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9791.     and     v.number in (26,193,195,197)
  9792.     and     not exists (        /* exclude revoke'd privileges */
  9793.             select *
  9794.             from     sysprotects p1
  9795.             where    p1.protecttype = 206
  9796.             and     p1.action = v.number
  9797.             and     p1.id = o.id
  9798.             and     p1.uid = o.uid)
  9799.     order by 4, 5, 6, 9, 1, 2
  9800.     END
  9801. ELSE
  9802.     BEGIN
  9803.     insert into #spcprivsrowset1
  9804.     select
  9805.         GRANTOR        = user_name(p.grantor),
  9806.         GRANTEE        = user_name(u.uid),
  9807.         TABLE_CATALOG    = db_name(),
  9808.         TABLE_SCHEMA    = user_name(o.uid),
  9809.         TABLE_NAME    = o.name,
  9810.         COLUMN_NAME    = c.name,
  9811.         COLUMN_GUID    = convert(binary(16),null),
  9812.         COLUMN_PROPID    = convert(int,null),
  9813.         PRIVILEGE_TYPE    = convert(varchar(30),
  9814.                     case p.action
  9815.                     when 193 then 'SELECT'
  9816.                     when 195 then 'INSERT'
  9817.                     when 197 then 'UPDATE'
  9818.                     else 'REFERENCES'
  9819.                     end),
  9820.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9821.     from 
  9822.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9823.     where
  9824.         o.type in ('U','V','S')
  9825.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9826.     and    c.id = o.id
  9827.     and     (@column_name is null or @column_name = c.name)
  9828.     and     c.id = p.id
  9829.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9830.     and     case 
  9831.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9832.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9833.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9834.             end & v.high <> 0            /* permission applies to this column */
  9835.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9836.     and     v.type = 'P'
  9837.     and     v.number = c.colid
  9838.     and    (@grantee is null or @grantee = user_name(u.uid))
  9839.             /* expand groups */
  9840.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9841.     and     p.protecttype <> 206    /* only grant rows */
  9842.     and     p.action in (26,193,195,197)
  9843.     and     o.uid <> u.uid            /* no rows for owner */
  9844.     and     not exists (            /* exclude revoke'd privileges */
  9845.             select *
  9846.             from sysprotects p1
  9847.             where
  9848.                 p1.protecttype = 206
  9849.             and     p1.action = p.action
  9850.             and     p1.id = p.id
  9851.             and     p1.uid = u.uid
  9852.             and     case 
  9853.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9854.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9855.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9856.                 end & v.high <> 0)            /* permission applies to this column */
  9857.     union all
  9858.     select    /*    Add rows for table owner */
  9859.         GRANTOR        = user_name(u.uid),
  9860.         GRANTEE        = user_name(o.uid),
  9861.         TABLE_CATALOG    = db_name(),
  9862.         TABLE_SCHEMA    = user_name(o.uid),
  9863.         TABLE_NAME    = o.name,
  9864.         COLUMN_NAME    = c.name,
  9865.         COLUMN_GUID    = convert(binary(16),null),
  9866.         COLUMN_PROPID    = convert(int,null),
  9867.         PRIVILEGE_TYPE    = convert(varchar(30),
  9868.                     case v.number
  9869.                     when 193 then 'SELECT'
  9870.                     when 195 then 'INSERT'
  9871.                     when 197 then 'UPDATE'
  9872.                     else 'REFERENCES'
  9873.                     end),
  9874.         IS_GRANTABLE    = convert(bit,1)    
  9875.     from 
  9876.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9877.     where
  9878.          o.type in ('U','V','S')
  9879.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9880.     and    (@grantee is null or @grantee = user_name(o.uid))
  9881.     and    c.id = o.id
  9882.     and     (@column_name is null or @column_name = c.name)
  9883.     and     u.suid = 1        /* grantor is dbo of database */
  9884.     and    (@grantor is null or @grantor = user_name(u.uid))
  9885.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9886.     and     v.number in (26,193,195,197)
  9887.     and     not exists (        /* exclude revoke'd privileges */
  9888.             select *
  9889.             from     sysprotects p1
  9890.             where    p1.protecttype = 206
  9891.             and     p1.action = v.number
  9892.             and     p1.id = o.id
  9893.             and     p1.uid = o.uid)
  9894.     order by 4, 5, 6, 9, 1, 2
  9895.     END
  9896.  
  9897. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  9898.     @scrollopt output, @ccopt output, @rows output
  9899.  
  9900. drop table #spcprivsrowset1
  9901.  
  9902. return isnull(@ret,0)
  9903. go
  9904. dump tran master with no_log
  9905. go
  9906. CREATE PROCEDURE sp_column_privileges_rowset;3
  9907. as
  9908.     select
  9909.         GRANTOR        = convert(sysname,' '),
  9910.         GRANTEE        = convert(sysname,' '),
  9911.         TABLE_CATALOG    = convert(sysname,' '),
  9912.         TABLE_SCHEMA    = convert(sysname,' '),
  9913.         TABLE_NAME    = convert(sysname,' '),
  9914.         COLUMN_NAME    = convert(sysname,' '),
  9915.         COLUMN_GUID    = convert(binary(16),null),
  9916.         COLUMN_PROPID    = convert(int,null),
  9917.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  9918.         IS_GRANTABLE    = convert(bit,1)
  9919.     where    1=0
  9920. go
  9921. dump tran master with no_log
  9922. go
  9923.  
  9924. if (charindex('7.00', @@version) = 0)
  9925. begin
  9926.     print ''
  9927.     print ''
  9928.     print 'Warning:'
  9929.     print 'you are installing the stored procedures '
  9930.     print 'on a pre 7.0 SQL Server.'
  9931.     print 'Ignore the following errors.'
  9932. end
  9933. else
  9934.     drop proc sp_column_privileges_rowset
  9935. go
  9936.  
  9937.  
  9938. /*    Procedure for 7.0 server */
  9939. CREATE PROCEDURE sp_column_privileges_rowset
  9940.     (
  9941.     @table_name         sysname,
  9942.     @table_schema        sysname = null,
  9943.     @column_name        sysname = null,
  9944.     @grantor        sysname = null,
  9945.     @grantee        sysname = null
  9946.     )
  9947. as
  9948.     select
  9949.         GRANTOR        = user_name(p.grantor),
  9950.         GRANTEE        = user_name(u.uid),
  9951.         TABLE_CATALOG    = db_name(),
  9952.         TABLE_SCHEMA    = user_name(o.uid),
  9953.         TABLE_NAME    = o.name,
  9954.         COLUMN_NAME    = c.name,
  9955.         COLUMN_GUID    = convert(uniqueidentifier,null),
  9956.         COLUMN_PROPID    = convert(int,null),
  9957.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  9958.                     case p.action
  9959.                     when 193 then N'SELECT'
  9960.                     when 195 then N'INSERT'
  9961.                     when 197 then N'UPDATE'
  9962.                     else N'REFERENCES'
  9963.                     end),
  9964.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9965.     from 
  9966.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  9967.     where
  9968.         o.name = @table_name
  9969.     and     o.type in ('U','V','S')
  9970.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9971.     and    c.id = o.id
  9972.     and     (@column_name is null or @column_name = c.name)
  9973.     and     c.id = p.id
  9974.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9975.     and     case 
  9976.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9977.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9978.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9979.             end & v.high <> 0            /* permission applies to this column */
  9980.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9981.     and     v.type = 'P'
  9982.     and     v.number = c.colid
  9983.     and    (@grantee is null or @grantee = user_name(u.uid))
  9984.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  9985.     and     (u.uid > 0 and u.uid < 16384)
  9986.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  9987.     and     p.protecttype <> 206    /* only grant rows */
  9988.     and     p.action in (26,193,195,197)
  9989.     and     o.uid <> u.uid            /* no rows for owner */
  9990.     and     not exists (            /* exclude revoke'd privileges */
  9991.             select *
  9992.             from sysprotects p1
  9993.             where
  9994.                 p1.protecttype = 206
  9995.             and     p1.action = p.action
  9996.             and     p1.id = p.id
  9997.             and     p1.uid = u.uid
  9998.             and     case 
  9999.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10000.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10001.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10002.                 end & v.high <> 0)            /* permission applies to this column */
  10003.     union all
  10004.     select    /*    Add rows for table owner */
  10005.         GRANTOR        = user_name(u.uid),
  10006.         GRANTEE        = user_name(o.uid),
  10007.         TABLE_CATALOG    = db_name(),
  10008.         TABLE_SCHEMA    = user_name(o.uid),
  10009.         TABLE_NAME    = o.name,
  10010.         COLUMN_NAME    = c.name,
  10011.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10012.         COLUMN_PROPID    = convert(int,null),
  10013.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10014.                     case v.number
  10015.                     when 193 then N'SELECT'
  10016.                     when 195 then N'INSERT'
  10017.                     when 197 then N'UPDATE'
  10018.                     else N'REFERENCES'
  10019.                     end),
  10020.         IS_GRANTABLE    = convert(bit,1)    
  10021.     from 
  10022.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10023.     where
  10024.         o.name = @table_name
  10025.     and     o.type in ('U','V','S')
  10026.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10027.     and    (@grantee is null or @grantee = user_name(o.uid))
  10028.     and    c.id = o.id
  10029.     and     (@column_name is null or @column_name = c.name)
  10030.     and     u.suid = 1        /* grantor is dbo of database */
  10031.     and    (@grantor is null or @grantor = user_name(u.uid))
  10032.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10033.     and     v.number in (26,193,195,197)
  10034.     and     not exists (        /* exclude revoke'd privileges */
  10035.             select *
  10036.             from     sysprotects p1
  10037.             where    p1.protecttype = 206
  10038.             and     p1.action = v.number
  10039.             and     p1.id = o.id
  10040.             and     p1.uid = o.uid)
  10041.     order by 4, 5, 6, 9, 1, 2
  10042. go
  10043. dump tran master with no_log
  10044. go
  10045. CREATE PROCEDURE sp_column_privileges_rowset;2
  10046.     (
  10047.     @table_schema        sysname = null,
  10048.     @column_name        sysname = null,
  10049.     @grantor        sysname = null,
  10050.     @grantee        sysname = null
  10051.     )
  10052. as
  10053.     select
  10054.         GRANTOR        = user_name(p.grantor),
  10055.         GRANTEE        = user_name(u.uid),
  10056.         TABLE_CATALOG    = db_name(),
  10057.         TABLE_SCHEMA    = user_name(o.uid),
  10058.         TABLE_NAME    = o.name,
  10059.         COLUMN_NAME    = c.name,
  10060.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10061.         COLUMN_PROPID    = convert(int,null),
  10062.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10063.                     case p.action
  10064.                     when 193 then N'SELECT'
  10065.                     when 195 then N'INSERT'
  10066.                     when 197 then N'UPDATE'
  10067.                     else N'REFERENCES'
  10068.                     end),
  10069.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10070.     from 
  10071.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  10072.     where
  10073.         o.type in ('U','V','S')
  10074.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10075.     and    c.id = o.id
  10076.     and     (@column_name is null or @column_name = c.name)
  10077.     and     c.id = p.id
  10078.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10079.     and     case 
  10080.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10081.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10082.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10083.             end & v.high <> 0            /* permission applies to this column */
  10084.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10085.     and     v.type = 'P'
  10086.     and     v.number = c.colid
  10087.     and    (@grantee is null or @grantee = user_name(u.uid))
  10088.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  10089.     and     (u.uid > 0 and u.uid < 16384)
  10090.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  10091.     and     p.protecttype <> 206    /* only grant rows */
  10092.     and     p.action in (26,193,195,197)
  10093.     and     o.uid <> u.uid            /* no rows for owner */
  10094.     and     not exists (            /* exclude revoke'd privileges */
  10095.             select *
  10096.             from sysprotects p1
  10097.             where
  10098.                 p1.protecttype = 206
  10099.             and     p1.action = p.action
  10100.             and     p1.id = p.id
  10101.             and     p1.uid = u.uid
  10102.             and     case 
  10103.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10104.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10105.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10106.                 end & v.high <> 0)            /* permission applies to this column */
  10107.     union all
  10108.     select    /*    Add rows for table owner */
  10109.         GRANTOR        = user_name(u.uid),
  10110.         GRANTEE        = user_name(o.uid),
  10111.         TABLE_CATALOG    = db_name(),
  10112.         TABLE_SCHEMA    = user_name(o.uid),
  10113.         TABLE_NAME    = o.name,
  10114.         COLUMN_NAME    = c.name,
  10115.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10116.         COLUMN_PROPID    = convert(int,null),
  10117.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10118.                     case v.number
  10119.                     when 193 then N'SELECT'
  10120.                     when 195 then N'INSERT'
  10121.                     when 197 then N'UPDATE'
  10122.                     else N'REFERENCES'
  10123.                     end),
  10124.         IS_GRANTABLE    = convert(bit,1)    
  10125.     from 
  10126.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10127.     where
  10128.         o.type in ('U','V','S')
  10129.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10130.     and    (@grantee is null or @grantee = user_name(o.uid))
  10131.     and    c.id = o.id
  10132.     and     (@column_name is null or @column_name = c.name)
  10133.     and     u.suid = 1        /* grantor is dbo of database */
  10134.     and    (@grantor is null or @grantor = user_name(u.uid))
  10135.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10136.     and     v.number in (26,193,195,197)
  10137.     and     not exists (        /* exclude revoke'd privileges */
  10138.             select *
  10139.             from     sysprotects p1
  10140.             where    p1.protecttype = 206
  10141.             and     p1.action = v.number
  10142.             and     p1.id = o.id
  10143.             and     p1.uid = o.uid)
  10144.     order by 4, 5, 6, 9, 1, 2
  10145. go
  10146. dump tran master with no_log
  10147. go
  10148. create procedure sp_column_privileges_rowset;5
  10149.     (
  10150.     @table_server        sysname,
  10151.     @table_catalog        sysname = null,
  10152.     @table_name        sysname = null,
  10153.     @table_schema        sysname = null,
  10154.     @column_name        sysname = null,
  10155.     @grantor        sysname = null,
  10156.     @grantee        sysname = null
  10157.     )
  10158. as
  10159.     select
  10160.         GRANTOR,
  10161.         GRANTEE,
  10162.         TABLE_CATALOG,
  10163.         TABLE_SCHEMA,
  10164.         TABLE_NAME,
  10165.         COLUMN_NAME,
  10166.         COLUMN_GUID,
  10167.         COLUMN_PROPID,    
  10168.         PRIVILEGE_TYPE,
  10169.         IS_GRANTABLE    
  10170.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  10171.                 @table_server,
  10172.                 @table_catalog,
  10173.                 @table_schema,
  10174.                 @table_name,
  10175.                 @column_name,
  10176.                 @grantor,
  10177.                 @grantee>
  10178.     order by 3, 4, 5, 6, 9, 1, 2
  10179. go
  10180.  
  10181. grant execute on sp_column_privileges_rowset to public
  10182. go
  10183.  
  10184. dump tran master with no_log
  10185. go
  10186. if (charindex('6.00', @@version) > 0)
  10187.     begin
  10188.     if (exists (select * from sysobjects
  10189.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  10190.         begin
  10191.         drop procedure sp_column_privileges_rowset
  10192.         dump tran master with no_log
  10193.         end
  10194.     end
  10195. go
  10196.  
  10197.  
  10198. print ''
  10199. print 'creating sp_columns_rowset'
  10200. go
  10201.  
  10202. /*    Procedure for 6.0 and 6.50 server */
  10203. create procedure sp_columns_rowset
  10204.     (
  10205.        @table_name    varchar(255),
  10206.     @table_schema     varchar(255) = null,
  10207.     @column_name    varchar(255) = null
  10208.     )
  10209. as     
  10210.     select
  10211.         TABLE_CATALOG        = db_name(),
  10212.         TABLE_SCHEMA        = user_name(o.uid),
  10213.         TABLE_NAME        = o.name,
  10214.         COLUMN_NAME        = c.name,
  10215.         COLUMN_GUID        = convert(binary(16),null),
  10216.         COLUMN_PROPID        = convert(int,null),
  10217.         ORDINAL_POSITION    = convert(int,c.colid),
  10218.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10219.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  10220.         COLUMN_FLAGS        = convert(int,
  10221.                         case when d.is_long = 1 
  10222.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10223.                         else 0
  10224.                         end
  10225.                     |    case when d.fixlen is not null
  10226.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10227.                         else 0
  10228.                         end
  10229.                     |    case when c.status&8 = 8
  10230.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10231.                         else 0
  10232.                         end
  10233.                     |    case 
  10234.                         when d.type_name = 'timestamp'
  10235.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10236.                         when (c.status&128) != 128
  10237.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  10238.                         else 0 
  10239.                         end),
  10240.         IS_NULLABLE        = convert(bit,c.status&8),
  10241.         DATA_TYPE        = d.oledb_data_type,
  10242.         TYPE_GUID        = convert(binary(16),null),
  10243.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10244.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10245.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10246.                         then coalesce(d.column_size,c.length)
  10247.                         else null 
  10248.                         end),
  10249.         CHARACTER_OCTET_LENGTH    = convert(int,
  10250.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10251.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10252.                         then coalesce(d.column_size,c.length)
  10253.                         else null 
  10254.                         end),
  10255.         NUMERIC_PRECISION    = convert(smallint,
  10256.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  10257.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10258.         NUMERIC_SCALE        = convert(smallint, 
  10259.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10260.         DATETIME_PRECISION    = convert(int,
  10261.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10262.                             when data_precision = 23 then 3 else 0 end),
  10263.         CHARACTER_SET_CATALOG    = convert(sysname,
  10264.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10265.                         then 'master' 
  10266.                         else null 
  10267.                         end),
  10268.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10269.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10270.                         then 'dbo' 
  10271.                         else null 
  10272.                         end),
  10273.         CHARACTER_SET_NAME    = convert(sysname,
  10274.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10275.                         then a_cha.name 
  10276.                         else null 
  10277.                         end),
  10278.         COLLATION_CATALOG    = convert(sysname,
  10279.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10280.                         then 'master' 
  10281.                         else null 
  10282.                         end),
  10283.         COLLATION_SCHEMA    = convert(sysname,
  10284.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10285.                         then 'dbo' 
  10286.                         else null 
  10287.                         end),
  10288.         COLLATION_NAME        = convert(sysname,
  10289.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10290.                         then b_cha.name 
  10291.                         else null 
  10292.                         end),
  10293.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10294.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10295.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10296.         DESCRIPTION        = convert(varchar(1),null)    
  10297.     
  10298.     from
  10299.         syscolumns c,
  10300.         syscomments m,
  10301.         sysobjects o,
  10302.         master.dbo.spt_provider_types d,
  10303.         systypes t,
  10304.         sysusers u,
  10305.         master.dbo.sysconfigures    cfg,
  10306.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10307.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  10308.     where
  10309.         o.name = @table_name
  10310.     and     o.type in ('U','V','S')
  10311.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  10312.     and     (
  10313.         suser_id() = 1     /* User is the System Administrator */
  10314.         or o.uid = user_id()     /* User created the object */
  10315.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  10316.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  10317.             from sysprotects p
  10318.             where p.id = o.id
  10319.             /* get rows for public,current user,user's group */
  10320.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  10321.             /* check for SELECT,EXECUTE privilege */
  10322.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  10323.             ) = 1    /* final magic...compare Grants      */
  10324.            )
  10325.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10326.     and    (@column_name is null or @column_name = c.name)
  10327.     and     o.id = c.id
  10328.     and     t.type = d.ss_dtype
  10329.     and    (t.usertype != 80 or d.type_name='timestamp')
  10330.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10331.     and     c.usertype = t.usertype
  10332.     and    c.cdefault *= m.id
  10333.     and    m.colid = 1
  10334.     and    cfg.comment = 'default sortorder ID' 
  10335.     and    a_cha.type = 1001 /* type is charset */
  10336.     and    b_cha.type = 2001 /* type is sortorder */
  10337.     and    a_cha.id = b_cha.csid
  10338.     and     b_cha.id = cfg.value
  10339.     order by 2, 3, 7
  10340. go
  10341. dump tran master with no_log
  10342. go
  10343. create procedure sp_columns_rowset;2
  10344.     (
  10345.     @table_schema     varchar(255) = null,
  10346.     @column_name    varchar(255) = null
  10347.     )
  10348. as     
  10349.     select
  10350.         TABLE_CATALOG        = db_name(),
  10351.         TABLE_SCHEMA        = user_name(o.uid),
  10352.         TABLE_NAME        = o.name,
  10353.         COLUMN_NAME        = c.name,
  10354.         COLUMN_GUID        = convert(binary(16),null),
  10355.         COLUMN_PROPID        = convert(int,null),
  10356.         ORDINAL_POSITION    = convert(int,c.colid),
  10357.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10358.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  10359.         COLUMN_FLAGS        = convert(int,
  10360.                         case when d.is_long = 1 
  10361.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10362.                         else 0
  10363.                         end
  10364.                     |    case when d.fixlen is not null
  10365.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10366.                         else 0
  10367.                         end
  10368.                     |    case when c.status&8 = 8
  10369.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10370.                         else 0
  10371.                         end
  10372.                     |    case 
  10373.                         when d.type_name = 'timestamp'
  10374.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10375.                         when (c.status&128) != 128
  10376.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  10377.                         else 0 
  10378.                         end),
  10379.         IS_NULLABLE        = convert(bit,c.status&8),
  10380.         DATA_TYPE        = d.oledb_data_type,
  10381.         TYPE_GUID        = convert(binary(16),null),
  10382.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10383.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10384.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10385.                         then coalesce(d.column_size,c.length)
  10386.                         else null 
  10387.                         end),
  10388.         CHARACTER_OCTET_LENGTH    = convert(int,
  10389.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10390.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10391.                         then coalesce(d.column_size,c.length)
  10392.                         else null 
  10393.                         end),
  10394.         NUMERIC_PRECISION    = convert(smallint,
  10395.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  10396.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10397.         NUMERIC_SCALE        = convert(smallint, 
  10398.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10399.         DATETIME_PRECISION    = convert(int,
  10400.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10401.                             when data_precision = 23 then 3 else 0 end),
  10402.         CHARACTER_SET_CATALOG    = convert(sysname,
  10403.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10404.                         then 'master' 
  10405.                         else null 
  10406.                         end),
  10407.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10408.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10409.                         then 'dbo' 
  10410.                         else null 
  10411.                         end),
  10412.         CHARACTER_SET_NAME    = convert(sysname,
  10413.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10414.                         then a_cha.name 
  10415.                         else null 
  10416.                         end),
  10417.         COLLATION_CATALOG    = convert(sysname,
  10418.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10419.                         then 'master' 
  10420.                         else null 
  10421.                         end),
  10422.         COLLATION_SCHEMA    = convert(sysname,
  10423.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10424.                         then 'dbo' 
  10425.                         else null 
  10426.                         end),
  10427.         COLLATION_NAME        = convert(sysname,
  10428.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10429.                         then b_cha.name 
  10430.                         else null 
  10431.                         end),
  10432.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10433.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10434.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10435.         DESCRIPTION        = convert(varchar(1),null)    
  10436.     
  10437.     from
  10438.         syscolumns c,
  10439.         syscomments m,
  10440.         sysobjects o,
  10441.         master.dbo.spt_provider_types d,
  10442.         systypes t,
  10443.         sysusers u,
  10444.         master.dbo.sysconfigures    cfg,
  10445.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10446.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  10447.     where
  10448.          o.type in ('U','V','S')
  10449.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  10450.     and     (
  10451.         suser_id() = 1     /* User is the System Administrator */
  10452.         or o.uid = user_id()     /* User created the object */
  10453.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  10454.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  10455.             from sysprotects p
  10456.             where p.id = o.id
  10457.             /* get rows for public,current user,user's group */
  10458.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  10459.             /* check for SELECT,EXECUTE privilege */
  10460.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  10461.             ) = 1    /* final magic...compare Grants      */
  10462.            )
  10463.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10464.     and    (@column_name is null or @column_name = c.name)
  10465.     and     o.id = c.id
  10466.     and     t.type = d.ss_dtype
  10467.     and    (t.usertype != 80 or d.type_name='timestamp')
  10468.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10469.     and     c.usertype = t.usertype
  10470.     and    c.cdefault *= m.id
  10471.     and     m.colid = 1
  10472.     and    cfg.comment = 'default sortorder ID' 
  10473.     and    a_cha.type = 1001 /* type is charset */
  10474.     and    b_cha.type = 2001 /* type is sortorder */
  10475.     and    a_cha.id = b_cha.csid
  10476.     and     b_cha.id = cfg.value
  10477.     order by 2, 3, 7
  10478. go
  10479. dump tran master with no_log
  10480. go
  10481.  
  10482. if (charindex('7.00', @@version) = 0)
  10483. begin
  10484.     print ''
  10485.     print ''
  10486.     print 'Warning:'
  10487.     print 'you are installing the stored procedures '
  10488.     print 'on a pre 7.0 SQL Server.'
  10489.     print 'Ignore the following errors.'
  10490. end
  10491. else
  10492.     drop proc sp_columns_rowset
  10493. go
  10494.  
  10495. /*    Procedure for 7.0 server */
  10496. create procedure sp_columns_rowset
  10497.     (
  10498.        @table_name    sysname,
  10499.     @table_schema     sysname = NULL,
  10500.     @column_name    sysname = NULL
  10501.     )
  10502. as     
  10503.     select
  10504.         TABLE_CATALOG        = db_name(),
  10505.         TABLE_SCHEMA        = user_name(o.uid),
  10506.         TABLE_NAME        = o.name,
  10507.         COLUMN_NAME        = c.name,
  10508.         COLUMN_GUID        = convert(uniqueidentifier,null),
  10509.         COLUMN_PROPID        = convert(int,null),
  10510.         ORDINAL_POSITION    = convert(int,c.colid),
  10511.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10512.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  10513.                         substring(m.text,2,datalength(m.text)/2-2)),
  10514.         COLUMN_FLAGS        = convert(int,
  10515.                         case when d.is_long = 1 
  10516.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10517.                         else 0
  10518.                         end
  10519.                     |    case when d.fixlen is not null
  10520.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10521.                         else 0
  10522.                         end
  10523.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  10524.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10525.                         else 0
  10526.                         end
  10527.                     |    case 
  10528.                         when d.type_name = 'timestamp'
  10529.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10530.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  10531.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  10532.                         else 0 
  10533.                         end),
  10534.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  10535.         DATA_TYPE        = d.oledb_data_type,
  10536.         TYPE_GUID        = convert(uniqueidentifier,null),
  10537.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10538.                         case 
  10539.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10540.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10541.                         then coalesce(d.column_size,c.length)
  10542.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10543.                         then coalesce(d.column_size,c.length/2)
  10544.                         else null 
  10545.                         end),
  10546.         CHARACTER_OCTET_LENGTH    = convert(int,
  10547.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10548.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10549.                         then coalesce(d.column_size,c.length)
  10550.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10551.                         then coalesce(d.column_size*2,c.length)
  10552.                         else null 
  10553.                         end),
  10554.         NUMERIC_PRECISION    = convert(smallint,
  10555.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  10556.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10557.         NUMERIC_SCALE        = convert(smallint, 
  10558.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10559.         DATETIME_PRECISION    = convert(int,
  10560.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10561.                             when data_precision = 23 then 3 else 0 end),
  10562.         CHARACTER_SET_CATALOG    = convert(sysname,
  10563.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10564.                         then N'master' 
  10565.                         else null 
  10566.                         end),
  10567.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10568.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10569.                         then N'dbo' 
  10570.                         else null 
  10571.                         end),
  10572.         CHARACTER_SET_NAME    = convert(sysname,
  10573.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10574.                         then a_cha.name 
  10575.                         else null 
  10576.                         end),
  10577.         COLLATION_CATALOG    = convert(sysname,
  10578.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10579.                         then N'master' 
  10580.                         else null 
  10581.                         end),
  10582.         COLLATION_SCHEMA    = convert(sysname,
  10583.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10584.                         then N'dbo' 
  10585.                         else null 
  10586.                         end),
  10587.         COLLATION_NAME        = convert(sysname,
  10588.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10589.                         then b_cha.name 
  10590.                         else null 
  10591.                         end),
  10592.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10593.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10594.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10595.         DESCRIPTION        = convert(nvarchar(1),null)    
  10596.     
  10597.     from
  10598.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  10599.         sysobjects o,
  10600.         master.dbo.spt_provider_types d,
  10601.         systypes t,
  10602.         master.dbo.sysconfigures    cfg,
  10603.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10604.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  10605.     where
  10606.         permissions(o.id, c.name) <> 0
  10607.     and    o.name = @table_name
  10608.     and     o.type in ('U','V','S')
  10609.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10610.     and    (@column_name is null or @column_name = c.name)
  10611.     and     o.id = c.id
  10612.     and     t.xtype = d.ss_dtype
  10613.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10614.     and     c.xusertype = t.xusertype
  10615.     and    cfg.comment = 'default sortorder id' 
  10616.     and    a_cha.type = 1001 /* type is charset */
  10617.     and    b_cha.type = 2001 /* type is sortorder */
  10618.     and    a_cha.id = b_cha.csid
  10619.     and     b_cha.id = cfg.value
  10620.     order by 2, 3, 7
  10621. go
  10622. dump tran master with no_log
  10623. go
  10624. create procedure sp_columns_rowset;2
  10625.     (
  10626.     @table_schema     sysname = NULL,
  10627.     @column_name    sysname = NULL
  10628.     )
  10629. as     
  10630.     select
  10631.         TABLE_CATALOG        = db_name(),
  10632.         TABLE_SCHEMA        = user_name(o.uid),
  10633.         TABLE_NAME        = o.name,
  10634.         COLUMN_NAME        = c.name,
  10635.         COLUMN_GUID        = convert(uniqueidentifier,null),
  10636.         COLUMN_PROPID        = convert(int,null),
  10637.         ORDINAL_POSITION    = convert(int,c.colid),
  10638.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10639.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  10640.                         substring(m.text,2,datalength(m.text)/2-2)),
  10641.         COLUMN_FLAGS        = convert(int,
  10642.                         case when d.is_long = 1 
  10643.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10644.                         else 0
  10645.                         end
  10646.                     |    case when d.fixlen is not null
  10647.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10648.                         else 0
  10649.                         end
  10650.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  10651.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10652.                         else 0
  10653.                         end
  10654.                     |    case 
  10655.                         when d.type_name = 'timestamp'
  10656.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10657.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  10658.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  10659.                         else 0 
  10660.                         end),
  10661.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  10662.         DATA_TYPE        = d.oledb_data_type,
  10663.         TYPE_GUID        = convert(uniqueidentifier,null),
  10664.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10665.                         case 
  10666.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10667.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10668.                         then coalesce(d.column_size,c.length)
  10669.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10670.                         then coalesce(d.column_size,c.length/2)
  10671.                         else null 
  10672.                         end),
  10673.         CHARACTER_OCTET_LENGTH    = convert(int,
  10674.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10675.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10676.                         then coalesce(d.column_size,c.length)
  10677.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10678.                         then coalesce(d.column_size*2,c.length)
  10679.                         else null 
  10680.                         end),
  10681.         NUMERIC_PRECISION    = convert(smallint,
  10682.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  10683.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10684.         NUMERIC_SCALE        = convert(smallint, 
  10685.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10686.         DATETIME_PRECISION    = convert(int,
  10687.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10688.                             when data_precision = 23 then 3 else 0 end),
  10689.         CHARACTER_SET_CATALOG    = convert(sysname,
  10690.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10691.                         then N'master' 
  10692.                         else null 
  10693.                         end),
  10694.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10695.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10696.                         then N'dbo' 
  10697.                         else null 
  10698.                         end),
  10699.         CHARACTER_SET_NAME    = convert(sysname,
  10700.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10701.                         then a_cha.name 
  10702.                         else null 
  10703.                         end),
  10704.         COLLATION_CATALOG    = convert(sysname,
  10705.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10706.                         then N'master' 
  10707.                         else null 
  10708.                         end),
  10709.         COLLATION_SCHEMA    = convert(sysname,
  10710.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10711.                         then N'dbo' 
  10712.                         else null 
  10713.                         end),
  10714.         COLLATION_NAME        = convert(sysname,
  10715.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10716.                         then b_cha.name 
  10717.                         else null 
  10718.                         end),
  10719.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10720.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10721.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10722.         DESCRIPTION        = convert(nvarchar(1),null)    
  10723.     
  10724.     from
  10725.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  10726.         sysobjects o,
  10727.         master.dbo.spt_provider_types d,
  10728.         systypes t,
  10729.         master.dbo.sysconfigures    cfg,
  10730.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  10731.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  10732.     where
  10733.         permissions(o.id, c.name) <> 0
  10734.     and     o.type in ('U','V','S')
  10735.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10736.     and    (@column_name is null or @column_name = c.name)
  10737.     and     o.id = c.id
  10738.     and     t.xtype = d.ss_dtype
  10739.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10740.     and     c.xusertype = t.xusertype
  10741.     and    cfg.comment = 'default sortorder id' 
  10742.     and a_cha.type = 1001 /* type is charset */
  10743.     and b_cha.type = 2001 /* type is sortorder */
  10744.     and    a_cha.id = b_cha.csid
  10745.     and     b_cha.id = cfg.value
  10746.     order by 2, 3, 7
  10747. go
  10748. dump tran master with no_log
  10749. go
  10750. create procedure sp_columns_rowset;5
  10751.     (
  10752.     @table_server        sysname,
  10753.     @table_catalog        sysname = null,
  10754.     @table_name        sysname = null,
  10755.     @table_schema        sysname = null,
  10756.     @column_name        sysname = null
  10757.     )
  10758. as
  10759.     select
  10760.         TABLE_CATALOG,
  10761.         TABLE_SCHEMA,
  10762.         TABLE_NAME,
  10763.         COLUMN_NAME,
  10764.         COLUMN_GUID,
  10765.         COLUMN_PROPID,
  10766.         ORDINAL_POSITION,
  10767.         COLUMN_HASDEFAULT,
  10768.         COLUMN_DEFAULT,
  10769.         COLUMN_FLAGS,
  10770.         IS_NULLABLE,
  10771.         DATA_TYPE,
  10772.         TYPE_GUID,
  10773.         CHARACTER_MAXIMUM_LENGTH,
  10774.         CHARACTER_OCTET_LENGTH,
  10775.         NUMERIC_PRECISION,
  10776.         NUMERIC_SCALE,
  10777.         DATETIME_PRECISION,
  10778.         CHARACTER_SET_CATALOG,
  10779.         CHARACTER_SET_SCHEMA,
  10780.         CHARACTER_SET_NAME,
  10781.         COLLATION_CATALOG,
  10782.         COLLATION_SCHEMA,
  10783.         COLLATION_NAME,
  10784.         DOMAIN_CATALOG,
  10785.         DOMAIN_SCHEMA,
  10786.         DOMAIN_NAME,
  10787.         DESCRIPTION
  10788.     from master.dbo.SYSREMOTE_COLUMNS <
  10789.                 @table_server,
  10790.                 @table_catalog,
  10791.                 @table_schema,
  10792.                 @table_name,
  10793.                 @column_name > 
  10794.     order by 1, 2, 3, 7
  10795. go
  10796.  
  10797. grant execute on sp_columns_rowset to public
  10798. go
  10799.  
  10800. dump tran master with no_log
  10801. go
  10802. if (charindex('6.00', @@version) > 0)
  10803.     begin
  10804.     if (exists (select * from sysobjects
  10805.             where name = 'sp_columns_rowset' and type = 'P '))
  10806.         begin
  10807.         drop procedure sp_columns_rowset
  10808.         dump tran master with no_log
  10809.         end
  10810.     end
  10811. go
  10812.  
  10813.  
  10814. print ''
  10815. print 'creating sp_foreign_keys_rowset'
  10816. go
  10817.  
  10818. /*    Procedure for 6.0 and 6.5 server */
  10819. CREATE PROCEDURE sp_foreign_keys_rowset
  10820.     (
  10821.      @pk_table_name        varchar(255) = null,
  10822.     @pk_table_schema    varchar(255) = null,
  10823.     @pk_table_catalog    varchar(255) = null,
  10824.     @fk_table_name        varchar(255) = null,
  10825.     @fk_table_schema    varchar(255) = null,
  10826.     @fk_table_catalog    varchar(255) = null
  10827.     )
  10828. as
  10829. IF @pk_table_name is not null
  10830.     BEGIN
  10831.     select
  10832.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10833.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10834.         PK_TABLE_NAME         = o1.name,
  10835.         PK_COLUMN_NAME         = c1.name,
  10836.         PK_COLUMN_GUID        = convert(binary(16),null),
  10837.         PK_COLUMN_PROPID    = convert(int,null),
  10838.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10839.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10840.         FK_TABLE_NAME         = o2.name,
  10841.         FK_COLUMN_NAME         = c2.name,
  10842.         FK_COLUMN_GUID        = convert(binary(16),null),
  10843.         FK_COLUMN_PROPID    = convert(int,null),
  10844.         ORDINAL                = convert(int,1),
  10845.         UPDATE_RULE            = 'NO ACTION',
  10846.         DELETE_RULE         = 'NO ACTION',
  10847.         PK_NAME                = object_name(s.constid),
  10848.         FK_NAME                = object_name(r.constid),
  10849.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  10850.     from
  10851.         sysobjects o1, sysobjects o2,
  10852.         syscolumns c1, syscolumns c2,
  10853.         sysreferences r, sysconstraints s
  10854.     where    
  10855.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10856.     and    o1.name = @pk_table_name
  10857.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10858.     and    o1.id = r.rkeyid
  10859.     and s.id = o1.id and (s.status & 0xf) = 1
  10860.     and    o1.id = c1.id
  10861.     and    c1.colid = r.rkey1
  10862.     and    r.fkeyid = o2.id
  10863.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10864.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10865.     and    o2.id = c2.id    
  10866.     and    c2.colid = r.fkey1
  10867.     union all
  10868.     select
  10869.         db_name(r.rkeydbid),
  10870.         user_name(o1.uid),
  10871.         o1.name,
  10872.         c1.name,
  10873.         convert(binary(16),null),
  10874.         convert(int,null),
  10875.         db_name(r.fkeydbid),
  10876.         user_name(o2.uid),
  10877.         o2.name,
  10878.         c2.name,
  10879.         convert(binary(16),null),
  10880.         convert(int,null),
  10881.         convert(int,2),
  10882.         'NO ACTION',
  10883.         'NO ACTION',
  10884.         object_name(s.constid),
  10885.         object_name(r.constid),
  10886.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  10887.     from
  10888.         sysobjects o1, sysobjects o2,
  10889.         syscolumns c1, syscolumns c2,
  10890.         sysreferences r, sysconstraints s
  10891.     where    
  10892.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10893.     and    o1.name = @pk_table_name
  10894.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10895.     and    o1.id = r.rkeyid
  10896.     and s.id = o1.id and (s.status & 0xf) = 1
  10897.      and    o1.id = c1.id
  10898.     and    c1.colid = r.rkey2
  10899.     and    r.keycnt >= 2
  10900.     and r.fkeyid = o2.id
  10901.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10902.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10903.     and    o2.id = c2.id    
  10904.     and    c2.colid = r.fkey2
  10905.     union all
  10906.     select
  10907.         db_name(r.rkeydbid),
  10908.         user_name(o1.uid),
  10909.         o1.name,
  10910.         c1.name,
  10911.         convert(binary(16),null),
  10912.         convert(int,null),
  10913.         db_name(r.fkeydbid),
  10914.         user_name(o2.uid),
  10915.         o2.name,
  10916.         c2.name,
  10917.         convert(binary(16),null),
  10918.         convert(int,null),
  10919.         convert(int,3),
  10920.         'NO ACTION',
  10921.         'NO ACTION',
  10922.         object_name(s.constid),
  10923.         object_name(r.constid),
  10924.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  10925.     from
  10926.         sysobjects o1, sysobjects o2,
  10927.         syscolumns c1, syscolumns c2,
  10928.         sysreferences r, sysconstraints s
  10929.     where    
  10930.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10931.     and    o1.name = @pk_table_name
  10932.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10933.     and    o1.id = r.rkeyid
  10934.     and s.id = o1.id and (s.status & 0xf) = 1
  10935.     and    o1.id = c1.id
  10936.     and    c1.colid = r.rkey3
  10937.     and    r.keycnt >= 3
  10938.     and r.fkeyid = o2.id
  10939.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10940.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10941.     and    o2.id = c2.id    
  10942.     and    c2.colid = r.fkey3
  10943.     union all
  10944.     select
  10945.         db_name(r.rkeydbid),
  10946.         user_name(o1.uid),
  10947.         o1.name,
  10948.         c1.name,
  10949.         convert(binary(16),null),
  10950.         convert(int,null),
  10951.         db_name(r.fkeydbid),
  10952.         user_name(o2.uid),
  10953.         o2.name,
  10954.         c2.name,
  10955.         convert(binary(16),null),
  10956.         convert(int,null),
  10957.         convert(int,4),
  10958.         'NO ACTION',
  10959.         'NO ACTION',
  10960.         object_name(s.constid),
  10961.         object_name(r.constid),
  10962.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  10963.     from
  10964.         sysobjects o1, sysobjects o2,
  10965.         syscolumns c1, syscolumns c2,
  10966.         sysreferences r, sysconstraints s
  10967.     where    
  10968.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10969.     and    o1.name = @pk_table_name
  10970.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10971.     and    o1.id = r.rkeyid
  10972.     and s.id = o1.id and (s.status & 0xf) = 1
  10973.     and    r.keycnt >= 4
  10974.     and    o1.id = c1.id
  10975.     and    c1.colid = r.rkey4
  10976.     and    r.fkeyid = o2.id
  10977.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10978.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10979.     and    o2.id = c2.id    
  10980.     and    c2.colid = r.fkey4
  10981.     union all
  10982.     select
  10983.         db_name(r.rkeydbid),
  10984.         user_name(o1.uid),
  10985.         o1.name,
  10986.         c1.name,
  10987.         convert(binary(16),null),
  10988.         convert(int,null),
  10989.         db_name(r.fkeydbid),
  10990.         user_name(o2.uid),
  10991.         o2.name,
  10992.         c2.name,
  10993.         convert(binary(16),null),
  10994.         convert(int,null),
  10995.         convert(int,5),
  10996.         'NO ACTION',
  10997.         'NO ACTION',
  10998.         object_name(s.constid),
  10999.         object_name(r.constid),
  11000.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11001.     from
  11002.         sysobjects o1, sysobjects o2,
  11003.         syscolumns c1, syscolumns c2,
  11004.         sysreferences r, sysconstraints s
  11005.     where    
  11006.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11007.     and    o1.name = @pk_table_name
  11008.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11009.     and    o1.id = r.rkeyid
  11010.     and s.id = o1.id and (s.status & 0xf) = 1
  11011.     and    r.keycnt >= 5
  11012.     and    o1.id = c1.id
  11013.     and    c1.colid = r.rkey5
  11014.     and r.fkeyid = o2.id
  11015.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11016.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11017.     and    o2.id = c2.id    
  11018.     and    c2.colid = r.fkey5
  11019.     union all
  11020.     select
  11021.         db_name(r.rkeydbid),
  11022.         user_name(o1.uid),
  11023.         o1.name,
  11024.         c1.name,
  11025.         convert(binary(16),null),
  11026.         convert(int,null),
  11027.         db_name(r.fkeydbid),
  11028.         user_name(o2.uid),
  11029.         o2.name,
  11030.         c2.name,
  11031.         convert(binary(16),null),
  11032.         convert(int,null),
  11033.         convert(int,6),
  11034.         'NO ACTION',
  11035.         'NO ACTION',
  11036.         object_name(s.constid),
  11037.         object_name(r.constid),
  11038.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11039.     from
  11040.         sysobjects o1, sysobjects o2,
  11041.         syscolumns c1, syscolumns c2,
  11042.         sysreferences r, sysconstraints s
  11043.     where    
  11044.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11045.     and    o1.name = @pk_table_name
  11046.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11047.     and    o1.id = r.rkeyid
  11048.     and s.id = o1.id and (s.status & 0xf) = 1
  11049.     and    r.keycnt >= 6
  11050.     and    o1.id = c1.id
  11051.     and    c1.colid = r.rkey6
  11052.     and r.fkeyid = o2.id
  11053.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11054.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11055.     and    o2.id = c2.id    
  11056.     and    c2.colid = r.fkey6
  11057.     union all
  11058.     select
  11059.         db_name(r.rkeydbid),
  11060.         user_name(o1.uid),
  11061.         o1.name,
  11062.         c1.name,
  11063.         convert(binary(16),null),
  11064.         convert(int,null),
  11065.         db_name(r.fkeydbid),
  11066.         user_name(o2.uid),
  11067.         o2.name,
  11068.         c2.name,
  11069.         convert(binary(16),null),
  11070.         convert(int,null),
  11071.         convert(int,7),
  11072.         'NO ACTION',
  11073.         'NO ACTION',
  11074.         object_name(s.constid),
  11075.         object_name(r.constid),
  11076.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11077.     from
  11078.         sysobjects o1, sysobjects o2,
  11079.         syscolumns c1, syscolumns c2,
  11080.         sysreferences r, sysconstraints s
  11081.     where    
  11082.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11083.     and    o1.name = @pk_table_name
  11084.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11085.     and    o1.id = r.rkeyid
  11086.     and s.id = o1.id and (s.status & 0xf) = 1
  11087.     and    r.keycnt >= 7
  11088.     and    o1.id = c1.id
  11089.     and    c1.colid = r.rkey7
  11090.     and r.fkeyid = o2.id
  11091.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11092.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11093.     and    o2.id = c2.id    
  11094.     and    c2.colid = r.fkey7
  11095.     union all
  11096.     select
  11097.         db_name(r.rkeydbid),
  11098.         user_name(o1.uid),
  11099.         o1.name,
  11100.         c1.name,
  11101.         convert(binary(16),null),
  11102.         convert(int,null),
  11103.         db_name(r.fkeydbid),
  11104.         user_name(o2.uid),
  11105.         o2.name,
  11106.         c2.name,
  11107.         convert(binary(16),null),
  11108.         convert(int,null),
  11109.         convert(int,8),
  11110.         'NO ACTION',
  11111.         'NO ACTION',
  11112.         object_name(s.constid),
  11113.         object_name(r.constid),
  11114.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11115.     from
  11116.         sysobjects o1, sysobjects o2,
  11117.         syscolumns c1, syscolumns c2,
  11118.         sysreferences r, sysconstraints s
  11119.     where    
  11120.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11121.     and    o1.name = @pk_table_name
  11122.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11123.     and    o1.id = r.rkeyid
  11124.     and s.id = o1.id and (s.status & 0xf) = 1
  11125.     and    r.keycnt >= 8
  11126.     and    o1.id = c1.id
  11127.     and    c1.colid = r.rkey8
  11128.     and r.fkeyid = o2.id
  11129.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11130.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11131.     and    o2.id = c2.id    
  11132.     and    c2.colid = r.fkey8
  11133.     union all
  11134.     select
  11135.         db_name(r.rkeydbid),
  11136.         user_name(o1.uid),
  11137.         o1.name,
  11138.         c1.name,
  11139.         convert(binary(16),null),
  11140.         convert(int,null),
  11141.         db_name(r.fkeydbid),
  11142.         user_name(o2.uid),
  11143.         o2.name,
  11144.         c2.name,
  11145.         convert(binary(16),null),
  11146.         convert(int,null),
  11147.         convert(int,9),
  11148.         'NO ACTION',
  11149.         'NO ACTION',
  11150.         object_name(s.constid),
  11151.         object_name(r.constid),
  11152.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11153.     from
  11154.         sysobjects o1, sysobjects o2,
  11155.         syscolumns c1, syscolumns c2,
  11156.         sysreferences r, sysconstraints s
  11157.     where    
  11158.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11159.     and    o1.name = @pk_table_name
  11160.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11161.     and    o1.id = r.rkeyid
  11162.     and s.id = o1.id and (s.status & 0xf) = 1
  11163.     and    r.keycnt >= 9
  11164.     and    o1.id = c1.id
  11165.     and    c1.colid = r.rkey9
  11166.     and r.fkeyid = o2.id
  11167.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11168.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11169.     and    o2.id = c2.id    
  11170.     and    c2.colid = r.fkey9
  11171.     union all
  11172.     select
  11173.         db_name(r.rkeydbid),
  11174.         user_name(o1.uid),
  11175.         o1.name,
  11176.         c1.name,
  11177.         convert(binary(16),null),
  11178.         convert(int,null),
  11179.         db_name(r.fkeydbid),
  11180.         user_name(o2.uid),
  11181.         o2.name,
  11182.         c2.name,
  11183.         convert(binary(16),null),
  11184.         convert(int,null),
  11185.         convert(int,10),
  11186.         'NO ACTION',
  11187.         'NO ACTION',
  11188.         object_name(s.constid),
  11189.         object_name(r.constid),
  11190.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/    
  11191.     from
  11192.         sysobjects o1, sysobjects o2,
  11193.         syscolumns c1, syscolumns c2,
  11194.         sysreferences r, sysconstraints s
  11195.     where    
  11196.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11197.     and    o1.name = @pk_table_name
  11198.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11199.     and    o1.id = r.rkeyid
  11200.     and s.id = o1.id and (s.status & 0xf) = 1
  11201.     and    r.keycnt >= 10
  11202.     and    o1.id = c1.id
  11203.     and    c1.colid = r.rkey10
  11204.     and r.fkeyid = o2.id
  11205.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11206.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11207.     and    o2.id = c2.id    
  11208.     and    c2.colid = r.fkey10
  11209.     union all
  11210.     select
  11211.         db_name(r.rkeydbid),
  11212.         user_name(o1.uid),
  11213.         o1.name,
  11214.         c1.name,
  11215.         convert(binary(16),null),
  11216.         convert(int,null),
  11217.         db_name(r.fkeydbid),
  11218.         user_name(o2.uid),
  11219.         o2.name,
  11220.         c2.name,
  11221.         convert(binary(16),null),
  11222.         convert(int,null),
  11223.         convert(int,11),
  11224.         'NO ACTION',
  11225.         'NO ACTION',
  11226.         object_name(s.constid),
  11227.         object_name(r.constid),
  11228.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11229.     from
  11230.         sysobjects o1, sysobjects o2,
  11231.         syscolumns c1, syscolumns c2,
  11232.         sysreferences r, sysconstraints s
  11233.     where    
  11234.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11235.     and    o1.name = @pk_table_name
  11236.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11237.     and    o1.id = r.rkeyid
  11238.     and s.id = o1.id and (s.status & 0xf) = 1
  11239.     and    r.keycnt >= 11
  11240.     and    o1.id = c1.id
  11241.     and    c1.colid = r.rkey11
  11242.     and r.fkeyid = o2.id
  11243.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11244.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11245.     and    o2.id = c2.id    
  11246.     and    c2.colid = r.fkey11
  11247.     union all
  11248.     select
  11249.         db_name(r.rkeydbid),
  11250.         user_name(o1.uid),
  11251.         o1.name,
  11252.         c1.name,
  11253.         convert(binary(16),null),
  11254.         convert(int,null),
  11255.         db_name(r.fkeydbid),
  11256.         user_name(o2.uid),
  11257.         o2.name,
  11258.         c2.name,
  11259.         convert(binary(16),null),
  11260.         convert(int,null),
  11261.         convert(int,12),
  11262.         'NO ACTION',
  11263.         'NO ACTION',
  11264.         object_name(s.constid),
  11265.         object_name(r.constid),
  11266.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11267.     from
  11268.         sysobjects o1, sysobjects o2,
  11269.         syscolumns c1, syscolumns c2,
  11270.         sysreferences r, sysconstraints s
  11271.     where    
  11272.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11273.     and    o1.name = @pk_table_name
  11274.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11275.     and    o1.id = r.rkeyid
  11276.     and s.id = o1.id and (s.status & 0xf) = 1
  11277.     and    r.keycnt >= 12
  11278.     and    o1.id = c1.id
  11279.     and    c1.colid = r.rkey12
  11280.     and r.fkeyid = o2.id
  11281.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11282.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11283.     and    o2.id = c2.id    
  11284.     and    c2.colid = r.fkey12
  11285.     union all
  11286.     select
  11287.         db_name(r.rkeydbid),
  11288.         user_name(o1.uid),
  11289.         o1.name,
  11290.         c1.name,
  11291.         convert(binary(16),null),
  11292.         convert(int,null),
  11293.         db_name(r.fkeydbid),
  11294.         user_name(o2.uid),
  11295.         o2.name,
  11296.         c2.name,
  11297.         convert(binary(16),null),
  11298.         convert(int,null),
  11299.         convert(int,13),
  11300.         'NO ACTION',
  11301.         'NO ACTION',
  11302.         object_name(s.constid),
  11303.         object_name(r.constid),
  11304.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11305.     from
  11306.         sysobjects o1, sysobjects o2,
  11307.         syscolumns c1, syscolumns c2,
  11308.         sysreferences r, sysconstraints s
  11309.     where    
  11310.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11311.     and    o1.name = @pk_table_name
  11312.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11313.     and    o1.id = r.rkeyid
  11314.     and s.id = o1.id and (s.status & 0xf) = 1
  11315.     and    r.keycnt >= 13
  11316.     and    o1.id = c1.id
  11317.     and    c1.colid = r.rkey13
  11318.     and r.fkeyid = o2.id
  11319.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11320.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11321.     and    o2.id = c2.id    
  11322.     and    c2.colid = r.fkey13
  11323.     union all
  11324.     select
  11325.         db_name(r.rkeydbid),
  11326.         user_name(o1.uid),
  11327.         o1.name,
  11328.         c1.name,
  11329.         convert(binary(16),null),
  11330.         convert(int,null),
  11331.         db_name(r.fkeydbid),
  11332.         user_name(o2.uid),
  11333.         o2.name,
  11334.         c2.name,
  11335.         convert(binary(16),null),
  11336.         convert(int,null),
  11337.         convert(int,14),
  11338.         'NO ACTION',
  11339.         'NO ACTION',
  11340.         object_name(s.constid),
  11341.         object_name(r.constid),
  11342.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11343.     from
  11344.         sysobjects o1, sysobjects o2,
  11345.         syscolumns c1, syscolumns c2,
  11346.         sysreferences r, sysconstraints s
  11347.     where    
  11348.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11349.     and    o1.name = @pk_table_name
  11350.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11351.     and    o1.id = r.rkeyid
  11352.     and s.id = o1.id and (s.status & 0xf) = 1
  11353.     and    r.keycnt >= 14
  11354.     and    o1.id = c1.id
  11355.     and    c1.colid = r.rkey14
  11356.     and r.fkeyid = o2.id
  11357.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11358.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11359.     and    o2.id = c2.id    
  11360.     and    c2.colid = r.fkey14
  11361.     union all
  11362.     select
  11363.         db_name(r.rkeydbid),
  11364.         user_name(o1.uid),
  11365.         o1.name,
  11366.         c1.name,
  11367.         convert(binary(16),null),
  11368.         convert(int,null),
  11369.         db_name(r.fkeydbid),
  11370.         user_name(o2.uid),
  11371.         o2.name,
  11372.         c2.name,
  11373.         convert(binary(16),null),
  11374.         convert(int,null),
  11375.         convert(int,15),
  11376.         'NO ACTION',
  11377.         'NO ACTION',
  11378.         object_name(s.constid),
  11379.         object_name(r.constid),
  11380.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11381.     from
  11382.         sysobjects o1, sysobjects o2,
  11383.         syscolumns c1, syscolumns c2,
  11384.         sysreferences r, sysconstraints s
  11385.     where    
  11386.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11387.     and    o1.name = @pk_table_name
  11388.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11389.     and    o1.id = r.rkeyid
  11390.     and s.id = o1.id and (s.status & 0xf) = 1
  11391.     and    r.keycnt >= 15
  11392.     and    o1.id = c1.id
  11393.     and    c1.colid = r.rkey15
  11394.     and r.fkeyid = o2.id
  11395.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11396.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11397.     and    o2.id = c2.id    
  11398.     and    c2.colid = r.fkey15
  11399.     union all
  11400.     select
  11401.         db_name(r.rkeydbid),
  11402.         user_name(o1.uid),
  11403.         o1.name,
  11404.         c1.name,
  11405.         convert(binary(16),null),
  11406.         convert(int,null),
  11407.         db_name(r.fkeydbid),
  11408.         user_name(o2.uid),
  11409.         o2.name,
  11410.         c2.name,
  11411.         convert(binary(16),null),
  11412.         convert(int,null),
  11413.         convert(int,16),
  11414.         'NO ACTION',
  11415.         'NO ACTION',
  11416.         object_name(s.constid),
  11417.         object_name(r.constid),
  11418.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11419.     from
  11420.         sysobjects o1, sysobjects o2,
  11421.         syscolumns c1, syscolumns c2,
  11422.         sysreferences r, sysconstraints s
  11423.     where    
  11424.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11425.     and    o1.name = @pk_table_name
  11426.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11427.     and    o1.id = r.rkeyid
  11428.     and s.id = o1.id and (s.status & 0xf) = 1
  11429.     and    r.keycnt >= 16
  11430.     and    o1.id = c1.id
  11431.     and    c1.colid = r.rkey16
  11432.     and r.fkeyid = o2.id
  11433.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11434.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11435.     and    o2.id = c2.id    
  11436.     and    c2.colid = r.fkey16
  11437.     order by 8,9,2,3,13
  11438.     END
  11439. ELSE IF @fk_table_name is not null
  11440.     BEGIN
  11441.     select
  11442.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11443.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11444.         PK_TABLE_NAME         = o1.name,
  11445.         PK_COLUMN_NAME         = c1.name,
  11446.         PK_COLUMN_GUID        = convert(binary(16),null),
  11447.         PK_COLUMN_PROPID    = convert(int,null),
  11448.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11449.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11450.         FK_TABLE_NAME         = o2.name,
  11451.         FK_COLUMN_NAME         = c2.name,
  11452.         FK_COLUMN_GUID        = convert(binary(16),null),
  11453.         FK_COLUMN_PROPID    = convert(int,null),
  11454.         ORDINAL                = convert(int,1),
  11455.         UPDATE_RULE            = 'NO ACTION',
  11456.         DELETE_RULE         = 'NO ACTION',
  11457.         PK_NAME                = object_name(s.constid),
  11458.         FK_NAME                = object_name(r.constid),
  11459.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11460.     from
  11461.         sysobjects o1, sysobjects o2,
  11462.         syscolumns c1, syscolumns c2,
  11463.         sysreferences r, sysconstraints s
  11464.     where    
  11465.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11466.     and    o2.name = @fk_table_name
  11467.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11468.     and    o2.id = r.fkeyid
  11469.     and    o2.id = c2.id
  11470.     and    c2.colid = r.fkey1
  11471.     and r.rkeyid = o1.id
  11472.     and s.id = o1.id and (s.status & 0xf) = 1
  11473.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11474.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11475.     and    o1.id = c1.id    
  11476.     and    c1.colid = r.rkey1
  11477.     union    all
  11478.     select
  11479.         db_name(r.rkeydbid),
  11480.         user_name(o1.uid),
  11481.         o1.name,
  11482.         c1.name,
  11483.         convert(binary(16),null),
  11484.         convert(int,null),
  11485.         db_name(r.fkeydbid),
  11486.         user_name(o2.uid),
  11487.         o2.name,
  11488.         c2.name,
  11489.         convert(binary(16),null),
  11490.         convert(int,null),
  11491.         convert(int,2),
  11492.         'NO ACTION',
  11493.         'NO ACTION',
  11494.         object_name(s.constid),
  11495.         object_name(r.constid),
  11496.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11497.     from
  11498.         sysobjects o1, sysobjects o2,
  11499.         syscolumns c1, syscolumns c2,
  11500.         sysreferences r, sysconstraints s
  11501.     where    
  11502.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11503.     and    o2.name = @fk_table_name
  11504.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11505.     and    o2.id = r.fkeyid
  11506.     and    r.keycnt >= 2
  11507.     and    o2.id = c2.id
  11508.     and    c2.colid = r.fkey2
  11509.     and r.rkeyid = o1.id
  11510.     and s.id = o1.id and (s.status & 0xf) = 1
  11511.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11512.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11513.     and    o1.id = c1.id    
  11514.     and    c1.colid = r.rkey2
  11515.     union    all
  11516.     select
  11517.         db_name(r.rkeydbid),
  11518.         user_name(o1.uid),
  11519.         o1.name,
  11520.         c1.name,
  11521.         convert(binary(16),null),
  11522.         convert(int,null),
  11523.         db_name(r.fkeydbid),
  11524.         user_name(o2.uid),
  11525.         o2.name,
  11526.         c2.name,
  11527.         convert(binary(16),null),
  11528.         convert(int,null),
  11529.         convert(int,3),
  11530.         'NO ACTION',
  11531.         'NO ACTION',
  11532.         object_name(s.constid),
  11533.         object_name(r.constid),
  11534.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11535.     from
  11536.         sysobjects o1, sysobjects o2,
  11537.         syscolumns c1, syscolumns c2,
  11538.         sysreferences r, sysconstraints s
  11539.     where    
  11540.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11541.     and    o2.name = @fk_table_name
  11542.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11543.     and    o2.id = r.fkeyid
  11544.     and    r.keycnt >= 3
  11545.     and    o2.id = c2.id
  11546.     and    c2.colid = r.fkey3
  11547.     and r.rkeyid = o1.id
  11548.     and s.id = o1.id and (s.status & 0xf) = 1
  11549.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11550.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11551.     and    o1.id = c1.id    
  11552.     and    c1.colid = r.rkey3
  11553.     union    all
  11554.     select
  11555.         db_name(r.rkeydbid),
  11556.         user_name(o1.uid),
  11557.         o1.name,
  11558.         c1.name,
  11559.         convert(binary(16),null),
  11560.         convert(int,null),
  11561.         db_name(r.fkeydbid),
  11562.         user_name(o2.uid),
  11563.         o2.name,
  11564.         c2.name,
  11565.         convert(binary(16),null),
  11566.         convert(int,null),
  11567.         convert(int,4),
  11568.         'NO ACTION',
  11569.         'NO ACTION',
  11570.         object_name(s.constid),
  11571.         object_name(r.constid),
  11572.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11573.     from
  11574.         sysobjects o1, sysobjects o2,
  11575.         syscolumns c1, syscolumns c2,
  11576.         sysreferences r, sysconstraints s
  11577.     where    
  11578.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11579.     and    o2.name = @fk_table_name
  11580.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11581.     and    o2.id = r.fkeyid
  11582.     and    r.keycnt >= 4
  11583.     and    o2.id = c2.id
  11584.     and    c2.colid = r.fkey4
  11585.     and r.rkeyid = o1.id
  11586.     and s.id = o1.id and (s.status & 0xf) = 1
  11587.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11588.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11589.     and    o1.id = c1.id    
  11590.     and    c1.colid = r.rkey4
  11591.     union    all
  11592.     select
  11593.         db_name(r.rkeydbid),
  11594.         user_name(o1.uid),
  11595.         o1.name,
  11596.         c1.name,
  11597.         convert(binary(16),null),
  11598.         convert(int,null),
  11599.         db_name(r.fkeydbid),
  11600.         user_name(o2.uid),
  11601.         o2.name,
  11602.         c2.name,
  11603.         convert(binary(16),null),
  11604.         convert(int,null),
  11605.         convert(int,5),
  11606.         'NO ACTION',
  11607.         'NO ACTION',
  11608.         object_name(s.constid),
  11609.         object_name(r.constid),
  11610.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11611.     from
  11612.         sysobjects o1, sysobjects o2,
  11613.         syscolumns c1, syscolumns c2,
  11614.         sysreferences r, sysconstraints s
  11615.     where    
  11616.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11617.     and    o2.name = @fk_table_name
  11618.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11619.     and    o2.id = r.fkeyid
  11620.     and    r.keycnt >= 5
  11621.     and    o2.id = c2.id
  11622.     and    c2.colid = r.fkey5
  11623.     and r.rkeyid = o1.id
  11624.     and s.id = o1.id and (s.status & 0xf) = 1
  11625.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11626.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11627.     and    o1.id = c1.id    
  11628.     and    c1.colid = r.rkey5
  11629.     union    all
  11630.     select
  11631.         db_name(r.rkeydbid),
  11632.         user_name(o1.uid),
  11633.         o1.name,
  11634.         c1.name,
  11635.         convert(binary(16),null),
  11636.         convert(int,null),
  11637.         db_name(r.fkeydbid),
  11638.         user_name(o2.uid),
  11639.         o2.name,
  11640.         c2.name,
  11641.         convert(binary(16),null),
  11642.         convert(int,null),
  11643.         convert(int,6),
  11644.         'NO ACTION',
  11645.         'NO ACTION',
  11646.         object_name(s.constid),
  11647.         object_name(r.constid),
  11648.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11649.     from
  11650.         sysobjects o1, sysobjects o2,
  11651.         syscolumns c1, syscolumns c2,
  11652.         sysreferences r, sysconstraints s
  11653.     where    
  11654.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11655.     and    o2.name = @fk_table_name
  11656.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11657.     and    o2.id = r.fkeyid
  11658.     and    r.keycnt >= 6
  11659.     and    o2.id = c2.id
  11660.     and    c2.colid = r.fkey6
  11661.     and r.rkeyid = o1.id
  11662.     and s.id = o1.id and (s.status & 0xf) = 1
  11663.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11664.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11665.     and    o1.id = c1.id    
  11666.     and    c1.colid = r.rkey6
  11667.     union    all
  11668.     select
  11669.         db_name(r.rkeydbid),
  11670.         user_name(o1.uid),
  11671.         o1.name,
  11672.         c1.name,
  11673.         convert(binary(16),null),
  11674.         convert(int,null),
  11675.         db_name(r.fkeydbid),
  11676.         user_name(o2.uid),
  11677.         o2.name,
  11678.         c2.name,
  11679.         convert(binary(16),null),
  11680.         convert(int,null),
  11681.         convert(int,7),
  11682.         'NO ACTION',
  11683.         'NO ACTION',
  11684.         object_name(s.constid),
  11685.         object_name(r.constid),
  11686.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11687.     from
  11688.         sysobjects o1, sysobjects o2,
  11689.         syscolumns c1, syscolumns c2,
  11690.         sysreferences r, sysconstraints s
  11691.     where    
  11692.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11693.     and    o2.name = @fk_table_name
  11694.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11695.     and    o2.id = r.fkeyid
  11696.     and    r.keycnt >= 7
  11697.     and    o2.id = c2.id
  11698.     and    c2.colid = r.fkey7
  11699.     and    r.rkeyid = o1.id
  11700.     and s.id = o1.id and (s.status & 0xf) = 1
  11701.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11702.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11703.     and    o1.id = c1.id    
  11704.     and    c1.colid = r.rkey7
  11705.     union    all
  11706.     select
  11707.         db_name(r.rkeydbid),
  11708.         user_name(o1.uid),
  11709.         o1.name,
  11710.         c1.name,
  11711.         convert(binary(16),null),
  11712.         convert(int,null),
  11713.         db_name(r.fkeydbid),
  11714.         user_name(o2.uid),
  11715.         o2.name,
  11716.         c2.name,
  11717.         convert(binary(16),null),
  11718.         convert(int,null),
  11719.         convert(int,8),
  11720.         'NO ACTION',
  11721.         'NO ACTION',
  11722.         object_name(s.constid),
  11723.         object_name(r.constid),
  11724.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11725.     from
  11726.         sysobjects o1, sysobjects o2,
  11727.         syscolumns c1, syscolumns c2,
  11728.         sysreferences r, sysconstraints s
  11729.     where    
  11730.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11731.     and    o2.name = @fk_table_name
  11732.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11733.     and    o2.id = r.fkeyid
  11734.     and    r.keycnt >= 8
  11735.     and    o2.id = c2.id
  11736.     and    c2.colid = r.fkey8
  11737.     and r.rkeyid = o1.id
  11738.     and s.id = o1.id and (s.status & 0xf) = 1
  11739.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11740.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11741.     and    o1.id = c1.id    
  11742.     and    c1.colid = r.rkey8
  11743.     union    all
  11744.     select
  11745.         db_name(r.rkeydbid),
  11746.         user_name(o1.uid),
  11747.         o1.name,
  11748.         c1.name,
  11749.         convert(binary(16),null),
  11750.         convert(int,null),
  11751.         db_name(r.fkeydbid),
  11752.         user_name(o2.uid),
  11753.         o2.name,
  11754.         c2.name,
  11755.         convert(binary(16),null),
  11756.         convert(int,null),
  11757.         convert(int,9),
  11758.         'NO ACTION',
  11759.         'NO ACTION',
  11760.         object_name(s.constid),
  11761.         object_name(r.constid),
  11762.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11763.     from
  11764.         sysobjects o1, sysobjects o2,
  11765.         syscolumns c1, syscolumns c2,
  11766.         sysreferences r, sysconstraints s
  11767.     where    
  11768.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11769.     and    o2.name = @fk_table_name
  11770.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11771.     and    o2.id = r.fkeyid
  11772.     and    r.keycnt >= 9
  11773.     and    o2.id = c2.id
  11774.     and    c2.colid = r.fkey9
  11775.     and r.rkeyid = o1.id
  11776.     and s.id = o1.id and (s.status & 0xf) = 1
  11777.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11778.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11779.     and    o1.id = c1.id    
  11780.     and    c1.colid = r.rkey9
  11781.     union    all
  11782.     select
  11783.         db_name(r.rkeydbid),
  11784.         user_name(o1.uid),
  11785.         o1.name,
  11786.         c1.name,
  11787.         convert(binary(16),null),
  11788.         convert(int,null),
  11789.         db_name(r.fkeydbid),
  11790.         user_name(o2.uid),
  11791.         o2.name,
  11792.         c2.name,
  11793.         convert(binary(16),null),
  11794.         convert(int,null),
  11795.         convert(int,10),
  11796.         'NO ACTION',
  11797.         'NO ACTION',
  11798.         object_name(s.constid),
  11799.         object_name(r.constid),
  11800.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11801.     from
  11802.         sysobjects o1, sysobjects o2,
  11803.         syscolumns c1, syscolumns c2,
  11804.         sysreferences r, sysconstraints s
  11805.     where    
  11806.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11807.     and    o2.name = @fk_table_name
  11808.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11809.     and    o2.id = r.fkeyid
  11810.     and    r.keycnt >= 10
  11811.     and    o2.id = c2.id
  11812.     and    c2.colid = r.fkey10
  11813.     and r.rkeyid = o1.id
  11814.     and s.id = o1.id and (s.status & 0xf) = 1
  11815.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11816.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11817.     and    o1.id = c1.id    
  11818.     and    c1.colid = r.rkey10
  11819.     union    all
  11820.     select
  11821.         db_name(r.rkeydbid),
  11822.         user_name(o1.uid),
  11823.         o1.name,
  11824.         c1.name,
  11825.         convert(binary(16),null),
  11826.         convert(int,null),
  11827.         db_name(r.fkeydbid),
  11828.         user_name(o2.uid),
  11829.         o2.name,
  11830.         c2.name,
  11831.         convert(binary(16),null),
  11832.         convert(int,null),
  11833.         convert(int,11),
  11834.         'NO ACTION',
  11835.         'NO ACTION',
  11836.         object_name(s.constid),
  11837.         object_name(r.constid),
  11838.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11839.     from
  11840.         sysobjects o1, sysobjects o2,
  11841.         syscolumns c1, syscolumns c2,
  11842.         sysreferences r, sysconstraints s
  11843.     where    
  11844.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11845.     and    o2.name = @fk_table_name
  11846.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11847.     and    o2.id = r.fkeyid
  11848.     and    r.keycnt >= 11
  11849.     and    o2.id = c2.id
  11850.     and    c2.colid = r.fkey11
  11851.     and r.rkeyid = o1.id
  11852.     and s.id = o1.id and (s.status & 0xf) = 1
  11853.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11854.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11855.     and    o1.id = c1.id    
  11856.     and    c1.colid = r.rkey11
  11857.     union    all
  11858.     select
  11859.         db_name(r.rkeydbid),
  11860.         user_name(o1.uid),
  11861.         o1.name,
  11862.         c1.name,
  11863.         convert(binary(16),null),
  11864.         convert(int,null),
  11865.         db_name(r.fkeydbid),
  11866.         user_name(o2.uid),
  11867.         o2.name,
  11868.         c2.name,
  11869.         convert(binary(16),null),
  11870.         convert(int,null),
  11871.         convert(int,12),
  11872.         'NO ACTION',
  11873.         'NO ACTION',
  11874.         object_name(s.constid),
  11875.         object_name(r.constid),
  11876.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11877.     from
  11878.         sysobjects o1, sysobjects o2,
  11879.         syscolumns c1, syscolumns c2,
  11880.         sysreferences r, sysconstraints s
  11881.     where    
  11882.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11883.     and    o2.name = @fk_table_name
  11884.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11885.     and    o2.id = r.fkeyid
  11886.     and    r.keycnt >= 12
  11887.     and    o2.id = c2.id
  11888.     and    c2.colid = r.fkey12
  11889.     and r.rkeyid = o1.id
  11890.     and s.id = o1.id and (s.status & 0xf) = 1
  11891.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11892.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11893.     and    o1.id = c1.id    
  11894.     and    c1.colid = r.rkey12
  11895.     union    all
  11896.     select
  11897.         db_name(r.rkeydbid),
  11898.         user_name(o1.uid),
  11899.         o1.name,
  11900.         c1.name,
  11901.         convert(binary(16),null),
  11902.         convert(int,null),
  11903.         db_name(r.fkeydbid),
  11904.         user_name(o2.uid),
  11905.         o2.name,
  11906.         c2.name,
  11907.         convert(binary(16),null),
  11908.         convert(int,null),
  11909.         convert(int,13),
  11910.         'NO ACTION',
  11911.         'NO ACTION',
  11912.         object_name(s.constid),
  11913.         object_name(r.constid),
  11914.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11915.     from
  11916.         sysobjects o1, sysobjects o2,
  11917.         syscolumns c1, syscolumns c2,
  11918.         sysreferences r, sysconstraints s
  11919.     where    
  11920.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11921.     and    o2.name = @fk_table_name
  11922.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11923.     and    o2.id = r.fkeyid
  11924.     and    r.keycnt >= 13
  11925.     and    o2.id = c2.id
  11926.     and    c2.colid = r.fkey13
  11927.     and r.rkeyid = o1.id
  11928.     and s.id = o1.id and (s.status & 0xf) = 1
  11929.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11930.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11931.     and    o1.id = c1.id    
  11932.     and    c1.colid = r.rkey13
  11933.     union    all
  11934.     select
  11935.         db_name(r.rkeydbid),
  11936.         user_name(o1.uid),
  11937.         o1.name,
  11938.         c1.name,
  11939.         convert(binary(16),null),
  11940.         convert(int,null),
  11941.         db_name(r.fkeydbid),
  11942.         user_name(o2.uid),
  11943.         o2.name,
  11944.         c2.name,
  11945.         convert(binary(16),null),
  11946.         convert(int,null),
  11947.         convert(int,14),
  11948.         'NO ACTION',
  11949.         'NO ACTION',
  11950.         object_name(s.constid),
  11951.         object_name(r.constid),
  11952.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11953.     from
  11954.         sysobjects o1, sysobjects o2,
  11955.         syscolumns c1, syscolumns c2,
  11956.         sysreferences r, sysconstraints s
  11957.     where    
  11958.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11959.     and    o2.name = @fk_table_name
  11960.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11961.     and    o2.id = r.fkeyid
  11962.     and    r.keycnt >= 14
  11963.     and    o2.id = c2.id
  11964.     and    c2.colid = r.fkey14
  11965.     and    r.rkeyid = o1.id
  11966.     and s.id = o1.id and (s.status & 0xf) = 1
  11967.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11968.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11969.     and    o1.id = c1.id    
  11970.     and    c1.colid = r.rkey14
  11971.     union    all
  11972.     select
  11973.         db_name(r.rkeydbid),
  11974.         user_name(o1.uid),
  11975.         o1.name,
  11976.         c1.name,
  11977.         convert(binary(16),null),
  11978.         convert(int,null),
  11979.         db_name(r.fkeydbid),
  11980.         user_name(o2.uid),
  11981.         o2.name,
  11982.         c2.name,
  11983.         convert(binary(16),null),
  11984.         convert(int,null),
  11985.         convert(int,15),
  11986.         'NO ACTION',
  11987.         'NO ACTION',
  11988.         object_name(s.constid),
  11989.         object_name(r.constid),
  11990.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11991.     from
  11992.         sysobjects o1, sysobjects o2,
  11993.         syscolumns c1, syscolumns c2,
  11994.         sysreferences r, sysconstraints s
  11995.     where    
  11996.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11997.     and    o2.name = @fk_table_name
  11998.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11999.     and    o2.id = r.fkeyid
  12000.     and    r.keycnt >= 15
  12001.     and    o2.id = c2.id
  12002.     and    c2.colid = r.fkey15
  12003.     and r.rkeyid = o1.id
  12004.     and s.id = o1.id and (s.status & 0xf) = 1
  12005.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12006.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12007.     and    o1.id = c1.id    
  12008.     and    c1.colid = r.rkey15
  12009.     union    all
  12010.     select
  12011.         db_name(r.rkeydbid),
  12012.         user_name(o1.uid),
  12013.         o1.name,
  12014.         c1.name,
  12015.         convert(binary(16),null),
  12016.         convert(int,null),
  12017.         db_name(r.fkeydbid),
  12018.         user_name(o2.uid),
  12019.         o2.name,
  12020.         c2.name,
  12021.         convert(binary(16),null),
  12022.         convert(int,null),
  12023.         convert(int,16),
  12024.         'NO ACTION',
  12025.         'NO ACTION',
  12026.         object_name(s.constid),
  12027.         object_name(r.constid),
  12028.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12029.     from
  12030.         sysobjects o1, sysobjects o2,
  12031.         syscolumns c1, syscolumns c2,
  12032.         sysreferences r, sysconstraints s
  12033.     where    
  12034.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12035.     and    o2.name = @fk_table_name
  12036.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12037.     and    o2.id = r.fkeyid
  12038.     and    r.keycnt >= 16
  12039.     and    o2.id = c2.id
  12040.     and    c2.colid = r.fkey16
  12041.     and r.rkeyid = o1.id
  12042.     and s.id = o1.id and (s.status & 0xf) = 1
  12043.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12044.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12045.     and    o1.id = c1.id    
  12046.     and    c1.colid = r.rkey16
  12047.     order by 8,9,2,3,13
  12048.     END
  12049. ELSE
  12050.     BEGIN
  12051.     select
  12052.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12053.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12054.         PK_TABLE_NAME         = o1.name,
  12055.         PK_COLUMN_NAME         = c1.name,
  12056.         PK_COLUMN_GUID        = convert(binary(16),null),
  12057.         PK_COLUMN_PROPID    = convert(int,null),
  12058.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12059.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12060.         FK_TABLE_NAME         = o2.name,
  12061.         FK_COLUMN_NAME         = c2.name,
  12062.         FK_COLUMN_GUID        = convert(binary(16),null),
  12063.         FK_COLUMN_PROPID    = convert(int,null),
  12064.         ORDINAL                = convert(int,1),
  12065.         UPDATE_RULE            = 'NO ACTION',
  12066.         DELETE_RULE         = 'NO ACTION',
  12067.         PK_NAME                = object_name(s.constid),
  12068.         FK_NAME                = object_name(r.constid),
  12069.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12070.     from
  12071.         sysobjects o1, sysobjects o2,
  12072.         syscolumns c1, syscolumns c2,
  12073.         sysreferences r, sysconstraints s
  12074.     where    
  12075.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12076.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12077.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12078.     and    o1.id = r.rkeyid
  12079.     and s.id = o1.id and (s.status & 0xf) = 1
  12080.     and    o1.id = c1.id
  12081.     and    c1.colid = r.rkey1
  12082.     and r.fkeyid = o2.id
  12083.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12084.     and    o2.id = c2.id    
  12085.     and    c2.colid = r.fkey1
  12086.     union all
  12087.     select
  12088.         db_name(r.rkeydbid),
  12089.         user_name(o1.uid),
  12090.         o1.name,
  12091.         c1.name,
  12092.         convert(binary(16),null),
  12093.         convert(int,null),
  12094.         db_name(r.fkeydbid),
  12095.         user_name(o2.uid),
  12096.         o2.name,
  12097.         c2.name,
  12098.         convert(binary(16),null),
  12099.         convert(int,null),
  12100.         convert(int,2),
  12101.         'NO ACTION',
  12102.         'NO ACTION',
  12103.         object_name(s.constid),
  12104.         object_name(r.constid),
  12105.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12106.     from
  12107.         sysobjects o1, sysobjects o2,
  12108.         syscolumns c1, syscolumns c2,
  12109.         sysreferences r, sysconstraints s
  12110.     where    
  12111.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12112.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12113.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12114.     and    o1.id = r.rkeyid
  12115.     and s.id = o1.id and (s.status & 0xf) = 1
  12116.     and    r.keycnt >= 2
  12117.     and    o1.id = c1.id
  12118.     and    c1.colid = r.rkey2
  12119.     and r.fkeyid = o2.id
  12120.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12121.     and    o2.id = c2.id    
  12122.     and    c2.colid = r.fkey2
  12123.     union all
  12124.     select
  12125.         db_name(r.rkeydbid),
  12126.         user_name(o1.uid),
  12127.         o1.name,
  12128.         c1.name,
  12129.         convert(binary(16),null),
  12130.         convert(int,null),
  12131.         db_name(r.fkeydbid),
  12132.         user_name(o2.uid),
  12133.         o2.name,
  12134.         c2.name,
  12135.         convert(binary(16),null),
  12136.         convert(int,null),
  12137.         convert(int,3),
  12138.         'NO ACTION',
  12139.         'NO ACTION',
  12140.         object_name(s.constid),
  12141.         object_name(r.constid),
  12142.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12143.     from
  12144.         sysobjects o1, sysobjects o2,
  12145.         syscolumns c1, syscolumns c2,
  12146.         sysreferences r, sysconstraints s
  12147.     where    
  12148.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12149.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12150.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12151.     and    o1.id = r.rkeyid
  12152.     and s.id = o1.id and (s.status & 0xf) = 1
  12153.     and    r.keycnt >= 3
  12154.     and    o1.id = c1.id
  12155.     and    c1.colid = r.rkey3
  12156.     and r.fkeyid = o2.id
  12157.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12158.     and    o2.id = c2.id    
  12159.     and    c2.colid = r.fkey3
  12160.     union all
  12161.     select
  12162.         db_name(r.rkeydbid),
  12163.         user_name(o1.uid),
  12164.         o1.name,
  12165.         c1.name,
  12166.         convert(binary(16),null),
  12167.         convert(int,null),
  12168.         db_name(r.fkeydbid),
  12169.         user_name(o2.uid),
  12170.         o2.name,
  12171.         c2.name,
  12172.         convert(binary(16),null),
  12173.         convert(int,null),
  12174.         convert(int,4),
  12175.         'NO ACTION',
  12176.         'NO ACTION',
  12177.         object_name(s.constid),
  12178.         object_name(r.constid),
  12179.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12180.     from
  12181.         sysobjects o1, sysobjects o2,
  12182.         syscolumns c1, syscolumns c2,
  12183.         sysreferences r, sysconstraints s
  12184.     where    
  12185.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12186.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12187.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12188.     and    o1.id = r.rkeyid
  12189.     and s.id = o1.id and (s.status & 0xf) = 1
  12190.     and    r.keycnt >= 4
  12191.     and    o1.id = c1.id
  12192.     and    c1.colid = r.rkey4
  12193.     and r.fkeyid = o2.id
  12194.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12195.     and    o2.id = c2.id    
  12196.     and    c2.colid = r.fkey4
  12197.     union all
  12198.     select
  12199.         db_name(r.rkeydbid),
  12200.         user_name(o1.uid),
  12201.         o1.name,
  12202.         c1.name,
  12203.         convert(binary(16),null),
  12204.         convert(int,null),
  12205.         db_name(r.fkeydbid),
  12206.         user_name(o2.uid),
  12207.         o2.name,
  12208.         c2.name,
  12209.         convert(binary(16),null),
  12210.         convert(int,null),
  12211.         convert(int,5),
  12212.         'NO ACTION',
  12213.         'NO ACTION',
  12214.         object_name(s.constid),
  12215.         object_name(r.constid),
  12216.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12217.     from
  12218.         sysobjects o1, sysobjects o2,
  12219.         syscolumns c1, syscolumns c2,
  12220.         sysreferences r, sysconstraints s
  12221.     where    
  12222.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12223.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12224.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12225.     and    o1.id = r.rkeyid
  12226.     and s.id = o1.id and (s.status & 0xf) = 1
  12227.     and    r.keycnt >= 5
  12228.     and    o1.id = c1.id
  12229.     and    c1.colid = r.rkey5
  12230.     and r.fkeyid = o2.id
  12231.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12232.     and    o2.id = c2.id    
  12233.     and    c2.colid = r.fkey5
  12234.     union all
  12235.     select
  12236.         db_name(r.rkeydbid),
  12237.         user_name(o1.uid),
  12238.         o1.name,
  12239.         c1.name,
  12240.         convert(binary(16),null),
  12241.         convert(int,null),
  12242.         db_name(r.fkeydbid),
  12243.         user_name(o2.uid),
  12244.         o2.name,
  12245.         c2.name,
  12246.         convert(binary(16),null),
  12247.         convert(int,null),
  12248.         convert(int,6),
  12249.         'NO ACTION',
  12250.         'NO ACTION',
  12251.         object_name(s.constid),
  12252.         object_name(r.constid),
  12253.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12254.     from
  12255.         sysobjects o1, sysobjects o2,
  12256.         syscolumns c1, syscolumns c2,
  12257.         sysreferences r, sysconstraints s
  12258.     where    
  12259.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12260.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12261.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12262.     and    o1.id = r.rkeyid
  12263.     and s.id = o1.id and (s.status & 0xf) = 1
  12264.     and    r.keycnt >= 6
  12265.     and    o1.id = c1.id
  12266.     and    c1.colid = r.rkey6
  12267.     and r.fkeyid = o2.id
  12268.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12269.     and    o2.id = c2.id    
  12270.     and    c2.colid = r.fkey6
  12271.     union all
  12272.     select
  12273.         db_name(r.rkeydbid),
  12274.         user_name(o1.uid),
  12275.         o1.name,
  12276.         c1.name,
  12277.         convert(binary(16),null),
  12278.         convert(int,null),
  12279.         db_name(r.fkeydbid),
  12280.         user_name(o2.uid),
  12281.         o2.name,
  12282.         c2.name,
  12283.         convert(binary(16),null),
  12284.         convert(int,null),
  12285.         convert(int,7),
  12286.         'NO ACTION',
  12287.         'NO ACTION',
  12288.         object_name(s.constid),
  12289.         object_name(r.constid),
  12290.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12291.     from
  12292.         sysobjects o1, sysobjects o2,
  12293.         syscolumns c1, syscolumns c2,
  12294.         sysreferences r, sysconstraints s
  12295.     where    
  12296.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12297.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12298.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12299.     and    o1.id = r.rkeyid
  12300.     and s.id = o1.id and (s.status & 0xf) = 1
  12301.     and    r.keycnt >= 7
  12302.     and    o1.id = c1.id
  12303.     and    c1.colid = r.rkey7
  12304.     and r.fkeyid = o2.id
  12305.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12306.     and    o2.id = c2.id    
  12307.     and    c2.colid = r.fkey7
  12308.     union all
  12309.     select
  12310.         db_name(r.rkeydbid),
  12311.         user_name(o1.uid),
  12312.         o1.name,
  12313.         c1.name,
  12314.         convert(binary(16),null),
  12315.         convert(int,null),
  12316.         db_name(r.fkeydbid),
  12317.         user_name(o2.uid),
  12318.         o2.name,
  12319.         c2.name,
  12320.         convert(binary(16),null),
  12321.         convert(int,null),
  12322.         convert(int,8),
  12323.         'NO ACTION',
  12324.         'NO ACTION',
  12325.         object_name(s.constid),
  12326.         object_name(r.constid),
  12327.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12328.     from
  12329.         sysobjects o1, sysobjects o2,
  12330.         syscolumns c1, syscolumns c2,
  12331.         sysreferences r, sysconstraints s
  12332.     where    
  12333.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12334.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12335.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12336.     and    o1.id = r.rkeyid
  12337.     and s.id = o1.id and (s.status & 0xf) = 1
  12338.     and    r.keycnt >= 8
  12339.     and    o1.id = c1.id
  12340.     and    c1.colid = r.rkey8
  12341.     and r.fkeyid = o2.id
  12342.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12343.     and    o2.id = c2.id    
  12344.     and    c2.colid = r.fkey8
  12345.     union all
  12346.     select
  12347.         db_name(r.rkeydbid),
  12348.         user_name(o1.uid),
  12349.         o1.name,
  12350.         c1.name,
  12351.         convert(binary(16),null),
  12352.         convert(int,null),
  12353.         db_name(r.fkeydbid),
  12354.         user_name(o2.uid),
  12355.         o2.name,
  12356.         c2.name,
  12357.         convert(binary(16),null),
  12358.         convert(int,null),
  12359.         convert(int,9),
  12360.         'NO ACTION',
  12361.         'NO ACTION',
  12362.         object_name(s.constid),
  12363.         object_name(r.constid),
  12364.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12365.     from
  12366.         sysobjects o1, sysobjects o2,
  12367.         syscolumns c1, syscolumns c2,
  12368.         sysreferences r, sysconstraints s
  12369.     where    
  12370.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12371.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12372.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12373.     and    o1.id = r.rkeyid
  12374.     and s.id = o1.id and (s.status & 0xf) = 1
  12375.     and    r.keycnt >= 9
  12376.     and    o1.id = c1.id
  12377.     and    c1.colid = r.rkey9
  12378.     and r.fkeyid = o2.id
  12379.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12380.     and    o2.id = c2.id    
  12381.     and    c2.colid = r.fkey9
  12382.     union all
  12383.     select
  12384.         db_name(r.rkeydbid),
  12385.         user_name(o1.uid),
  12386.         o1.name,
  12387.         c1.name,
  12388.         convert(binary(16),null),
  12389.         convert(int,null),
  12390.         db_name(r.fkeydbid),
  12391.         user_name(o2.uid),
  12392.         o2.name,
  12393.         c2.name,
  12394.         convert(binary(16),null),
  12395.         convert(int,null),
  12396.         convert(int,10),
  12397.         'NO ACTION',
  12398.         'NO ACTION',
  12399.         object_name(s.constid),
  12400.         object_name(r.constid),
  12401.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12402.     from
  12403.         sysobjects o1, sysobjects o2,
  12404.         syscolumns c1, syscolumns c2,
  12405.         sysreferences r, sysconstraints s
  12406.     where    
  12407.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12408.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12409.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12410.     and    o1.id = r.rkeyid
  12411.     and s.id = o1.id and (s.status & 0xf) = 1
  12412.     and    r.keycnt >= 10
  12413.     and    o1.id = c1.id
  12414.     and    c1.colid = r.rkey10
  12415.     and r.fkeyid = o2.id
  12416.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12417.     and    o2.id = c2.id    
  12418.     and    c2.colid = r.fkey10
  12419.     union all
  12420.     select
  12421.         db_name(r.rkeydbid),
  12422.         user_name(o1.uid),
  12423.         o1.name,
  12424.         c1.name,
  12425.         convert(binary(16),null),
  12426.         convert(int,null),
  12427.         db_name(r.fkeydbid),
  12428.         user_name(o2.uid),
  12429.         o2.name,
  12430.         c2.name,
  12431.         convert(binary(16),null),
  12432.         convert(int,null),
  12433.         convert(int,11),
  12434.         'NO ACTION',
  12435.         'NO ACTION',
  12436.         object_name(s.constid),
  12437.         object_name(r.constid),
  12438.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12439.     from
  12440.         sysobjects o1, sysobjects o2,
  12441.         syscolumns c1, syscolumns c2,
  12442.         sysreferences r, sysconstraints s
  12443.     where    
  12444.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12445.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12446.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12447.     and    o1.id = r.rkeyid
  12448.     and s.id = o1.id and (s.status & 0xf) = 1
  12449.     and    r.keycnt >= 11
  12450.     and    o1.id = c1.id
  12451.     and    c1.colid = r.rkey11
  12452.     and r.fkeyid = o2.id
  12453.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12454.     and    o2.id = c2.id    
  12455.     and    c2.colid = r.fkey11
  12456.     union all
  12457.     select
  12458.         db_name(r.rkeydbid),
  12459.         user_name(o1.uid),
  12460.         o1.name,
  12461.         c1.name,
  12462.         convert(binary(16),null),
  12463.         convert(int,null),
  12464.         db_name(r.fkeydbid),
  12465.         user_name(o2.uid),
  12466.         o2.name,
  12467.         c2.name,
  12468.         convert(binary(16),null),
  12469.         convert(int,null),
  12470.         convert(int,12),
  12471.         'NO ACTION',
  12472.         'NO ACTION',
  12473.         object_name(s.constid),
  12474.         object_name(r.constid),
  12475.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12476.     from
  12477.         sysobjects o1, sysobjects o2,
  12478.         syscolumns c1, syscolumns c2,
  12479.         sysreferences r, sysconstraints s
  12480.     where    
  12481.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12482.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12483.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12484.     and    o1.id = r.rkeyid
  12485.     and s.id = o1.id and (s.status & 0xf) = 1
  12486.     and    r.keycnt >= 12
  12487.     and    o1.id = c1.id
  12488.     and    c1.colid = r.rkey12
  12489.     and r.fkeyid = o2.id
  12490.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12491.     and    o2.id = c2.id    
  12492.     and    c2.colid = r.fkey12
  12493.     union all
  12494.     select
  12495.         db_name(r.rkeydbid),
  12496.         user_name(o1.uid),
  12497.         o1.name,
  12498.         c1.name,
  12499.         convert(binary(16),null),
  12500.         convert(int,null),
  12501.         db_name(r.fkeydbid),
  12502.         user_name(o2.uid),
  12503.         o2.name,
  12504.         c2.name,
  12505.         convert(binary(16),null),
  12506.         convert(int,null),
  12507.         convert(int,13),
  12508.         'NO ACTION',
  12509.         'NO ACTION',
  12510.         object_name(s.constid),
  12511.         object_name(r.constid),
  12512.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12513.     from
  12514.         sysobjects o1, sysobjects o2,
  12515.         syscolumns c1, syscolumns c2,
  12516.         sysreferences r, sysconstraints s
  12517.     where    
  12518.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12519.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12520.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12521.     and    o1.id = r.rkeyid
  12522.     and s.id = o1.id and (s.status & 0xf) = 1
  12523.     and    r.keycnt >= 13
  12524.     and    o1.id = c1.id
  12525.     and    c1.colid = r.rkey13
  12526.     and r.fkeyid = o2.id
  12527.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12528.     and    o2.id = c2.id    
  12529.     and    c2.colid = r.fkey13
  12530.     union all
  12531.     select
  12532.         db_name(r.rkeydbid),
  12533.         user_name(o1.uid),
  12534.         o1.name,
  12535.         c1.name,
  12536.         convert(binary(16),null),
  12537.         convert(int,null),
  12538.         db_name(r.fkeydbid),
  12539.         user_name(o2.uid),
  12540.         o2.name,
  12541.         c2.name,
  12542.         convert(binary(16),null),
  12543.         convert(int,null),
  12544.         convert(int,14),
  12545.         'NO ACTION',
  12546.         'NO ACTION',
  12547.         object_name(s.constid),
  12548.         object_name(r.constid),
  12549.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12550.     from
  12551.         sysobjects o1, sysobjects o2,
  12552.         syscolumns c1, syscolumns c2,
  12553.         sysreferences r, sysconstraints s
  12554.     where    
  12555.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12556.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12557.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12558.     and    o1.id = r.rkeyid
  12559.     and s.id = o1.id and (s.status & 0xf) = 1
  12560.     and    r.keycnt >= 14
  12561.     and    o1.id = c1.id
  12562.     and    c1.colid = r.rkey14
  12563.     and r.fkeyid = o2.id
  12564.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12565.     and    o2.id = c2.id    
  12566.     and    c2.colid = r.fkey14
  12567.     union all
  12568.     select
  12569.         db_name(r.rkeydbid),
  12570.         user_name(o1.uid),
  12571.         o1.name,
  12572.         c1.name,
  12573.         convert(binary(16),null),
  12574.         convert(int,null),
  12575.         db_name(r.fkeydbid),
  12576.         user_name(o2.uid),
  12577.         o2.name,
  12578.         c2.name,
  12579.         convert(binary(16),null),
  12580.         convert(int,null),
  12581.         convert(int,15),
  12582.         'NO ACTION',
  12583.         'NO ACTION',
  12584.         object_name(s.constid),
  12585.         object_name(r.constid),
  12586.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12587.     from
  12588.         sysobjects o1, sysobjects o2,
  12589.         syscolumns c1, syscolumns c2,
  12590.         sysreferences r, sysconstraints s
  12591.     where    
  12592.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12593.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12594.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12595.     and    o1.id = r.rkeyid
  12596.     and s.id = o1.id and (s.status & 0xf) = 1
  12597.     and    r.keycnt >= 15
  12598.     and    o1.id = c1.id
  12599.     and    c1.colid = r.rkey15
  12600.     and r.fkeyid = o2.id
  12601.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12602.     and    o2.id = c2.id    
  12603.     and    c2.colid = r.fkey15
  12604.     union all
  12605.     select
  12606.         db_name(r.rkeydbid),
  12607.         user_name(o1.uid),
  12608.         o1.name,
  12609.         c1.name,
  12610.         convert(binary(16),null),
  12611.         convert(int,null),
  12612.         db_name(r.fkeydbid),
  12613.         user_name(o2.uid),
  12614.         o2.name,
  12615.         c2.name,
  12616.         convert(binary(16),null),
  12617.         convert(int,null),
  12618.         convert(int,16),
  12619.         'NO ACTION',
  12620.         'NO ACTION',
  12621.         object_name(s.constid),
  12622.         object_name(r.constid),
  12623.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12624.     from
  12625.         sysobjects o1, sysobjects o2,
  12626.         syscolumns c1, syscolumns c2,
  12627.         sysreferences r, sysconstraints s
  12628.     where    
  12629.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12630.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12631.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12632.     and    o1.id = r.rkeyid
  12633.     and s.id = o1.id and (s.status & 0xf) = 1
  12634.     and    o1.id = c1.id
  12635.     and    r.keycnt >= 16
  12636.     and    c1.colid = r.rkey16
  12637.     and r.fkeyid = o2.id
  12638.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12639.     and    o2.id = c2.id    
  12640.     and    c2.colid = r.fkey16
  12641.     order by 8,9,2,3,13
  12642.     END
  12643. go
  12644. dump tran master with no_log
  12645. go
  12646. CREATE PROCEDURE sp_foreign_keys_rowset;2
  12647.     (
  12648.        @handle            int output,
  12649.        @scrollopt        int output,
  12650.     @ccopt            int output,
  12651.     @rows            int output,
  12652.      @pk_table_name        varchar(255) = null,
  12653.     @pk_table_schema    varchar(255) = null,
  12654.     @pk_table_catalog    varchar(255) = null,
  12655.     @fk_table_name        varchar(255) = null,
  12656.     @fk_table_schema    varchar(255) = null,
  12657.     @fk_table_catalog    varchar(255) = null
  12658.     )
  12659. as
  12660.     declare @ret int
  12661.     
  12662.     SET NOCOUNT ON
  12663.  
  12664.     create table #spfkeysrowset1
  12665.         (
  12666.         PK_TABLE_CATALOG    sysname not null,
  12667.         PK_TABLE_SCHEMA        sysname not null,
  12668.         PK_TABLE_NAME         sysname not null,
  12669.         PK_COLUMN_NAME         sysname not null,
  12670.         PK_COLUMN_GUID        binary(16) null,
  12671.         PK_COLUMN_PROPID    int null,
  12672.         FK_TABLE_CATALOG    sysname not null,
  12673.         FK_TABLE_SCHEMA        sysname not null,
  12674.         FK_TABLE_NAME         sysname not null,
  12675.         FK_COLUMN_NAME         sysname not null,
  12676.         FK_COLUMN_GUID        binary(16) null,
  12677.         FK_COLUMN_PROPID    int null,
  12678.         ORDINAL                int not null,
  12679.         UPDATE_RULE            sysname not null,
  12680.         DELETE_RULE         sysname not null,
  12681.         PK_NAME                sysname not null,
  12682.         FK_NAME                sysname not null,
  12683.         DEFERRABILITY        smallint not null        
  12684.         )
  12685.  
  12686. IF @pk_table_name is not null
  12687.     BEGIN
  12688.     insert into #spfkeysrowset1
  12689.     select
  12690.         db_name(r.rkeydbid),
  12691.         user_name(o1.uid),
  12692.         o1.name,
  12693.         c1.name,
  12694.         convert(binary(16),null),
  12695.         convert(int,null),
  12696.         db_name(r.fkeydbid),
  12697.         user_name(o2.uid),
  12698.         o2.name,
  12699.         c2.name,
  12700.         convert(binary(16),null),
  12701.         convert(int,null),
  12702.         convert(int,1),
  12703.         'NO ACTION',
  12704.         'NO ACTION',
  12705.         object_name(s.constid),
  12706.         object_name(r.constid),
  12707.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12708.     from
  12709.         sysobjects o1, sysobjects o2,
  12710.         syscolumns c1, syscolumns c2,
  12711.         sysreferences r, sysconstraints s
  12712.     where    
  12713.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12714.     and    o1.name = @pk_table_name
  12715.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12716.     and    o1.id = r.rkeyid
  12717.     and s.id = o1.id and (s.status & 0xf) = 1
  12718.     and    o1.id = c1.id
  12719.     and    c1.colid = r.rkey1
  12720.     and r.fkeyid = o2.id
  12721.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12722.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12723.     and    o2.id = c2.id    
  12724.     and    c2.colid = r.fkey1
  12725.     union all
  12726.     select
  12727.         db_name(r.rkeydbid),
  12728.         user_name(o1.uid),
  12729.         o1.name,
  12730.         c1.name,
  12731.         convert(binary(16),null),
  12732.         convert(int,null),
  12733.         db_name(r.fkeydbid),
  12734.         user_name(o2.uid),
  12735.         o2.name,
  12736.         c2.name,
  12737.         convert(binary(16),null),
  12738.         convert(int,null),
  12739.         convert(int,2),
  12740.         'NO ACTION',
  12741.         'NO ACTION',
  12742.         object_name(s.constid),
  12743.         object_name(r.constid),
  12744.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12745.     from
  12746.         sysobjects o1, sysobjects o2,
  12747.         syscolumns c1, syscolumns c2,
  12748.         sysreferences r, sysconstraints s
  12749.     where    
  12750.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12751.     and    o1.name = @pk_table_name
  12752.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12753.     and    o1.id = r.rkeyid
  12754.     and s.id = o1.id and (s.status & 0xf) = 1
  12755.     and    o1.id = c1.id
  12756.     and    c1.colid = r.rkey2
  12757.     and    r.keycnt >= 2
  12758.     and r.fkeyid = o2.id
  12759.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12760.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12761.     and    o2.id = c2.id    
  12762.     and    c2.colid = r.fkey2
  12763.     union all
  12764.     select
  12765.         db_name(r.rkeydbid),
  12766.         user_name(o1.uid),
  12767.         o1.name,
  12768.         c1.name,
  12769.         convert(binary(16),null),
  12770.         convert(int,null),
  12771.         db_name(r.fkeydbid),
  12772.         user_name(o2.uid),
  12773.         o2.name,
  12774.         c2.name,
  12775.         convert(binary(16),null),
  12776.         convert(int,null),
  12777.         convert(int,3),
  12778.         'NO ACTION',
  12779.         'NO ACTION',
  12780.         object_name(s.constid),
  12781.         object_name(r.constid),
  12782.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12783.     from
  12784.         sysobjects o1, sysobjects o2,
  12785.         syscolumns c1, syscolumns c2,
  12786.         sysreferences r, sysconstraints s
  12787.     where    
  12788.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12789.     and    o1.name = @pk_table_name
  12790.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12791.     and    o1.id = r.rkeyid
  12792.     and s.id = o1.id and (s.status & 0xf) = 1
  12793.     and    o1.id = c1.id
  12794.     and    c1.colid = r.rkey3
  12795.     and    r.keycnt >= 3
  12796.     and r.fkeyid = o2.id
  12797.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12798.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12799.     and    o2.id = c2.id    
  12800.     and    c2.colid = r.fkey3
  12801.     union all
  12802.     select
  12803.         db_name(r.rkeydbid),
  12804.         user_name(o1.uid),
  12805.         o1.name,
  12806.         c1.name,
  12807.         convert(binary(16),null),
  12808.         convert(int,null),
  12809.         db_name(r.fkeydbid),
  12810.         user_name(o2.uid),
  12811.         o2.name,
  12812.         c2.name,
  12813.         convert(binary(16),null),
  12814.         convert(int,null),
  12815.         convert(int,4),
  12816.         'NO ACTION',
  12817.         'NO ACTION',
  12818.         object_name(s.constid),
  12819.         object_name(r.constid),
  12820.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12821.     from
  12822.         sysobjects o1, sysobjects o2,
  12823.         syscolumns c1, syscolumns c2,
  12824.         sysreferences r, sysconstraints s
  12825.     where    
  12826.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12827.     and    o1.name = @pk_table_name
  12828.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12829.     and    o1.id = r.rkeyid
  12830.     and s.id = o1.id and (s.status & 0xf) = 1
  12831.     and    r.keycnt >= 4
  12832.     and    o1.id = c1.id
  12833.     and    c1.colid = r.rkey4
  12834.     and r.fkeyid = o2.id
  12835.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12836.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12837.     and    o2.id = c2.id    
  12838.     and    c2.colid = r.fkey4
  12839.     union all
  12840.     select
  12841.         db_name(r.rkeydbid),
  12842.         user_name(o1.uid),
  12843.         o1.name,
  12844.         c1.name,
  12845.         convert(binary(16),null),
  12846.         convert(int,null),
  12847.         db_name(r.fkeydbid),
  12848.         user_name(o2.uid),
  12849.         o2.name,
  12850.         c2.name,
  12851.         convert(binary(16),null),
  12852.         convert(int,null),
  12853.         convert(int,5),
  12854.         'NO ACTION',
  12855.         'NO ACTION',
  12856.         object_name(s.constid),
  12857.         object_name(r.constid),
  12858.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12859.     from
  12860.         sysobjects o1, sysobjects o2,
  12861.         syscolumns c1, syscolumns c2,
  12862.         sysreferences r, sysconstraints s
  12863.     where    
  12864.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12865.     and    o1.name = @pk_table_name
  12866.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12867.     and    o1.id = r.rkeyid
  12868.     and s.id = o1.id and (s.status & 0xf) = 1
  12869.     and    r.keycnt >= 5
  12870.     and    o1.id = c1.id
  12871.     and    c1.colid = r.rkey5
  12872.     and r.fkeyid = o2.id
  12873.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12874.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12875.     and    o2.id = c2.id    
  12876.     and    c2.colid = r.fkey5
  12877.     union all
  12878.     select
  12879.         db_name(r.rkeydbid),
  12880.         user_name(o1.uid),
  12881.         o1.name,
  12882.         c1.name,
  12883.         convert(binary(16),null),
  12884.         convert(int,null),
  12885.         db_name(r.fkeydbid),
  12886.         user_name(o2.uid),
  12887.         o2.name,
  12888.         c2.name,
  12889.         convert(binary(16),null),
  12890.         convert(int,null),
  12891.         convert(int,6),
  12892.         'NO ACTION',
  12893.         'NO ACTION',
  12894.         object_name(s.constid),
  12895.         object_name(r.constid),
  12896.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12897.     from
  12898.         sysobjects o1, sysobjects o2,
  12899.         syscolumns c1, syscolumns c2,
  12900.         sysreferences r, sysconstraints s
  12901.     where    
  12902.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12903.     and    o1.name = @pk_table_name
  12904.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12905.     and    o1.id = r.rkeyid
  12906.     and s.id = o1.id and (s.status & 0xf) = 1
  12907.     and    r.keycnt >= 6
  12908.     and    o1.id = c1.id
  12909.     and    c1.colid = r.rkey6
  12910.     and r.fkeyid = o2.id
  12911.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12912.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12913.     and    o2.id = c2.id    
  12914.     and    c2.colid = r.fkey6
  12915.     union all
  12916.     select
  12917.         db_name(r.rkeydbid),
  12918.         user_name(o1.uid),
  12919.         o1.name,
  12920.         c1.name,
  12921.         convert(binary(16),null),
  12922.         convert(int,null),
  12923.         db_name(r.fkeydbid),
  12924.         user_name(o2.uid),
  12925.         o2.name,
  12926.         c2.name,
  12927.         convert(binary(16),null),
  12928.         convert(int,null),
  12929.         convert(int,7),
  12930.         'NO ACTION',
  12931.         'NO ACTION',
  12932.         object_name(s.constid),
  12933.         object_name(r.constid),
  12934.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12935.     from
  12936.         sysobjects o1, sysobjects o2,
  12937.         syscolumns c1, syscolumns c2,
  12938.         sysreferences r, sysconstraints s
  12939.     where    
  12940.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12941.     and    o1.name = @pk_table_name
  12942.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12943.     and    o1.id = r.rkeyid
  12944.     and s.id = o1.id and (s.status & 0xf) = 1
  12945.     and    r.keycnt >= 7
  12946.     and    o1.id = c1.id
  12947.     and    c1.colid = r.rkey7
  12948.     and r.fkeyid = o2.id
  12949.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12950.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12951.     and    o2.id = c2.id    
  12952.     and    c2.colid = r.fkey7
  12953.     union all
  12954.     select
  12955.         db_name(r.rkeydbid),
  12956.         user_name(o1.uid),
  12957.         o1.name,
  12958.         c1.name,
  12959.         convert(binary(16),null),
  12960.         convert(int,null),
  12961.         db_name(r.fkeydbid),
  12962.         user_name(o2.uid),
  12963.         o2.name,
  12964.         c2.name,
  12965.         convert(binary(16),null),
  12966.         convert(int,null),
  12967.         convert(int,8),
  12968.         'NO ACTION',
  12969.         'NO ACTION',
  12970.         object_name(s.constid),
  12971.         object_name(r.constid),
  12972.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12973.     from
  12974.         sysobjects o1, sysobjects o2,
  12975.         syscolumns c1, syscolumns c2,
  12976.         sysreferences r, sysconstraints s
  12977.     where    
  12978.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12979.     and    o1.name = @pk_table_name
  12980.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12981.     and    o1.id = r.rkeyid
  12982.     and s.id = o1.id and (s.status & 0xf) = 1
  12983.     and    r.keycnt >= 8
  12984.     and    o1.id = c1.id
  12985.     and    c1.colid = r.rkey8
  12986.     and r.fkeyid = o2.id
  12987.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12988.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12989.     and    o2.id = c2.id    
  12990.     and    c2.colid = r.fkey8
  12991.     union all
  12992.     select
  12993.         db_name(r.rkeydbid),
  12994.         user_name(o1.uid),
  12995.         o1.name,
  12996.         c1.name,
  12997.         convert(binary(16),null),
  12998.         convert(int,null),
  12999.         db_name(r.fkeydbid),
  13000.         user_name(o2.uid),
  13001.         o2.name,
  13002.         c2.name,
  13003.         convert(binary(16),null),
  13004.         convert(int,null),
  13005.         convert(int,9),
  13006.         'NO ACTION',
  13007.         'NO ACTION',
  13008.         object_name(s.constid),
  13009.         object_name(r.constid),
  13010.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13011.     from
  13012.         sysobjects o1, sysobjects o2,
  13013.         syscolumns c1, syscolumns c2,
  13014.         sysreferences r, sysconstraints s
  13015.     where    
  13016.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13017.     and    o1.name = @pk_table_name
  13018.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13019.     and    o1.id = r.rkeyid
  13020.     and s.id = o1.id and (s.status & 0xf) = 1
  13021.     and    r.keycnt >= 9
  13022.     and    o1.id = c1.id
  13023.     and    c1.colid = r.rkey9
  13024.     and r.fkeyid = o2.id
  13025.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13026.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13027.     and    o2.id = c2.id    
  13028.     and    c2.colid = r.fkey9
  13029.     union all
  13030.     select
  13031.         db_name(r.rkeydbid),
  13032.         user_name(o1.uid),
  13033.         o1.name,
  13034.         c1.name,
  13035.         convert(binary(16),null),
  13036.         convert(int,null),
  13037.         db_name(r.fkeydbid),
  13038.         user_name(o2.uid),
  13039.         o2.name,
  13040.         c2.name,
  13041.         convert(binary(16),null),
  13042.         convert(int,null),
  13043.         convert(int,10),
  13044.         'NO ACTION',
  13045.         'NO ACTION',
  13046.         object_name(s.constid),
  13047.         object_name(r.constid),
  13048.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13049.     from
  13050.         sysobjects o1, sysobjects o2,
  13051.         syscolumns c1, syscolumns c2,
  13052.         sysreferences r, sysconstraints s
  13053.     where    
  13054.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13055.     and    o1.name = @pk_table_name
  13056.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13057.     and    o1.id = r.rkeyid
  13058.     and s.id = o1.id and (s.status & 0xf) = 1
  13059.     and    r.keycnt >= 10
  13060.     and    o1.id = c1.id
  13061.     and    c1.colid = r.rkey10
  13062.     and r.fkeyid = o2.id
  13063.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13064.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13065.     and    o2.id = c2.id    
  13066.     and    c2.colid = r.fkey10
  13067.     union all
  13068.     select
  13069.         db_name(r.rkeydbid),
  13070.         user_name(o1.uid),
  13071.         o1.name,
  13072.         c1.name,
  13073.         convert(binary(16),null),
  13074.         convert(int,null),
  13075.         db_name(r.fkeydbid),
  13076.         user_name(o2.uid),
  13077.         o2.name,
  13078.         c2.name,
  13079.         convert(binary(16),null),
  13080.         convert(int,null),
  13081.         convert(int,11),
  13082.         'NO ACTION',
  13083.         'NO ACTION',
  13084.         object_name(s.constid),
  13085.         object_name(r.constid),
  13086.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13087.     from
  13088.         sysobjects o1, sysobjects o2,
  13089.         syscolumns c1, syscolumns c2,
  13090.         sysreferences r, sysconstraints s
  13091.     where    
  13092.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13093.     and    o1.name = @pk_table_name
  13094.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13095.     and    o1.id = r.rkeyid
  13096.     and s.id = o1.id and (s.status & 0xf) = 1
  13097.     and    r.keycnt >= 11
  13098.     and    o1.id = c1.id
  13099.     and    c1.colid = r.rkey11
  13100.     and r.fkeyid = o2.id
  13101.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13102.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13103.     and    o2.id = c2.id    
  13104.     and    c2.colid = r.fkey11
  13105.     union all
  13106.     select
  13107.         db_name(r.rkeydbid),
  13108.         user_name(o1.uid),
  13109.         o1.name,
  13110.         c1.name,
  13111.         convert(binary(16),null),
  13112.         convert(int,null),
  13113.         db_name(r.fkeydbid),
  13114.         user_name(o2.uid),
  13115.         o2.name,
  13116.         c2.name,
  13117.         convert(binary(16),null),
  13118.         convert(int,null),
  13119.         convert(int,12),
  13120.         'NO ACTION',
  13121.         'NO ACTION',
  13122.         object_name(s.constid),
  13123.         object_name(r.constid),
  13124.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13125.     from
  13126.         sysobjects o1, sysobjects o2,
  13127.         syscolumns c1, syscolumns c2,
  13128.         sysreferences r, sysconstraints s
  13129.     where    
  13130.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13131.     and    o1.name = @pk_table_name
  13132.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13133.     and    o1.id = r.rkeyid
  13134.     and s.id = o1.id and (s.status & 0xf) = 1
  13135.     and    r.keycnt >= 12
  13136.     and    o1.id = c1.id
  13137.     and    c1.colid = r.rkey12
  13138.     and r.fkeyid = o2.id
  13139.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13140.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13141.     and    o2.id = c2.id    
  13142.     and    c2.colid = r.fkey12
  13143.     union all
  13144.     select
  13145.         db_name(r.rkeydbid),
  13146.         user_name(o1.uid),
  13147.         o1.name,
  13148.         c1.name,
  13149.         convert(binary(16),null),
  13150.         convert(int,null),
  13151.         db_name(r.fkeydbid),
  13152.         user_name(o2.uid),
  13153.         o2.name,
  13154.         c2.name,
  13155.         convert(binary(16),null),
  13156.         convert(int,null),
  13157.         convert(int,13),
  13158.         'NO ACTION',
  13159.         'NO ACTION',
  13160.         object_name(s.constid),
  13161.         object_name(r.constid),
  13162.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13163.     from
  13164.         sysobjects o1, sysobjects o2,
  13165.         syscolumns c1, syscolumns c2,
  13166.         sysreferences r, sysconstraints s
  13167.     where    
  13168.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13169.     and    o1.name = @pk_table_name
  13170.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13171.     and    o1.id = r.rkeyid
  13172.     and s.id = o1.id and (s.status & 0xf) = 1
  13173.     and    r.keycnt >= 13
  13174.     and    o1.id = c1.id
  13175.     and    c1.colid = r.rkey13
  13176.     and r.fkeyid = o2.id
  13177.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13178.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13179.     and    o2.id = c2.id    
  13180.     and    c2.colid = r.fkey13
  13181.     union all
  13182.     select
  13183.         db_name(r.rkeydbid),
  13184.         user_name(o1.uid),
  13185.         o1.name,
  13186.         c1.name,
  13187.         convert(binary(16),null),
  13188.         convert(int,null),
  13189.         db_name(r.fkeydbid),
  13190.         user_name(o2.uid),
  13191.         o2.name,
  13192.         c2.name,
  13193.         convert(binary(16),null),
  13194.         convert(int,null),
  13195.         convert(int,14),
  13196.         'NO ACTION',
  13197.         'NO ACTION',
  13198.         object_name(s.constid),
  13199.         object_name(r.constid),
  13200.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13201.     from
  13202.         sysobjects o1, sysobjects o2,
  13203.         syscolumns c1, syscolumns c2,
  13204.         sysreferences r, sysconstraints s
  13205.     where    
  13206.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13207.     and    o1.name = @pk_table_name
  13208.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13209.     and    o1.id = r.rkeyid
  13210.     and s.id = o1.id and (s.status & 0xf) = 1
  13211.     and    r.keycnt >= 14
  13212.     and    o1.id = c1.id
  13213.     and    c1.colid = r.rkey14
  13214.     and r.fkeyid = o2.id
  13215.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13216.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13217.     and    o2.id = c2.id    
  13218.     and    c2.colid = r.fkey14
  13219.     union all
  13220.     select
  13221.         db_name(r.rkeydbid),
  13222.         user_name(o1.uid),
  13223.         o1.name,
  13224.         c1.name,
  13225.         convert(binary(16),null),
  13226.         convert(int,null),
  13227.         db_name(r.fkeydbid),
  13228.         user_name(o2.uid),
  13229.         o2.name,
  13230.         c2.name,
  13231.         convert(binary(16),null),
  13232.         convert(int,null),
  13233.         convert(int,15),
  13234.         'NO ACTION',
  13235.         'NO ACTION',
  13236.         object_name(s.constid),
  13237.         object_name(r.constid),
  13238.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13239.     from
  13240.         sysobjects o1, sysobjects o2,
  13241.         syscolumns c1, syscolumns c2,
  13242.         sysreferences r, sysconstraints s
  13243.     where    
  13244.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13245.     and    o1.name = @pk_table_name
  13246.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13247.     and    o1.id = r.rkeyid
  13248.     and s.id = o1.id and (s.status & 0xf) = 1
  13249.     and    r.keycnt >= 15
  13250.     and    o1.id = c1.id
  13251.     and    c1.colid = r.rkey15
  13252.     and r.fkeyid = o2.id
  13253.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13254.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13255.     and    o2.id = c2.id    
  13256.     and    c2.colid = r.fkey15
  13257.     union all
  13258.     select
  13259.         db_name(r.rkeydbid),
  13260.         user_name(o1.uid),
  13261.         o1.name,
  13262.         c1.name,
  13263.         convert(binary(16),null),
  13264.         convert(int,null),
  13265.         db_name(r.fkeydbid),
  13266.         user_name(o2.uid),
  13267.         o2.name,
  13268.         c2.name,
  13269.         convert(binary(16),null),
  13270.         convert(int,null),
  13271.         convert(int,16),
  13272.         'NO ACTION',
  13273.         'NO ACTION',
  13274.         object_name(s.constid),
  13275.         object_name(r.constid),
  13276.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13277.     from
  13278.         sysobjects o1, sysobjects o2,
  13279.         syscolumns c1, syscolumns c2,
  13280.         sysreferences r, sysconstraints s
  13281.     where    
  13282.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13283.     and    o1.name = @pk_table_name
  13284.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13285.     and    o1.id = r.rkeyid
  13286.     and s.id = o1.id and (s.status & 0xf) = 1
  13287.     and    r.keycnt >= 16
  13288.     and    o1.id = c1.id
  13289.     and    c1.colid = r.rkey16
  13290.     and r.fkeyid = o2.id
  13291.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13292.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13293.     and    o2.id = c2.id    
  13294.     and    c2.colid = r.fkey16
  13295.     order by 8,9,2,3,13
  13296.     END
  13297. ELSE IF @fk_table_name is not null
  13298.     BEGIN
  13299.     insert into #spfkeysrowset1
  13300.     select
  13301.         db_name(r.rkeydbid),
  13302.         user_name(o1.uid),
  13303.         o1.name,
  13304.         c1.name,
  13305.         convert(binary(16),null),
  13306.         convert(int,null),
  13307.         db_name(r.fkeydbid),
  13308.         user_name(o2.uid),
  13309.         o2.name,
  13310.         c2.name,
  13311.         convert(binary(16),null),
  13312.         convert(int,null),
  13313.         convert(int,1),
  13314.         'NO ACTION',
  13315.         'NO ACTION',
  13316.         object_name(s.constid),
  13317.         object_name(r.constid),
  13318.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13319.     from
  13320.         sysobjects o1, sysobjects o2,
  13321.         syscolumns c1, syscolumns c2,
  13322.         sysreferences r, sysconstraints s
  13323.     where    
  13324.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13325.     and    o2.name = @fk_table_name
  13326.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13327.     and    o2.id = r.fkeyid
  13328.     and    o2.id = c2.id
  13329.     and    c2.colid = r.fkey1
  13330.     and r.rkeyid = o1.id
  13331.     and s.id = o1.id and (s.status & 0xf) = 1
  13332.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13333.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13334.     and    o1.id = c1.id    
  13335.     and    c1.colid = r.rkey1
  13336.     union    all
  13337.     select
  13338.         db_name(r.rkeydbid),
  13339.         user_name(o1.uid),
  13340.         o1.name,
  13341.         c1.name,
  13342.         convert(binary(16),null),
  13343.         convert(int,null),
  13344.         db_name(r.fkeydbid),
  13345.         user_name(o2.uid),
  13346.         o2.name,
  13347.         c2.name,
  13348.         convert(binary(16),null),
  13349.         convert(int,null),
  13350.         convert(int,2),
  13351.         'NO ACTION',
  13352.         'NO ACTION',
  13353.         object_name(s.constid),
  13354.         object_name(r.constid),
  13355.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13356.     from
  13357.         sysobjects o1, sysobjects o2,
  13358.         syscolumns c1, syscolumns c2,
  13359.         sysreferences r, sysconstraints s
  13360.     where    
  13361.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13362.     and    o2.name = @fk_table_name
  13363.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13364.     and    o2.id = r.fkeyid
  13365.     and    r.keycnt >= 2
  13366.     and    o2.id = c2.id
  13367.     and    c2.colid = r.fkey2
  13368.     and r.rkeyid = o1.id
  13369.     and s.id = o1.id and (s.status & 0xf) = 1
  13370.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13371.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13372.     and    o1.id = c1.id    
  13373.     and    c1.colid = r.rkey2
  13374.     union    all
  13375.     select
  13376.         db_name(r.rkeydbid),
  13377.         user_name(o1.uid),
  13378.         o1.name,
  13379.         c1.name,
  13380.         convert(binary(16),null),
  13381.         convert(int,null),
  13382.         db_name(r.fkeydbid),
  13383.         user_name(o2.uid),
  13384.         o2.name,
  13385.         c2.name,
  13386.         convert(binary(16),null),
  13387.         convert(int,null),
  13388.         convert(int,3),
  13389.         'NO ACTION',
  13390.         'NO ACTION',
  13391.         object_name(s.constid),
  13392.         object_name(r.constid),
  13393.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13394.     from
  13395.         sysobjects o1, sysobjects o2,
  13396.         syscolumns c1, syscolumns c2,
  13397.         sysreferences r, sysconstraints s
  13398.     where    
  13399.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13400.     and    o2.name = @fk_table_name
  13401.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13402.     and    o2.id = r.fkeyid
  13403.     and    r.keycnt >= 3
  13404.     and    o2.id = c2.id
  13405.     and    c2.colid = r.fkey3
  13406.     and    r.rkeyid = o1.id
  13407.     and s.id = o1.id and (s.status & 0xf) = 1
  13408.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13409.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13410.     and    o1.id = c1.id    
  13411.     and    c1.colid = r.rkey3
  13412.     union    all
  13413.     select
  13414.         db_name(r.rkeydbid),
  13415.         user_name(o1.uid),
  13416.         o1.name,
  13417.         c1.name,
  13418.         convert(binary(16),null),
  13419.         convert(int,null),
  13420.         db_name(r.fkeydbid),
  13421.         user_name(o2.uid),
  13422.         o2.name,
  13423.         c2.name,
  13424.         convert(binary(16),null),
  13425.         convert(int,null),
  13426.         convert(int,4),
  13427.         'NO ACTION',
  13428.         'NO ACTION',
  13429.         object_name(s.constid),
  13430.         object_name(r.constid),
  13431.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13432.     from
  13433.         sysobjects o1, sysobjects o2,
  13434.         syscolumns c1, syscolumns c2,
  13435.         sysreferences r, sysconstraints s
  13436.     where    
  13437.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13438.     and    o2.name = @fk_table_name
  13439.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13440.     and    o2.id = r.fkeyid
  13441.     and    r.keycnt >= 4
  13442.     and    o2.id = c2.id
  13443.     and    c2.colid = r.fkey4
  13444.     and r.rkeyid = o1.id
  13445.     and s.id = o1.id and (s.status & 0xf) = 1
  13446.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13447.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13448.     and    o1.id = c1.id    
  13449.     and    c1.colid = r.rkey4
  13450.     union    all
  13451.     select
  13452.         db_name(r.rkeydbid),
  13453.         user_name(o1.uid),
  13454.         o1.name,
  13455.         c1.name,
  13456.         convert(binary(16),null),
  13457.         convert(int,null),
  13458.         db_name(r.fkeydbid),
  13459.         user_name(o2.uid),
  13460.         o2.name,
  13461.         c2.name,
  13462.         convert(binary(16),null),
  13463.         convert(int,null),
  13464.         convert(int,5),
  13465.         'NO ACTION',
  13466.         'NO ACTION',
  13467.         object_name(s.constid),
  13468.         object_name(r.constid),
  13469.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13470.     from
  13471.         sysobjects o1, sysobjects o2,
  13472.         syscolumns c1, syscolumns c2,
  13473.         sysreferences r, sysconstraints s
  13474.     where    
  13475.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13476.     and    o2.name = @fk_table_name
  13477.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13478.     and    o2.id = r.fkeyid
  13479.     and    r.keycnt >= 5
  13480.     and    o2.id = c2.id
  13481.     and    c2.colid = r.fkey5
  13482.     and r.rkeyid = o1.id
  13483.     and s.id = o1.id and (s.status & 0xf) = 1
  13484.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13485.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13486.     and    o1.id = c1.id    
  13487.     and    c1.colid = r.rkey5
  13488.     union    all
  13489.     select
  13490.         db_name(r.rkeydbid),
  13491.         user_name(o1.uid),
  13492.         o1.name,
  13493.         c1.name,
  13494.         convert(binary(16),null),
  13495.         convert(int,null),
  13496.         db_name(r.fkeydbid),
  13497.         user_name(o2.uid),
  13498.         o2.name,
  13499.         c2.name,
  13500.         convert(binary(16),null),
  13501.         convert(int,null),
  13502.         convert(int,6),
  13503.         'NO ACTION',
  13504.         'NO ACTION',
  13505.         object_name(s.constid),
  13506.         object_name(r.constid),
  13507.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13508.     from
  13509.         sysobjects o1, sysobjects o2,
  13510.         syscolumns c1, syscolumns c2,
  13511.         sysreferences r, sysconstraints s
  13512.     where    
  13513.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13514.     and    o2.name = @fk_table_name
  13515.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13516.     and    o2.id = r.fkeyid
  13517.     and    r.keycnt >= 6
  13518.     and    o2.id = c2.id
  13519.     and    c2.colid = r.fkey6
  13520.     and r.rkeyid = o1.id
  13521.     and s.id = o1.id and (s.status & 0xf) = 1
  13522.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13523.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13524.     and    o1.id = c1.id    
  13525.     and    c1.colid = r.rkey6
  13526.     union    all
  13527.     select
  13528.         db_name(r.rkeydbid),
  13529.         user_name(o1.uid),
  13530.         o1.name,
  13531.         c1.name,
  13532.         convert(binary(16),null),
  13533.         convert(int,null),
  13534.         db_name(r.fkeydbid),
  13535.         user_name(o2.uid),
  13536.         o2.name,
  13537.         c2.name,
  13538.         convert(binary(16),null),
  13539.         convert(int,null),
  13540.         convert(int,7),
  13541.         'NO ACTION',
  13542.         'NO ACTION',
  13543.         object_name(s.constid),
  13544.         object_name(r.constid),
  13545.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13546.     from
  13547.         sysobjects o1, sysobjects o2,
  13548.         syscolumns c1, syscolumns c2,
  13549.         sysreferences r, sysconstraints s
  13550.     where    
  13551.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13552.     and    o2.name = @fk_table_name
  13553.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13554.     and    o2.id = r.fkeyid
  13555.     and    r.keycnt >= 7
  13556.     and    o2.id = c2.id
  13557.     and    c2.colid = r.fkey7
  13558.     and r.rkeyid = o1.id
  13559.     and s.id = o1.id and (s.status & 0xf) = 1
  13560.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13561.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13562.     and    o1.id = c1.id    
  13563.     and    c1.colid = r.rkey7
  13564.     union    all
  13565.     select
  13566.         db_name(r.rkeydbid),
  13567.         user_name(o1.uid),
  13568.         o1.name,
  13569.         c1.name,
  13570.         convert(binary(16),null),
  13571.         convert(int,null),
  13572.         db_name(r.fkeydbid),
  13573.         user_name(o2.uid),
  13574.         o2.name,
  13575.         c2.name,
  13576.         convert(binary(16),null),
  13577.         convert(int,null),
  13578.         convert(int,8),
  13579.         'NO ACTION',
  13580.         'NO ACTION',
  13581.         object_name(s.constid),
  13582.         object_name(r.constid),
  13583.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13584.     from
  13585.         sysobjects o1, sysobjects o2,
  13586.         syscolumns c1, syscolumns c2,
  13587.         sysreferences r, sysconstraints s
  13588.     where    
  13589.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13590.     and    o2.name = @fk_table_name
  13591.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13592.     and    o2.id = r.fkeyid
  13593.     and    r.keycnt >= 8
  13594.     and    o2.id = c2.id
  13595.     and    c2.colid = r.fkey8
  13596.     and r.rkeyid = o1.id
  13597.     and s.id = o1.id and (s.status & 0xf) = 1
  13598.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13599.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13600.     and    o1.id = c1.id    
  13601.     and    c1.colid = r.rkey8
  13602.     union    all
  13603.     select
  13604.         db_name(r.rkeydbid),
  13605.         user_name(o1.uid),
  13606.         o1.name,
  13607.         c1.name,
  13608.         convert(binary(16),null),
  13609.         convert(int,null),
  13610.         db_name(r.fkeydbid),
  13611.         user_name(o2.uid),
  13612.         o2.name,
  13613.         c2.name,
  13614.         convert(binary(16),null),
  13615.         convert(int,null),
  13616.         convert(int,9),
  13617.         'NO ACTION',
  13618.         'NO ACTION',
  13619.         object_name(s.constid),
  13620.         object_name(r.constid),
  13621.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13622.     from
  13623.         sysobjects o1, sysobjects o2,
  13624.         syscolumns c1, syscolumns c2,
  13625.         sysreferences r, sysconstraints s
  13626.     where    
  13627.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13628.     and    o2.name = @fk_table_name
  13629.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13630.     and    o2.id = r.fkeyid
  13631.     and    r.keycnt >= 9
  13632.     and    o2.id = c2.id
  13633.     and    c2.colid = r.fkey9
  13634.     and r.rkeyid = o1.id
  13635.     and s.id = o1.id and (s.status & 0xf) = 1
  13636.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13637.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13638.     and    o1.id = c1.id    
  13639.     and    c1.colid = r.rkey9
  13640.     union    all
  13641.     select
  13642.         db_name(r.rkeydbid),
  13643.         user_name(o1.uid),
  13644.         o1.name,
  13645.         c1.name,
  13646.         convert(binary(16),null),
  13647.         convert(int,null),
  13648.         db_name(r.fkeydbid),
  13649.         user_name(o2.uid),
  13650.         o2.name,
  13651.         c2.name,
  13652.         convert(binary(16),null),
  13653.         convert(int,null),
  13654.         convert(int,10),
  13655.         'NO ACTION',
  13656.         'NO ACTION',
  13657.         object_name(s.constid),
  13658.         object_name(r.constid),
  13659.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13660.     from
  13661.         sysobjects o1, sysobjects o2,
  13662.         syscolumns c1, syscolumns c2,
  13663.         sysreferences r, sysconstraints s
  13664.     where    
  13665.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13666.     and    o2.name = @fk_table_name
  13667.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13668.     and    o2.id = r.fkeyid
  13669.     and    r.keycnt >= 10
  13670.     and    o2.id = c2.id
  13671.     and    c2.colid = r.fkey10
  13672.     and r.rkeyid = o1.id
  13673.     and s.id = o1.id and (s.status & 0xf) = 1
  13674.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13675.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13676.     and    o1.id = c1.id    
  13677.     and    c1.colid = r.rkey10
  13678.     union    all
  13679.     select
  13680.         db_name(r.rkeydbid),
  13681.         user_name(o1.uid),
  13682.         o1.name,
  13683.         c1.name,
  13684.         convert(binary(16),null),
  13685.         convert(int,null),
  13686.         db_name(r.fkeydbid),
  13687.         user_name(o2.uid),
  13688.         o2.name,
  13689.         c2.name,
  13690.         convert(binary(16),null),
  13691.         convert(int,null),
  13692.         convert(int,11),
  13693.         'NO ACTION',
  13694.         'NO ACTION',
  13695.         object_name(s.constid),
  13696.         object_name(r.constid),
  13697.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13698.     from
  13699.         sysobjects o1, sysobjects o2,
  13700.         syscolumns c1, syscolumns c2,
  13701.         sysreferences r, sysconstraints s
  13702.     where    
  13703.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13704.     and    o2.name = @fk_table_name
  13705.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13706.     and    o2.id = r.fkeyid
  13707.     and    r.keycnt >= 11
  13708.     and    o2.id = c2.id
  13709.     and    c2.colid = r.fkey11
  13710.     and r.rkeyid = o1.id
  13711.     and s.id = o1.id and (s.status & 0xf) = 1
  13712.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13713.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13714.     and    o1.id = c1.id    
  13715.     and    c1.colid = r.rkey11
  13716.     union    all
  13717.     select
  13718.         db_name(r.rkeydbid),
  13719.         user_name(o1.uid),
  13720.         o1.name,
  13721.         c1.name,
  13722.         convert(binary(16),null),
  13723.         convert(int,null),
  13724.         db_name(r.fkeydbid),
  13725.         user_name(o2.uid),
  13726.         o2.name,
  13727.         c2.name,
  13728.         convert(binary(16),null),
  13729.         convert(int,null),
  13730.         convert(int,12),
  13731.         'NO ACTION',
  13732.         'NO ACTION',
  13733.         object_name(s.constid),
  13734.         object_name(r.constid),
  13735.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  13736.     from
  13737.         sysobjects o1, sysobjects o2,
  13738.         syscolumns c1, syscolumns c2,
  13739.         sysreferences r, sysconstraints s
  13740.     where    
  13741.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13742.     and    o2.name = @fk_table_name
  13743.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13744.     and    o2.id = r.fkeyid
  13745.     and    r.keycnt >= 12
  13746.     and    o2.id = c2.id
  13747.     and    c2.colid = r.fkey12
  13748.     and r.rkeyid = o1.id
  13749.     and s.id = o1.id and (s.status & 0xf) = 1
  13750.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13751.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13752.     and    o1.id = c1.id    
  13753.     and    c1.colid = r.rkey12
  13754.     union    all
  13755.     select
  13756.         db_name(r.rkeydbid),
  13757.         user_name(o1.uid),
  13758.         o1.name,
  13759.         c1.name,
  13760.         convert(binary(16),null),
  13761.         convert(int,null),
  13762.         db_name(r.fkeydbid),
  13763.         user_name(o2.uid),
  13764.         o2.name,
  13765.         c2.name,
  13766.         convert(binary(16),null),
  13767.         convert(int,null),
  13768.         convert(int,13),
  13769.         'NO ACTION',
  13770.         'NO ACTION',
  13771.         object_name(s.constid),
  13772.         object_name(r.constid),
  13773.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13774.     from
  13775.         sysobjects o1, sysobjects o2,
  13776.         syscolumns c1, syscolumns c2,
  13777.         sysreferences r, sysconstraints s
  13778.     where    
  13779.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13780.     and    o2.name = @fk_table_name
  13781.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13782.     and    o2.id = r.fkeyid
  13783.     and    r.keycnt >= 13
  13784.     and    o2.id = c2.id
  13785.     and    c2.colid = r.fkey13
  13786.     and r.rkeyid = o1.id
  13787.     and s.id = o1.id and (s.status & 0xf) = 1
  13788.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13789.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13790.     and    o1.id = c1.id    
  13791.     and    c1.colid = r.rkey13
  13792.     union    all
  13793.     select
  13794.         db_name(r.rkeydbid),
  13795.         user_name(o1.uid),
  13796.         o1.name,
  13797.         c1.name,
  13798.         convert(binary(16),null),
  13799.         convert(int,null),
  13800.         db_name(r.fkeydbid),
  13801.         user_name(o2.uid),
  13802.         o2.name,
  13803.         c2.name,
  13804.         convert(binary(16),null),
  13805.         convert(int,null),
  13806.         convert(int,14),
  13807.         'NO ACTION',
  13808.         'NO ACTION',
  13809.         object_name(s.constid),
  13810.         object_name(r.constid),
  13811.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13812.     from
  13813.         sysobjects o1, sysobjects o2,
  13814.         syscolumns c1, syscolumns c2,
  13815.         sysreferences r, sysconstraints s
  13816.     where    
  13817.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13818.     and    o2.name = @fk_table_name
  13819.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13820.     and    o2.id = r.fkeyid
  13821.     and    r.keycnt >= 14
  13822.     and    o2.id = c2.id
  13823.     and    c2.colid = r.fkey14
  13824.     and r.rkeyid = o1.id
  13825.     and s.id = o1.id and (s.status & 0xf) = 1
  13826.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13827.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13828.     and    o1.id = c1.id    
  13829.     and    c1.colid = r.rkey14
  13830.     union    all
  13831.     select
  13832.         db_name(r.rkeydbid),
  13833.         user_name(o1.uid),
  13834.         o1.name,
  13835.         c1.name,
  13836.         convert(binary(16),null),
  13837.         convert(int,null),
  13838.         db_name(r.fkeydbid),
  13839.         user_name(o2.uid),
  13840.         o2.name,
  13841.         c2.name,
  13842.         convert(binary(16),null),
  13843.         convert(int,null),
  13844.         convert(int,15),
  13845.         'NO ACTION',
  13846.         'NO ACTION',
  13847.         object_name(s.constid),
  13848.         object_name(r.constid),
  13849.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13850.     from
  13851.         sysobjects o1, sysobjects o2,
  13852.         syscolumns c1, syscolumns c2,
  13853.         sysreferences r, sysconstraints s
  13854.     where    
  13855.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13856.     and    o2.name = @fk_table_name
  13857.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13858.     and    o2.id = r.fkeyid
  13859.     and    r.keycnt >= 15
  13860.     and    o2.id = c2.id
  13861.     and    c2.colid = r.fkey15
  13862.     and r.rkeyid = o1.id
  13863.     and s.id = o1.id and (s.status & 0xf) = 1
  13864.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13865.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13866.     and    o1.id = c1.id    
  13867.     and    c1.colid = r.rkey15
  13868.     union    all
  13869.     select
  13870.         db_name(r.rkeydbid),
  13871.         user_name(o1.uid),
  13872.         o1.name,
  13873.         c1.name,
  13874.         convert(binary(16),null),
  13875.         convert(int,null),
  13876.         db_name(r.fkeydbid),
  13877.         user_name(o2.uid),
  13878.         o2.name,
  13879.         c2.name,
  13880.         convert(binary(16),null),
  13881.         convert(int,null),
  13882.         convert(int,16),
  13883.         'NO ACTION',
  13884.         'NO ACTION',
  13885.         object_name(s.constid),
  13886.         object_name(r.constid),
  13887.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13888.     from
  13889.         sysobjects o1, sysobjects o2,
  13890.         syscolumns c1, syscolumns c2,
  13891.         sysreferences r, sysconstraints s
  13892.     where    
  13893.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13894.     and    o2.name = @fk_table_name
  13895.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13896.     and    o2.id = r.fkeyid
  13897.     and    r.keycnt >= 16
  13898.     and    o2.id = c2.id
  13899.     and    c2.colid = r.fkey16
  13900.     and r.rkeyid = o1.id
  13901.     and s.id = o1.id and (s.status & 0xf) = 1
  13902.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13903.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13904.     and    o1.id = c1.id    
  13905.     and    c1.colid = r.rkey16
  13906.     order by 8,9,2,3,13
  13907.     END
  13908. ELSE
  13909.     BEGIN
  13910.     insert into #spfkeysrowset1
  13911.     select
  13912.         db_name(r.rkeydbid),
  13913.         user_name(o1.uid),
  13914.         o1.name,
  13915.         c1.name,
  13916.         convert(binary(16),null),
  13917.         convert(int,null),
  13918.         db_name(r.fkeydbid),
  13919.         user_name(o2.uid),
  13920.         o2.name,
  13921.         c2.name,
  13922.         convert(binary(16),null),
  13923.         convert(int,null),
  13924.         convert(int,1),
  13925.         'NO ACTION',
  13926.         'NO ACTION',
  13927.         object_name(s.constid),
  13928.         object_name(r.constid),
  13929.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13930.     from
  13931.         sysobjects o1, sysobjects o2,
  13932.         syscolumns c1, syscolumns c2,
  13933.         sysreferences r, sysconstraints s
  13934.     where    
  13935.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13936.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13937.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13938.     and    o1.id = r.rkeyid
  13939.     and s.id = o1.id and (s.status & 0xf) = 1
  13940.     and    o1.id = c1.id
  13941.     and    c1.colid = r.rkey1
  13942.     and r.fkeyid = o2.id
  13943.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13944.     and    o2.id = c2.id    
  13945.     and    c2.colid = r.fkey1
  13946.     union all
  13947.     select
  13948.         db_name(r.rkeydbid),
  13949.         user_name(o1.uid),
  13950.         o1.name,
  13951.         c1.name,
  13952.         convert(binary(16),null),
  13953.         convert(int,null),
  13954.         db_name(r.fkeydbid),
  13955.         user_name(o2.uid),
  13956.         o2.name,
  13957.         c2.name,
  13958.         convert(binary(16),null),
  13959.         convert(int,null),
  13960.         convert(int,2),
  13961.         'NO ACTION',
  13962.         'NO ACTION',
  13963.         object_name(s.constid),
  13964.         object_name(r.constid),
  13965.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13966.     from
  13967.         sysobjects o1, sysobjects o2,
  13968.         syscolumns c1, syscolumns c2,
  13969.         sysreferences r, sysconstraints s
  13970.     where    
  13971.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13972.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13973.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13974.     and    o1.id = r.rkeyid
  13975.     and s.id = o1.id and (s.status & 0xf) = 1
  13976.     and    r.keycnt >= 2
  13977.     and    o1.id = c1.id
  13978.     and    c1.colid = r.rkey2
  13979.     and r.fkeyid = o2.id
  13980.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13981.     and    o2.id = c2.id    
  13982.     and    c2.colid = r.fkey2
  13983.     union all
  13984.     select
  13985.         db_name(r.rkeydbid),
  13986.         user_name(o1.uid),
  13987.         o1.name,
  13988.         c1.name,
  13989.         convert(binary(16),null),
  13990.         convert(int,null),
  13991.         db_name(r.fkeydbid),
  13992.         user_name(o2.uid),
  13993.         o2.name,
  13994.         c2.name,
  13995.         convert(binary(16),null),
  13996.         convert(int,null),
  13997.         convert(int,3),
  13998.         'NO ACTION',
  13999.         'NO ACTION',
  14000.         object_name(s.constid),
  14001.         object_name(r.constid),
  14002.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14003.     from
  14004.         sysobjects o1, sysobjects o2,
  14005.         syscolumns c1, syscolumns c2,
  14006.         sysreferences r, sysconstraints s
  14007.     where    
  14008.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14009.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14010.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14011.     and    o1.id = r.rkeyid
  14012.     and s.id = o1.id and (s.status & 0xf) = 1
  14013.     and    r.keycnt >= 3
  14014.     and    o1.id = c1.id
  14015.     and    c1.colid = r.rkey3
  14016.     and r.fkeyid = o2.id
  14017.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14018.     and    o2.id = c2.id    
  14019.     and    c2.colid = r.fkey3
  14020.     union all
  14021.     select
  14022.         db_name(r.rkeydbid),
  14023.         user_name(o1.uid),
  14024.         o1.name,
  14025.         c1.name,
  14026.         convert(binary(16),null),
  14027.         convert(int,null),
  14028.         db_name(r.fkeydbid),
  14029.         user_name(o2.uid),
  14030.         o2.name,
  14031.         c2.name,
  14032.         convert(binary(16),null),
  14033.         convert(int,null),
  14034.         convert(int,4),
  14035.         'NO ACTION',
  14036.         'NO ACTION',
  14037.         object_name(s.constid),
  14038.         object_name(r.constid),
  14039.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14040.     from
  14041.         sysobjects o1, sysobjects o2,
  14042.         syscolumns c1, syscolumns c2,
  14043.         sysreferences r, sysconstraints s
  14044.     where    
  14045.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14046.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14047.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14048.     and    o1.id = r.rkeyid
  14049.     and s.id = o1.id and (s.status & 0xf) = 1
  14050.     and    r.keycnt >= 4
  14051.     and    o1.id = c1.id
  14052.     and    c1.colid = r.rkey4
  14053.     and r.fkeyid = o2.id
  14054.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14055.     and    o2.id = c2.id    
  14056.     and    c2.colid = r.fkey4
  14057.     union all
  14058.     select
  14059.         db_name(r.rkeydbid),
  14060.         user_name(o1.uid),
  14061.         o1.name,
  14062.         c1.name,
  14063.         convert(binary(16),null),
  14064.         convert(int,null),
  14065.         db_name(r.fkeydbid),
  14066.         user_name(o2.uid),
  14067.         o2.name,
  14068.         c2.name,
  14069.         convert(binary(16),null),
  14070.         convert(int,null),
  14071.         convert(int,5),
  14072.         'NO ACTION',
  14073.         'NO ACTION',
  14074.         object_name(s.constid),
  14075.         object_name(r.constid),
  14076.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14077.     from
  14078.         sysobjects o1, sysobjects o2,
  14079.         syscolumns c1, syscolumns c2,
  14080.         sysreferences r, sysconstraints s
  14081.     where    
  14082.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14083.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14084.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14085.     and    o1.id = r.rkeyid
  14086.     and s.id = o1.id and (s.status & 0xf) = 1
  14087.     and    r.keycnt >= 5
  14088.     and    o1.id = c1.id
  14089.     and    c1.colid = r.rkey5
  14090.     and r.fkeyid = o2.id
  14091.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14092.     and    o2.id = c2.id    
  14093.     and    c2.colid = r.fkey5
  14094.     union all
  14095.     select
  14096.         db_name(r.rkeydbid),
  14097.         user_name(o1.uid),
  14098.         o1.name,
  14099.         c1.name,
  14100.         convert(binary(16),null),
  14101.         convert(int,null),
  14102.         db_name(r.fkeydbid),
  14103.         user_name(o2.uid),
  14104.         o2.name,
  14105.         c2.name,
  14106.         convert(binary(16),null),
  14107.         convert(int,null),
  14108.         convert(int,6),
  14109.         'NO ACTION',
  14110.         'NO ACTION',
  14111.         object_name(s.constid),
  14112.         object_name(r.constid),
  14113.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14114.     from
  14115.         sysobjects o1, sysobjects o2,
  14116.         syscolumns c1, syscolumns c2,
  14117.         sysreferences r, sysconstraints s
  14118.     where    
  14119.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14120.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14121.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14122.     and    o1.id = r.rkeyid
  14123.     and s.id = o1.id and (s.status & 0xf) = 1
  14124.     and    r.keycnt >= 6
  14125.     and    o1.id = c1.id
  14126.     and    c1.colid = r.rkey6
  14127.     and r.fkeyid = o2.id
  14128.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14129.     and    o2.id = c2.id    
  14130.     and    c2.colid = r.fkey6
  14131.     union all
  14132.     select
  14133.         db_name(r.rkeydbid),
  14134.         user_name(o1.uid),
  14135.         o1.name,
  14136.         c1.name,
  14137.         convert(binary(16),null),
  14138.         convert(int,null),
  14139.         db_name(r.fkeydbid),
  14140.         user_name(o2.uid),
  14141.         o2.name,
  14142.         c2.name,
  14143.         convert(binary(16),null),
  14144.         convert(int,null),
  14145.         convert(int,7),
  14146.         'NO ACTION',
  14147.         'NO ACTION',
  14148.         object_name(s.constid),
  14149.         object_name(r.constid),
  14150.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14151.     from
  14152.         sysobjects o1, sysobjects o2,
  14153.         syscolumns c1, syscolumns c2,
  14154.         sysreferences r, sysconstraints s
  14155.     where    
  14156.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14157.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14158.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14159.     and    o1.id = r.rkeyid
  14160.     and s.id = o1.id and (s.status & 0xf) = 1
  14161.     and    r.keycnt >= 7
  14162.     and    o1.id = c1.id
  14163.     and    c1.colid = r.rkey7
  14164.     and r.fkeyid = o2.id
  14165.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14166.     and    o2.id = c2.id    
  14167.     and    c2.colid = r.fkey7
  14168.     union all
  14169.     select
  14170.         db_name(r.rkeydbid),
  14171.         user_name(o1.uid),
  14172.         o1.name,
  14173.         c1.name,
  14174.         convert(binary(16),null),
  14175.         convert(int,null),
  14176.         db_name(r.fkeydbid),
  14177.         user_name(o2.uid),
  14178.         o2.name,
  14179.         c2.name,
  14180.         convert(binary(16),null),
  14181.         convert(int,null),
  14182.         convert(int,8),
  14183.         'NO ACTION',
  14184.         'NO ACTION',
  14185.         object_name(s.constid),
  14186.         object_name(r.constid),
  14187.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14188.     from
  14189.         sysobjects o1, sysobjects o2,
  14190.         syscolumns c1, syscolumns c2,
  14191.         sysreferences r, sysconstraints s
  14192.     where    
  14193.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14194.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14195.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14196.     and    o1.id = r.rkeyid
  14197.     and s.id = o1.id and (s.status & 0xf) = 1
  14198.     and    r.keycnt >= 8
  14199.     and    o1.id = c1.id
  14200.     and    c1.colid = r.rkey8
  14201.     and r.fkeyid = o2.id
  14202.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14203.     and    o2.id = c2.id    
  14204.     and    c2.colid = r.fkey8
  14205.     union all
  14206.     select
  14207.         db_name(r.rkeydbid),
  14208.         user_name(o1.uid),
  14209.         o1.name,
  14210.         c1.name,
  14211.         convert(binary(16),null),
  14212.         convert(int,null),
  14213.         db_name(r.fkeydbid),
  14214.         user_name(o2.uid),
  14215.         o2.name,
  14216.         c2.name,
  14217.         convert(binary(16),null),
  14218.         convert(int,null),
  14219.         convert(int,9),
  14220.         'NO ACTION',
  14221.         'NO ACTION',
  14222.         object_name(s.constid),
  14223.         object_name(r.constid),
  14224.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14225.     from
  14226.         sysobjects o1, sysobjects o2,
  14227.         syscolumns c1, syscolumns c2,
  14228.         sysreferences r, sysconstraints s
  14229.     where    
  14230.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14231.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14232.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14233.     and    o1.id = r.rkeyid
  14234.     and s.id = o1.id and (s.status & 0xf) = 1
  14235.     and    r.keycnt >= 9
  14236.     and    o1.id = c1.id
  14237.     and    c1.colid = r.rkey9
  14238.     and r.fkeyid = o2.id
  14239.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14240.     and    o2.id = c2.id    
  14241.     and    c2.colid = r.fkey9
  14242.     union all
  14243.     select
  14244.         db_name(r.rkeydbid),
  14245.         user_name(o1.uid),
  14246.         o1.name,
  14247.         c1.name,
  14248.         convert(binary(16),null),
  14249.         convert(int,null),
  14250.         db_name(r.fkeydbid),
  14251.         user_name(o2.uid),
  14252.         o2.name,
  14253.         c2.name,
  14254.         convert(binary(16),null),
  14255.         convert(int,null),
  14256.         convert(int,10),
  14257.         'NO ACTION',
  14258.         'NO ACTION',
  14259.         object_name(s.constid),
  14260.         object_name(r.constid),
  14261.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14262.     from
  14263.         sysobjects o1, sysobjects o2,
  14264.         syscolumns c1, syscolumns c2,
  14265.         sysreferences r, sysconstraints s
  14266.     where    
  14267.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14268.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14269.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14270.     and    o1.id = r.rkeyid
  14271.     and s.id = o1.id and (s.status & 0xf) = 1
  14272.     and    r.keycnt >= 10
  14273.     and    o1.id = c1.id
  14274.     and    c1.colid = r.rkey10
  14275.     and r.fkeyid = o2.id
  14276.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14277.     and    o2.id = c2.id    
  14278.     and    c2.colid = r.fkey10
  14279.     union all
  14280.     select
  14281.         db_name(r.rkeydbid),
  14282.         user_name(o1.uid),
  14283.         o1.name,
  14284.         c1.name,
  14285.         convert(binary(16),null),
  14286.         convert(int,null),
  14287.         db_name(r.fkeydbid),
  14288.         user_name(o2.uid),
  14289.         o2.name,
  14290.         c2.name,
  14291.         convert(binary(16),null),
  14292.         convert(int,null),
  14293.         convert(int,11),
  14294.         'NO ACTION',
  14295.         'NO ACTION',
  14296.         object_name(s.constid),
  14297.         object_name(r.constid),
  14298.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14299.     from
  14300.         sysobjects o1, sysobjects o2,
  14301.         syscolumns c1, syscolumns c2,
  14302.         sysreferences r, sysconstraints s
  14303.     where    
  14304.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14305.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14306.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14307.     and    o1.id = r.rkeyid
  14308.     and s.id = o1.id and (s.status & 0xf) = 1
  14309.     and    r.keycnt >= 11
  14310.     and    o1.id = c1.id
  14311.     and    c1.colid = r.rkey11
  14312.     and r.fkeyid = o2.id
  14313.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14314.     and    o2.id = c2.id    
  14315.     and    c2.colid = r.fkey11
  14316.     union all
  14317.     select
  14318.         db_name(r.rkeydbid),
  14319.         user_name(o1.uid),
  14320.         o1.name,
  14321.         c1.name,
  14322.         convert(binary(16),null),
  14323.         convert(int,null),
  14324.         db_name(r.fkeydbid),
  14325.         user_name(o2.uid),
  14326.         o2.name,
  14327.         c2.name,
  14328.         convert(binary(16),null),
  14329.         convert(int,null),
  14330.         convert(int,12),
  14331.         'NO ACTION',
  14332.         'NO ACTION',
  14333.         object_name(s.constid),
  14334.         object_name(r.constid),
  14335.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14336.     from
  14337.         sysobjects o1, sysobjects o2,
  14338.         syscolumns c1, syscolumns c2,
  14339.         sysreferences r, sysconstraints s
  14340.     where    
  14341.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14342.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14343.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14344.     and    o1.id = r.rkeyid
  14345.     and s.id = o1.id and (s.status & 0xf) = 1
  14346.     and    r.keycnt >= 12
  14347.     and    o1.id = c1.id
  14348.     and    c1.colid = r.rkey12
  14349.     and r.fkeyid = o2.id
  14350.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14351.     and    o2.id = c2.id    
  14352.     and    c2.colid = r.fkey12
  14353.     union all
  14354.     select
  14355.         db_name(r.rkeydbid),
  14356.         user_name(o1.uid),
  14357.         o1.name,
  14358.         c1.name,
  14359.         convert(binary(16),null),
  14360.         convert(int,null),
  14361.         db_name(r.fkeydbid),
  14362.         user_name(o2.uid),
  14363.         o2.name,
  14364.         c2.name,
  14365.         convert(binary(16),null),
  14366.         convert(int,null),
  14367.         convert(int,13),
  14368.         'NO ACTION',
  14369.         'NO ACTION',
  14370.         object_name(s.constid),
  14371.         object_name(r.constid),
  14372.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14373.     from
  14374.         sysobjects o1, sysobjects o2,
  14375.         syscolumns c1, syscolumns c2,
  14376.         sysreferences r, sysconstraints s
  14377.     where    
  14378.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14379.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14380.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14381.     and    o1.id = r.rkeyid
  14382.     and s.id = o1.id and (s.status & 0xf) = 1
  14383.     and    r.keycnt >= 13
  14384.     and    o1.id = c1.id
  14385.     and    c1.colid = r.rkey13
  14386.     and r.fkeyid = o2.id
  14387.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14388.     and    o2.id = c2.id    
  14389.     and    c2.colid = r.fkey13
  14390.     union all
  14391.     select
  14392.         db_name(r.rkeydbid),
  14393.         user_name(o1.uid),
  14394.         o1.name,
  14395.         c1.name,
  14396.         convert(binary(16),null),
  14397.         convert(int,null),
  14398.         db_name(r.fkeydbid),
  14399.         user_name(o2.uid),
  14400.         o2.name,
  14401.         c2.name,
  14402.         convert(binary(16),null),
  14403.         convert(int,null),
  14404.         convert(int,14),
  14405.         'NO ACTION',
  14406.         'NO ACTION',
  14407.         object_name(s.constid),
  14408.         object_name(r.constid),
  14409.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14410.     from
  14411.         sysobjects o1, sysobjects o2,
  14412.         syscolumns c1, syscolumns c2,
  14413.         sysreferences r, sysconstraints s
  14414.     where    
  14415.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14416.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14417.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14418.     and    o1.id = r.rkeyid
  14419.     and s.id = o1.id and (s.status & 0xf) = 1
  14420.     and    r.keycnt >= 14
  14421.     and    o1.id = c1.id
  14422.     and    c1.colid = r.rkey14
  14423.     and r.fkeyid = o2.id
  14424.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14425.     and    o2.id = c2.id    
  14426.     and    c2.colid = r.fkey14
  14427.     union all
  14428.     select
  14429.         db_name(r.rkeydbid),
  14430.         user_name(o1.uid),
  14431.         o1.name,
  14432.         c1.name,
  14433.         convert(binary(16),null),
  14434.         convert(int,null),
  14435.         db_name(r.fkeydbid),
  14436.         user_name(o2.uid),
  14437.         o2.name,
  14438.         c2.name,
  14439.         convert(binary(16),null),
  14440.         convert(int,null),
  14441.         convert(int,15),
  14442.         'NO ACTION',
  14443.         'NO ACTION',
  14444.         object_name(s.constid),
  14445.         object_name(r.constid),
  14446.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14447.     from
  14448.         sysobjects o1, sysobjects o2,
  14449.         syscolumns c1, syscolumns c2,
  14450.         sysreferences r, sysconstraints s
  14451.     where    
  14452.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14453.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14454.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14455.     and    o1.id = r.rkeyid
  14456.     and s.id = o1.id and (s.status & 0xf) = 1
  14457.     and    r.keycnt >= 15
  14458.     and    o1.id = c1.id
  14459.     and    c1.colid = r.rkey15
  14460.     and r.fkeyid = o2.id
  14461.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14462.     and    o2.id = c2.id    
  14463.     and    c2.colid = r.fkey15
  14464.     union all
  14465.     select
  14466.         db_name(r.rkeydbid),
  14467.         user_name(o1.uid),
  14468.         o1.name,
  14469.         c1.name,
  14470.         convert(binary(16),null),
  14471.         convert(int,null),
  14472.         db_name(r.fkeydbid),
  14473.         user_name(o2.uid),
  14474.         o2.name,
  14475.         c2.name,
  14476.         convert(binary(16),null),
  14477.         convert(int,null),
  14478.         convert(int,16),
  14479.         'NO ACTION',
  14480.         'NO ACTION',
  14481.         object_name(s.constid),
  14482.         object_name(r.constid),
  14483.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14484.     from
  14485.         sysobjects o1, sysobjects o2,
  14486.         syscolumns c1, syscolumns c2,
  14487.         sysreferences r, sysconstraints s
  14488.     where    
  14489.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14490.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14491.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14492.     and    o1.id = r.rkeyid
  14493.     and s.id = o1.id and (s.status & 0xf) = 1
  14494.     and    o1.id = c1.id
  14495.     and    r.keycnt >= 16
  14496.     and    c1.colid = r.rkey16
  14497.     and r.fkeyid = o2.id
  14498.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14499.     and    o2.id = c2.id    
  14500.     and    c2.colid = r.fkey16
  14501.     order by 8,9,2,3,13
  14502.     END
  14503.  
  14504.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  14505.         @scrollopt output, @ccopt output, @rows output
  14506.  
  14507.     drop table #spfkeysrowset1
  14508.        return isnull(@ret,0)
  14509. go
  14510. dump tran master with no_log
  14511. go
  14512. CREATE PROCEDURE sp_foreign_keys_rowset;3
  14513. as
  14514.     select
  14515.         PK_TABLE_CATALOG    = convert(sysname,' '),
  14516.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  14517.         PK_TABLE_NAME         = convert(sysname,' '),
  14518.         PK_COLUMN_NAME         = convert(sysname,' '),
  14519.         PK_COLUMN_GUID        = convert(binary(16),null),
  14520.         PK_COLUMN_PROPID    = convert(int,null),
  14521.         FK_TABLE_CATALOG    = convert(sysname,' '),
  14522.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  14523.         FK_TABLE_NAME         = convert(sysname,' '),
  14524.         FK_COLUMN_NAME         = convert(sysname,' '),
  14525.         FK_COLUMN_GUID        = convert(binary(16),null),
  14526.         FK_COLUMN_PROPID    = convert(int,null),
  14527.         ORDINAL                = convert(int,1),
  14528.         UPDATE_RULE            = 'NO ACTION',
  14529.         DELETE_RULE         = 'NO ACTION',
  14530.         PK_NAME                = convert(sysname, ' '),
  14531.         FK_NAME                = convert(sysname, ' '),
  14532.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14533.     where    1=0
  14534. go
  14535. dump tran master with no_log
  14536. go
  14537.  
  14538.  
  14539. if (charindex('7.00', @@version) = 0)
  14540. begin
  14541.     print ''
  14542.     print ''
  14543.     print 'Warning:'
  14544.     print 'you are installing the stored procedures '
  14545.     print 'on a pre 7.0 SQL Server.'
  14546.     print 'Ignore the following errors.'
  14547. end
  14548. else
  14549.     drop proc sp_foreign_keys_rowset
  14550. go
  14551.  
  14552.  
  14553. /*    Procedure for 7.0 server */
  14554. CREATE PROCEDURE sp_foreign_keys_rowset
  14555.     (
  14556.     @pk_table_name        sysname,
  14557.     @pk_table_schema    sysname = null,
  14558.     @fk_table_name        sysname = null,
  14559.     @fk_table_schema    sysname = null,
  14560.     @fk_table_catalog    sysname = null
  14561.     )
  14562. as
  14563.     select
  14564.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14565.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14566.         PK_TABLE_NAME         = o1.name,
  14567.         PK_COLUMN_NAME         = c1.name,
  14568.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14569.         PK_COLUMN_PROPID    = convert(int,null),
  14570.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14571.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14572.         FK_TABLE_NAME         = o2.name,
  14573.         FK_COLUMN_NAME         = c2.name,
  14574.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14575.         FK_COLUMN_PROPID    = convert(int,null),
  14576.         ORDINAL                = convert(int,1),
  14577.         UPDATE_RULE            = N'NO ACTION',
  14578.         DELETE_RULE         = N'NO ACTION',
  14579.         PK_NAME                = object_name(s.constid),
  14580.         FK_NAME                = object_name(r.constid),
  14581.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14582.     from
  14583.         sysobjects o1, sysobjects o2,
  14584.         syscolumns c1, syscolumns c2,
  14585.         sysreferences r, sysconstraints s
  14586.     where    
  14587.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14588.     and    o1.name = @pk_table_name
  14589.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14590.     and    o1.id = r.rkeyid
  14591.     and s.id = o1.id and (s.status & 0xf) = 1
  14592.     and    o1.id = c1.id
  14593.     and    c1.colid = r.rkey1
  14594.     and r.fkeyid = o2.id
  14595.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14596.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14597.     and    o2.id = c2.id    
  14598.     and    c2.colid = r.fkey1
  14599.     union all
  14600.     select
  14601.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14602.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14603.         PK_TABLE_NAME         = o1.name,
  14604.         PK_COLUMN_NAME         = c1.name,
  14605.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14606.         PK_COLUMN_PROPID    = convert(int,null),
  14607.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14608.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14609.         FK_TABLE_NAME         = o2.name,
  14610.         FK_COLUMN_NAME         = c2.name,
  14611.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14612.         FK_COLUMN_PROPID    = convert(int,null),
  14613.         ORDINAL                = convert(int,2),
  14614.         UPDATE_RULE            = N'NO ACTION',
  14615.         DELETE_RULE         = N'NO ACTION',
  14616.         PK_NAME                = object_name(s.constid),
  14617.         FK_NAME                = object_name(r.constid),
  14618.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14619.     from
  14620.         sysobjects o1, sysobjects o2,
  14621.         syscolumns c1, syscolumns c2,
  14622.         sysreferences r, sysconstraints s
  14623.     where    
  14624.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14625.     and    o1.name = @pk_table_name
  14626.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14627.     and    o1.id = r.rkeyid
  14628.     and s.id = o1.id and (s.status & 0xf) = 1
  14629.     and    o1.id = c1.id
  14630.     and    c1.colid = r.rkey2
  14631.     and    r.keycnt >= 2
  14632.     and r.fkeyid = o2.id
  14633.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14634.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14635.     and    o2.id = c2.id    
  14636.     and    c2.colid = r.fkey2
  14637.     union all
  14638.     select
  14639.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14640.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14641.         PK_TABLE_NAME         = o1.name,
  14642.         PK_COLUMN_NAME         = c1.name,
  14643.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14644.         PK_COLUMN_PROPID    = convert(int,null),
  14645.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14646.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14647.         FK_TABLE_NAME         = o2.name,
  14648.         FK_COLUMN_NAME         = c2.name,
  14649.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14650.         FK_COLUMN_PROPID    = convert(int,null),
  14651.         ORDINAL                = convert(int,3),
  14652.         UPDATE_RULE            = N'NO ACTION',
  14653.         DELETE_RULE         = N'NO ACTION',
  14654.         PK_NAME                = object_name(s.constid),
  14655.         FK_NAME                = object_name(r.constid),
  14656.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14657.     from
  14658.         sysobjects o1, sysobjects o2,
  14659.         syscolumns c1, syscolumns c2,
  14660.         sysreferences r, sysconstraints s
  14661.     where    
  14662.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14663.     and    o1.name = @pk_table_name
  14664.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14665.     and    o1.id = r.rkeyid
  14666.     and s.id = o1.id and (s.status & 0xf) = 1
  14667.     and    o1.id = c1.id
  14668.     and    c1.colid = r.rkey3
  14669.     and    r.keycnt >= 3
  14670.     and r.fkeyid = o2.id
  14671.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14672.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14673.     and    o2.id = c2.id    
  14674.     and    c2.colid = r.fkey3
  14675.     union all
  14676.     select
  14677.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14678.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14679.         PK_TABLE_NAME         = o1.name,
  14680.         PK_COLUMN_NAME         = c1.name,
  14681.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14682.         PK_COLUMN_PROPID    = convert(int,null),
  14683.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14684.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14685.         FK_TABLE_NAME         = o2.name,
  14686.         FK_COLUMN_NAME         = c2.name,
  14687.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14688.         FK_COLUMN_PROPID    = convert(int,null),
  14689.         ORDINAL                = convert(int,4),
  14690.         UPDATE_RULE            = N'NO ACTION',
  14691.         DELETE_RULE         = N'NO ACTION',
  14692.         PK_NAME                = object_name(s.constid),
  14693.         FK_NAME                = object_name(r.constid),
  14694.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14695.     from
  14696.         sysobjects o1, sysobjects o2,
  14697.         syscolumns c1, syscolumns c2,
  14698.         sysreferences r, sysconstraints s
  14699.     where    
  14700.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14701.     and    o1.name = @pk_table_name
  14702.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14703.     and    o1.id = r.rkeyid
  14704.     and s.id = o1.id and (s.status & 0xf) = 1
  14705.     and    r.keycnt >= 4
  14706.     and    o1.id = c1.id
  14707.     and    c1.colid = r.rkey4
  14708.     and r.fkeyid = o2.id
  14709.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14710.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14711.     and    o2.id = c2.id    
  14712.     and    c2.colid = r.fkey4
  14713.     union all
  14714.     select
  14715.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14716.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14717.         PK_TABLE_NAME         = o1.name,
  14718.         PK_COLUMN_NAME         = c1.name,
  14719.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14720.         PK_COLUMN_PROPID    = convert(int,null),
  14721.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14722.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14723.         FK_TABLE_NAME         = o2.name,
  14724.         FK_COLUMN_NAME         = c2.name,
  14725.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14726.         FK_COLUMN_PROPID    = convert(int,null),
  14727.         ORDINAL                = convert(int,5),
  14728.         UPDATE_RULE            = N'NO ACTION',
  14729.         DELETE_RULE         = N'NO ACTION',
  14730.         PK_NAME                = object_name(s.constid),
  14731.         FK_NAME                = object_name(r.constid),
  14732.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14733.     from
  14734.         sysobjects o1, sysobjects o2,
  14735.         syscolumns c1, syscolumns c2,
  14736.         sysreferences r, sysconstraints s
  14737.     where    
  14738.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14739.     and    o1.name = @pk_table_name
  14740.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14741.     and    o1.id = r.rkeyid
  14742.     and s.id = o1.id and (s.status & 0xf) = 1
  14743.     and    r.keycnt >= 5
  14744.     and    o1.id = c1.id
  14745.     and    c1.colid = r.rkey5
  14746.     and r.fkeyid = o2.id
  14747.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14748.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14749.     and    o2.id = c2.id    
  14750.     and    c2.colid = r.fkey5
  14751.     union all
  14752.     select
  14753.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14754.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14755.         PK_TABLE_NAME         = o1.name,
  14756.         PK_COLUMN_NAME         = c1.name,
  14757.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14758.         PK_COLUMN_PROPID    = convert(int,null),
  14759.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14760.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14761.         FK_TABLE_NAME         = o2.name,
  14762.         FK_COLUMN_NAME         = c2.name,
  14763.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14764.         FK_COLUMN_PROPID    = convert(int,null),
  14765.         ORDINAL                = convert(int,6),
  14766.         UPDATE_RULE            = N'NO ACTION',
  14767.         DELETE_RULE         = N'NO ACTION',
  14768.         PK_NAME                = object_name(s.constid),
  14769.         FK_NAME                = object_name(r.constid),
  14770.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14771.     from
  14772.         sysobjects o1, sysobjects o2,
  14773.         syscolumns c1, syscolumns c2,
  14774.         sysreferences r, sysconstraints s
  14775.     where    
  14776.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14777.     and    o1.name = @pk_table_name
  14778.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14779.     and    o1.id = r.rkeyid
  14780.     and s.id = o1.id and (s.status & 0xf) = 1
  14781.     and    r.keycnt >= 6
  14782.     and    o1.id = c1.id
  14783.     and    c1.colid = r.rkey6
  14784.     and r.fkeyid = o2.id
  14785.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14786.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14787.     and    o2.id = c2.id    
  14788.     and    c2.colid = r.fkey6
  14789.     union all
  14790.     select
  14791.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14792.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14793.         PK_TABLE_NAME         = o1.name,
  14794.         PK_COLUMN_NAME         = c1.name,
  14795.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14796.         PK_COLUMN_PROPID    = convert(int,null),
  14797.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14798.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14799.         FK_TABLE_NAME         = o2.name,
  14800.         FK_COLUMN_NAME         = c2.name,
  14801.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14802.         FK_COLUMN_PROPID    = convert(int,null),
  14803.         ORDINAL                = convert(int,7),
  14804.         UPDATE_RULE            = N'NO ACTION',
  14805.         DELETE_RULE         = N'NO ACTION',
  14806.         PK_NAME                = object_name(s.constid),
  14807.         FK_NAME                = object_name(r.constid),
  14808.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14809.     from
  14810.         sysobjects o1, sysobjects o2,
  14811.         syscolumns c1, syscolumns c2,
  14812.         sysreferences r, sysconstraints s
  14813.     where    
  14814.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14815.     and    o1.name = @pk_table_name
  14816.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14817.     and    o1.id = r.rkeyid
  14818.     and s.id = o1.id and (s.status & 0xf) = 1
  14819.     and    r.keycnt >= 7
  14820.     and    o1.id = c1.id
  14821.     and    c1.colid = r.rkey7
  14822.     and r.fkeyid = o2.id
  14823.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14824.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14825.     and    o2.id = c2.id    
  14826.     and    c2.colid = r.fkey7
  14827.     union all
  14828.     select
  14829.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14830.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14831.         PK_TABLE_NAME         = o1.name,
  14832.         PK_COLUMN_NAME         = c1.name,
  14833.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14834.         PK_COLUMN_PROPID    = convert(int,null),
  14835.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14836.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14837.         FK_TABLE_NAME         = o2.name,
  14838.         FK_COLUMN_NAME         = c2.name,
  14839.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14840.         FK_COLUMN_PROPID    = convert(int,null),
  14841.         ORDINAL                = convert(int,8),
  14842.         UPDATE_RULE            = N'NO ACTION',
  14843.         DELETE_RULE         = N'NO ACTION',
  14844.         PK_NAME                = object_name(s.constid),
  14845.         FK_NAME                = object_name(r.constid),
  14846.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14847.     from
  14848.         sysobjects o1, sysobjects o2,
  14849.         syscolumns c1, syscolumns c2,
  14850.         sysreferences r, sysconstraints s
  14851.     where    
  14852.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14853.     and    o1.name = @pk_table_name
  14854.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14855.     and    o1.id = r.rkeyid
  14856.     and s.id = o1.id and (s.status & 0xf) = 1
  14857.     and    r.keycnt >= 8
  14858.     and    o1.id = c1.id
  14859.     and    c1.colid = r.rkey8
  14860.     and r.fkeyid = o2.id
  14861.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14862.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14863.     and    o2.id = c2.id    
  14864.     and    c2.colid = r.fkey8
  14865.     union all
  14866.     select
  14867.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14868.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14869.         PK_TABLE_NAME         = o1.name,
  14870.         PK_COLUMN_NAME         = c1.name,
  14871.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14872.         PK_COLUMN_PROPID    = convert(int,null),
  14873.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14874.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14875.         FK_TABLE_NAME         = o2.name,
  14876.         FK_COLUMN_NAME         = c2.name,
  14877.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14878.         FK_COLUMN_PROPID    = convert(int,null),
  14879.         ORDINAL                = convert(int,9),
  14880.         UPDATE_RULE            = N'NO ACTION',
  14881.         DELETE_RULE         = N'NO ACTION',
  14882.         PK_NAME                = object_name(s.constid),
  14883.         FK_NAME                = object_name(r.constid),
  14884.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14885.     from
  14886.         sysobjects o1, sysobjects o2,
  14887.         syscolumns c1, syscolumns c2,
  14888.         sysreferences r, sysconstraints s
  14889.     where    
  14890.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14891.     and    o1.name = @pk_table_name
  14892.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14893.     and    o1.id = r.rkeyid
  14894.     and s.id = o1.id and (s.status & 0xf) = 1
  14895.     and    r.keycnt >= 9
  14896.     and    o1.id = c1.id
  14897.     and    c1.colid = r.rkey9
  14898.     and r.fkeyid = o2.id
  14899.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14900.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14901.     and    o2.id = c2.id    
  14902.     and    c2.colid = r.fkey9
  14903.     union all
  14904.     select
  14905.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14906.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14907.         PK_TABLE_NAME         = o1.name,
  14908.         PK_COLUMN_NAME         = c1.name,
  14909.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14910.         PK_COLUMN_PROPID    = convert(int,null),
  14911.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14912.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14913.         FK_TABLE_NAME         = o2.name,
  14914.         FK_COLUMN_NAME         = c2.name,
  14915.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14916.         FK_COLUMN_PROPID    = convert(int,null),
  14917.         ORDINAL                = convert(int,10),
  14918.         UPDATE_RULE            = N'NO ACTION',
  14919.         DELETE_RULE         = N'NO ACTION',
  14920.         PK_NAME                = object_name(s.constid),
  14921.         FK_NAME                = object_name(r.constid),
  14922.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14923.     from
  14924.         sysobjects o1, sysobjects o2,
  14925.         syscolumns c1, syscolumns c2,
  14926.         sysreferences r, sysconstraints s
  14927.     where    
  14928.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14929.     and    o1.name = @pk_table_name
  14930.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14931.     and    o1.id = r.rkeyid
  14932.     and s.id = o1.id and (s.status & 0xf) = 1
  14933.     and    r.keycnt >= 10
  14934.     and    o1.id = c1.id
  14935.     and    c1.colid = r.rkey10
  14936.     and r.fkeyid = o2.id
  14937.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14938.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14939.     and    o2.id = c2.id    
  14940.     and    c2.colid = r.fkey10
  14941.     union all
  14942.     select
  14943.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14944.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14945.         PK_TABLE_NAME         = o1.name,
  14946.         PK_COLUMN_NAME         = c1.name,
  14947.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14948.         PK_COLUMN_PROPID    = convert(int,null),
  14949.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14950.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14951.         FK_TABLE_NAME         = o2.name,
  14952.         FK_COLUMN_NAME         = c2.name,
  14953.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14954.         FK_COLUMN_PROPID    = convert(int,null),
  14955.         ORDINAL                = convert(int,11),
  14956.         UPDATE_RULE            = N'NO ACTION',
  14957.         DELETE_RULE         = N'NO ACTION',
  14958.         PK_NAME                = object_name(s.constid),
  14959.         FK_NAME                = object_name(r.constid),
  14960.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14961.     from
  14962.         sysobjects o1, sysobjects o2,
  14963.         syscolumns c1, syscolumns c2,
  14964.         sysreferences r, sysconstraints s
  14965.     where    
  14966.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14967.     and    o1.name = @pk_table_name
  14968.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14969.     and    o1.id = r.rkeyid
  14970.     and s.id = o1.id and (s.status & 0xf) = 1
  14971.     and    r.keycnt >= 11
  14972.     and    o1.id = c1.id
  14973.     and    c1.colid = r.rkey11
  14974.     and r.fkeyid = o2.id
  14975.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14976.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14977.     and    o2.id = c2.id    
  14978.     and    c2.colid = r.fkey11
  14979.     union all
  14980.     select
  14981.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14982.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14983.         PK_TABLE_NAME         = o1.name,
  14984.         PK_COLUMN_NAME         = c1.name,
  14985.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14986.         PK_COLUMN_PROPID    = convert(int,null),
  14987.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14988.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14989.         FK_TABLE_NAME         = o2.name,
  14990.         FK_COLUMN_NAME         = c2.name,
  14991.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14992.         FK_COLUMN_PROPID    = convert(int,null),
  14993.         ORDINAL                = convert(int,12),
  14994.         UPDATE_RULE            = N'NO ACTION',
  14995.         DELETE_RULE         = N'NO ACTION',
  14996.         PK_NAME                = object_name(s.constid),
  14997.         FK_NAME                = object_name(r.constid),
  14998.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14999.     from
  15000.         sysobjects o1, sysobjects o2,
  15001.         syscolumns c1, syscolumns c2,
  15002.         sysreferences r, sysconstraints s
  15003.     where    
  15004.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15005.     and    o1.name = @pk_table_name
  15006.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15007.     and    o1.id = r.rkeyid
  15008.     and s.id = o1.id and (s.status & 0xf) = 1
  15009.     and    r.keycnt >= 12
  15010.     and    o1.id = c1.id
  15011.     and    c1.colid = r.rkey12
  15012.     and r.fkeyid = o2.id
  15013.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15014.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15015.     and    o2.id = c2.id    
  15016.     and    c2.colid = r.fkey12
  15017.     union all
  15018.     select
  15019.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15020.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15021.         PK_TABLE_NAME         = o1.name,
  15022.         PK_COLUMN_NAME         = c1.name,
  15023.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15024.         PK_COLUMN_PROPID    = convert(int,null),
  15025.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15026.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15027.         FK_TABLE_NAME         = o2.name,
  15028.         FK_COLUMN_NAME         = c2.name,
  15029.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15030.         FK_COLUMN_PROPID    = convert(int,null),
  15031.         ORDINAL                = convert(int,13),
  15032.         UPDATE_RULE            = N'NO ACTION',
  15033.         DELETE_RULE         = N'NO ACTION',
  15034.         PK_NAME                = object_name(s.constid),
  15035.         FK_NAME                = object_name(r.constid),
  15036.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15037.     from
  15038.         sysobjects o1, sysobjects o2,
  15039.         syscolumns c1, syscolumns c2,
  15040.         sysreferences r, sysconstraints s
  15041.     where    
  15042.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15043.     and    o1.name = @pk_table_name
  15044.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15045.     and    o1.id = r.rkeyid
  15046.     and s.id = o1.id and (s.status & 0xf) = 1
  15047.     and    r.keycnt >= 13
  15048.     and    o1.id = c1.id
  15049.     and    c1.colid = r.rkey13
  15050.     and r.fkeyid = o2.id
  15051.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15052.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15053.     and    o2.id = c2.id    
  15054.     and    c2.colid = r.fkey13
  15055.     union all
  15056.     select
  15057.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15058.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15059.         PK_TABLE_NAME         = o1.name,
  15060.         PK_COLUMN_NAME         = c1.name,
  15061.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15062.         PK_COLUMN_PROPID    = convert(int,null),
  15063.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15064.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15065.         FK_TABLE_NAME         = o2.name,
  15066.         FK_COLUMN_NAME         = c2.name,
  15067.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15068.         FK_COLUMN_PROPID    = convert(int,null),
  15069.         ORDINAL                = convert(int,14),
  15070.         UPDATE_RULE            = N'NO ACTION',
  15071.         DELETE_RULE         = N'NO ACTION',
  15072.         PK_NAME                = object_name(s.constid),
  15073.         FK_NAME                = object_name(r.constid),
  15074.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15075.     from
  15076.         sysobjects o1, sysobjects o2,
  15077.         syscolumns c1, syscolumns c2,
  15078.         sysreferences r, sysconstraints s
  15079.     where    
  15080.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15081.     and    o1.name = @pk_table_name
  15082.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15083.     and    o1.id = r.rkeyid
  15084.     and s.id = o1.id and (s.status & 0xf) = 1
  15085.     and    r.keycnt >= 14
  15086.     and    o1.id = c1.id
  15087.     and    c1.colid = r.rkey14
  15088.     and r.fkeyid = o2.id
  15089.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15090.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15091.     and    o2.id = c2.id    
  15092.     and    c2.colid = r.fkey14
  15093.     union all
  15094.     select
  15095.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15096.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15097.         PK_TABLE_NAME         = o1.name,
  15098.         PK_COLUMN_NAME         = c1.name,
  15099.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15100.         PK_COLUMN_PROPID    = convert(int,null),
  15101.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15102.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15103.         FK_TABLE_NAME         = o2.name,
  15104.         FK_COLUMN_NAME         = c2.name,
  15105.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15106.         FK_COLUMN_PROPID    = convert(int,null),
  15107.         ORDINAL                = convert(int,15),
  15108.         UPDATE_RULE            = N'NO ACTION',
  15109.         DELETE_RULE         = N'NO ACTION',
  15110.         PK_NAME                = object_name(s.constid),
  15111.         FK_NAME                = object_name(r.constid),
  15112.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15113.     from
  15114.         sysobjects o1, sysobjects o2,
  15115.         syscolumns c1, syscolumns c2,
  15116.         sysreferences r, sysconstraints s
  15117.     where    
  15118.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15119.     and    o1.name = @pk_table_name
  15120.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15121.     and    o1.id = r.rkeyid
  15122.     and s.id = o1.id and (s.status & 0xf) = 1
  15123.     and    r.keycnt >= 15
  15124.     and    o1.id = c1.id
  15125.     and    c1.colid = r.rkey15
  15126.     and r.fkeyid = o2.id
  15127.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15128.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15129.     and    o2.id = c2.id    
  15130.     and    c2.colid = r.fkey15
  15131.     union all
  15132.     select
  15133.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15134.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15135.         PK_TABLE_NAME         = o1.name,
  15136.         PK_COLUMN_NAME         = c1.name,
  15137.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15138.         PK_COLUMN_PROPID    = convert(int,null),
  15139.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15140.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15141.         FK_TABLE_NAME         = o2.name,
  15142.         FK_COLUMN_NAME         = c2.name,
  15143.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15144.         FK_COLUMN_PROPID    = convert(int,null),
  15145.         ORDINAL                = convert(int,16),
  15146.         UPDATE_RULE            = N'NO ACTION',
  15147.         DELETE_RULE         = N'NO ACTION',
  15148.         PK_NAME                = object_name(s.constid),
  15149.         FK_NAME                = object_name(r.constid),
  15150.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15151.     from
  15152.         sysobjects o1, sysobjects o2,
  15153.         syscolumns c1, syscolumns c2,
  15154.         sysreferences r, sysconstraints s
  15155.     where    
  15156.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15157.     and    o1.name = @pk_table_name
  15158.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15159.     and    o1.id = r.rkeyid
  15160.     and s.id = o1.id and (s.status & 0xf) = 1
  15161.     and    r.keycnt >= 16
  15162.     and    o1.id = c1.id
  15163.     and    c1.colid = r.rkey16
  15164.     and r.fkeyid = o2.id
  15165.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15166.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15167.     and    o2.id = c2.id    
  15168.     and    c2.colid = r.fkey16
  15169.     order by 8,9,2,3,13
  15170. go
  15171. dump tran master with no_log
  15172. go
  15173. CREATE PROCEDURE sp_foreign_keys_rowset;2
  15174.     (
  15175.     @fk_table_name        sysname,
  15176.     @fk_table_schema    sysname = null,
  15177.     @pk_table_name        sysname = null,
  15178.     @pk_table_schema    sysname = null,
  15179.     @pk_table_catalog    sysname = null
  15180.     )
  15181. as
  15182.     select
  15183.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15184.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15185.         PK_TABLE_NAME         = o1.name,
  15186.         PK_COLUMN_NAME         = c1.name,
  15187.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15188.         PK_COLUMN_PROPID    = convert(int,null),
  15189.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15190.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15191.         FK_TABLE_NAME         = o2.name,
  15192.         FK_COLUMN_NAME         = c2.name,
  15193.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15194.         FK_COLUMN_PROPID    = convert(int,null),
  15195.         ORDINAL                = convert(int,1),
  15196.         UPDATE_RULE            = N'NO ACTION',
  15197.         DELETE_RULE         = N'NO ACTION',
  15198.         PK_NAME                = object_name(s.constid),
  15199.         FK_NAME                = object_name(r.constid),
  15200.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15201.     from
  15202.         sysobjects o1, sysobjects o2,
  15203.         syscolumns c1, syscolumns c2,
  15204.         sysreferences r, sysconstraints s
  15205.     where    
  15206.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15207.     and    o2.name = @fk_table_name
  15208.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15209.     and    o2.id = r.fkeyid
  15210.     and    o2.id = c2.id
  15211.     and    c2.colid = r.fkey1
  15212.     and r.rkeyid = o1.id
  15213.     and s.id = o1.id and (s.status & 0xf) = 1
  15214.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15215.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15216.     and    o1.id = c1.id    
  15217.     and    c1.colid = r.rkey1
  15218.     union    all
  15219.     select
  15220.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15221.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15222.         PK_TABLE_NAME         = o1.name,
  15223.         PK_COLUMN_NAME         = c1.name,
  15224.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15225.         PK_COLUMN_PROPID    = convert(int,null),
  15226.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15227.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15228.         FK_TABLE_NAME         = o2.name,
  15229.         FK_COLUMN_NAME         = c2.name,
  15230.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15231.         FK_COLUMN_PROPID    = convert(int,null),
  15232.         ORDINAL                = convert(int,2),
  15233.         UPDATE_RULE            = N'NO ACTION',
  15234.         DELETE_RULE         = N'NO ACTION',
  15235.         PK_NAME                = object_name(s.constid),
  15236.         FK_NAME                = object_name(r.constid),
  15237.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15238.     from
  15239.         sysobjects o1, sysobjects o2,
  15240.         syscolumns c1, syscolumns c2,
  15241.         sysreferences r, sysconstraints s
  15242.     where    
  15243.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15244.     and    o2.name = @fk_table_name
  15245.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15246.     and    o2.id = r.fkeyid
  15247.     and    r.keycnt >= 2
  15248.     and    o2.id = c2.id
  15249.     and    c2.colid = r.fkey2
  15250.     and    r.rkeyid = o1.id
  15251.     and s.id = o1.id and (s.status & 0xf) = 1
  15252.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15253.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15254.     and    o1.id = c1.id    
  15255.     and    c1.colid = r.rkey2
  15256.     union    all
  15257.     select
  15258.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15259.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15260.         PK_TABLE_NAME         = o1.name,
  15261.         PK_COLUMN_NAME         = c1.name,
  15262.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15263.         PK_COLUMN_PROPID    = convert(int,null),
  15264.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15265.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15266.         FK_TABLE_NAME         = o2.name,
  15267.         FK_COLUMN_NAME         = c2.name,
  15268.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15269.         FK_COLUMN_PROPID    = convert(int,null),
  15270.         ORDINAL                = convert(int,3),
  15271.         UPDATE_RULE            = N'NO ACTION',
  15272.         DELETE_RULE         = N'NO ACTION',
  15273.         PK_NAME                = object_name(s.constid),
  15274.         FK_NAME                = object_name(r.constid),
  15275.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15276.     from
  15277.         sysobjects o1, sysobjects o2,
  15278.         syscolumns c1, syscolumns c2,
  15279.         sysreferences r, sysconstraints s
  15280.     where    
  15281.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15282.     and    o2.name = @fk_table_name
  15283.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15284.     and    o2.id = r.fkeyid
  15285.     and    r.keycnt >= 3
  15286.     and    o2.id = c2.id
  15287.     and    c2.colid = r.fkey3
  15288.     and r.rkeyid = o1.id
  15289.     and s.id = o1.id and (s.status & 0xf) = 1
  15290.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15291.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15292.     and    o1.id = c1.id    
  15293.     and    c1.colid = r.rkey3
  15294.     union    all
  15295.     select
  15296.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15297.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15298.         PK_TABLE_NAME         = o1.name,
  15299.         PK_COLUMN_NAME         = c1.name,
  15300.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15301.         PK_COLUMN_PROPID    = convert(int,null),
  15302.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15303.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15304.         FK_TABLE_NAME         = o2.name,
  15305.         FK_COLUMN_NAME         = c2.name,
  15306.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15307.         FK_COLUMN_PROPID    = convert(int,null),
  15308.         ORDINAL                = convert(int,4),
  15309.         UPDATE_RULE            = N'NO ACTION',
  15310.         DELETE_RULE         = N'NO ACTION',
  15311.         PK_NAME                = object_name(s.constid),
  15312.         FK_NAME                = object_name(r.constid),
  15313.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15314.     from
  15315.         sysobjects o1, sysobjects o2,
  15316.         syscolumns c1, syscolumns c2,
  15317.         sysreferences r, sysconstraints s
  15318.     where    
  15319.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15320.     and    o2.name = @fk_table_name
  15321.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15322.     and    o2.id = r.fkeyid
  15323.     and    r.keycnt >= 4
  15324.     and    o2.id = c2.id
  15325.     and    c2.colid = r.fkey4
  15326.     and r.rkeyid = o1.id
  15327.     and s.id = o1.id and (s.status & 0xf) = 1
  15328.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15329.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15330.     and    o1.id = c1.id    
  15331.     and    c1.colid = r.rkey4
  15332.     union    all
  15333.     select
  15334.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15335.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15336.         PK_TABLE_NAME         = o1.name,
  15337.         PK_COLUMN_NAME         = c1.name,
  15338.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15339.         PK_COLUMN_PROPID    = convert(int,null),
  15340.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15341.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15342.         FK_TABLE_NAME         = o2.name,
  15343.         FK_COLUMN_NAME         = c2.name,
  15344.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15345.         FK_COLUMN_PROPID    = convert(int,null),
  15346.         ORDINAL                = convert(int,5),
  15347.         UPDATE_RULE            = N'NO ACTION',
  15348.         DELETE_RULE         = N'NO ACTION',
  15349.         PK_NAME                = object_name(s.constid),
  15350.         FK_NAME                = object_name(r.constid),
  15351.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15352.     from
  15353.         sysobjects o1, sysobjects o2,
  15354.         syscolumns c1, syscolumns c2,
  15355.         sysreferences r, sysconstraints s
  15356.     where    
  15357.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15358.     and    o2.name = @fk_table_name
  15359.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15360.     and    o2.id = r.fkeyid
  15361.     and    r.keycnt >= 5
  15362.     and    o2.id = c2.id
  15363.     and    c2.colid = r.fkey5
  15364.     and    r.rkeyid = o1.id
  15365.     and s.id = o1.id and (s.status & 0xf) = 1
  15366.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15367.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15368.     and    o1.id = c1.id    
  15369.     and    c1.colid = r.rkey5
  15370.     union    all
  15371.     select
  15372.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15373.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15374.         PK_TABLE_NAME         = o1.name,
  15375.         PK_COLUMN_NAME         = c1.name,
  15376.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15377.         PK_COLUMN_PROPID    = convert(int,null),
  15378.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15379.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15380.         FK_TABLE_NAME         = o2.name,
  15381.         FK_COLUMN_NAME         = c2.name,
  15382.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15383.         FK_COLUMN_PROPID    = convert(int,null),
  15384.         ORDINAL                = convert(int,6),
  15385.         UPDATE_RULE            = N'NO ACTION',
  15386.         DELETE_RULE         = N'NO ACTION',
  15387.         PK_NAME                = object_name(s.constid),
  15388.         FK_NAME                = object_name(r.constid),
  15389.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15390.     from
  15391.         sysobjects o1, sysobjects o2,
  15392.         syscolumns c1, syscolumns c2,
  15393.         sysreferences r, sysconstraints s
  15394.     where    
  15395.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15396.     and    o2.name = @fk_table_name
  15397.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15398.     and    o2.id = r.fkeyid
  15399.     and    r.keycnt >= 6
  15400.     and    o2.id = c2.id
  15401.     and    c2.colid = r.fkey6
  15402.     and r.rkeyid = o1.id
  15403.     and s.id = o1.id and (s.status & 0xf) = 1
  15404.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15405.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15406.     and    o1.id = c1.id    
  15407.     and    c1.colid = r.rkey6
  15408.     union    all
  15409.     select
  15410.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15411.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15412.         PK_TABLE_NAME         = o1.name,
  15413.         PK_COLUMN_NAME         = c1.name,
  15414.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15415.         PK_COLUMN_PROPID    = convert(int,null),
  15416.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15417.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15418.         FK_TABLE_NAME         = o2.name,
  15419.         FK_COLUMN_NAME         = c2.name,
  15420.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15421.         FK_COLUMN_PROPID    = convert(int,null),
  15422.         ORDINAL                = convert(int,7),
  15423.         UPDATE_RULE            = N'NO ACTION',
  15424.         DELETE_RULE         = N'NO ACTION',
  15425.         PK_NAME                = object_name(s.constid),
  15426.         FK_NAME                = object_name(r.constid),
  15427.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15428.     from
  15429.         sysobjects o1, sysobjects o2,
  15430.         syscolumns c1, syscolumns c2,
  15431.         sysreferences r, sysconstraints s
  15432.     where    
  15433.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15434.     and    o2.name = @fk_table_name
  15435.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15436.     and    o2.id = r.fkeyid
  15437.     and    r.keycnt >= 7
  15438.     and    o2.id = c2.id
  15439.     and    c2.colid = r.fkey7
  15440.     and    r.rkeyid = o1.id
  15441.     and s.id = o1.id and (s.status & 0xf) = 1
  15442.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15443.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15444.     and    o1.id = c1.id    
  15445.     and    c1.colid = r.rkey7
  15446.     union    all
  15447.     select
  15448.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15449.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15450.         PK_TABLE_NAME         = o1.name,
  15451.         PK_COLUMN_NAME         = c1.name,
  15452.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15453.         PK_COLUMN_PROPID    = convert(int,null),
  15454.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15455.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15456.         FK_TABLE_NAME         = o2.name,
  15457.         FK_COLUMN_NAME         = c2.name,
  15458.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15459.         FK_COLUMN_PROPID    = convert(int,null),
  15460.         ORDINAL                = convert(int,8),
  15461.         UPDATE_RULE            = N'NO ACTION',
  15462.         DELETE_RULE         = N'NO ACTION',
  15463.         PK_NAME                = object_name(s.constid),
  15464.         FK_NAME                = object_name(r.constid),
  15465.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15466.     from
  15467.         sysobjects o1, sysobjects o2,
  15468.         syscolumns c1, syscolumns c2,
  15469.         sysreferences r, sysconstraints s
  15470.     where    
  15471.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15472.     and    o2.name = @fk_table_name
  15473.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15474.     and    o2.id = r.fkeyid
  15475.     and    r.keycnt >= 8
  15476.     and    o2.id = c2.id
  15477.     and    c2.colid = r.fkey8
  15478.     and r.rkeyid = o1.id
  15479.     and s.id = o1.id and (s.status & 0xf) = 1
  15480.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15481.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15482.     and    o1.id = c1.id    
  15483.     and    c1.colid = r.rkey8
  15484.     union    all
  15485.     select
  15486.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15487.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15488.         PK_TABLE_NAME         = o1.name,
  15489.         PK_COLUMN_NAME         = c1.name,
  15490.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15491.         PK_COLUMN_PROPID    = convert(int,null),
  15492.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15493.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15494.         FK_TABLE_NAME         = o2.name,
  15495.         FK_COLUMN_NAME         = c2.name,
  15496.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15497.         FK_COLUMN_PROPID    = convert(int,null),
  15498.         ORDINAL                = convert(int,9),
  15499.         UPDATE_RULE            = N'NO ACTION',
  15500.         DELETE_RULE         = N'NO ACTION',
  15501.         PK_NAME                = object_name(s.constid),
  15502.         FK_NAME                = object_name(r.constid),
  15503.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15504.     from
  15505.         sysobjects o1, sysobjects o2,
  15506.         syscolumns c1, syscolumns c2,
  15507.         sysreferences r, sysconstraints s
  15508.     where    
  15509.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15510.     and    o2.name = @fk_table_name
  15511.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15512.     and    o2.id = r.fkeyid
  15513.     and    r.keycnt >= 9
  15514.     and    o2.id = c2.id
  15515.     and    c2.colid = r.fkey9
  15516.     and    r.rkeyid = o1.id
  15517.     and s.id = o1.id and (s.status & 0xf) = 1
  15518.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15519.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15520.     and    o1.id = c1.id    
  15521.     and    c1.colid = r.rkey9
  15522.     union    all
  15523.     select
  15524.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15525.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15526.         PK_TABLE_NAME         = o1.name,
  15527.         PK_COLUMN_NAME         = c1.name,
  15528.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15529.         PK_COLUMN_PROPID    = convert(int,null),
  15530.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15531.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15532.         FK_TABLE_NAME         = o2.name,
  15533.         FK_COLUMN_NAME         = c2.name,
  15534.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15535.         FK_COLUMN_PROPID    = convert(int,null),
  15536.         ORDINAL                = convert(int,10),
  15537.         UPDATE_RULE            = N'NO ACTION',
  15538.         DELETE_RULE         = N'NO ACTION',
  15539.         PK_NAME                = object_name(s.constid),
  15540.         FK_NAME                = object_name(r.constid),
  15541.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15542.     from
  15543.         sysobjects o1, sysobjects o2,
  15544.         syscolumns c1, syscolumns c2,
  15545.         sysreferences r, sysconstraints s
  15546.     where    
  15547.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15548.     and    o2.name = @fk_table_name
  15549.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15550.     and    o2.id = r.fkeyid
  15551.     and    r.keycnt >= 10
  15552.     and    o2.id = c2.id
  15553.     and    c2.colid = r.fkey10
  15554.     and r.rkeyid = o1.id
  15555.     and s.id = o1.id and (s.status & 0xf) = 1
  15556.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15557.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15558.     and    o1.id = c1.id    
  15559.     and    c1.colid = r.rkey10
  15560.     union    all
  15561.     select
  15562.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15563.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15564.         PK_TABLE_NAME         = o1.name,
  15565.         PK_COLUMN_NAME         = c1.name,
  15566.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15567.         PK_COLUMN_PROPID    = convert(int,null),
  15568.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15569.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15570.         FK_TABLE_NAME         = o2.name,
  15571.         FK_COLUMN_NAME         = c2.name,
  15572.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15573.         FK_COLUMN_PROPID    = convert(int,null),
  15574.         ORDINAL                = convert(int,11),
  15575.         UPDATE_RULE            = N'NO ACTION',
  15576.         DELETE_RULE         = N'NO ACTION',
  15577.         PK_NAME                = object_name(s.constid),
  15578.         FK_NAME                = object_name(r.constid),
  15579.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15580.     from
  15581.         sysobjects o1, sysobjects o2,
  15582.         syscolumns c1, syscolumns c2,
  15583.         sysreferences r, sysconstraints s
  15584.     where    
  15585.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15586.     and    o2.name = @fk_table_name
  15587.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15588.     and    o2.id = r.fkeyid
  15589.     and    r.keycnt >= 11
  15590.     and    o2.id = c2.id
  15591.     and    c2.colid = r.fkey11
  15592.     and r.rkeyid = o1.id
  15593.     and s.id = o1.id and (s.status & 0xf) = 1
  15594.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15595.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15596.     and    o1.id = c1.id    
  15597.     and    c1.colid = r.rkey11
  15598.     union    all
  15599.     select
  15600.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15601.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15602.         PK_TABLE_NAME         = o1.name,
  15603.         PK_COLUMN_NAME         = c1.name,
  15604.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15605.         PK_COLUMN_PROPID    = convert(int,null),
  15606.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15607.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15608.         FK_TABLE_NAME         = o2.name,
  15609.         FK_COLUMN_NAME         = c2.name,
  15610.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15611.         FK_COLUMN_PROPID    = convert(int,null),
  15612.         ORDINAL                = convert(int,12),
  15613.         UPDATE_RULE            = N'NO ACTION',
  15614.         DELETE_RULE         = N'NO ACTION',
  15615.         PK_NAME                = object_name(s.constid),
  15616.         FK_NAME                = object_name(r.constid),
  15617.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15618.     from
  15619.         sysobjects o1, sysobjects o2,
  15620.         syscolumns c1, syscolumns c2,
  15621.         sysreferences r, sysconstraints s
  15622.     where    
  15623.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15624.     and    o2.name = @fk_table_name
  15625.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15626.     and    o2.id = r.fkeyid
  15627.     and    r.keycnt >= 12
  15628.     and    o2.id = c2.id
  15629.     and    c2.colid = r.fkey12
  15630.     and r.rkeyid = o1.id
  15631.     and s.id = o1.id and (s.status & 0xf) = 1
  15632.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15633.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15634.     and    o1.id = c1.id    
  15635.     and    c1.colid = r.rkey12
  15636.     union    all
  15637.     select
  15638.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15639.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15640.         PK_TABLE_NAME         = o1.name,
  15641.         PK_COLUMN_NAME         = c1.name,
  15642.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15643.         PK_COLUMN_PROPID    = convert(int,null),
  15644.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15645.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15646.         FK_TABLE_NAME         = o2.name,
  15647.         FK_COLUMN_NAME         = c2.name,
  15648.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15649.         FK_COLUMN_PROPID    = convert(int,null),
  15650.         ORDINAL                = convert(int,13),
  15651.         UPDATE_RULE            = N'NO ACTION',
  15652.         DELETE_RULE         = N'NO ACTION',
  15653.         PK_NAME                = object_name(s.constid),
  15654.         FK_NAME                = object_name(r.constid),
  15655.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15656.     from
  15657.         sysobjects o1, sysobjects o2,
  15658.         syscolumns c1, syscolumns c2,
  15659.         sysreferences r, sysconstraints s
  15660.     where    
  15661.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15662.     and    o2.name = @fk_table_name
  15663.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15664.     and    o2.id = r.fkeyid
  15665.     and    r.keycnt >= 13
  15666.     and    o2.id = c2.id
  15667.     and    c2.colid = r.fkey13
  15668.     and r.rkeyid = o1.id
  15669.     and s.id = o1.id and (s.status & 0xf) = 1
  15670.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15671.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15672.     and    o1.id = c1.id    
  15673.     and    c1.colid = r.rkey13
  15674.     union    all
  15675.     select
  15676.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15677.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15678.         PK_TABLE_NAME         = o1.name,
  15679.         PK_COLUMN_NAME         = c1.name,
  15680.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15681.         PK_COLUMN_PROPID    = convert(int,null),
  15682.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15683.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15684.         FK_TABLE_NAME         = o2.name,
  15685.         FK_COLUMN_NAME         = c2.name,
  15686.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15687.         FK_COLUMN_PROPID    = convert(int,null),
  15688.         ORDINAL                = convert(int,14),
  15689.         UPDATE_RULE            = N'NO ACTION',
  15690.         DELETE_RULE         = N'NO ACTION',
  15691.         PK_NAME                = object_name(s.constid),
  15692.         FK_NAME                = object_name(r.constid),
  15693.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15694.     from
  15695.         sysobjects o1, sysobjects o2,
  15696.         syscolumns c1, syscolumns c2,
  15697.         sysreferences r, sysconstraints s
  15698.     where    
  15699.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15700.     and    o2.name = @fk_table_name
  15701.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15702.     and    o2.id = r.fkeyid
  15703.     and    r.keycnt >= 14
  15704.     and    o2.id = c2.id
  15705.     and    c2.colid = r.fkey14
  15706.     and r.rkeyid = o1.id
  15707.     and s.id = o1.id and (s.status & 0xf) = 1
  15708.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15709.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15710.     and    o1.id = c1.id    
  15711.     and    c1.colid = r.rkey14
  15712.     union    all
  15713.     select
  15714.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15715.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15716.         PK_TABLE_NAME         = o1.name,
  15717.         PK_COLUMN_NAME         = c1.name,
  15718.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15719.         PK_COLUMN_PROPID    = convert(int,null),
  15720.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15721.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15722.         FK_TABLE_NAME         = o2.name,
  15723.         FK_COLUMN_NAME         = c2.name,
  15724.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15725.         FK_COLUMN_PROPID    = convert(int,null),
  15726.         ORDINAL                = convert(int,15),
  15727.         UPDATE_RULE            = N'NO ACTION',
  15728.         DELETE_RULE         = N'NO ACTION',
  15729.         PK_NAME                = object_name(s.constid),
  15730.         FK_NAME                = object_name(r.constid),
  15731.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  15732.     from
  15733.         sysobjects o1, sysobjects o2,
  15734.         syscolumns c1, syscolumns c2,
  15735.         sysreferences r, sysconstraints s
  15736.     where    
  15737.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15738.     and    o2.name = @fk_table_name
  15739.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15740.     and    o2.id = r.fkeyid
  15741.     and    r.keycnt >= 15
  15742.     and    o2.id = c2.id
  15743.     and    c2.colid = r.fkey15
  15744.     and r.rkeyid = o1.id
  15745.     and s.id = o1.id and (s.status & 0xf) = 1
  15746.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15747.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15748.     and    o1.id = c1.id    
  15749.     and    c1.colid = r.rkey15
  15750.     union    all
  15751.     select
  15752.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15753.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15754.         PK_TABLE_NAME         = o1.name,
  15755.         PK_COLUMN_NAME         = c1.name,
  15756.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15757.         PK_COLUMN_PROPID    = convert(int,null),
  15758.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15759.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15760.         FK_TABLE_NAME         = o2.name,
  15761.         FK_COLUMN_NAME         = c2.name,
  15762.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15763.         FK_COLUMN_PROPID    = convert(int,null),
  15764.         ORDINAL                = convert(int,16),
  15765.         UPDATE_RULE            = N'NO ACTION',
  15766.         DELETE_RULE         = N'NO ACTION',
  15767.         PK_NAME                = object_name(s.constid),
  15768.         FK_NAME                = object_name(r.constid),
  15769.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  15770.     from
  15771.         sysobjects o1, sysobjects o2,
  15772.         syscolumns c1, syscolumns c2,
  15773.         sysreferences r, sysconstraints s
  15774.     where    
  15775.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15776.     and    o2.name = @fk_table_name
  15777.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15778.     and    o2.id = r.fkeyid
  15779.     and    r.keycnt >= 16
  15780.     and    o2.id = c2.id
  15781.     and    c2.colid = r.fkey16
  15782.     and r.rkeyid = o1.id
  15783.     and s.id = o1.id and (s.status & 0xf) = 1
  15784.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15785.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15786.     and    o1.id = c1.id    
  15787.     and    c1.colid = r.rkey16
  15788.     order by 8,9,2,3,13
  15789. go
  15790. dump tran master with no_log
  15791. go
  15792. CREATE PROCEDURE sp_foreign_keys_rowset;3
  15793.     (
  15794.     @pk_table_schema    sysname = null,
  15795.     @pk_table_catalog    sysname = null,
  15796.     @fk_table_schema    sysname = null,
  15797.     @fk_table_catalog    sysname = null
  15798.     )
  15799. as
  15800.     select
  15801.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15802.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15803.         PK_TABLE_NAME         = o1.name,
  15804.         PK_COLUMN_NAME         = c1.name,
  15805.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15806.         PK_COLUMN_PROPID    = convert(int,null),
  15807.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15808.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15809.         FK_TABLE_NAME         = o2.name,
  15810.         FK_COLUMN_NAME         = c2.name,
  15811.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15812.         FK_COLUMN_PROPID    = convert(int,null),
  15813.         ORDINAL                = convert(int,1),
  15814.         UPDATE_RULE            = N'NO ACTION',
  15815.         DELETE_RULE         = N'NO ACTION',
  15816.         PK_NAME                = object_name(s.constid),
  15817.         FK_NAME                = object_name(r.constid),
  15818.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  15819.     from
  15820.         sysobjects o1, sysobjects o2,
  15821.         syscolumns c1, syscolumns c2,
  15822.         sysreferences r, sysconstraints s
  15823.     where    
  15824.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15825.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15826.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15827.     and    o1.id = r.rkeyid
  15828.     and s.id = o1.id and (s.status & 0xf) = 1
  15829.     and    o1.id = c1.id
  15830.     and    c1.colid = r.rkey1
  15831.     and r.fkeyid = o2.id
  15832.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15833.     and    o2.id = c2.id    
  15834.     and    c2.colid = r.fkey1
  15835.     union all
  15836.     select
  15837.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15838.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15839.         PK_TABLE_NAME         = o1.name,
  15840.         PK_COLUMN_NAME         = c1.name,
  15841.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15842.         PK_COLUMN_PROPID    = convert(int,null),
  15843.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15844.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15845.         FK_TABLE_NAME         = o2.name,
  15846.         FK_COLUMN_NAME         = c2.name,
  15847.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15848.         FK_COLUMN_PROPID    = convert(int,null),
  15849.         ORDINAL                = convert(int,2),
  15850.         UPDATE_RULE            = N'NO ACTION',
  15851.         DELETE_RULE         = N'NO ACTION',
  15852.         PK_NAME                = object_name(s.constid),
  15853.         FK_NAME                = object_name(r.constid),
  15854.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15855.     from
  15856.         sysobjects o1, sysobjects o2,
  15857.         syscolumns c1, syscolumns c2,
  15858.         sysreferences r, sysconstraints s
  15859.     where    
  15860.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15861.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15862.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15863.     and    o1.id = r.rkeyid
  15864.     and s.id = o1.id and (s.status & 0xf) = 1
  15865.     and    r.keycnt >= 2
  15866.     and    o1.id = c1.id
  15867.     and    c1.colid = r.rkey2
  15868.     and r.fkeyid = o2.id
  15869.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15870.     and    o2.id = c2.id    
  15871.     and    c2.colid = r.fkey2
  15872.     union all
  15873.     select
  15874.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15875.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15876.         PK_TABLE_NAME         = o1.name,
  15877.         PK_COLUMN_NAME         = c1.name,
  15878.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15879.         PK_COLUMN_PROPID    = convert(int,null),
  15880.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15881.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15882.         FK_TABLE_NAME         = o2.name,
  15883.         FK_COLUMN_NAME         = c2.name,
  15884.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15885.         FK_COLUMN_PROPID    = convert(int,null),
  15886.         ORDINAL                = convert(int,3),
  15887.         UPDATE_RULE            = N'NO ACTION',
  15888.         DELETE_RULE         = N'NO ACTION',
  15889.         PK_NAME                = object_name(s.constid),
  15890.         FK_NAME                = object_name(r.constid),
  15891.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15892.     from
  15893.         sysobjects o1, sysobjects o2,
  15894.         syscolumns c1, syscolumns c2,
  15895.         sysreferences r, sysconstraints s
  15896.     where    
  15897.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15898.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15899.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15900.     and    o1.id = r.rkeyid
  15901.     and s.id = o1.id and (s.status & 0xf) = 1
  15902.     and    r.keycnt >= 3
  15903.     and    o1.id = c1.id
  15904.     and    c1.colid = r.rkey3
  15905.     and r.fkeyid = o2.id
  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.fkey3
  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,4),
  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.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15935.     and     (@fk_table_catalog is null or @fk_table_catalog = db_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 >= 4
  15940.     and    o1.id = c1.id
  15941.     and    c1.colid = r.rkey4
  15942.     and r.fkeyid = o2.id
  15943.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15944.     and    o2.id = c2.id    
  15945.     and    c2.colid = r.fkey4
  15946.     union all
  15947.     select
  15948.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15949.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15950.         PK_TABLE_NAME         = o1.name,
  15951.         PK_COLUMN_NAME         = c1.name,
  15952.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15953.         PK_COLUMN_PROPID    = convert(int,null),
  15954.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15955.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15956.         FK_TABLE_NAME         = o2.name,
  15957.         FK_COLUMN_NAME         = c2.name,
  15958.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15959.         FK_COLUMN_PROPID    = convert(int,null),
  15960.         ORDINAL                = convert(int,5),
  15961.         UPDATE_RULE            = N'NO ACTION',
  15962.         DELETE_RULE         = N'NO ACTION',
  15963.         PK_NAME                = object_name(s.constid),
  15964.         FK_NAME                = object_name(r.constid),
  15965.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15966.     from
  15967.         sysobjects o1, sysobjects o2,
  15968.         syscolumns c1, syscolumns c2,
  15969.         sysreferences r, sysconstraints s
  15970.     where    
  15971.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15972.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15973.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15974.     and    o1.id = r.rkeyid
  15975.     and s.id = o1.id and (s.status & 0xf) = 1
  15976.     and    r.keycnt >= 5
  15977.     and    o1.id = c1.id
  15978.     and    c1.colid = r.rkey5
  15979.     and r.fkeyid = o2.id
  15980.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15981.     and    o2.id = c2.id    
  15982.     and    c2.colid = r.fkey5
  15983.     union all
  15984.     select
  15985.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15986.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15987.         PK_TABLE_NAME         = o1.name,
  15988.         PK_COLUMN_NAME         = c1.name,
  15989.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15990.         PK_COLUMN_PROPID    = convert(int,null),
  15991.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15992.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15993.         FK_TABLE_NAME         = o2.name,
  15994.         FK_COLUMN_NAME         = c2.name,
  15995.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15996.         FK_COLUMN_PROPID    = convert(int,null),
  15997.         ORDINAL                = convert(int,6),
  15998.         UPDATE_RULE            = N'NO ACTION',
  15999.         DELETE_RULE         = N'NO ACTION',
  16000.         PK_NAME                = object_name(s.constid),
  16001.         FK_NAME                = object_name(r.constid),
  16002.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16003.     from
  16004.         sysobjects o1, sysobjects o2,
  16005.         syscolumns c1, syscolumns c2,
  16006.         sysreferences r, sysconstraints s
  16007.     where    
  16008.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16009.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16010.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16011.     and    o1.id = r.rkeyid
  16012.     and s.id = o1.id and (s.status & 0xf) = 1
  16013.     and    r.keycnt >= 6
  16014.     and    o1.id = c1.id
  16015.     and    c1.colid = r.rkey6
  16016.     and r.fkeyid = o2.id
  16017.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16018.     and    o2.id = c2.id    
  16019.     and    c2.colid = r.fkey6
  16020.     union all
  16021.     select
  16022.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16023.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16024.         PK_TABLE_NAME         = o1.name,
  16025.         PK_COLUMN_NAME         = c1.name,
  16026.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16027.         PK_COLUMN_PROPID    = convert(int,null),
  16028.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16029.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16030.         FK_TABLE_NAME         = o2.name,
  16031.         FK_COLUMN_NAME         = c2.name,
  16032.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16033.         FK_COLUMN_PROPID    = convert(int,null),
  16034.         ORDINAL                = convert(int,7),
  16035.         UPDATE_RULE            = N'NO ACTION',
  16036.         DELETE_RULE         = N'NO ACTION',
  16037.         PK_NAME                = object_name(s.constid),
  16038.         FK_NAME                = object_name(r.constid),
  16039.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16040.     from
  16041.         sysobjects o1, sysobjects o2,
  16042.         syscolumns c1, syscolumns c2,
  16043.         sysreferences r, sysconstraints s
  16044.     where    
  16045.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16046.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16047.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16048.     and    o1.id = r.rkeyid
  16049.     and s.id = o1.id and (s.status & 0xf) = 1
  16050.     and    r.keycnt >= 7
  16051.     and    o1.id = c1.id
  16052.     and    c1.colid = r.rkey7
  16053.     and r.fkeyid = o2.id
  16054.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16055.     and    o2.id = c2.id    
  16056.     and    c2.colid = r.fkey7
  16057.     union all
  16058.     select
  16059.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16060.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16061.         PK_TABLE_NAME         = o1.name,
  16062.         PK_COLUMN_NAME         = c1.name,
  16063.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16064.         PK_COLUMN_PROPID    = convert(int,null),
  16065.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16066.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16067.         FK_TABLE_NAME         = o2.name,
  16068.         FK_COLUMN_NAME         = c2.name,
  16069.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16070.         FK_COLUMN_PROPID    = convert(int,null),
  16071.         ORDINAL                = convert(int,8),
  16072.         UPDATE_RULE            = N'NO ACTION',
  16073.         DELETE_RULE         = N'NO ACTION',
  16074.         PK_NAME                = object_name(s.constid),
  16075.         FK_NAME                = object_name(r.constid),
  16076.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16077.     from
  16078.         sysobjects o1, sysobjects o2,
  16079.         syscolumns c1, syscolumns c2,
  16080.         sysreferences r, sysconstraints s
  16081.     where    
  16082.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16083.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16084.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16085.     and    o1.id = r.rkeyid
  16086.     and s.id = o1.id and (s.status & 0xf) = 1
  16087.     and    r.keycnt >= 8
  16088.     and    o1.id = c1.id
  16089.     and    c1.colid = r.rkey8
  16090.     and r.fkeyid = o2.id
  16091.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16092.     and    o2.id = c2.id    
  16093.     and    c2.colid = r.fkey8
  16094.     union all
  16095.     select
  16096.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16097.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16098.         PK_TABLE_NAME         = o1.name,
  16099.         PK_COLUMN_NAME         = c1.name,
  16100.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16101.         PK_COLUMN_PROPID    = convert(int,null),
  16102.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16103.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16104.         FK_TABLE_NAME         = o2.name,
  16105.         FK_COLUMN_NAME         = c2.name,
  16106.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16107.         FK_COLUMN_PROPID    = convert(int,null),
  16108.         ORDINAL                = convert(int,9),
  16109.         UPDATE_RULE            = N'NO ACTION',
  16110.         DELETE_RULE         = N'NO ACTION',
  16111.         PK_NAME                = object_name(s.constid),
  16112.         FK_NAME                = object_name(r.constid),
  16113.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16114.     from
  16115.         sysobjects o1, sysobjects o2,
  16116.         syscolumns c1, syscolumns c2,
  16117.         sysreferences r, sysconstraints s
  16118.     where    
  16119.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16120.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16121.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16122.     and    o1.id = r.rkeyid
  16123.     and s.id = o1.id and (s.status & 0xf) = 1
  16124.     and    r.keycnt >= 9
  16125.     and    o1.id = c1.id
  16126.     and    c1.colid = r.rkey9
  16127.     and r.fkeyid = o2.id
  16128.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16129.     and    o2.id = c2.id    
  16130.     and    c2.colid = r.fkey9
  16131.     union all
  16132.     select
  16133.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16134.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16135.         PK_TABLE_NAME         = o1.name,
  16136.         PK_COLUMN_NAME         = c1.name,
  16137.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16138.         PK_COLUMN_PROPID    = convert(int,null),
  16139.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16140.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16141.         FK_TABLE_NAME         = o2.name,
  16142.         FK_COLUMN_NAME         = c2.name,
  16143.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16144.         FK_COLUMN_PROPID    = convert(int,null),
  16145.         ORDINAL                = convert(int,10),
  16146.         UPDATE_RULE            = N'NO ACTION',
  16147.         DELETE_RULE         = N'NO ACTION',
  16148.         PK_NAME                = object_name(s.constid),
  16149.         FK_NAME                = object_name(r.constid),
  16150.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16151.     from
  16152.         sysobjects o1, sysobjects o2,
  16153.         syscolumns c1, syscolumns c2,
  16154.         sysreferences r, sysconstraints s
  16155.     where    
  16156.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16157.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16158.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16159.     and    o1.id = r.rkeyid
  16160.     and s.id = o1.id and (s.status & 0xf) = 1
  16161.     and    r.keycnt >= 10
  16162.     and    o1.id = c1.id
  16163.     and    c1.colid = r.rkey10
  16164.     and r.fkeyid = o2.id
  16165.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16166.     and    o2.id = c2.id    
  16167.     and    c2.colid = r.fkey10
  16168.     union all
  16169.     select
  16170.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16171.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16172.         PK_TABLE_NAME         = o1.name,
  16173.         PK_COLUMN_NAME         = c1.name,
  16174.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16175.         PK_COLUMN_PROPID    = convert(int,null),
  16176.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16177.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16178.         FK_TABLE_NAME         = o2.name,
  16179.         FK_COLUMN_NAME         = c2.name,
  16180.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16181.         FK_COLUMN_PROPID    = convert(int,null),
  16182.         ORDINAL                = convert(int,11),
  16183.         UPDATE_RULE            = N'NO ACTION',
  16184.         DELETE_RULE         = N'NO ACTION',
  16185.         PK_NAME                = object_name(s.constid),
  16186.         FK_NAME                = object_name(r.constid),
  16187.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16188.     from
  16189.         sysobjects o1, sysobjects o2,
  16190.         syscolumns c1, syscolumns c2,
  16191.         sysreferences r, sysconstraints s
  16192.     where    
  16193.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16194.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16195.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16196.     and    o1.id = r.rkeyid
  16197.     and s.id = o1.id and (s.status & 0xf) = 1
  16198.     and    r.keycnt >= 11
  16199.     and    o1.id = c1.id
  16200.     and    c1.colid = r.rkey11
  16201.     and r.fkeyid = o2.id
  16202.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16203.     and    o2.id = c2.id    
  16204.     and    c2.colid = r.fkey11
  16205.     union all
  16206.     select
  16207.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16208.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16209.         PK_TABLE_NAME         = o1.name,
  16210.         PK_COLUMN_NAME         = c1.name,
  16211.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16212.         PK_COLUMN_PROPID    = convert(int,null),
  16213.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16214.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16215.         FK_TABLE_NAME         = o2.name,
  16216.         FK_COLUMN_NAME         = c2.name,
  16217.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16218.         FK_COLUMN_PROPID    = convert(int,null),
  16219.         ORDINAL                = convert(int,12),
  16220.         UPDATE_RULE            = N'NO ACTION',
  16221.         DELETE_RULE         = N'NO ACTION',
  16222.         PK_NAME                = object_name(s.constid),
  16223.         FK_NAME                = object_name(r.constid),
  16224.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16225.     from
  16226.         sysobjects o1, sysobjects o2,
  16227.         syscolumns c1, syscolumns c2,
  16228.         sysreferences r, sysconstraints s
  16229.     where    
  16230.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16231.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16232.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16233.     and    o1.id = r.rkeyid
  16234.     and s.id = o1.id and (s.status & 0xf) = 1
  16235.     and    r.keycnt >= 12
  16236.     and    o1.id = c1.id
  16237.     and    c1.colid = r.rkey12
  16238.     and r.fkeyid = o2.id
  16239.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16240.     and    o2.id = c2.id    
  16241.     and    c2.colid = r.fkey12
  16242.     union all
  16243.     select
  16244.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16245.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16246.         PK_TABLE_NAME         = o1.name,
  16247.         PK_COLUMN_NAME         = c1.name,
  16248.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16249.         PK_COLUMN_PROPID    = convert(int,null),
  16250.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16251.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16252.         FK_TABLE_NAME         = o2.name,
  16253.         FK_COLUMN_NAME         = c2.name,
  16254.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16255.         FK_COLUMN_PROPID    = convert(int,null),
  16256.         ORDINAL                = convert(int,13),
  16257.         UPDATE_RULE            = N'NO ACTION',
  16258.         DELETE_RULE         = N'NO ACTION',
  16259.         PK_NAME                = object_name(s.constid),
  16260.         FK_NAME                = object_name(r.constid),
  16261.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16262.     from
  16263.         sysobjects o1, sysobjects o2,
  16264.         syscolumns c1, syscolumns c2,
  16265.         sysreferences r, sysconstraints s
  16266.     where    
  16267.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16268.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16269.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16270.     and    o1.id = r.rkeyid
  16271.     and s.id = o1.id and (s.status & 0xf) = 1
  16272.     and    r.keycnt >= 13
  16273.     and    o1.id = c1.id
  16274.     and    c1.colid = r.rkey13
  16275.     and r.fkeyid = o2.id
  16276.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16277.     and    o2.id = c2.id    
  16278.     and    c2.colid = r.fkey13
  16279.     union all
  16280.     select
  16281.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16282.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16283.         PK_TABLE_NAME         = o1.name,
  16284.         PK_COLUMN_NAME         = c1.name,
  16285.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16286.         PK_COLUMN_PROPID    = convert(int,null),
  16287.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16288.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16289.         FK_TABLE_NAME         = o2.name,
  16290.         FK_COLUMN_NAME         = c2.name,
  16291.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16292.         FK_COLUMN_PROPID    = convert(int,null),
  16293.         ORDINAL                = convert(int,14),
  16294.         UPDATE_RULE            = N'NO ACTION',
  16295.         DELETE_RULE         = N'NO ACTION',
  16296.         PK_NAME                = object_name(s.constid),
  16297.         FK_NAME                = object_name(r.constid),
  16298.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16299.     from
  16300.         sysobjects o1, sysobjects o2,
  16301.         syscolumns c1, syscolumns c2,
  16302.         sysreferences r, sysconstraints s
  16303.     where    
  16304.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16305.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16306.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16307.     and    o1.id = r.rkeyid
  16308.     and s.id = o1.id and (s.status & 0xf) = 1
  16309.     and    r.keycnt >= 14
  16310.     and    o1.id = c1.id
  16311.     and    c1.colid = r.rkey14
  16312.     and r.fkeyid = o2.id
  16313.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16314.     and    o2.id = c2.id    
  16315.     and    c2.colid = r.fkey14
  16316.     union all
  16317.     select
  16318.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16319.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16320.         PK_TABLE_NAME         = o1.name,
  16321.         PK_COLUMN_NAME         = c1.name,
  16322.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16323.         PK_COLUMN_PROPID    = convert(int,null),
  16324.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16325.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16326.         FK_TABLE_NAME         = o2.name,
  16327.         FK_COLUMN_NAME         = c2.name,
  16328.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16329.         FK_COLUMN_PROPID    = convert(int,null),
  16330.         ORDINAL                = convert(int,15),
  16331.         UPDATE_RULE            = N'NO ACTION',
  16332.         DELETE_RULE         = N'NO ACTION',
  16333.         PK_NAME                = object_name(s.constid),
  16334.         FK_NAME                = object_name(r.constid),
  16335.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16336.     from
  16337.         sysobjects o1, sysobjects o2,
  16338.         syscolumns c1, syscolumns c2,
  16339.         sysreferences r, sysconstraints s
  16340.     where    
  16341.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16342.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16343.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16344.     and    o1.id = r.rkeyid
  16345.     and s.id = o1.id and (s.status & 0xf) = 1
  16346.     and    r.keycnt >= 15
  16347.     and    o1.id = c1.id
  16348.     and    c1.colid = r.rkey15
  16349.     and r.fkeyid = o2.id
  16350.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16351.     and    o2.id = c2.id    
  16352.     and    c2.colid = r.fkey15
  16353.     union all
  16354.     select
  16355.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16356.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16357.         PK_TABLE_NAME         = o1.name,
  16358.         PK_COLUMN_NAME         = c1.name,
  16359.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16360.         PK_COLUMN_PROPID    = convert(int,null),
  16361.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16362.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16363.         FK_TABLE_NAME         = o2.name,
  16364.         FK_COLUMN_NAME         = c2.name,
  16365.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16366.         FK_COLUMN_PROPID    = convert(int,null),
  16367.         ORDINAL                = convert(int,16),
  16368.         UPDATE_RULE            = N'NO ACTION',
  16369.         DELETE_RULE         = N'NO ACTION',
  16370.         PK_NAME                = object_name(s.constid),
  16371.         FK_NAME                = object_name(r.constid),
  16372.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16373.     from
  16374.         sysobjects o1, sysobjects o2,
  16375.         syscolumns c1, syscolumns c2,
  16376.         sysreferences r, sysconstraints s
  16377.     where    
  16378.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16379.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16380.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16381.     and    o1.id = r.rkeyid
  16382.     and s.id = o1.id and (s.status & 0xf) = 1
  16383.     and    o1.id = c1.id
  16384.     and    r.keycnt >= 16
  16385.     and    c1.colid = r.rkey16
  16386.     and r.fkeyid = o2.id
  16387.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16388.     and    o2.id = c2.id    
  16389.     and    c2.colid = r.fkey16
  16390.     order by 8,9,2,3,13
  16391. go
  16392. dump tran master with no_log
  16393. go
  16394. create procedure sp_foreign_keys_rowset;5
  16395.     (
  16396.     @server_name        sysname,
  16397.     @pk_catalog_name    sysname = null,
  16398.     @fk_catalog_name    sysname = null,
  16399.     @pk_table_name        sysname = null,
  16400.     @pk_table_schema    sysname = null,
  16401.     @fk_table_name        sysname = null,
  16402.     @fk_table_schema    sysname = null
  16403.     )
  16404. as
  16405.     select
  16406.         PK_TABLE_CATALOG,
  16407.         PK_TABLE_SCHEMA,    
  16408.         PK_TABLE_NAME,     
  16409.         PK_COLUMN_NAME,     
  16410.         PK_COLUMN_GUID,    
  16411.         PK_COLUMN_PROPID,
  16412.         FK_TABLE_CATALOG,
  16413.         FK_TABLE_SCHEMA, 
  16414.         FK_TABLE_NAME,     
  16415.         FK_COLUMN_NAME,     
  16416.         FK_COLUMN_GUID,    
  16417.         FK_COLUMN_PROPID,
  16418.         ORDINAL,        
  16419.         UPDATE_RULE,    
  16420.         DELETE_RULE
  16421.         --PK_NAME,        
  16422.         --FK_NAME,
  16423.         --DEFERRABILITY    
  16424.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  16425.                 @server_name,
  16426.                 @pk_catalog_name,
  16427.                 @pk_table_schema,
  16428.                 @pk_table_name,
  16429.                 @fk_catalog_name,
  16430.                 @fk_table_schema,
  16431.                 @fk_table_name >
  16432.     order by 7,8,9,1,2,3,13
  16433. go
  16434.  
  16435. grant execute on sp_foreign_keys_rowset to public
  16436. go
  16437.  
  16438. dump tran master with no_log
  16439. go
  16440. if (charindex('6.00', @@version) > 0)
  16441.     begin
  16442.     if (exists (select * from sysobjects
  16443.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  16444.         begin
  16445.         drop procedure sp_foreign_keys_rowset
  16446.         dump tran master with no_log
  16447.         end
  16448.     end
  16449. go
  16450.  
  16451.  
  16452. print ''
  16453. print 'creating sp_indexes_rowset'
  16454. go
  16455.  
  16456. /*    6.0 and 6.5 version */
  16457. create procedure sp_indexes_rowset
  16458.     (
  16459.     @table_name    varchar(255), 
  16460.     @index_name    varchar(255) = null,
  16461.     @table_schema    varchar(255) = null     
  16462.     )
  16463. as
  16464.     select    TABLE_CATALOG        = db_name(),
  16465.         TABLE_SCHEMA        = user_name(o.uid),
  16466.         TABLE_NAME        = o.name,
  16467.         INDEX_CATALOG        = db_name(),        
  16468.         INDEX_SCHEMA        = user_name(o.uid),
  16469.         INDEX_NAME        = x.name,
  16470.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16471.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16472.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16473.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16474.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16475.         INITIAL_SIZE        = convert(int,null),
  16476.         NULLS            = convert(int,null),
  16477.         SORT_BOOKMARKS        = convert(bit,0),
  16478.         AUTO_UPDATE        = convert(bit,1),
  16479.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16480.         ORDINAL_POSITION    = convert(int,c.colid),
  16481.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16482.         COLUMN_GUID        = convert(binary(16),null),
  16483.         COLUMN_PROPID        = convert(int,null),
  16484.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16485.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16486.         PAGES            = convert(int, x.dpages),
  16487.         FILTER_CONDITION    = convert(varchar(1),null),
  16488.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  16489.         
  16490.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16491.     where    o.type in ('U')
  16492.     and     o.name = @table_name
  16493.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16494.     and    (@index_name is null or @index_name = x.name)
  16495.     and     x.id = o.id
  16496.     and     x.id = c.id
  16497.     and      c.colid < x.keycnt+(x.status&16)/16
  16498.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16499.     and     (
  16500.         suser_id() = 1     /* User is the System Administrator */
  16501.         or o.uid = user_id()     /* User created the object */
  16502.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16503.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16504.             from sysprotects p
  16505.             where p.id = o.id
  16506.             /* get rows for public,current user,user's group */
  16507.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16508.             /* check for SELECT,EXECUTE privilege */
  16509.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16510.             ) = 1    /* final magic...compare Grants      */
  16511.            )
  16512.     order by 8 desc, 4, 5, 6, 17
  16513. go
  16514. dump tran master with no_log
  16515. go
  16516. create procedure sp_indexes_rowset;2
  16517.     (
  16518.     @index_name    varchar(255) = null,
  16519.     @table_schema    varchar(255) = null     
  16520.     )
  16521. as
  16522.     select    TABLE_CATALOG        = db_name(),
  16523.         TABLE_SCHEMA        = user_name(o.uid),
  16524.         TABLE_NAME        = o.name,
  16525.         INDEX_CATALOG        = db_name(),        
  16526.         INDEX_SCHEMA        = user_name(o.uid),
  16527.         INDEX_NAME        = x.name,
  16528.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16529.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16530.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16531.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16532.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16533.         INITIAL_SIZE        = convert(int,null),
  16534.         NULLS            = convert(int,null),
  16535.         SORT_BOOKMARKS        = convert(bit,0),
  16536.         AUTO_UPDATE        = convert(bit,1),
  16537.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16538.         ORDINAL_POSITION    = convert(int,c.colid),
  16539.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16540.         COLUMN_GUID        = convert(binary(16),null),
  16541.         COLUMN_PROPID        = convert(int,null),
  16542.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16543.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16544.         PAGES            = convert(int, x.dpages),
  16545.         FILTER_CONDITION    = convert(varchar(1),null),
  16546.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  16547.         
  16548.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16549.     where    o.type in ('U')
  16550.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16551.     and    (@index_name is null or @index_name = x.name)
  16552.     and     x.id = o.id
  16553.     and     x.id = c.id
  16554.     and      c.colid < x.keycnt+(x.status&16)/16
  16555.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16556.     and     (
  16557.         suser_id() = 1     /* User is the System Administrator */
  16558.         or o.uid = user_id()     /* User created the object */
  16559.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16560.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16561.             from sysprotects p
  16562.             where p.id = o.id
  16563.             /* get rows for public,current user,user's group */
  16564.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16565.             /* check for SELECT,EXECUTE privilege */
  16566.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16567.             ) = 1    /* final magic...compare Grants      */
  16568.            )
  16569.     order by 8 desc, 4, 5, 6, 17
  16570. go
  16571. dump tran master with no_log
  16572. go
  16573.  
  16574.  
  16575. if (charindex('7.00', @@version) > 0)
  16576.     drop procedure sp_indexes_rowset
  16577. else
  16578. begin
  16579.     print ''
  16580.     print ''
  16581.     print 'Warning:'
  16582.     print 'you are installing the stored procedures '
  16583.     print 'on a pre 7.0 SQL Server.'
  16584.     print 'Ignore the following errors.'
  16585. end
  16586. go
  16587.  
  16588. /*     7.0 version */
  16589. create procedure sp_indexes_rowset
  16590.     (
  16591.     @table_name    sysname, 
  16592.     @index_name    sysname = null,
  16593.     @table_schema    sysname = null     
  16594.     )
  16595. as
  16596.     select    TABLE_CATALOG        = db_name(),
  16597.         TABLE_SCHEMA        = user_name(o.uid),
  16598.         TABLE_NAME        = o.name,
  16599.         INDEX_CATALOG        = db_name(),        
  16600.         INDEX_SCHEMA        = user_name(o.uid),
  16601.         INDEX_NAME        = x.name,
  16602.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16603.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16604.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16605.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16606.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16607.         INITIAL_SIZE        = convert(int,null),
  16608.         NULLS            = convert(int,null),
  16609.         SORT_BOOKMARKS        = convert(bit,0),
  16610.         AUTO_UPDATE        = convert(bit,1),
  16611.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16612.         ORDINAL_POSITION    = convert(int,c.colid),
  16613.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16614.         COLUMN_GUID        = convert(uniqueidentifier,null),
  16615.         COLUMN_PROPID        = convert(int,null),
  16616.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16617.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16618.         PAGES            = convert(int, x.dpages),
  16619.         FILTER_CONDITION    = convert(nvarchar(1),null),
  16620.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  16621.         
  16622.     from    sysobjects o, sysindexes x, syscolumns c
  16623.     where    o.type in ('U')
  16624.     and     o.name = @table_name
  16625.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16626.     and    (@index_name is null or @index_name = x.name)
  16627.     and     x.id = o.id
  16628.     and     x.id = c.id
  16629.     and      c.colid < x.keycnt+(x.status&16)/16
  16630.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  16631.     and     permissions(o.id) <> 0
  16632.     and     (x.status&32) = 0  -- No hypothetical indexes
  16633.     order by 8 desc, 4, 5, 6, 17
  16634. go
  16635. dump tran master with no_log
  16636. go
  16637. create procedure sp_indexes_rowset;2
  16638.     (
  16639.     @index_name    sysname = null,
  16640.     @table_schema    sysname = null     
  16641.     )
  16642. as
  16643.     select    TABLE_CATALOG        = db_name(),
  16644.         TABLE_SCHEMA        = user_name(o.uid),
  16645.         TABLE_NAME        = o.name,
  16646.         INDEX_CATALOG        = db_name(),        
  16647.         INDEX_SCHEMA        = user_name(o.uid),
  16648.         INDEX_NAME        = x.name,
  16649.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16650.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16651.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16652.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16653.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16654.         INITIAL_SIZE        = convert(int,null),
  16655.         NULLS            = convert(int,null),
  16656.         SORT_BOOKMARKS        = convert(bit,0),
  16657.         AUTO_UPDATE        = convert(bit,1),
  16658.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16659.         ORDINAL_POSITION    = convert(int,c.colid),
  16660.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16661.         COLUMN_GUID        = convert(uniqueidentifier,null),
  16662.         COLUMN_PROPID        = convert(int,null),
  16663.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16664.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16665.         PAGES            = convert(int, x.dpages),
  16666.         FILTER_CONDITION    = convert(nvarchar(1),null),
  16667.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  16668.         
  16669.     from    sysobjects o, sysindexes x, syscolumns c
  16670.     where    o.type in ('U')
  16671.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16672.     and    (@index_name is null or @index_name = x.name)
  16673.     and     x.id = o.id
  16674.     and     x.id = c.id
  16675.     and      c.colid < x.keycnt+(x.status&16)/16
  16676.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  16677.     and     permissions(o.id) <> 0
  16678.     and     (x.status&32) = 0  -- No hypothetical indexes
  16679.     order by 8 desc, 4, 5, 6, 17
  16680. go
  16681. dump tran master with no_log
  16682. go
  16683. create procedure sp_indexes_rowset;5
  16684.     (
  16685.     @table_server        sysname,
  16686.     @table_catalog        sysname = null,
  16687.     @table_name        sysname = null,
  16688.     @index_name        sysname = null,
  16689.     @table_schema        sysname = null
  16690.     )
  16691. as
  16692.     select
  16693.         TABLE_CATALOG,    
  16694.         TABLE_SCHEMA,    
  16695.         TABLE_NAME,    
  16696.         INDEX_CATALOG,        
  16697.         INDEX_SCHEMA,    
  16698.         INDEX_NAME,    
  16699.         PRIMARY_KEY,    
  16700.         "UNIQUE",    
  16701.         "CLUSTERED",    
  16702.         "TYPE",        
  16703.         FILL_FACTOR,    
  16704.         INITIAL_SIZE,    
  16705.         NULLS,        
  16706.         SORT_BOOKMARKS,    
  16707.         AUTO_UPDATE,    
  16708.         NULL_COLLATION,    
  16709.         ORDINAL_POSITION,
  16710.         COLUMN_NAME,    
  16711.         COLUMN_GUID,    
  16712.         COLUMN_PROPID,    
  16713.         COLLATION,    
  16714.         CARDINALITY,    
  16715.         PAGES,        
  16716.         FILTER_CONDITION
  16717.     --    INTEGRATED
  16718.     from master.dbo.SYSREMOTE_INDEXES <
  16719.                 @table_server,
  16720.                 @table_catalog,
  16721.                 @table_schema,
  16722.                         @index_name,
  16723.                 NULL,            /* TYPE (index type) */
  16724.                 @table_name >
  16725.     order by 8 desc, 4, 5, 6, 17
  16726. go
  16727.  
  16728. grant execute on sp_indexes_rowset to public
  16729. go
  16730.  
  16731. dump tran master with no_log
  16732. go
  16733. if (charindex('6.00', @@version) > 0)
  16734.     begin
  16735.     if (exists (select * from sysobjects
  16736.             where name = 'sp_indexes_rowset' and type = 'P '))
  16737.         begin
  16738.         drop procedure sp_indexes_rowset
  16739.         dump tran master with no_log
  16740.         end
  16741.     end
  16742. go
  16743.  
  16744.  
  16745. print ''
  16746. print 'creating sp_primary_keys_rowset'
  16747. go
  16748.  
  16749. /*    Procedure for 6.0 and 6.5 servers */
  16750. create procedure sp_primary_keys_rowset
  16751.     (
  16752.        @table_name        varchar(255),
  16753.     @table_schema         varchar(244) = null
  16754.     )
  16755. as
  16756.     select    TABLE_CATALOG    = db_name(),
  16757.             TABLE_SCHEMA    = user_name(o.uid),
  16758.             TABLE_NAME        = o.name,    
  16759.             COLUMN_NAME        = c.name,
  16760.             COLUMN_GUID        = convert(binary(16),null),
  16761.             COLUMN_PROPID    = convert(int,null),
  16762.             ORDINAL            = convert(int,c1.colid),
  16763.             PK_NAME            = i.name
  16764.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  16765.     where    o.type in ('U')
  16766.     and     o.name = @table_name
  16767.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16768.     and     o.id = c.id
  16769.     and     o.id = i.id
  16770.     and     (i.status & 0x800) = 0x800
  16771.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16772.     and     c1.id = c.id
  16773.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16774.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16775.     and     (
  16776.         suser_id() = 1     /* User is the System Administrator */
  16777.         or o.uid = user_id()     /* User created the object */
  16778.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16779.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16780.             from sysprotects p
  16781.             where p.id = o.id
  16782.             /* get rows for public,current user,user's group */
  16783.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16784.             /* check for SELECT,EXECUTE privilege */
  16785.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16786.             ) = 1    /* final magic...compare Grants      */
  16787.            )
  16788.     order by 2, 3
  16789. go
  16790. dump tran master with no_log
  16791. go
  16792. create procedure sp_primary_keys_rowset;2
  16793.     (
  16794.      @table_schema         varchar(244) = null
  16795.     )
  16796. as
  16797.     select    TABLE_CATALOG    = db_name(),
  16798.             TABLE_SCHEMA    = user_name(o.uid),
  16799.             TABLE_NAME        = o.name,    
  16800.             COLUMN_NAME        = c.name,
  16801.             COLUMN_GUID        = convert(binary(16),null),
  16802.             COLUMN_PROPID    = convert(int,null),
  16803.             ORDINAL            = convert(int,c1.colid),
  16804.             PK_NAME            = i.name
  16805.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  16806.     where    o.type in ('U')
  16807.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16808.     and     o.id = c.id
  16809.     and     o.id = i.id
  16810.     and     (i.status & 0x800) = 0x800
  16811.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16812.     and     c1.id = c.id
  16813.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16814.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16815.     and     (
  16816.         suser_id() = 1     /* User is the System Administrator */
  16817.         or o.uid = user_id()     /* User created the object */
  16818.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16819.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16820.             from sysprotects p
  16821.             where p.id = o.id
  16822.             /* get rows for public,current user,user's group */
  16823.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16824.             /* check for SELECT,EXECUTE privilege */
  16825.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16826.             ) = 1    /* final magic...compare Grants      */
  16827.            )
  16828.     order by 2, 3
  16829. go
  16830.  
  16831. if (charindex('7.00', @@version) = 0)
  16832. begin
  16833.     print ''
  16834.     print ''
  16835.     print 'Warning:'
  16836.     print 'you are installing the stored procedures '
  16837.     print 'on a pre 7.0 SQL Server.'
  16838.     print 'Ignore the following errors.'
  16839. end
  16840. else
  16841.     drop proc sp_primary_keys_rowset
  16842. go
  16843.  
  16844. /*    Procedure for 7.0 servers */
  16845. create procedure sp_primary_keys_rowset
  16846.     (
  16847.        @table_name        sysname,
  16848.     @table_schema         sysname = null
  16849.     )
  16850. as
  16851.     select    TABLE_CATALOG    = db_name(),
  16852.             TABLE_SCHEMA    = user_name(o.uid),
  16853.             TABLE_NAME        = o.name,    
  16854.             COLUMN_NAME        = c.name,
  16855.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16856.             COLUMN_PROPID    = convert(int,null),
  16857.             ORDINAL            = convert(int,c1.colid),
  16858.             PK_NAME            = i.name
  16859.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16860.     where    o.type in ('U')
  16861.     and     o.name = @table_name
  16862.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16863.     and     o.id = c.id
  16864.     and     o.id = i.id
  16865.     and     (i.status & 0x800) = 0x800
  16866.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16867.     and     c1.id = c.id
  16868.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16869.     and    permissions(o.id) <> 0
  16870.     order by 2, 3
  16871. go
  16872. dump tran master with no_log
  16873. go
  16874. create procedure sp_primary_keys_rowset;2
  16875.     (
  16876.      @table_schema     sysname = null
  16877.     )
  16878. as
  16879.     select    TABLE_CATALOG    = db_name(),
  16880.             TABLE_SCHEMA    = user_name(o.uid),
  16881.             TABLE_NAME        = o.name,    
  16882.             COLUMN_NAME        = c.name,
  16883.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16884.             COLUMN_PROPID    = convert(int,null),
  16885.             ORDINAL            = convert(int,c1.colid),
  16886.             PK_NAME            = i.name
  16887.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16888.     where    o.type in ('U')
  16889.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16890.     and     o.id = c.id
  16891.     and     o.id = i.id
  16892.     and     (i.status & 0x800) = 0x800
  16893.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16894.     and     c1.id = c.id
  16895.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16896.     and    permissions(o.id) <> 0
  16897.     order by 2, 3
  16898. go
  16899. dump tran master with no_log
  16900. go
  16901. create procedure sp_primary_keys_rowset;3
  16902.     (
  16903.        @table_name        sysname = null,
  16904.     @table_schema         sysname = null
  16905.     )
  16906. as
  16907. IF @table_name is not NULL
  16908.     BEGIN
  16909.     select    TABLE_CATALOG    = db_name(),
  16910.             TABLE_SCHEMA    = user_name(o.uid),
  16911.             TABLE_NAME        = o.name,    
  16912.             COLUMN_NAME        = c.name,
  16913.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16914.             COLUMN_PROPID    = convert(int,null),
  16915.             ORDINAL            = convert(int,c1.colid),
  16916.             PK_NAME            = i.name
  16917.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16918.     where    o.type in ('U')
  16919.     and     o.name = @table_name
  16920.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16921.     and     o.id = c.id
  16922.     and     o.id = i.id
  16923.     and     (i.status & 0x800) = 0x800
  16924.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16925.     and     c1.id = c.id
  16926.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16927.     and    permissions(o.id) <> 0
  16928.     END
  16929. ELSE
  16930.     BEGIN
  16931.     select    TABLE_CATALOG    = db_name(),
  16932.             TABLE_SCHEMA    = user_name(o.uid),
  16933.             TABLE_NAME        = o.name,    
  16934.             COLUMN_NAME        = c.name,
  16935.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16936.             COLUMN_PROPID    = convert(int,null),
  16937.             ORDINAL            = convert(int,c1.colid),
  16938.             PK_NAME            = i.name
  16939.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16940.     where    o.type in ('U')
  16941.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16942.     and     o.id = c.id
  16943.     and     o.id = i.id
  16944.     and     (i.status & 0x800) = 0x800
  16945.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16946.     and     c1.id = c.id
  16947.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16948.     and    permissions(o.id) <> 0
  16949.     END
  16950. go
  16951. dump tran master with no_log
  16952. go
  16953. create procedure sp_primary_keys_rowset;5
  16954.     (
  16955.     @table_server        sysname,
  16956.     @table_catalog        sysname = null,
  16957.     @table_name            sysname = null,
  16958.     @table_schema        sysname = null
  16959.     )
  16960. as
  16961.     select
  16962.         TABLE_CATALOG,    
  16963.         TABLE_SCHEMA,    
  16964.         TABLE_NAME,    
  16965.         COLUMN_NAME,    
  16966.         COLUMN_GUID,    
  16967.         COLUMN_PROPID,    
  16968.         ORDINAL
  16969.         --PK_NAME        
  16970.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  16971.                 @table_server,
  16972.                 @table_catalog,
  16973.                 @table_schema,
  16974.                 @table_name >
  16975.     order by 1,2,3
  16976. go
  16977.  
  16978. grant execute on sp_primary_keys_rowset to public
  16979. go
  16980.  
  16981. dump tran master with no_log
  16982. go
  16983. if (charindex('6.00', @@version) > 0)
  16984.     begin
  16985.     if (exists (select * from sysobjects
  16986.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  16987.         begin
  16988.         drop procedure sp_primary_keys_rowset
  16989.         dump tran master with no_log
  16990.         end
  16991.     end
  16992. go
  16993.  
  16994.  
  16995. print ''
  16996. print 'creating sp_provider_types_rowset'
  16997. go
  16998.  
  16999. /*    Procedure for 6.0 and 6.50 servers */
  17000. create proc sp_provider_types_rowset
  17001.     (
  17002.     @data_type     smallint = null,
  17003.     @best_match    tinyint  = null
  17004.     )
  17005. as
  17006.     select
  17007.         TYPE_NAME         = case when t.usertype = 80 then t.name 
  17008.                         else d.type_name 
  17009.                         end,
  17010.         DATA_TYPE         = d.oledb_data_type,                            
  17011.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17012.                             then @@max_precision
  17013.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  17014.         LITERAL_PREFIX         = d.literal_prefix,
  17015.         LITERAL_SUFFIX         = d.literal_suffix,
  17016.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  17017.         IS_NULLABLE        = t.allownulls,
  17018.         CASE_SENSITIVE        = d.case_sensitive,
  17019.         SEARCHABLE         = d.searchable,
  17020.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17021.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17022.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17023.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  17024.                         else d.local_type_name
  17025.                         end,
  17026.         MINIMUM_SCALE        = convert(smallint,
  17027.                         case 
  17028.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17029.                         else null 
  17030.                         end),
  17031.         MAXIMUM_SCALE        = convert(smallint,
  17032.                         case 
  17033.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17034.                         then @@max_precision
  17035.                         else null 
  17036.                         end),
  17037.         GUID            = convert(binary(16),null),
  17038.         TYPELIB            = convert(varchar(1),null),
  17039.         VERSION            = convert(varchar(1),null),
  17040.         IS_LONG            = d.is_long,
  17041.         BEST_MATCH        = case when t.usertype = 80
  17042.                         then convert(bit,0)
  17043.                         else d.best_match
  17044.                         end,
  17045.         IS_FIXEDLENGTH        = convert(bit, 
  17046.                         case when d.fixlen is null then 0 else 1 end)
  17047.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  17048.     where    d.ss_dtype = t.type 
  17049.     and    t.usertype <= 100
  17050.     and    t.usertype <> 18 /* sysname */
  17051.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  17052.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  17053.     and     t.usertype *= e.user_type 
  17054.     and     e.AUTO_INCREMENT = 0
  17055.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  17056.     and    (@data_type is null or d.oledb_data_type = @data_type)
  17057.     and    (@best_match is null or d.best_match = @best_match)
  17058.     order by 2
  17059. go
  17060. dump tran master with no_log
  17061. go
  17062.  
  17063. if (charindex('7.00', @@version) = 0)
  17064. begin
  17065.     print ''
  17066.     print ''
  17067.     print 'Warning:'
  17068.     print 'you are installing the stored procedures '
  17069.     print 'on a pre 7.0 SQL Server.'
  17070.     print 'Ignore the following errors.'
  17071. end
  17072. else
  17073.     drop proc sp_provider_types_rowset
  17074. go
  17075.  
  17076. /*    Procedure for 7.0 server */
  17077. create proc sp_provider_types_rowset
  17078.     (
  17079.     @data_type     smallint = null,
  17080.     @best_match    tinyint  = null
  17081.     )
  17082. as
  17083.     select
  17084.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  17085.                         else d.type_name 
  17086.                         end,
  17087.         DATA_TYPE         = d.oledb_data_type,                            
  17088.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17089.                             then @@max_precision
  17090.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  17091.         LITERAL_PREFIX         = d.literal_prefix,
  17092.         LITERAL_SUFFIX         = d.literal_suffix,
  17093.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  17094.         IS_NULLABLE        = t.allownulls,
  17095.         CASE_SENSITIVE        = d.case_sensitive,
  17096.         SEARCHABLE         = d.searchable,
  17097.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17098.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17099.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17100.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  17101.                         else d.local_type_name
  17102.                         end,
  17103.         MINIMUM_SCALE        = convert(smallint,
  17104.                         case 
  17105.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17106.                         else null 
  17107.                         end),
  17108.         MAXIMUM_SCALE        = convert(smallint,
  17109.                         case 
  17110.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17111.                         then @@max_precision
  17112.                         else null 
  17113.                         end),
  17114.         GUID            = convert(uniqueidentifier,null),
  17115.         TYPELIB            = convert(nvarchar(1),null),
  17116.         VERSION            = convert(nvarchar(1),null),
  17117.         IS_LONG            = d.is_long,
  17118.         BEST_MATCH        = case    when t.usertype = 80
  17119.                         then convert(bit,0)
  17120.                         else d.best_match
  17121.                         end,
  17122.         IS_FIXEDLENGTH        = convert(bit, 
  17123.                         case when d.fixlen is null then 0 else 1 end)
  17124.  
  17125.     from master.dbo.spt_provider_types d
  17126.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  17127.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  17128.             t.xusertype = e.user_type
  17129.             and e.AUTO_INCREMENT = 0
  17130.     where    (@data_type is null or d.oledb_data_type = @data_type)
  17131.     and    (@best_match is null or d.best_match = @best_match)    
  17132.     and    t.usertype <= 255
  17133.     and    t.usertype <> 18 /* sysname */
  17134.     order by 2
  17135. go
  17136.  
  17137. grant execute on sp_provider_types_rowset to public
  17138. go
  17139.  
  17140. dump tran master with no_log
  17141. go
  17142. if (charindex('6.00', @@version) > 0)
  17143.     begin
  17144.     if (exists (select * from sysobjects
  17145.         where name = 'sp_provider_types_rowset' and type = 'P '))
  17146.         begin
  17147.         drop procedure sp_provider_types_rowset
  17148.         dump tran master with no_log
  17149.         end
  17150.     end
  17151. go
  17152.  
  17153.  
  17154. print ''
  17155. print 'creating sp_procedure_params_rowset'
  17156. go
  17157.  
  17158. /*    Procedure for 6.0 and 6.50 servers */
  17159. create procedure sp_procedure_params_rowset
  17160.     (
  17161.        @procedure_name        varchar(255) = null,
  17162.     @group_number        int = null,
  17163.     @procedure_schema     varchar(255) = null,
  17164.     @parameter_name        varchar(255) = null
  17165.     )
  17166. as
  17167. IF @procedure_name is not null
  17168.     BEGIN
  17169.     select
  17170.         PROCEDURE_CATALOG     = db_name(),
  17171.         PROCEDURE_SCHEMA     = user_name(),
  17172.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17173.         PARAMETER_NAME         = c.name,
  17174.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17175.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17176.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17177.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17178.         IS_NULLABLE        = convert(bit,
  17179.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17180.                         then 0 else 1
  17181.                         end),
  17182.         DATA_TYPE        = d.oledb_data_type,
  17183.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17184.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17185.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17186.                         then coalesce(d.column_size,c.length)
  17187.                         else null 
  17188.                         end),
  17189.         CHARACTER_OCTET_LENGTH    = convert(int,
  17190.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17191.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17192.                         then coalesce(d.column_size,c.length)
  17193.                         else null 
  17194.                         end),
  17195.         NUMERIC_PRECISION    = convert(smallint,
  17196.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  17197.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17198.         NUMERIC_SCALE        = convert(smallint, 
  17199.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17200.         DESCRIPTION        = convert(varchar(1),null),
  17201.         TYPE_NAME        = d.type_name,
  17202.         LOCAL_TYPE_NAME        = d.local_type_name
  17203.     
  17204.     from
  17205.         syscolumns c,
  17206.         sysobjects o,
  17207.         master.dbo.spt_provider_types d,
  17208.         systypes t
  17209.     where
  17210.         o.name = @procedure_name
  17211.     and    o.type = 'P'                            /* Just Procedures */
  17212.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17213.     and    o.id = c.id
  17214.     and    c.number = @group_number
  17215.     and    c.type = d.ss_dtype
  17216.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17217.     and    c.usertype = t.usertype
  17218.     and    (t.usertype != 80 or d.type_name='timestamp')
  17219.     and    (@parameter_name is null or @parameter_name = c.name)
  17220.     UNION ALL
  17221.     SELECT           /* return value row*/
  17222.         PROCEDURE_CATALOG     = db_name(),
  17223.         PROCEDURE_SCHEMA     = user_name(),
  17224.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17225.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17226.         ORDINAL_POSITION     = convert(smallint,0),
  17227.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17228.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17229.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17230.         IS_NULLABLE        = convert(bit,0),
  17231.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17232.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17233.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17234.         NUMERIC_PRECISION    = convert(smallint,10),
  17235.         NUMERIC_SCALE        = convert(smallint,null),
  17236.         DESCRIPTION        = convert(varchar(1),null),
  17237.         TYPE_NAME        = convert(sysname,'int'),
  17238.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17239.     from
  17240.         syscomments c,
  17241.         sysobjects o
  17242.     where
  17243.         o.name = @procedure_name
  17244.     and    o.type = 'P'                        /* Just Procedures */
  17245.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17246.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17247.     and    c.id = o.id
  17248.     and    c.number = @group_number
  17249.     and c.colid = 1
  17250.     order by 2, 3, 5
  17251.     END
  17252. ELSE
  17253.     BEGIN
  17254.     select
  17255.         PROCEDURE_CATALOG     = db_name(),
  17256.         PROCEDURE_SCHEMA     = user_name(),
  17257.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17258.         PARAMETER_NAME         = c.name,
  17259.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17260.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17261.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17262.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17263.         IS_NULLABLE        = convert(bit,
  17264.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17265.                         then 0 else 1
  17266.                         end),
  17267.         DATA_TYPE        = d.oledb_data_type,
  17268.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17269.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17270.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17271.                         then coalesce(d.column_size,c.length)
  17272.                         else null 
  17273.                         end),
  17274.         CHARACTER_OCTET_LENGTH    = convert(int,
  17275.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17276.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17277.                         then coalesce(d.column_size,c.length)
  17278.                         else null 
  17279.                         end),
  17280.         NUMERIC_PRECISION    = convert(smallint,
  17281.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17282.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17283.         NUMERIC_SCALE        = convert(smallint, 
  17284.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17285.         DESCRIPTION        = convert(varchar(1),null),
  17286.         TYPE_NAME        = d.type_name,
  17287.         LOCAL_TYPE_NAME        = d.local_type_name
  17288.     
  17289.     from
  17290.         syscolumns c,
  17291.         sysobjects o,
  17292.         master.dbo.spt_provider_types d,
  17293.         systypes t
  17294.     where
  17295.         o.type = 'P'                            /* Just Procedures */
  17296.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17297.     and    o.id = c.id
  17298.     and    c.type = d.ss_dtype
  17299.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17300.     and    c.usertype = t.usertype
  17301.     and    (t.usertype != 80 or d.type_name='timestamp')
  17302.     and    (@parameter_name is null or @parameter_name = c.name)
  17303.     UNION ALL
  17304.     SELECT           /* return value row*/
  17305.         PROCEDURE_CATALOG     = db_name(),
  17306.         PROCEDURE_SCHEMA     = user_name(),
  17307.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17308.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17309.         ORDINAL_POSITION     = convert(smallint,0),
  17310.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17311.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17312.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17313.         IS_NULLABLE        = convert(bit,0),
  17314.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17315.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17316.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17317.         NUMERIC_PRECISION    = convert(smallint,10),
  17318.         NUMERIC_SCALE        = convert(smallint,null),
  17319.         DESCRIPTION        = convert(varchar(1),null),
  17320.         TYPE_NAME        = convert(sysname,'int'),
  17321.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17322.     from
  17323.         syscomments c,
  17324.         sysobjects o
  17325.     where
  17326.         o.type = 'P'                        /* Just Procedures */
  17327.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17328.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17329.     and    c.id = o.id
  17330.     and     c.colid = 1
  17331.     order by 2, 3, 5
  17332.     END
  17333. go
  17334. dump tran master with no_log
  17335. go
  17336. create procedure sp_procedure_params_rowset;2
  17337.     (
  17338.       @handle            int output,
  17339.        @scrollopt        int output,
  17340.     @ccopt            int output,
  17341.     @rows            int output,
  17342.        @procedure_name        varchar(255) = null,
  17343.     @group_number        int = null,
  17344.     @procedure_schema     varchar(255) = null,
  17345.     @parameter_name        varchar(255) = null
  17346.     )
  17347. as
  17348.     declare @ret int
  17349.  
  17350.  
  17351. SET NOCOUNT ON
  17352.  
  17353.     create table #spprocparamrowset1
  17354.         (
  17355.         PROCEDURE_CATALOG     sysname not null,
  17356.         PROCEDURE_SCHEMA     sysname not null,
  17357.         PROCEDURE_NAME         varchar(35) not null,
  17358.         PARAMETER_NAME         sysname not null,
  17359.         ORDINAL_POSITION     smallint not null,
  17360.         PARAMETER_TYPE         smallint null,
  17361.         PARAMETER_HASDEFAULT    tinyint null,
  17362.         PARAMETER_DEFAULT    varchar(255) null,
  17363.         IS_NULLABLE        bit not null,
  17364.         DATA_TYPE        smallint null,
  17365.         CHARACTER_MAXIMUM_LENGTH int null,
  17366.         CHARACTER_OCTET_LENGTH    int null,
  17367.         NUMERIC_PRECISION    smallint null,
  17368.         NUMERIC_SCALE        smallint null,
  17369.         DESCRIPTION        varchar(1) null,
  17370.         TYPE_NAME        sysname null,
  17371.         LOCAL_TYPE_NAME        sysname null,
  17372.         )
  17373.  
  17374. IF @procedure_name is not null
  17375.     BEGIN
  17376.     insert into #spprocparamrowset1
  17377.     select
  17378.         PROCEDURE_CATALOG     = db_name(),
  17379.         PROCEDURE_SCHEMA     = user_name(),
  17380.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17381.         PARAMETER_NAME         = c.name,
  17382.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17383.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17384.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17385.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17386.         IS_NULLABLE        = convert(bit,
  17387.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17388.                         then 0 else 1
  17389.                         end),
  17390.         DATA_TYPE        = d.oledb_data_type,
  17391.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17392.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17393.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17394.                         then coalesce(d.column_size,c.length)
  17395.                         else null 
  17396.                         end),
  17397.         CHARACTER_OCTET_LENGTH    = convert(int,
  17398.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17399.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17400.                         then coalesce(d.column_size,c.length)
  17401.                         else null 
  17402.                         end),
  17403.         NUMERIC_PRECISION    = convert(smallint,
  17404.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17405.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17406.         NUMERIC_SCALE        = convert(smallint, 
  17407.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17408.         DESCRIPTION        = convert(varchar(1),null),
  17409.         TYPE_NAME        = d.type_name,
  17410.         LOCAL_TYPE_NAME        = d.local_type_name
  17411.     
  17412.     from
  17413.         syscolumns c,
  17414.         sysobjects o,
  17415.         master.dbo.spt_provider_types d,
  17416.         systypes t
  17417.     where
  17418.         o.name = @procedure_name
  17419.     and    o.type = 'P'                            /* Just Procedures */
  17420.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17421.     and    o.id = c.id
  17422.     and    c.number = @group_number
  17423.     and    c.type = d.ss_dtype
  17424.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17425.     and    c.usertype = t.usertype
  17426.     and    (t.usertype != 80 or d.type_name='timestamp')
  17427.     and    (@parameter_name is null or @parameter_name = c.name)
  17428.     UNION ALL
  17429.     SELECT           /* return value row*/
  17430.         PROCEDURE_CATALOG     = db_name(),
  17431.         PROCEDURE_SCHEMA     = user_name(),
  17432.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17433.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17434.         ORDINAL_POSITION     = convert(smallint,0),
  17435.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17436.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17437.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17438.         IS_NULLABLE        = convert(bit,0),
  17439.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17440.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17441.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17442.         NUMERIC_PRECISION    = convert(smallint,10),
  17443.         NUMERIC_SCALE        = convert(smallint,null),
  17444.         DESCRIPTION        = convert(varchar(1),null),
  17445.         TYPE_NAME        = convert(sysname,'int'),
  17446.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17447.     from
  17448.         syscomments c,
  17449.         sysobjects o
  17450.     where
  17451.         o.name = @procedure_name
  17452.     and    o.type = 'P'                        /* Just Procedures */
  17453.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17454.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17455.     and    c.id = o.id
  17456.     and    c.number = @group_number
  17457.     and c.colid = 1
  17458.     order by 2, 3, 5
  17459.     END
  17460. ELSE
  17461.     BEGIN
  17462.     insert into #spprocparamrowset1
  17463.     select
  17464.         PROCEDURE_CATALOG     = db_name(),
  17465.         PROCEDURE_SCHEMA     = user_name(),
  17466.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17467.         PARAMETER_NAME         = c.name,
  17468.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17469.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17470.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17471.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17472.         IS_NULLABLE        = convert(bit,
  17473.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17474.                         then 0 else 1
  17475.                         end),
  17476.         DATA_TYPE        = d.oledb_data_type,
  17477.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17478.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17479.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17480.                         then coalesce(d.column_size,c.length)
  17481.                         else null 
  17482.                         end),
  17483.         CHARACTER_OCTET_LENGTH    = convert(int,
  17484.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17485.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17486.                         then coalesce(d.column_size,c.length)
  17487.                         else null 
  17488.                         end),
  17489.         NUMERIC_PRECISION    = convert(smallint,
  17490.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17491.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17492.         NUMERIC_SCALE        = convert(smallint, 
  17493.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17494.         DESCRIPTION        = convert(varchar(1),null),
  17495.         TYPE_NAME        = d.type_name,
  17496.         LOCAL_TYPE_NAME        = d.local_type_name
  17497.     
  17498.     from
  17499.         syscolumns c,
  17500.         sysobjects o,
  17501.         master.dbo.spt_provider_types d,
  17502.         systypes t
  17503.     where
  17504.         o.type = 'P'                            /* Just Procedures */
  17505.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17506.     and    o.id = c.id
  17507.     and    c.type = d.ss_dtype
  17508.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17509.     and    c.usertype = t.usertype
  17510.     and    (t.usertype != 80 or d.type_name='timestamp')
  17511.     and    (@parameter_name is null or @parameter_name = c.name)
  17512.     UNION ALL
  17513.     SELECT           /* return value row*/
  17514.         PROCEDURE_CATALOG     = db_name(),
  17515.         PROCEDURE_SCHEMA     = user_name(),
  17516.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17517.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17518.         ORDINAL_POSITION     = convert(smallint,0),
  17519.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17520.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17521.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17522.         IS_NULLABLE        = convert(bit,0),
  17523.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17524.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17525.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17526.         NUMERIC_PRECISION    = convert(smallint,10),
  17527.         NUMERIC_SCALE        = convert(smallint,null),
  17528.         DESCRIPTION        = convert(varchar(1),null),
  17529.         TYPE_NAME        = convert(sysname,'int'),
  17530.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17531.     from
  17532.         syscomments c,
  17533.         sysobjects o
  17534.     where
  17535.         o.type = 'P'                        /* Just Procedures */
  17536.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17537.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17538.     and    c.id = o.id
  17539.     and     c.colid = 1
  17540.     order by 2, 3, 5
  17541.     END
  17542.  
  17543.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  17544.         @scrollopt output, @ccopt output, @rows output
  17545.  
  17546.     drop table #spprocparamrowset1
  17547.        return isnull(@ret,0)
  17548. go
  17549. dump tran master with no_log
  17550. go
  17551. create procedure sp_procedure_params_rowset;3
  17552. as
  17553.     select
  17554.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  17555.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  17556.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  17557.         PARAMETER_NAME         = convert(sysname, ' '),
  17558.         ORDINAL_POSITION     = convert(smallint, 0),
  17559.         PARAMETER_TYPE         = convert(smallint, 0),
  17560.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17561.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17562.         IS_NULLABLE        = convert(bit, 0),
  17563.         DATA_TYPE        = convert(smallint, 0),
  17564.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  17565.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  17566.         NUMERIC_PRECISION    = convert(smallint, 0),
  17567.         NUMERIC_SCALE        = convert(smallint, 0),
  17568.         DESCRIPTION        = convert(varchar(1),null),
  17569.         TYPE_NAME        = convert(sysname,null),
  17570.         LOCAL_TYPE_NAME        = convert(sysname,null)
  17571.     where    1=0
  17572. go
  17573. dump tran master with no_log
  17574. go
  17575.  
  17576.  
  17577. if (charindex('7.00', @@version) > 0)
  17578.     drop procedure sp_procedure_params_rowset
  17579. else
  17580. begin
  17581.     print ''
  17582.     print ''
  17583.     print 'Warning:'
  17584.     print 'you are installing the stored procedures '
  17585.     print 'on a pre 7.0 SQL Server.'
  17586.     print 'Ignore the following errors.'
  17587. end
  17588. go
  17589.  
  17590. /*    Procedure for 7.0 servers */
  17591. create procedure sp_procedure_params_rowset
  17592.     (
  17593.        @procedure_name        sysname,
  17594.     @group_number        int = 1,
  17595.     @procedure_schema     sysname = null,
  17596.     @parameter_name        sysname = null
  17597.     )
  17598. as
  17599.     select
  17600.         PROCEDURE_CATALOG     = db_name(),
  17601.         PROCEDURE_SCHEMA     = user_name(),
  17602.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17603.         PARAMETER_NAME         = c.name,
  17604.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17605.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  17606.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17607.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17608.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  17609.         DATA_TYPE        = d.oledb_data_type,
  17610.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17611.                         case 
  17612.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17613.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17614.                         then coalesce(d.column_size,c.length)
  17615.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17616.                         then coalesce(d.column_size,c.length/2)
  17617.                         else null 
  17618.                         end),
  17619.         CHARACTER_OCTET_LENGTH    = convert(int,
  17620.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17621.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17622.                         then coalesce(d.column_size,c.length)
  17623.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17624.                         then coalesce(d.column_size*2,c.length)
  17625.                         else null 
  17626.                         end),
  17627.         NUMERIC_PRECISION    = convert(smallint,
  17628.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17629.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17630.         NUMERIC_SCALE        = convert(smallint, 
  17631.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17632.         DESCRIPTION        = convert(nvarchar(1),null),
  17633.         TYPE_NAME        = d.type_name,
  17634.         LOCAL_TYPE_NAME        = d.local_type_name
  17635.     
  17636.     from
  17637.         sysobjects o,
  17638.         syscolumns c,
  17639.         master.dbo.spt_provider_types d,
  17640.         systypes t
  17641.     where
  17642.         o.name = @procedure_name
  17643.     and    o.type = 'P'                            /* Just Procedures */
  17644.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17645.     and    o.id = c.id
  17646.     and    c.number = @group_number
  17647.     and    c.xtype = d.ss_dtype
  17648.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17649.     and    c.xusertype = t.xusertype
  17650.     and    (@parameter_name is null or @parameter_name = c.name)
  17651.     UNION ALL
  17652.     SELECT           /* return value row*/
  17653.         PROCEDURE_CATALOG     = db_name(),
  17654.         PROCEDURE_SCHEMA     = user_name(),
  17655.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17656.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17657.         ORDINAL_POSITION     = convert(smallint,0),
  17658.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17659.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17660.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17661.         IS_NULLABLE        = convert(bit,0),
  17662.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17663.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17664.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17665.         NUMERIC_PRECISION    = convert(smallint,10),
  17666.         NUMERIC_SCALE        = convert(smallint,null),
  17667.         DESCRIPTION        = convert(nvarchar(1),null),
  17668.         TYPE_NAME        = convert(sysname,N'int'),
  17669.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  17670.     from
  17671.         sysobjects o,
  17672.         syscomments c
  17673.     where
  17674.         o.name = @procedure_name
  17675.     and    o.id = c.id 
  17676.     and    c.number = @group_number
  17677.     and    c.colid = 1
  17678.     and    o.type = 'P'                        /* Just Procedures */
  17679.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17680.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17681.     order by 2, 3, 5
  17682. go
  17683. dump tran master with no_log
  17684. go
  17685. create procedure sp_procedure_params_rowset;2
  17686.     (
  17687.     @procedure_schema     sysname = null,
  17688.     @parameter_name        sysname = null
  17689.     )
  17690. as
  17691.     select
  17692.         PROCEDURE_CATALOG     = db_name(),
  17693.         PROCEDURE_SCHEMA     = user_name(),
  17694.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17695.         PARAMETER_NAME         = c.name,
  17696.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17697.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  17698.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17699.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17700.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  17701.         DATA_TYPE        = d.oledb_data_type,
  17702.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17703.                         case 
  17704.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17705.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17706.                         then coalesce(d.column_size,c.length)
  17707.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17708.                         then coalesce(d.column_size,c.length/2)
  17709.                         else null 
  17710.                         end),
  17711.         CHARACTER_OCTET_LENGTH    = convert(int,
  17712.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17713.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17714.                         then coalesce(d.column_size,c.length)
  17715.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17716.                         then coalesce(d.column_size*2,c.length)
  17717.                         else null 
  17718.                         end),
  17719.         NUMERIC_PRECISION    = convert(smallint,
  17720.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17721.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17722.         NUMERIC_SCALE        = convert(smallint, 
  17723.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17724.         DESCRIPTION        = convert(nvarchar(1),null),
  17725.         TYPE_NAME        = d.type_name,
  17726.         LOCAL_TYPE_NAME        = d.local_type_name
  17727.     
  17728.     from
  17729.         sysobjects o,
  17730.         syscolumns c,
  17731.         master.dbo.spt_provider_types d,
  17732.         systypes t
  17733.     where
  17734.         o.type = 'P'                            /* Just Procedures */
  17735.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17736.     and    o.id = c.id
  17737.     and    c.xtype = d.ss_dtype
  17738.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17739.     and    c.xusertype = t.xusertype
  17740.     and    (@parameter_name is null or @parameter_name = c.name)
  17741.     UNION ALL
  17742.     SELECT           /* return value row*/
  17743.         PROCEDURE_CATALOG     = db_name(),
  17744.         PROCEDURE_SCHEMA     = user_name(),
  17745.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17746.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17747.         ORDINAL_POSITION     = convert(smallint,0),
  17748.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17749.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17750.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17751.         IS_NULLABLE        = convert(bit,0),
  17752.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17753.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17754.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17755.         NUMERIC_PRECISION    = convert(smallint,10),
  17756.         NUMERIC_SCALE        = convert(smallint,null),
  17757.         DESCRIPTION        = convert(nvarchar(1),null),
  17758.         TYPE_NAME        = convert(sysname,N'int'),
  17759.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  17760.     from
  17761.         sysobjects o,
  17762.         syscomments c
  17763.     where
  17764.         o.type = 'P'        /* Just Procedures */
  17765.     and    o.id = c.id 
  17766.     and    c.colid = 1
  17767.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17768.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17769.     order by 2, 3, 5
  17770. go
  17771.  
  17772.  
  17773. grant execute on sp_procedure_params_rowset to public
  17774. go
  17775.  
  17776. dump tran master with no_log
  17777. go
  17778. if (charindex('6.00', @@version) > 0)
  17779.     begin
  17780.     if (exists (select * from sysobjects
  17781.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  17782.         begin
  17783.         drop procedure sp_procedure_params_rowset
  17784.         dump tran master with no_log
  17785.         end
  17786.     end
  17787. go
  17788.  
  17789.  
  17790. print ''
  17791. print 'creating sp_procedures_rowset'
  17792. go
  17793.  
  17794.  
  17795. /* pre 7.00 version */
  17796. create procedure sp_procedures_rowset
  17797.     (
  17798.     @procedure_name        varchar(255), 
  17799.     @group_number        int = 1,
  17800.     @procedure_schema    varchar(255) = null
  17801.     )        
  17802. as
  17803.     select
  17804.         PROCEDURE_CATALOG    = db_name(),
  17805.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17806.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  17807.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17808.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  17809.         DESCRIPTION        = convert(varchar(1),null),
  17810.         DATE_CREATED        = o.crdate,
  17811.         DATE_MODIFIED        = convert(datetime,null)
  17812.     from     
  17813.         sysobjects o, 
  17814.         syscomments p,
  17815.         sysusers u
  17816.     where
  17817.             o.name = @procedure_name
  17818.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17819.     and     o.type = 'P'        /* Object type of Procedure */
  17820.     and     p.colid = 1
  17821.     and     p.id = o.id
  17822.     and    p.number = @group_number
  17823.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17824.     and     (    suser_id() = 1     /* User is the System Administrator */
  17825.         or     o.uid = user_id()    /* User created the object */
  17826.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17827.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17828.              from sysprotects p
  17829.              where p.id = o.id
  17830.                  /*  get rows for public,current user,user's group */
  17831.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17832.                  /* check for SELECT,EXECUTE privilege */
  17833.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17834.             ) = 1     /* final magic...compare Grants    */
  17835.         )
  17836.     order by 2, 3
  17837. go
  17838. dump tran master with no_log
  17839. go
  17840. create procedure sp_procedures_rowset;2
  17841.     (
  17842.     @procedure_schema    varchar(255) = null
  17843.     )        
  17844. as
  17845.     select
  17846.         PROCEDURE_CATALOG    = db_name(),
  17847.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17848.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  17849.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17850.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  17851.         DESCRIPTION        = convert(varchar(1),null),
  17852.         DATE_CREATED        = o.crdate,
  17853.         DATE_MODIFIED        = convert(datetime,null)
  17854.     from     
  17855.         sysobjects o, 
  17856.         syscomments p,
  17857.         sysusers u
  17858.     where
  17859.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17860.     and     o.type = 'P'        /* Object type of Procedure */
  17861.     and     p.colid = 1
  17862.     and     p.id = o.id
  17863.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17864.     and     (    suser_id() = 1     /* User is the System Administrator */
  17865.         or     o.uid = user_id()    /* User created the object */
  17866.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17867.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17868.              from sysprotects p
  17869.              where p.id = o.id
  17870.                  /*  get rows for public,current user,user's group */
  17871.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17872.                  /* check for SELECT,EXECUTE privilege */
  17873.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17874.             ) = 1     /* final magic...compare Grants    */
  17875.         )
  17876.     order by 2, 3
  17877. go
  17878. dump tran master with no_log
  17879. go
  17880.  
  17881. if (charindex('7.00', @@version) > 0)
  17882.     drop procedure sp_procedures_rowset
  17883. else
  17884. begin
  17885.     print ''
  17886.     print ''
  17887.     print 'Warning:'
  17888.     print 'you are installing the stored procedures '
  17889.     print 'on a pre 7.0 SQL Server.'
  17890.     print 'Ignore the following errors.'
  17891. end
  17892. go
  17893.  
  17894. /* 7.00 version */
  17895. create procedure sp_procedures_rowset
  17896.     (
  17897.     @procedure_name        sysname, 
  17898.     @group_number        int = 1,
  17899.     @procedure_schema    sysname = null
  17900.     )        
  17901. as
  17902.     select
  17903.         PROCEDURE_CATALOG    = db_name(),
  17904.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17905.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  17906.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17907.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  17908.         DESCRIPTION        = convert(nvarchar(1),null),
  17909.         DATE_CREATED        = o.crdate,
  17910.         DATE_MODIFIED        = convert(datetime,null)
  17911.     from     
  17912.         sysobjects o, 
  17913.         syscomments p
  17914.     where
  17915.         permissions(o.id) <> 0
  17916.     and    o.name = @procedure_name
  17917.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17918.     and     o.type = 'P'        /* Object type of Procedure */
  17919.     and     p.colid = 1
  17920.     and     p.id = o.id
  17921.     and    p.number = @group_number
  17922.     order by 2, 3
  17923. go
  17924. dump tran master with no_log
  17925. go
  17926. create procedure sp_procedures_rowset;2
  17927.     (
  17928.     @procedure_schema    sysname = null
  17929.     )        
  17930. as
  17931.     select
  17932.         PROCEDURE_CATALOG    = db_name(),
  17933.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17934.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  17935.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17936.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  17937.         DESCRIPTION        = convert(nvarchar(1),null),
  17938.         DATE_CREATED        = o.crdate,
  17939.         DATE_MODIFIED        = convert(datetime,null)
  17940.     from     sysobjects o, syscomments p
  17941.     where
  17942.         permissions(o.id) <> 0
  17943.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17944.     and     o.type = 'P'        /* Object type of Procedure */
  17945.     and p.colid = 1
  17946.     and p.id = o.id
  17947.     order by 2, 3
  17948. go
  17949.  
  17950. grant execute on sp_procedures_rowset to public
  17951. go
  17952.  
  17953. dump tran master with no_log
  17954. go
  17955. if (charindex('6.00', @@version) > 0)
  17956.     begin
  17957.     if (exists (select * from sysobjects
  17958.             where name = 'sp_procedures_rowset' and type = 'P '))
  17959.         begin
  17960.         drop procedure sp_procedures_rowset
  17961.         dump tran master with no_log
  17962.         end
  17963.     end
  17964. go
  17965.  
  17966.  
  17967. print ''
  17968. print 'creating sp_schemata_rowset'
  17969. go
  17970.  
  17971. /*    6.0 and 6.5 version */
  17972. create procedure sp_schemata_rowset
  17973.     (
  17974.     @schema_name    varchar(90) = null, 
  17975.     @schema_owner    varchar(90) = null
  17976.     )        
  17977. as
  17978.     select    distinct
  17979.         CATALOG_NAME            = db_name(),
  17980.         SCHEMA_NAME            = user_name(o.uid),    
  17981.         SCHEMA_OWNER            = user_name(o.uid),    
  17982.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  17983.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  17984.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  17985.     from    
  17986.         sysobjects o,
  17987.         master.dbo.sysconfigures    cfg,
  17988.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  17989.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  17990.     where   
  17991.         (@schema_name is null or @schema_name = user_name(o.uid))
  17992.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  17993.     and    a_cha.type = 1001 /* type is charset */
  17994.     and     b_cha.type = 2001 /* type is sortorder */
  17995.     and     a_cha.id = b_cha.csid
  17996.     and     b_cha.id = cfg.value
  17997.     order by 2
  17998. go
  17999. dump tran master with no_log
  18000. go
  18001.  
  18002. if (charindex('7.00', @@version) > 0)
  18003.     drop procedure sp_schemata_rowset
  18004. else
  18005. begin
  18006.     print ''
  18007.     print ''
  18008.     print 'Warning:'
  18009.     print 'you are installing the stored procedures '
  18010.     print 'on a pre 7.0 SQL Server.'
  18011.     print 'Ignore the following errors.'
  18012. end
  18013. go
  18014.  
  18015. /* 7.00 version */
  18016. create procedure sp_schemata_rowset
  18017.     (
  18018.     @schema_name    sysname = null, 
  18019.     @schema_owner    sysname = null
  18020.     )        
  18021. as
  18022.     select    distinct
  18023.         CATALOG_NAME            = db_name(),
  18024.         SCHEMA_NAME            = user_name(o.uid),    
  18025.         SCHEMA_OWNER            = user_name(o.uid),    
  18026.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  18027.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  18028.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  18029.     from    
  18030.         sysobjects o,
  18031.         master.dbo.sysconfigures    cfg,
  18032.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  18033.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  18034.     where   
  18035.         (@schema_name is null or @schema_name = user_name(o.uid))
  18036.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  18037.     and    a_cha.type = 1001 /* type is charset */
  18038.     and     b_cha.type = 2001 /* type is sortorder */
  18039.     and     a_cha.id = b_cha.csid
  18040.     and     b_cha.id = cfg.value
  18041.     order by 2
  18042. go
  18043. dump tran master with no_log
  18044. go
  18045. go
  18046. /* The following stored procedure is used for Sphinx and Hydra */
  18047. create procedure sp_schemata_rowset;3
  18048. as
  18049.     select
  18050.         CATALOG_NAME            = convert(sysname,' '),
  18051.         SCHEMA_NAME            = convert(sysname,' '),    
  18052.         SCHEMA_OWNER            = convert(sysname,' '),    
  18053.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  18054.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  18055.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  18056.     where    1=0
  18057. go
  18058. grant execute on sp_schemata_rowset to public
  18059. go
  18060.  
  18061. dump tran master with no_log
  18062. go
  18063. if (charindex('6.00', @@version) > 0)
  18064.     begin
  18065.     if (exists (select * from sysobjects
  18066.             where name = 'sp_schemata_rowset' and type = 'P '))
  18067.         begin
  18068.         drop procedure sp_schemata_rowset
  18069.         dump tran master with no_log
  18070.         end
  18071.     end
  18072. go
  18073.  
  18074.  
  18075. print ''
  18076. print 'creating sp_statistics_rowset'
  18077. go
  18078.  
  18079.  
  18080. /*    6.0 and 6.5 version */
  18081. create procedure sp_statistics_rowset
  18082.     (
  18083.     @table_name    varchar(255),
  18084.     @table_schema    varchar(255) = null     
  18085.     )
  18086. as
  18087.     select    db_name()                as TABLE_CATALOG,        
  18088.         user_name(o.uid)            as TABLE_SCHEMA,
  18089.         o.name                    as TABLE_NAME,
  18090.         x.rows                    as CARDINALITY
  18091.     from    sysobjects o, sysindexes x, sysusers u
  18092.     where    o.type in ('U')
  18093.     and     o.name = @table_name
  18094.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18095.     and     x.id = o.id
  18096.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18097.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18098.     and     (
  18099.         suser_id() = 1     /* User is the System Administrator */
  18100.         or o.uid = user_id()     /* User created the object */
  18101.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18102.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18103.             from sysprotects p
  18104.             where p.id = o.id
  18105.             /* get rows for public,current user,user's group */
  18106.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18107.             /* check for SELECT,EXECUTE privilege */
  18108.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18109.             ) = 1    /* final magic...compare Grants      */
  18110.            )
  18111.     order by 2, 3
  18112. go
  18113. dump tran master with no_log
  18114. go
  18115. create procedure sp_statistics_rowset;2
  18116.     (
  18117.     @table_schema    varchar(255) = null     
  18118.     )
  18119. as
  18120.     select    db_name()                as TABLE_CATALOG,        
  18121.         user_name(o.uid)            as TABLE_SCHEMA,
  18122.         o.name                    as TABLE_NAME,
  18123.         x.rows                    as CARDINALITY
  18124.     from    sysobjects o, sysindexes x, sysusers u
  18125.     where    o.type in ('U')
  18126.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18127.     and     x.id = o.id
  18128.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18129.     and     (
  18130.         suser_id() = 1     /* User is the System Administrator */
  18131.         or o.uid = user_id()     /* User created the object */
  18132.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18133.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18134.             from sysprotects p
  18135.             where p.id = o.id
  18136.             /* get rows for public,current user,user's group */
  18137.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18138.             /* check for SELECT,EXECUTE privilege */
  18139.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18140.             ) = 1    /* final magic...compare Grants      */
  18141.            )        
  18142.     order by 2, 3
  18143. go
  18144. dump tran master with no_log
  18145. go
  18146.  
  18147. if (charindex('7.00', @@version) > 0)
  18148.     drop procedure sp_statistics_rowset
  18149. else
  18150. begin
  18151.     print ''
  18152.     print ''
  18153.     print 'Warning:'
  18154.     print 'you are installing the stored procedures '
  18155.     print 'on a pre 7.0 SQL Server.'
  18156.     print 'Ignore the following errors.'
  18157. end
  18158. go
  18159.  
  18160. /*     7.0 version */
  18161. create procedure sp_statistics_rowset
  18162.     (
  18163.     @table_name    sysname,
  18164.     @table_schema    sysname = null     
  18165.     )
  18166. as
  18167.     select    db_name()                as TABLE_CATALOG,        
  18168.         user_name(o.uid)            as TABLE_SCHEMA,
  18169.         o.name                    as TABLE_NAME,
  18170.         x.rows                    as CARDINALITY
  18171.     from    sysobjects o, sysindexes x
  18172.     where    o.type in ('U')
  18173.     and     o.name = @table_name
  18174.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18175.     and     x.id = o.id
  18176.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18177.     and     permissions(o.id) <> 0
  18178.     order by 2, 3
  18179. go
  18180. dump tran master with no_log
  18181. go
  18182. create procedure sp_statistics_rowset;2
  18183.     (
  18184.     @table_schema    sysname = null     
  18185.     )
  18186. as
  18187.     select    db_name()                as TABLE_CATALOG,        
  18188.         user_name(o.uid)            as TABLE_SCHEMA,
  18189.         o.name                    as TABLE_NAME,
  18190.         x.rows                    as CARDINALITY
  18191.     from    sysobjects o, sysindexes x
  18192.     where    o.type in ('U')
  18193.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18194.     and     x.id = o.id
  18195.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18196.     and     permissions(o.id) <> 0
  18197.     order by 2, 3
  18198. go
  18199.  
  18200. grant execute on sp_statistics_rowset to public
  18201. go
  18202.  
  18203. dump tran master with no_log
  18204. go
  18205. if (charindex('6.00', @@version) > 0)
  18206.     begin
  18207.     if (exists (select * from sysobjects
  18208.             where name = 'sp_statistics_rowset' and type = 'P '))
  18209.         begin
  18210.         drop procedure sp_statistics_rowset
  18211.         dump tran master with no_log
  18212.         end
  18213.     end
  18214. go
  18215.  
  18216.  
  18217. print ''
  18218. print 'creating sp_tables_rowset'
  18219. go
  18220.  
  18221.  
  18222. /*    Procedure for 6.50 and earlier servers */
  18223. create procedure sp_tables_rowset
  18224.     (
  18225.     @table_name    varchar(255), 
  18226.     @table_schema    varchar(255) = null,    
  18227.     @table_type    varchar(255) = null 
  18228.     )
  18229. as
  18230.     select    TABLE_CATALOG    = db_name(),
  18231.         TABLE_SCHEMA    = user_name(o.uid),
  18232.         TABLE_NAME    = o.name,
  18233.         TABLE_TYPE    = convert(varchar(30),
  18234.                     case o.type 
  18235.                     when 'U' then 'TABLE'
  18236.                     when 'V' then 'VIEW'
  18237.                     when 'S' then 'SYSTEM TABLE'
  18238.                     end),
  18239.         TABLE_GUID    = convert(binary(16), null),
  18240.         DESCRIPTION    = convert(varchar(1), null),
  18241.         TABLE_PROPID    = convert(int,null),
  18242.         DATE_CREATED    = o.crdate,
  18243.         DATE_MODIFIED    = convert(datetime,null)
  18244.     from    sysusers u, sysobjects o
  18245.     where    o.type in ('U','V','S')
  18246.     and     o.name = @table_name
  18247.     and     (    @table_schema is null
  18248.         or    @table_schema = user_name(o.uid)
  18249.         )
  18250.     and     (
  18251.             @table_type is null
  18252.         or    @table_type = case o.type 
  18253.                     when 'U' then 'TABLE'
  18254.                     when 'V' then 'VIEW'
  18255.                     when 'S' then 'SYSTEM TABLE'
  18256.                     end
  18257.         )
  18258.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18259.     and     (
  18260.         suser_id() = 1     /* User is the System Administrator */
  18261.         or o.uid = user_id()     /* User created the object */
  18262.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18263.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18264.             from sysprotects p
  18265.             /* join to correlate with all rows in sysobjects */
  18266.             where p.id = o.id
  18267.             /* get rows for public,current user,user's group */
  18268.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18269.             /* check for SELECT,EXECUTE privilege */
  18270.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18271.             ) = 1    /* final magic...compare Grants      */
  18272.         )        
  18273.     order by 4, 2, 3
  18274. go
  18275. dump tran master with no_log
  18276. go
  18277. create procedure sp_tables_rowset;2
  18278.     (
  18279.     @table_schema    varchar(255) = null,    
  18280.     @table_type    varchar(255) = null 
  18281.     )
  18282. as
  18283.     select    TABLE_CATALOG    = db_name(),
  18284.         TABLE_SCHEMA    = user_name(o.uid),
  18285.         TABLE_NAME    = o.name,
  18286.         TABLE_TYPE    = convert(varchar(30),
  18287.                     case o.type 
  18288.                     when 'U' then 'TABLE'
  18289.                     when 'V' then 'VIEW'
  18290.                     when 'S' then 'SYSTEM TABLE'
  18291.                     end),
  18292.         TABLE_GUID    = convert(binary(16), null),
  18293.         DESCRIPTION    = convert(varchar(1), null),
  18294.         TABLE_PROPID    = convert(int,null),
  18295.         DATE_CREATED    = o.crdate,
  18296.         DATE_MODIFIED    = convert(datetime,null)
  18297.     from    sysusers u, sysobjects o
  18298.     where    o.type in ('U','V','S')
  18299.     and     (    @table_schema is null
  18300.         or    @table_schema = user_name(o.uid)
  18301.         )
  18302.     and     (
  18303.             @table_type is null
  18304.         or    @table_type = case o.type 
  18305.                     when 'U' then 'TABLE'
  18306.                     when 'V' then 'VIEW'
  18307.                     when 'S' then 'SYSTEM TABLE'
  18308.                     end
  18309.         )
  18310.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18311.     and     (
  18312.         suser_id() = 1     /* User is the System Administrator */
  18313.         or o.uid = user_id()     /* User created the object */
  18314.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18315.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18316.             from sysprotects p
  18317.             /* join to correlate with all rows in sysobjects */
  18318.             where p.id = o.id
  18319.             /* get rows for public,current user,user's group */
  18320.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18321.             /* check for SELECT,EXECUTE privilege */
  18322.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18323.             ) = 1    /* final magic...compare Grants      */
  18324.         )        
  18325.     order by 4, 2, 3
  18326. go
  18327. dump tran master with no_log
  18328. go
  18329.  
  18330. if (charindex('7.00', @@version) > 0)
  18331.     drop procedure sp_tables_rowset
  18332. else
  18333. begin
  18334.     print ''
  18335.     print ''
  18336.     print 'Warning:'
  18337.     print 'you are installing the stored procedures '
  18338.     print 'on a pre 7.0 SQL Server.'
  18339.     print 'Ignore the following errors.'
  18340. end
  18341. go
  18342.  
  18343. /*    Procedure for 7.00 server */
  18344. create procedure sp_tables_rowset
  18345.     (
  18346.     @table_name    sysname, 
  18347.     @table_schema    sysname = null,    
  18348.     @table_type    nvarchar(255) = null 
  18349.     )
  18350. as
  18351.     select    *
  18352.     from    (select    TABLE_CATALOG    = db_name(),
  18353.             TABLE_SCHEMA    = user_name(uid),
  18354.             TABLE_NAME    = name,
  18355.             TABLE_TYPE    = convert(nvarchar(30),
  18356.                         case type 
  18357.                         when 'U' then 
  18358.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18359.                             then N'TABLE' else N'SYSTEM TABLE' end
  18360.                         when 'S' then N'SYSTEM TABLE'
  18361.                         when 'V' then 
  18362.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18363.                             then N'VIEW' else N'SYSTEM VIEW' end
  18364.                         end),
  18365.             TABLE_GUID    = convert(uniqueidentifier, null),
  18366.             DESCRIPTION    = convert(nvarchar(1), null),
  18367.             TABLE_PROPID    = convert(int,null),
  18368.             DATE_CREATED    = crdate,
  18369.             DATE_MODIFIED    = convert(datetime,null)
  18370.         from    sysobjects
  18371.         where    name = @table_name
  18372.         and    type in ('U','V','S')
  18373.         and    permissions(id) <> 0
  18374.         ) as o
  18375.  
  18376.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18377.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  18378.     order by 4, 2, 3
  18379. go
  18380. dump tran master with no_log
  18381. go
  18382. create procedure sp_tables_rowset;2
  18383.     (
  18384.     @table_schema    sysname = null,    
  18385.     @table_type    nvarchar(255) = null 
  18386.     )
  18387. as
  18388.     select    *
  18389.     from    (select    TABLE_CATALOG    = db_name(),
  18390.             TABLE_SCHEMA    = user_name(uid),
  18391.             TABLE_NAME    = name,
  18392.             TABLE_TYPE    = convert(nvarchar(30),
  18393.                         case type 
  18394.                         when 'U' then 
  18395.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18396.                             then N'TABLE' else N'SYSTEM TABLE' end
  18397.                         when 'S' then N'SYSTEM TABLE'
  18398.                         when 'V' then 
  18399.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18400.                             then N'VIEW' else N'SYSTEM VIEW' end
  18401.                         end),
  18402.             TABLE_GUID    = convert(uniqueidentifier, null),
  18403.             DESCRIPTION    = convert(nvarchar(1), null),
  18404.             TABLE_PROPID    = convert(int,null),
  18405.             DATE_CREATED    = crdate,
  18406.             DATE_MODIFIED    = convert(datetime,null)
  18407.         from    sysobjects
  18408.         where    type in ('U','V','S')
  18409.         and    permissions(id) <> 0
  18410.         ) as o
  18411.  
  18412.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18413.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  18414.     order by 4, 2, 3
  18415. go
  18416. dump tran master with no_log
  18417. go
  18418. create procedure sp_tables_rowset;5
  18419.     (
  18420.     @table_server        sysname,
  18421.     @table_catalog        sysname = null,
  18422.     @table_name        sysname = null,
  18423.     @table_schema        sysname = null,
  18424.     @table_type        sysname = null
  18425.     )
  18426. as
  18427.     select
  18428.         TABLE_CATALOG,
  18429.         TABLE_SCHEMA,
  18430.         TABLE_NAME,
  18431.         TABLE_TYPE,
  18432.         TABLE_GUID,
  18433.         DESCRIPTION
  18434.     --    TABLE_PROPID,
  18435.     --    DATE_CREATED,
  18436.     --    DATE_MODIFIED
  18437.     from master.dbo.SYSREMOTE_TABLES <
  18438.                 @table_server,
  18439.                 @table_catalog,
  18440.                 @table_schema,
  18441.                 @table_name,
  18442.                 @table_type >
  18443.     order by 4,1,2,3
  18444. go
  18445.  
  18446. grant execute on sp_tables_rowset to public
  18447. go
  18448.  
  18449. dump tran master with no_log
  18450. go
  18451. if (charindex('6.00', @@version) > 0)
  18452.     begin
  18453.     if (exists (select * from sysobjects
  18454.             where name = 'sp_tables_rowset' and type = 'P '))
  18455.         begin
  18456.         drop procedure sp_tables_rowset
  18457.         dump tran master with no_log
  18458.         end
  18459.     end
  18460. go
  18461.  
  18462.  
  18463.  
  18464. print ''
  18465. print 'creating sp_tables_info_rowset'
  18466. go
  18467.  
  18468.  
  18469. /*    Procedure for 6.50 and earlier servers */
  18470. create procedure sp_tables_info_rowset
  18471.     (
  18472.     @table_name    varchar(255), 
  18473.     @table_schema    varchar(255) = null,    
  18474.     @table_type    varchar(255) = null 
  18475.     )
  18476. as
  18477.     select    TABLE_CATALOG        = db_name(),
  18478.         TABLE_SCHEMA        = user_name(o.uid),
  18479.         TABLE_NAME        = o.name,
  18480.         TABLE_TYPE        = convert(varchar(30),
  18481.                         case o.type 
  18482.                         when 'U' then 'TABLE'
  18483.                         when 'V' then 'VIEW'
  18484.                         when 'S' then 'SYSTEM TABLE'
  18485.                         end),
  18486.         TABLE_GUID        = convert(binary(16), null),
  18487.         BOOKMARKS        = convert(bit, 1),
  18488.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18489.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18490.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18491.         BOOKMARK_INFORMATION    = convert(int, 0),
  18492.         TABLE_VERSION        = convert(int, o.schema_ver),
  18493.         CARDINALITY        = x.rows,
  18494.         DESCRIPTION        = convert(varchar(1), null),
  18495.         TABLE_PROPID        = convert(int, null)
  18496.  
  18497.     from    sysusers u, 
  18498.         sysobjects o,
  18499.         sysindexes x
  18500.  
  18501.     where    o.type in ('U','V','S')
  18502.     and     o.name = @table_name
  18503.     and     (    @table_schema is null
  18504.         or    @table_schema = user_name(o.uid)
  18505.         )
  18506.     and     (
  18507.             @table_type is null
  18508.         or    @table_type = case o.type 
  18509.                     when 'U' then 'TABLE'
  18510.                     when 'V' then 'VIEW'
  18511.                     when 'S' then 'SYSTEM TABLE'
  18512.                     end
  18513.         )
  18514.     and    o.id *= x.id
  18515.     and    x.indid in (0,1)
  18516.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18517.     and     (
  18518.         suser_id() = 1     /* User is the System Administrator */
  18519.         or o.uid = user_id()     /* User created the object */
  18520.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18521.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18522.             from sysprotects p
  18523.             /* join to correlate with all rows in sysobjects */
  18524.             where p.id = o.id
  18525.             /* get rows for public,current user,user's group */
  18526.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18527.             /* check for SELECT,EXECUTE privilege */
  18528.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18529.             ) = 1    /* final magic...compare Grants      */
  18530.         )        
  18531.     order by 4, 2, 3
  18532. go
  18533. dump tran master with no_log
  18534. go
  18535. create procedure sp_tables_info_rowset;2
  18536.     (
  18537.     @table_schema    varchar(255) = null,    
  18538.     @table_type    varchar(255) = null 
  18539.     )
  18540. as
  18541.     select    TABLE_CATALOG        = db_name(),
  18542.         TABLE_SCHEMA        = user_name(o.uid),
  18543.         TABLE_NAME        = o.name,
  18544.         TABLE_TYPE        = convert(varchar(30),
  18545.                         case o.type 
  18546.                         when 'U' then 'TABLE'
  18547.                         when 'V' then 'VIEW'
  18548.                         when 'S' then 'SYSTEM TABLE'
  18549.                         end),
  18550.         TABLE_GUID        = convert(binary(16), null),
  18551.         BOOKMARKS        = convert(bit, 1),
  18552.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18553.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18554.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18555.         BOOKMARK_INFORMATION    = convert(int, 0),
  18556.         TABLE_VERSION        = convert(int, o.schema_ver),
  18557.         CARDINALITY        = x.rows,
  18558.         DESCRIPTION        = convert(varchar(1), null),
  18559.         TABLE_PROPID        = convert(int, null)
  18560.  
  18561.     from    sysusers u, 
  18562.         sysobjects o,
  18563.         sysindexes x
  18564.  
  18565.     where    o.type in ('U','V','S')
  18566.     and     (    @table_schema is null
  18567.         or    @table_schema = user_name(o.uid)
  18568.         )
  18569.     and     (
  18570.             @table_type is null
  18571.         or    @table_type = case o.type 
  18572.                     when 'U' then 'TABLE'
  18573.                     when 'V' then 'VIEW'
  18574.                     when 'S' then 'SYSTEM TABLE'
  18575.                     end
  18576.         )
  18577.     and    o.id *= x.id
  18578.     and    x.indid in (0,1)
  18579.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18580.     and     (
  18581.         suser_id() = 1     /* User is the System Administrator */
  18582.         or o.uid = user_id()     /* User created the object */
  18583.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18584.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18585.             from sysprotects p
  18586.             /* join to correlate with all rows in sysobjects */
  18587.             where p.id = o.id
  18588.             /* get rows for public,current user,user's group */
  18589.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18590.             /* check for SELECT,EXECUTE privilege */
  18591.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18592.             ) = 1    /* final magic...compare Grants      */
  18593.         )        
  18594.     order by 4, 2, 3
  18595. go
  18596. dump tran master with no_log
  18597. go
  18598.  
  18599. if (charindex('7.00', @@version) > 0)
  18600.     drop procedure sp_tables_info_rowset
  18601. else
  18602. begin
  18603.     print ''
  18604.     print ''
  18605.     print 'Warning:'
  18606.     print 'you are installing the stored procedures '
  18607.     print 'on a pre 7.0 SQL Server.'
  18608.     print 'Ignore the following errors.'
  18609. end
  18610. go
  18611.  
  18612. /*    Procedure for 7.00 server */
  18613. create procedure sp_tables_info_rowset
  18614.     (
  18615.     @table_name    sysname, 
  18616.     @table_schema    sysname = null,    
  18617.     @table_type    nvarchar(255) = null 
  18618.     )
  18619. as
  18620.     select    *
  18621.     from    (select    TABLE_CATALOG        = db_name(),
  18622.             TABLE_SCHEMA        = user_name(o.uid),
  18623.             TABLE_NAME        = o.name,
  18624.             TABLE_TYPE        = convert(nvarchar(30),
  18625.                             case o.type 
  18626.                             when 'U' then 
  18627.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18628.                                 then N'TABLE' else N'SYSTEM TABLE' end
  18629.                             when 'S' then N'SYSTEM TABLE'
  18630.                             when 'V' then 
  18631.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18632.                                 then N'VIEW' else N'SYSTEM VIEW' end
  18633.                             end),
  18634.             TABLE_GUID        = convert(uniqueidentifier, null),
  18635.             BOOKMARKS        = convert(bit, 1),
  18636.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18637.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18638.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18639.             BOOKMARK_INFORMATION    = convert(int, 0),
  18640.             TABLE_VERSION        = convert(int, o.schema_ver),
  18641.             CARDINALITY        = x.rows,
  18642.             DESCRIPTION        = convert(nvarchar(1), null),
  18643.             TABLE_PROPID        = convert(int, null) 
  18644.     
  18645.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  18646.         where    o.name = @table_name
  18647.         and     o.type in ('U','V','S')
  18648.         and    permissions(o.id) <> 0) as t
  18649.  
  18650.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18651.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  18652.     order by 4, 2, 3
  18653. go
  18654. dump tran master with no_log
  18655. go
  18656. create procedure sp_tables_info_rowset;2
  18657.     (
  18658.     @table_schema    sysname = null,    
  18659.     @table_type    nvarchar(255) = null 
  18660.     )
  18661. as
  18662.     select    *
  18663.     from    (select    TABLE_CATALOG        = db_name(),
  18664.             TABLE_SCHEMA        = user_name(o.uid),
  18665.             TABLE_NAME        = o.name,
  18666.             TABLE_TYPE        = convert(nvarchar(30),
  18667.                             case o.type 
  18668.                             when 'U' then 
  18669.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18670.                                 then N'TABLE' else N'SYSTEM TABLE' end
  18671.                             when 'S' then N'SYSTEM TABLE'
  18672.                             when 'V' then 
  18673.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18674.                                 then N'VIEW' else N'SYSTEM VIEW' end
  18675.                             end),
  18676.             TABLE_GUID        = convert(uniqueidentifier, null),
  18677.             BOOKMARKS        = convert(bit, 1),
  18678.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18679.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18680.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18681.             BOOKMARK_INFORMATION    = convert(int, 0),
  18682.             TABLE_VERSION        = convert(int, o.schema_ver),
  18683.             CARDINALITY        = x.rows,
  18684.             DESCRIPTION        = convert(nvarchar(1), null),
  18685.             TABLE_PROPID        = convert(int, null) 
  18686.     
  18687.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  18688.         where     o.type in ('U','V','S')
  18689.         and    permissions(o.id) <> 0) as t
  18690.  
  18691.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18692.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  18693.     order by 4, 2, 3
  18694. go
  18695.  
  18696. grant execute on sp_tables_info_rowset to public
  18697. go
  18698.  
  18699. dump tran master with no_log
  18700. go
  18701. if (charindex('6.00', @@version) > 0)
  18702.     begin
  18703.     if (exists (select * from sysobjects
  18704.         where name = 'sp_tables_info_rowset' and type = 'P '))
  18705.         begin
  18706.         drop procedure sp_tables_info_rowset
  18707.         dump tran master with no_log
  18708.         end
  18709.     end
  18710. go
  18711.  
  18712. print ''
  18713. print 'creating sp_table_constraints_rowset'
  18714. go
  18715.  
  18716.  
  18717. /*    Procedure for 6.50 and earlier servers */
  18718. create procedure sp_table_constraints_rowset
  18719.     (
  18720.     @table_name            varchar(255), 
  18721.     @table_schema        varchar(255) = null,
  18722.     @table_catalog        varchar(255) = null,
  18723.     @constraint_name    varchar(255) = null,
  18724.     @constraint_schema    varchar(255) = null,
  18725.     @constraint_catalog    varchar(255) = null,
  18726.     @constraint_type    varchar(255) = null 
  18727.     )
  18728. as
  18729.     select
  18730.         CONSTRAINT_CATALOG    = db_name(),
  18731.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18732.         CONSTRAINT_NAME        = c_obj.name,
  18733.         TABLE_CATALOG        = db_name(),
  18734.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18735.         TABLE_NAME            = t_obj.name,
  18736.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18737.                                 when 1 then 'PRIMARY KEY'
  18738.                                 when 2 then    'UNIQUE'
  18739.                                 when 3 then    'FOREIGN KEY'
  18740.                                 when 4 then    'CHECK'
  18741.                                  end, 
  18742.         IS_DEFERRABLE        = convert(bit, 0),
  18743.         INITIALLY_DEFERRED    = convert(bit, 0),
  18744.         DESCRIPTION            = convert(varchar(1), null)
  18745.  
  18746.     from 
  18747.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18748.     where
  18749.             t_obj.name    = @table_name
  18750.         and t_obj.type in ('U','S')
  18751.         and (@table_catalog is null or @table_catalog = db_name())
  18752.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18753.         and c.id = t_obj.id
  18754.         and (c.status & 0xf) between 1 and 4
  18755.         and c_obj.id    = c.constid
  18756.         and    c_obj.uid    = user_id()
  18757.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18758.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18759.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18760.         and (@constraint_type is null
  18761.             or (c.status & 0xf)
  18762.                 = case @constraint_type
  18763.                     when 'PRIMARY KEY' then 1
  18764.                     when 'UNIQUE' then 2
  18765.                     when 'FOREIGN KEY' then 3
  18766.                     when 'CHECK' then 4
  18767.                     end)
  18768. order by 2,3,5,6,7 
  18769. go
  18770. dump tran master with no_log
  18771. go
  18772. create procedure sp_table_constraints_rowset;2
  18773.     (
  18774.     @table_schema        varchar(255) = null,
  18775.     @table_catalog        varchar(255) = null,
  18776.     @constraint_name    varchar(255) = null,
  18777.     @constraint_schema    varchar(255) = null,
  18778.     @constraint_catalog    varchar(255) = null,
  18779.     @constraint_type    varchar(255) = null 
  18780.     )
  18781. as
  18782.     select
  18783.         CONSTRAINT_CATALOG    = db_name(),
  18784.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18785.         CONSTRAINT_NAME        = c_obj.name,
  18786.         TABLE_CATALOG        = db_name(),
  18787.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18788.         TABLE_NAME            = t_obj.name,
  18789.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18790.                                 when 1 then 'PRIMARY KEY'
  18791.                                 when 2 then    'UNIQUE'
  18792.                                 when 3 then    'FOREIGN KEY'
  18793.                                 when 4 then    'CHECK'
  18794.                                  end, 
  18795.         IS_DEFERRABLE        = convert(bit, 0),
  18796.         INITIALLY_DEFERRED    = convert(bit, 0),
  18797.         DESCRIPTION            = convert(varchar(1), null)
  18798.  
  18799.     from 
  18800.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18801.     where
  18802.             t_obj.type in ('U','S')
  18803.         and (@table_catalog is null or @table_catalog = db_name())
  18804.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18805.         and c.id = t_obj.id
  18806.         and (c.status & 0xf) between 1 and 4
  18807.         and c_obj.id    = c.constid
  18808.         and    c_obj.uid    = user_id()
  18809.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18810.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18811.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18812.         and (@constraint_type is null
  18813.             or (c.status & 0xf)
  18814.                 = case @constraint_type
  18815.                     when 'PRIMARY KEY' then 1
  18816.                     when 'UNIQUE' then 2
  18817.                     when 'FOREIGN KEY' then 3
  18818.                     when 'CHECK' then 4
  18819.                     end)
  18820. order by 2,3,5,6,7 
  18821. go
  18822. dump tran master with no_log
  18823. go
  18824.  
  18825.  
  18826. if (charindex('7.00', @@version) > 0)
  18827.     drop procedure sp_table_constraints_rowset
  18828. else
  18829. begin
  18830.     print ''
  18831.     print ''
  18832.     print 'Warning:'
  18833.     print 'you are installing the stored procedures '
  18834.     print 'on a pre 7.0 SQL Server.'
  18835.     print 'Ignore the following errors.'
  18836. end
  18837. go
  18838.  
  18839. /*    Procedure for 7.00 server */
  18840. create procedure sp_table_constraints_rowset
  18841.     (
  18842.     @table_name            sysname, 
  18843.     @table_schema        sysname = null,
  18844.     @table_catalog        sysname = null,
  18845.     @constraint_name    sysname = null,
  18846.     @constraint_schema    sysname = null,
  18847.     @constraint_catalog    sysname = null,
  18848.     @constraint_type    nvarchar(255) = null 
  18849.     )
  18850. as
  18851.     select
  18852.         CONSTRAINT_CATALOG    = db_name(),
  18853.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18854.         CONSTRAINT_NAME        = c_obj.name,
  18855.         TABLE_CATALOG        = db_name(),
  18856.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18857.         TABLE_NAME            = t_obj.name,
  18858.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18859.                                 when 1 then N'PRIMARY KEY'
  18860.                                 when 2 then    N'UNIQUE'
  18861.                                 when 3 then    N'FOREIGN KEY'
  18862.                                 when 4 then    N'CHECK'
  18863.                                  end, 
  18864.         IS_DEFERRABLE        = convert(bit, 0),
  18865.         INITIALLY_DEFERRED    = convert(bit, 0),
  18866.         DESCRIPTION            = convert(nvarchar(1), null)
  18867.  
  18868.     from 
  18869.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18870.     where
  18871.             t_obj.name    = @table_name
  18872.         and t_obj.type in ('U','S')
  18873.         and (@table_catalog is null or @table_catalog = db_name())
  18874.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18875.         and c.id = t_obj.id
  18876.         and (c.status & 0xf) between 1 and 4
  18877.         and c_obj.id    = c.constid
  18878.         and    c_obj.uid    = user_id()
  18879.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18880.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18881.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18882.         and (@constraint_type is null
  18883.             or (c.status & 0xf)
  18884.                 = case @constraint_type
  18885.                     when N'PRIMARY KEY' then 1
  18886.                     when N'UNIQUE' then 2
  18887.                     when N'FOREIGN KEY' then 3
  18888.                     when N'CHECK' then 4
  18889.                     end)
  18890. order by 2,3,5,6,7 
  18891. go
  18892. dump tran master with no_log
  18893. go
  18894. create procedure sp_table_constraints_rowset;2
  18895.     (
  18896.     @table_schema        sysname = null,
  18897.     @table_catalog        sysname = null,
  18898.     @constraint_name    sysname = null,
  18899.     @constraint_schema    sysname = null,
  18900.     @constraint_catalog    sysname = null,
  18901.     @constraint_type    nvarchar(255) = null 
  18902.     )
  18903. as
  18904.     select
  18905.         CONSTRAINT_CATALOG    = db_name(),
  18906.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18907.         CONSTRAINT_NAME        = c_obj.name,
  18908.         TABLE_CATALOG        = db_name(),
  18909.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18910.         TABLE_NAME            = t_obj.name,
  18911.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18912.                                 when 1 then N'PRIMARY KEY'
  18913.                                 when 2 then    N'UNIQUE'
  18914.                                 when 3 then    N'FOREIGN KEY'
  18915.                                 when 4 then    N'CHECK'
  18916.                                  end, 
  18917.         IS_DEFERRABLE        = convert(bit, 0),
  18918.         INITIALLY_DEFERRED    = convert(bit, 0),
  18919.         DESCRIPTION            = convert(nvarchar(1), null)
  18920.  
  18921.     from 
  18922.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18923.     where
  18924.             t_obj.type in ('U','S')
  18925.         and (@table_catalog is null or @table_catalog = db_name())
  18926.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18927.         and c.id = t_obj.id
  18928.         and (c.status & 0xf) between 1 and 4
  18929.         and c_obj.id    = c.constid
  18930.         and    c_obj.uid    = user_id()
  18931.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18932.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18933.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18934.         and (@constraint_type is null
  18935.             or (c.status & 0xf)
  18936.                 = case @constraint_type
  18937.                     when N'PRIMARY KEY' then 1
  18938.                     when N'UNIQUE' then 2
  18939.                     when N'FOREIGN KEY' then 3
  18940.                     when N'CHECK' then 4
  18941.                     end)
  18942. order by 2,3,5,6,7 
  18943. go
  18944. dump tran master with no_log
  18945.  
  18946.  
  18947. grant execute on sp_table_constraints_rowset to public
  18948. go
  18949.  
  18950. dump tran master with no_log
  18951. go
  18952. if (charindex('6.00', @@version) > 0)
  18953.     begin
  18954.     if (exists (select * from sysobjects
  18955.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  18956.         begin
  18957.         drop procedure sp_table_constraints_rowset
  18958.         dump tran master with no_log
  18959.         end
  18960.     end
  18961. go
  18962.  
  18963. print ''
  18964. print 'creating sp_table_privileges_rowset'
  18965. go
  18966.  
  18967. /*    Procedure for 6.0 and 6.5 server */
  18968. CREATE PROCEDURE sp_table_privileges_rowset
  18969.     (
  18970.     @table_name    varchar(255) = null,
  18971.     @table_schema    varchar(255) = null,
  18972.     @grantor    varchar(255) = null,
  18973.     @grantee    varchar(255) = null
  18974.     )
  18975. as
  18976. IF @table_name is not null
  18977.     BEGIN
  18978.     select
  18979.         GRANTOR        = user_name(p.grantor),
  18980.         GRANTEE        = user_name(u.uid),
  18981.         TABLE_CATALOG    = db_name(),
  18982.         TABLE_SCHEMA    = user_name(o.uid),
  18983.         TABLE_NAME    = o.name,
  18984.         PRIVILEGE_TYPE    = convert(varchar(30),
  18985.                     case p.action
  18986.                     when 193 then 'SELECT'
  18987.                     when 195 then 'INSERT'
  18988.                     when 196 then 'DELETE'
  18989.                     when 197 then 'UPDATE'
  18990.                     else 'REFERENCES'
  18991.                     end),
  18992.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  18993.     from 
  18994.         sysprotects p, sysobjects o, sysusers u
  18995.     where
  18996.         o.name = @table_name
  18997.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  18998.     and    (@grantee is null or @grantee = user_name(u.uid))
  18999.     and     o.type in ('U','V','S')
  19000.     and    p.id = o.id
  19001.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19002.             /* expand groups */
  19003.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19004.     and     p.protecttype <> 206    /* only grant rows */
  19005.     and     p.action in (26,193,195,196,197)
  19006.     and     o.uid <> u.uid            /* no rows for owner */
  19007.     and     not exists (            /* exclude revoke'd privileges */
  19008.             select     *
  19009.             from     sysprotects p1
  19010.             where    p1.protecttype = 206
  19011.             and     p1.action = p.action
  19012.             and     p1.id = p.id
  19013.             and     p1.uid = u.uid)
  19014.     union all
  19015.     select    /*    Add rows for table owner */
  19016.         GRANTOR        = user_name(u.uid),
  19017.         GRANTEE        = user_name(o.uid),
  19018.         TABLE_CATALOG    = db_name(),
  19019.         TABLE_SCHEMA    = user_name(o.uid),
  19020.         TABLE_NAME    = o.name,
  19021.         PRIVILEGE_TYPE    = convert(varchar(30),
  19022.                     case v.number
  19023.                     when 193 then 'SELECT'
  19024.                     when 195 then 'INSERT'
  19025.                     when 196 then 'DELETE'
  19026.                     when 197 then 'UPDATE'
  19027.                     else 'REFERENCES'
  19028.                     end),
  19029.         IS_GRANTABLE    = convert(bit,1)    
  19030.     from 
  19031.         sysobjects o, master.dbo.spt_values v, sysusers u
  19032.     where
  19033.         o.name = @table_name
  19034.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19035.     and    (@grantee is null or @grantee = user_name(u.uid))
  19036.     and     o.type in ('U','V','S')
  19037.     and     u.suid = 1        /* grantor is dbo of database */
  19038.     and    (@grantor is null or @grantor = user_name(u.uid))
  19039.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19040.     and     v.number in (26,193,195,196,197)
  19041.     and     not exists (    /* exclude revoke'd privileges */
  19042.             select     *
  19043.             from     sysprotects p1
  19044.             where    p1.protecttype = 206
  19045.             and     p1.action = v.number
  19046.             and     p1.id = o.id
  19047.             and     p1.uid = o.uid)
  19048.     order by 4,5,6,1,2
  19049.     END
  19050. ELSE
  19051.     BEGIN
  19052.     select
  19053.         GRANTOR        = user_name(p.grantor),
  19054.         GRANTEE        = user_name(u.uid),
  19055.         TABLE_CATALOG    = db_name(),
  19056.         TABLE_SCHEMA    = user_name(o.uid),
  19057.         TABLE_NAME    = o.name,
  19058.         PRIVILEGE_TYPE    = convert(varchar(30),
  19059.                     case p.action
  19060.                     when 193 then 'SELECT'
  19061.                     when 195 then 'INSERT'
  19062.                     when 196 then 'DELETE'
  19063.                     when 197 then 'UPDATE'
  19064.                     else 'REFERENCES'
  19065.                     end),
  19066.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19067.     from 
  19068.         sysprotects p, sysobjects o, sysusers u
  19069.     where
  19070.         o.type in ('U','V','S')
  19071.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19072.     and    (@grantee is null or @grantee = user_name(u.uid))
  19073.     and    p.id = o.id
  19074.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19075.             /* expand groups */
  19076.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19077.     and     p.protecttype <> 206    /* only grant rows */
  19078.     and     p.action in (26,193,195,196,197)
  19079.     and     o.uid <> u.uid            /* no rows for owner */
  19080.     and     not exists (            /* exclude revoke'd privileges */
  19081.             select     *
  19082.             from     sysprotects p1
  19083.             where    p1.protecttype = 206
  19084.             and     p1.action = p.action
  19085.             and     p1.id = p.id
  19086.             and     p1.uid = u.uid)
  19087.     union all
  19088.     select    /*    Add rows for table owner */
  19089.         GRANTOR        = user_name(u.uid),
  19090.         GRANTEE        = user_name(o.uid),
  19091.         TABLE_CATALOG    = db_name(),
  19092.         TABLE_SCHEMA    = user_name(o.uid),
  19093.         TABLE_NAME    = o.name,
  19094.         PRIVILEGE_TYPE    = convert(varchar(30),
  19095.                     case v.number
  19096.                     when 193 then 'SELECT'
  19097.                     when 195 then 'INSERT'
  19098.                     when 196 then 'DELETE'
  19099.                     when 197 then 'UPDATE'
  19100.                     else 'REFERENCES'
  19101.                     end),
  19102.         IS_GRANTABLE    = convert(bit,1)    
  19103.     from 
  19104.         sysobjects o, master.dbo.spt_values v, sysusers u
  19105.     where
  19106.         o.type in ('U','V','S')
  19107.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19108.     and    (@grantee is null or @grantee = user_name(u.uid))
  19109.     and     u.suid = 1        /* grantor is dbo of database */
  19110.     and    (@grantor is null or @grantor = user_name(u.uid))
  19111.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19112.     and     v.number in (26,193,195,196,197)
  19113.     and     not exists (    /* exclude revoke'd privileges */
  19114.             select     *
  19115.             from     sysprotects p1
  19116.             where    p1.protecttype = 206
  19117.             and     p1.action = v.number
  19118.             and     p1.id = o.id
  19119.             and     p1.uid = o.uid)
  19120.     order by 4,5,6,1,2
  19121.     END
  19122. go
  19123. dump tran master with no_log
  19124. go
  19125. CREATE PROCEDURE sp_table_privileges_rowset;2
  19126.     (
  19127.        @handle        int output,
  19128.        @scrollopt    int output,
  19129.     @ccopt        int output,
  19130.     @rows        int output,
  19131.     @table_name     varchar(255) = null,
  19132.     @table_schema    varchar(255) = null,
  19133.     @grantor    varchar(255) = null,
  19134.     @grantee    varchar(255) = null
  19135.     )
  19136. as
  19137. declare @ret int
  19138. SET NOCOUNT ON
  19139. create table #sptprivsrowset1
  19140.     (
  19141.     GRANTOR        sysname not null,
  19142.     GRANTEE        sysname not null,
  19143.     TABLE_CATALOG    sysname not null,
  19144.     TABLE_SCHEMA    sysname not null,
  19145.     TABLE_NAME    sysname not null,
  19146.     PRIVILEGE_TYPE    sysname not null,
  19147.     IS_GRANTABLE    bit not null
  19148.     )
  19149.     
  19150. IF @table_name is not null
  19151.     BEGIN
  19152.     insert into #sptprivsrowset1
  19153.     select
  19154.         GRANTOR        = user_name(p.grantor),
  19155.         GRANTEE        = user_name(u.uid),
  19156.         TABLE_CATALOG    = db_name(),
  19157.         TABLE_SCHEMA    = user_name(o.uid),
  19158.         TABLE_NAME    = o.name,
  19159.         PRIVILEGE_TYPE    = convert(varchar(30),
  19160.                     case p.action
  19161.                     when 193 then 'SELECT'
  19162.                     when 195 then 'INSERT'
  19163.                     when 196 then 'DELETE'
  19164.                     when 197 then 'UPDATE'
  19165.                     else 'REFERENCES'
  19166.                     end),
  19167.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19168.     from 
  19169.         sysprotects p, sysobjects o, sysusers u
  19170.     where
  19171.         o.name = @table_name
  19172.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19173.     and    (@grantee is null or @grantee = user_name(u.uid))
  19174.     and     o.type in ('U','V','S')
  19175.     and    p.id = o.id
  19176.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19177.             /* expand groups */
  19178.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19179.     and     p.protecttype <> 206    /* only grant rows */
  19180.     and     p.action in (26,193,195,196,197)
  19181.     and     o.uid <> u.uid            /* no rows for owner */
  19182.     and     not exists (            /* exclude revoke'd privileges */
  19183.             select     *
  19184.             from     sysprotects p1
  19185.             where    p1.protecttype = 206
  19186.             and     p1.action = p.action
  19187.             and     p1.id = p.id
  19188.             and     p1.uid = u.uid)
  19189.     union all
  19190.     select    /*    Add rows for table owner */
  19191.         GRANTOR        = user_name(u.uid),
  19192.         GRANTEE        = user_name(o.uid),
  19193.         TABLE_CATALOG    = db_name(),
  19194.         TABLE_SCHEMA    = user_name(o.uid),
  19195.         TABLE_NAME    = o.name,
  19196.         PRIVILEGE_TYPE    = convert(varchar(30),
  19197.                     case v.number
  19198.                     when 193 then 'SELECT'
  19199.                     when 195 then 'INSERT'
  19200.                     when 196 then 'DELETE'
  19201.                     when 197 then 'UPDATE'
  19202.                     else 'REFERENCES'
  19203.                     end),
  19204.         IS_GRANTABLE    = convert(bit,1)    
  19205.     from 
  19206.         sysobjects o, master.dbo.spt_values v, sysusers u
  19207.     where
  19208.         o.name = @table_name
  19209.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19210.     and    (@grantee is null or @grantee = user_name(u.uid))
  19211.     and     o.type in ('U','V','S')
  19212.     and     u.suid = 1        /* grantor is dbo of database */
  19213.     and    (@grantor is null or @grantor = user_name(u.uid))
  19214.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19215.     and     v.number in (26,193,195,196,197)
  19216.     and     not exists (    /* exclude revoke'd privileges */
  19217.             select     *
  19218.             from     sysprotects p1
  19219.             where    p1.protecttype = 206
  19220.             and     p1.action = v.number
  19221.             and     p1.id = o.id
  19222.             and     p1.uid = o.uid)
  19223.     order by 4,5,6,1,2
  19224.     END
  19225. ELSE
  19226.     BEGIN
  19227.     insert into #sptprivsrowset1
  19228.     select
  19229.         GRANTOR        = user_name(p.grantor),
  19230.         GRANTEE        = user_name(u.uid),
  19231.         TABLE_CATALOG    = db_name(),
  19232.         TABLE_SCHEMA    = user_name(o.uid),
  19233.         TABLE_NAME    = o.name,
  19234.         PRIVILEGE_TYPE    = convert(varchar(30),
  19235.                     case p.action
  19236.                     when 193 then 'SELECT'
  19237.                     when 195 then 'INSERT'
  19238.                     when 196 then 'DELETE'
  19239.                     when 197 then 'UPDATE'
  19240.                     else 'REFERENCES'
  19241.                     end),
  19242.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19243.     from 
  19244.         sysprotects p, sysobjects o, sysusers u
  19245.     where
  19246.         o.type in ('U','V','S')
  19247.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19248.     and    (@grantee is null or @grantee = user_name(u.uid))
  19249.     and    p.id = o.id
  19250.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19251.             /* expand groups */
  19252.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19253.     and     p.protecttype <> 206    /* only grant rows */
  19254.     and     p.action in (26,193,195,196,197)
  19255.     and     o.uid <> u.uid            /* no rows for owner */
  19256.     and     not exists (            /* exclude revoke'd privileges */
  19257.             select     *
  19258.             from     sysprotects p1
  19259.             where    p1.protecttype = 206
  19260.             and     p1.action = p.action
  19261.             and     p1.id = p.id
  19262.             and     p1.uid = u.uid)
  19263.     union all
  19264.     select    /*    Add rows for table owner */
  19265.         GRANTOR        = user_name(u.uid),
  19266.         GRANTEE        = user_name(o.uid),
  19267.         TABLE_CATALOG    = db_name(),
  19268.         TABLE_SCHEMA    = user_name(o.uid),
  19269.         TABLE_NAME    = o.name,
  19270.         PRIVILEGE_TYPE    = convert(varchar(30),
  19271.                     case v.number
  19272.                     when 193 then 'SELECT'
  19273.                     when 195 then 'INSERT'
  19274.                     when 196 then 'DELETE'
  19275.                     when 197 then 'UPDATE'
  19276.                     else 'REFERENCES'
  19277.                     end),
  19278.         IS_GRANTABLE    = convert(bit,1)    
  19279.     from 
  19280.         sysobjects o, master.dbo.spt_values v, sysusers u
  19281.     where
  19282.         o.type in ('U','V','S')
  19283.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19284.     and    (@grantee is null or @grantee = user_name(u.uid))
  19285.     and     u.suid = 1        /* grantor is dbo of database */
  19286.     and    (@grantor is null or @grantor = user_name(u.uid))
  19287.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19288.     and     v.number in (26,193,195,196,197)
  19289.     and     not exists (    /* exclude revoke'd privileges */
  19290.             select     *
  19291.             from     sysprotects p1
  19292.             where    p1.protecttype = 206
  19293.             and     p1.action = v.number
  19294.             and     p1.id = o.id
  19295.             and     p1.uid = o.uid)
  19296.     order by 4,5,6,1,2
  19297.     END
  19298.  
  19299. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  19300.     @scrollopt output, @ccopt output, @rows output
  19301.  
  19302. drop table #sptprivsrowset1
  19303. return isnull(@ret,0)
  19304. go
  19305. dump tran master with no_log
  19306. go
  19307. CREATE PROCEDURE sp_table_privileges_rowset;3
  19308. as
  19309.     select
  19310.         GRANTOR        = convert(sysname, ' '),
  19311.         GRANTEE        = convert(sysname, ' '),
  19312.         TABLE_CATALOG    = convert(sysname, ' '),
  19313.         TABLE_SCHEMA    = convert(sysname, ' '),
  19314.         TABLE_NAME    = convert(sysname, ' '),
  19315.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  19316.         IS_GRANTABLE    = convert(bit, 0)
  19317.     where    1=0
  19318. go
  19319.  
  19320. if (charindex('7.00', @@version) = 0)
  19321. begin
  19322.     print ''
  19323.     print ''
  19324.     print 'Warning:'
  19325.     print 'you are installing the stored procedures '
  19326.     print 'on a pre 7.0 SQL Server.'
  19327.     print 'Ignore the following errors.'
  19328. end
  19329. else
  19330.     drop proc sp_table_privileges_rowset
  19331. go
  19332.  
  19333.  
  19334. /*    Procedure for 7.0 server */
  19335. CREATE PROCEDURE sp_table_privileges_rowset
  19336.     (
  19337.     @table_name         sysname,
  19338.     @table_schema        sysname = null,
  19339.     @grantor        sysname = null,
  19340.     @grantee        sysname = null
  19341.     )
  19342. as
  19343.     select
  19344.         GRANTOR        = user_name(p.grantor),
  19345.         GRANTEE        = user_name(u.uid),
  19346.         TABLE_CATALOG    = db_name(),
  19347.         TABLE_SCHEMA    = user_name(o.uid),
  19348.         TABLE_NAME    = o.name,
  19349.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19350.                     case p.action
  19351.                     when 193 then N'SELECT'
  19352.                     when 195 then N'INSERT'
  19353.                     when 196 then N'DELETE'
  19354.                     when 197 then N'UPDATE'
  19355.                     else N'REFERENCES'
  19356.                     end),
  19357.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19358.     from 
  19359.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  19360.     where
  19361.         o.name = @table_name
  19362.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19363.     and    (@grantee is null or @grantee = user_name(u.uid))
  19364.     and     o.type in ('U','V','S')
  19365.     and    p.id = o.id
  19366.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19367.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  19368.     and     (u.uid > 0 and u.uid < 16384)
  19369.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  19370.     and     p.protecttype <> 206    /* only grant rows */
  19371.     and     p.action in (26,193,195,196,197)
  19372.     and     o.uid <> u.uid            /* no rows for owner */
  19373.     and     not exists (            /* exclude revoke'd privileges */
  19374.             select     *
  19375.             from     sysprotects p1
  19376.             where    p1.protecttype = 206
  19377.             and     p1.action = p.action
  19378.             and     p1.id = p.id
  19379.             and     p1.uid = u.uid)
  19380.     union all
  19381.     select    /*    Add rows for table owner */
  19382.         GRANTOR        = user_name(u.uid),
  19383.         GRANTEE        = user_name(o.uid),
  19384.         TABLE_CATALOG    = db_name(),
  19385.         TABLE_SCHEMA    = user_name(o.uid),
  19386.         TABLE_NAME    = o.name,
  19387.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19388.                     case v.number
  19389.                     when 193 then N'SELECT'
  19390.                     when 195 then N'INSERT'
  19391.                     when 196 then N'DELETE'
  19392.                     when 197 then N'UPDATE'
  19393.                     else N'REFERENCES'
  19394.                     end),
  19395.         IS_GRANTABLE    = convert(bit,1)    
  19396.     from 
  19397.         sysobjects o, master.dbo.spt_values v, sysusers u
  19398.     where
  19399.         o.name = @table_name
  19400.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19401.     and    (@grantee is null or @grantee = user_name(u.uid))
  19402.     and     o.type in ('U','V','S')
  19403.     and     u.suid = 1        /* grantor is dbo of database */
  19404.     and    (@grantor is null or @grantor = user_name(u.uid))
  19405.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19406.     and     v.number in (26,193,195,196,197)
  19407.     and     not exists (    /* exclude revoke'd privileges */
  19408.             select     *
  19409.             from     sysprotects p1
  19410.             where    p1.protecttype = 206
  19411.             and     p1.action = v.number
  19412.             and     p1.id = o.id
  19413.             and     p1.uid = o.uid)
  19414.     order by 4,5,6,1,2
  19415. go
  19416. dump tran master with no_log
  19417. go
  19418. CREATE PROCEDURE sp_table_privileges_rowset;2
  19419.     (
  19420.     @table_schema        sysname = null,
  19421.     @grantor        sysname = null,
  19422.     @grantee        sysname = null
  19423.     )
  19424. as
  19425.     select
  19426.         GRANTOR        = user_name(p.grantor),
  19427.         GRANTEE        = user_name(u.uid),
  19428.         TABLE_CATALOG    = db_name(),
  19429.         TABLE_SCHEMA    = user_name(o.uid),
  19430.         TABLE_NAME    = o.name,
  19431.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19432.                     case p.action
  19433.                     when 193 then N'SELECT'
  19434.                     when 195 then N'INSERT'
  19435.                     when 196 then N'DELETE'
  19436.                     when 197 then N'UPDATE'
  19437.                     else N'REFERENCES'
  19438.                     end),
  19439.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19440.     from 
  19441.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  19442.     where
  19443.         o.type in ('U','V','S')
  19444.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19445.     and    (@grantee is null or @grantee = user_name(u.uid))
  19446.     and    p.id = o.id
  19447.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19448.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  19449.     and     (u.uid > 0 and u.uid < 16384)
  19450.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  19451.     and     p.protecttype <> 206    /* only grant rows */
  19452.     and     p.action in (26,193,195,196,197)
  19453.     and     o.uid <> u.uid            /* no rows for owner */
  19454.     and     not exists (            /* exclude revoke'd privileges */
  19455.             select     *
  19456.             from     sysprotects p1
  19457.             where    p1.protecttype = 206
  19458.             and     p1.action = p.action
  19459.             and     p1.id = p.id
  19460.             and     p1.uid = u.uid)
  19461.     union all
  19462.     select    /*    Add rows for table owner */
  19463.         GRANTOR        = user_name(u.uid),
  19464.         GRANTEE        = user_name(o.uid),
  19465.         TABLE_CATALOG    = db_name(),
  19466.         TABLE_SCHEMA    = user_name(o.uid),
  19467.         TABLE_NAME    = o.name,
  19468.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19469.                     case v.number
  19470.                     when 193 then N'SELECT'
  19471.                     when 195 then N'INSERT'
  19472.                     when 196 then N'DELETE'
  19473.                     when 197 then N'UPDATE'
  19474.                     else N'REFERENCES'
  19475.                     end),
  19476.         IS_GRANTABLE    = convert(bit,1)    
  19477.     from 
  19478.         sysobjects o, master.dbo.spt_values v, sysusers u
  19479.     where
  19480.         o.type in ('U','V','S')
  19481.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19482.     and    (@grantee is null or @grantee = user_name(u.uid))
  19483.     and     u.suid = 1        /* grantor is dbo of database */
  19484.     and    (@grantor is null or @grantor = user_name(u.uid))
  19485.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19486.     and     v.number in (26,193,195,196,197)
  19487.     and     not exists (    /* exclude revoke'd privileges */
  19488.             select     *
  19489.             from     sysprotects p1
  19490.             where    p1.protecttype = 206
  19491.             and     p1.action = v.number
  19492.             and     p1.id = o.id
  19493.             and     p1.uid = o.uid)
  19494.     order by 4,5,6,1,2
  19495. go
  19496. dump tran master with no_log
  19497. go
  19498. create procedure sp_table_privileges_rowset;5
  19499.     (
  19500.     @table_server        sysname,
  19501.     @table_catalog        sysname = null,
  19502.     @table_name        sysname = null,
  19503.     @table_schema        sysname = null,
  19504.     @grantor        sysname = null,
  19505.     @grantee        sysname = null
  19506.     )
  19507. as
  19508.     select
  19509.         GRANTOR,        
  19510.         GRANTEE,        
  19511.         TABLE_CATALOG,    
  19512.         TABLE_SCHEMA,    
  19513.         TABLE_NAME,    
  19514.         PRIVILEGE_TYPE,    
  19515.         IS_GRANTABLE    
  19516.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  19517.                 @table_server,
  19518.                 @table_catalog,
  19519.                 @table_schema,
  19520.                 @table_name,
  19521.                 @grantor,
  19522.                 @grantee >
  19523.     order by 3,4,5,6,1,2
  19524. go
  19525.  
  19526. grant execute on sp_table_privileges_rowset to public
  19527. go
  19528. dump tran master with no_log
  19529. go
  19530. if (charindex('6.00', @@version) > 0)
  19531.     begin
  19532.     if (exists (select * from sysobjects
  19533.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  19534.         begin
  19535.         drop proc sp_table_privileges_rowset
  19536.         dump tran master with no_log
  19537.         end
  19538.     end
  19539. go
  19540.  
  19541.  
  19542. print ''
  19543. print 'creating sp_linkedservers_rowset'
  19544. go
  19545. if (charindex('7.00', @@version) = 0)
  19546. begin
  19547.     print ''
  19548.     print ''
  19549.     print 'Warning:'
  19550.     print 'you are installing the stored procedures '
  19551.     print 'on a pre 7.0 SQL Server.'
  19552.     print 'Ignore the following errors.'
  19553. end
  19554. go
  19555. /*    Procedure for 7.0 server */
  19556. create proc sp_linkedservers_rowset
  19557.     (
  19558.     @srvname     sysname
  19559.     )
  19560. as
  19561.     select
  19562.         SVR_NAME         = srvname,
  19563.         SVR_PRODUCT        = srvproduct,
  19564.         SVR_PROVIDERNAME    = providername,
  19565.         SVR_DATASOURCE        = datasource,
  19566.         SVR_PROVIDERSTRING    = providerstring,
  19567.         SVR_LOCATION        = location,
  19568.         SVR_CATALOG        = catalog
  19569.     from master.dbo.sysservers
  19570.     where srvname = @srvname and (srvstatus & 128) = 128
  19571.     order by 1
  19572. go
  19573. dump tran master with no_log
  19574. go
  19575. create proc sp_linkedservers_rowset;2
  19576. as
  19577.     select
  19578.         SVR_NAME         = srvname,
  19579.         SVR_PRODUCT        = srvproduct,
  19580.         SVR_PROVIDERNAME    = providername,
  19581.         SVR_DATASOURCE        = datasource,
  19582.         SVR_PROVIDERSTRING    = providerstring,
  19583.         SVR_LOCATION        = location,
  19584.         SVR_CATALOG        = catalog
  19585.     from master.dbo.sysservers
  19586.     where (srvstatus & 128) = 128
  19587.     order by 1
  19588. go
  19589.  
  19590. grant execute on sp_linkedservers_rowset to public
  19591. go
  19592.  
  19593. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  19594.  
  19595.  
  19596. dump tran master with no_log
  19597. go
  19598.  
  19599.  
  19600.  
  19601.  
  19602. if (exists (select * from sysobjects
  19603.         where name = 'sp_configure' and type = 'P '))
  19604.     begin
  19605.         exec sp_configure 'allow updates',0
  19606.         reconfigure with override
  19607.     end
  19608. go
  19609.  
  19610. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  19611.  
  19612. go
  19613.  
  19614. if exists (select * from sysobjects where name = 'sp_check_objects'
  19615.             and type = 'P ')
  19616.     begin
  19617.         /* Only supported on 6.0 servers */
  19618.         print ''
  19619.         print 'Checking objects created by instcat.sql.'
  19620.  
  19621.         exec sp_check_objects 'catalog'
  19622.     end
  19623. go
  19624.  
  19625. print ''
  19626. print 'instcat.sql completed successfully.'
  19627. go
  19628.  
  19629. set quoted_identifier off
  19630. go
  19631.  
  19632. dump tran master with no_log
  19633. go
  19634. checkpoint
  19635. go
  19636. /**/
  19637.