		drop table spt_provider_types
		dump tran master with no_log
		end
	end
go

if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/* 
** (leih 2/11/98) 
** The following SP is shared by instcat.sql and the server to add information
** related to server language collations. The server calls it at the end of
** collation change to update the catalog with the new language collations
** Everything in this SP should NOT use tempdb.  
*/
create procedure sp_add_server_sortinfo
as
-- spt_server_info has unigue clustered index on attribute_id
delete spt_server_info where attribute_id in (16,18)

--insert into spt_server_info
--	select 18,'COLLATION_SEQ',
--		'charset='+t2.name+' sort_order='+t1.name
--		+' charset_num='+rtrim(convert(char(4),t1.csid))+
--		' sort_order_num='+rtrim(convert(char(4),t1.id))
--	from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
--	where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
declare @case_sensitive bit
declare @ncase_sensitive bit
select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
select @sortid = value from sysconfigures where config = 1123
select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
		+ ' charset_num=' + rtrim(convert(char(4),@csid))+
		' sort_order_num=' + rtrim(convert(char(4),@sortid))
		from syscharsets where id = @csid

insert into spt_server_info
		values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))

if @case_sensitive = 1 /* If case sensitive server */
begin
	insert into spt_server_info
		values (16,'IDENTIFIER_CASE','SENSITIVE')
end
else
begin
	insert into spt_server_info
		values (16,'IDENTIFIER_CASE','MIXED')
end


update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
	where DATA_TYPE in (-1, 1, 12) /* CHAR types */
update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
	where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
update spt_provider_types set case_sensitive = @case_sensitive 
	where oledb_data_type = 129 /*DBTYPE_STR*/
update spt_provider_types set case_sensitive = @ncase_sensitive
	where oledb_data_type = 130 /*DBTYPE_WSTR*/

go

if (charindex('7.00', @@version) <> 0)
begin
	exec sp_add_server_sortinfo
end
go

print ''
print 'creating sp_catalogs_rowset'
go

create procedure sp_catalogs_rowset
	(
	@catalog_name		varchar(255)
	)    	
as
	select
		CATALOG_NAME	= name,
		DESCRIPTION	= convert(varchar(1),null)
	from 	master.dbo.sysdatabases
	where	name = @catalog_name
go
dump tran master with no_log
go
create procedure sp_catalogs_rowset;2
	(
	@dummy		int /* remove when Hydra bug 17032 is fixed */
	)
as
	select
		CATALOG_NAME	= name,
		DESCRIPTION	= convert(varchar(1),null)
	from 	master.dbo.sysdatabases
	order by 1
go
dump tran master with no_log
go

if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
else
	drop proc sp_catalogs_rowset
go

/*	Procedure for 7.0 server */
create procedure sp_catalogs_rowset
	(
	@catalog_name		sysname
	)    	
as
	select
		CATALOG_NAME	= name,
		DESCRIPTION	= convert(nvarchar(1),null)
	from 	master.dbo.sysdatabases
	where	name = @catalog_name
go
dump tran master with no_log
go
create procedure sp_catalogs_rowset;2
	(
	@dummy		int /* remove when Hydra bug 17032 is fixed */
	)
as
	select
		CATALOG_NAME	= name,
		DESCRIPTION	= convert(nvarchar(1),null)
	from 	master.dbo.sysdatabases
	order by 1
go
dump tran master with no_log
go
create procedure sp_catalogs_rowset;5
	(
	@server_name	sysname,
	@catalog_name	sysname = NULL
	)
as
	select	CATALOG_NAME,
		DESCRIPTION
	from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
	order by 1
go

grant execute on sp_catalogs_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_catalogs_rowset' and type = 'P '))
		begin
		drop procedure sp_catalogs_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_column_privileges_rowset'
go

/*	Procedure for 6.0 and 6.5 server */
CREATE PROCEDURE sp_column_privileges_rowset
	(
	@table_name 	varchar(255) = null,
	@table_schema	varchar(255) = null,
	@column_name	varchar(255) = null,
	@grantor	varchar(255) = null,
	@grantee	varchar(255) = null
	)
as
IF @table_name is not null
	BEGIN
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
	where
		o.name = @table_name
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	c.id = p.id
	and	(@grantor is null or @grantor = user_name(p.grantor))
	and 	case 
		    when substring(p.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
		    when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
		    else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
		    end & v.high <> 0			/* permission applies to this column */
	and 	v.number <= (select count(*) from syscolumns where id = o.id)	/* ranges from 1 to # of columns in table */
	and 	v.type = 'P'
	and 	v.number = c.colid
	and	(@grantee is null or @grantee = user_name(u.uid))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select *
			from sysprotects p1
			where
				p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid
			and 	case 
				when substring(p1.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
				when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
                                else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
				end & v.high <> 0)			/* permission applies to this column */
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
	where
		o.name = @table_name
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'		/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,197)
	and 	not exists (		/* exclude revoke'd privileges */
			select *
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4, 5, 6, 9, 1, 2
	END
ELSE
	BEGIN
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	c.id = p.id
	and	(@grantor is null or @grantor = user_name(p.grantor))
	and 	case 
		    when substring(p.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
		    when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
		    else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
		    end & v.high <> 0			/* permission applies to this column */
	and 	v.number <= (select count(*) from syscolumns where id = o.id)	/* ranges from 1 to # of columns in table */
	and 	v.type = 'P'
	and 	v.number = c.colid
	and	(@grantee is null or @grantee = user_name(u.uid))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select *
			from sysprotects p1
			where
				p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid
			and 	case 
				when substring(p1.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
				when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
                                else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
				end & v.high <> 0)			/* permission applies to this column */
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
	where
	 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'		/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,197)
	and 	not exists (		/* exclude revoke'd privileges */
			select *
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4, 5, 6, 9, 1, 2
	END
go
dump tran master with no_log
go
CREATE PROCEDURE sp_column_privileges_rowset;2
	(
   	@handle		int output,
   	@scrollopt	int output,
	@ccopt		int output,
	@rows		int output,
	@table_name 	varchar(255) = null,
	@table_schema	varchar(255) = null,
	@column_name	varchar(255) = null,
	@grantor	varchar(255) = null,
	@grantee	varchar(255) = null
	)
as

declare @ret int

SET NOCOUNT ON
	
create table #spcprivsrowset1
	(
	GRANTOR		sysname not null,
	GRANTEE		sysname not null,
	TABLE_CATALOG	sysname not null,
	TABLE_SCHEMA	sysname not null,
	TABLE_NAME	sysname not null,
	COLUMN_NAME	sysname not null,
	COLUMN_GUID	binary(16) null,
	COLUMN_PROPID	int null,
	PRIVILEGE_TYPE	sysname not null,
	IS_GRANTABLE	bit not null
	)

IF @table_name is not null
	BEGIN
	insert into #spcprivsrowset1
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
	where
		o.name = @table_name
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	c.id = p.id
	and	(@grantor is null or @grantor = user_name(p.grantor))
	and 	case 
		    when substring(p.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
		    when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
		    else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
		    end & v.high <> 0			/* permission applies to this column */
	and 	v.number <= (select count(*) from syscolumns where id = o.id)	/* ranges from 1 to # of columns in table */
	and 	v.type = 'P'
	and 	v.number = c.colid
	and	(@grantee is null or @grantee = user_name(u.uid))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select *
			from sysprotects p1
			where
				p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid
			and 	case 
				when substring(p1.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
				when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
                                else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
				end & v.high <> 0)			/* permission applies to this column */
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
	where
		o.name = @table_name
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'		/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,197)
	and 	not exists (		/* exclude revoke'd privileges */
			select *
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4, 5, 6, 9, 1, 2
	END
ELSE
	BEGIN
	insert into #spcprivsrowset1
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	c.id = p.id
	and	(@grantor is null or @grantor = user_name(p.grantor))
	and 	case 
		    when substring(p.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
		    when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
		    else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
		    end & v.high <> 0			/* permission applies to this column */
	and 	v.number <= (select count(*) from syscolumns where id = o.id)	/* ranges from 1 to # of columns in table */
	and 	v.type = 'P'
	and 	v.number = c.colid
	and	(@grantee is null or @grantee = user_name(u.uid))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select *
			from sysprotects p1
			where
				p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid
			and 	case 
				when substring(p1.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
				when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
                                else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
				end & v.high <> 0)			/* permission applies to this column */
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
	where
	 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'		/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,197)
	and 	not exists (		/* exclude revoke'd privileges */
			select *
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4, 5, 6, 9, 1, 2
	END

exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
	@scrollopt output, @ccopt output, @rows output

drop table #spcprivsrowset1

return isnull(@ret,0)
go
dump tran master with no_log
go
CREATE PROCEDURE sp_column_privileges_rowset;3
as
	select
		GRANTOR		= convert(sysname,' '),
		GRANTEE		= convert(sysname,' '),
		TABLE_CATALOG	= convert(sysname,' '),
		TABLE_SCHEMA	= convert(sysname,' '),
		TABLE_NAME	= convert(sysname,' '),
		COLUMN_NAME	= convert(sysname,' '),
		COLUMN_GUID	= convert(binary(16),null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(varchar(30),' '),
		IS_GRANTABLE	= convert(bit,1)
	where	1=0
go
dump tran master with no_log
go

if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
else
	drop proc sp_column_privileges_rowset
go


/*	Procedure for 7.0 server */
CREATE PROCEDURE sp_column_privileges_rowset
	(
	@table_name 		sysname,
	@table_schema		sysname = null,
	@column_name		sysname = null,
	@grantor		sysname = null,
	@grantee		sysname = null
	)
as
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(uniqueidentifier,null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case p.action
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
	where
		o.name = @table_name
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	c.id = p.id
	and	(@grantor is null or @grantor = user_name(p.grantor))
	and 	case 
		    when substring(p.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
		    when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
		    else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
		    end & v.high <> 0			/* permission applies to this column */
	and 	v.number <= (select count(*) from syscolumns where id = o.id)	/* ranges from 1 to # of columns in table */
	and 	v.type = 'P'
	and 	v.number = c.colid
	and	(@grantee is null or @grantee = user_name(u.uid))
		/* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
	and 	(u.uid > 0 and u.uid < 16384)
	and 	((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))	
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select *
			from sysprotects p1
			where
				p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid
			and 	case 
				when substring(p1.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
				when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
                                else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
				end & v.high <> 0)			/* permission applies to this column */
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(uniqueidentifier,null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case v.number
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
	where
		o.name = @table_name
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'		/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,197)
	and 	not exists (		/* exclude revoke'd privileges */
			select *
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4, 5, 6, 9, 1, 2
go
dump tran master with no_log
go
CREATE PROCEDURE sp_column_privileges_rowset;2
	(
	@table_schema		sysname = null,
	@column_name		sysname = null,
	@grantor		sysname = null,
	@grantee		sysname = null
	)
as
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(uniqueidentifier,null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case p.action
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	c.id = p.id
	and	(@grantor is null or @grantor = user_name(p.grantor))
	and 	case 
		    when substring(p.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
		    when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
		    else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
		    end & v.high <> 0			/* permission applies to this column */
	and 	v.number <= (select count(*) from syscolumns where id = o.id)	/* ranges from 1 to # of columns in table */
	and 	v.type = 'P'
	and 	v.number = c.colid
	and	(@grantee is null or @grantee = user_name(u.uid))
		/* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
	and 	(u.uid > 0 and u.uid < 16384)
	and 	((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))	
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select *
			from sysprotects p1
			where
				p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid
			and 	case 
				when substring(p1.columns, 1, 1) & 1 is null then 255	/* all columns have permission */
				when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
                                else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
				end & v.high <> 0)			/* permission applies to this column */
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		COLUMN_NAME	= c.name,
		COLUMN_GUID	= convert(uniqueidentifier,null),
		COLUMN_PROPID	= convert(int,null),
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case v.number
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(o.uid))
	and	c.id = o.id
	and 	(@column_name is null or @column_name = c.name)
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'		/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,197)
	and 	not exists (		/* exclude revoke'd privileges */
			select *
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4, 5, 6, 9, 1, 2
go
dump tran master with no_log
go
create procedure sp_column_privileges_rowset;5
	(
	@table_server		sysname,
	@table_catalog		sysname = null,
	@table_name		sysname = null,
	@table_schema		sysname = null,
	@column_name		sysname = null,
	@grantor		sysname = null,
	@grantee		sysname = null
	)
as
	select
		GRANTOR,
		GRANTEE,
		TABLE_CATALOG,
		TABLE_SCHEMA,
		TABLE_NAME,
		COLUMN_NAME,
		COLUMN_GUID,
		COLUMN_PROPID,	
		PRIVILEGE_TYPE,
		IS_GRANTABLE	
		from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name,
				@column_name,
				@grantor,
				@grantee>
	order by 3, 4, 5, 6, 9, 1, 2
go

grant execute on sp_column_privileges_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_column_privileges_rowset' and type = 'P '))
		begin
		drop procedure sp_column_privileges_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_columns_rowset'
go

/*	Procedure for 6.0 and 6.50 server */
create procedure sp_columns_rowset
	(
   	@table_name	varchar(255),
	@table_schema 	varchar(255) = null,
	@column_name	varchar(255) = null
	)
as 	
	set nocount on

	/* Declare bindings */
	DECLARE @TABLECATALOG    sysname,
		@TABLESCHEMA     sysname,
		@TABLENAME       sysname,
		@COLUMNNAME      sysname,
		@ORDINALPOSITION int

	/* Declare tracking variables */
	DECLARE @currCatalog sysname,
 		@currSchema  sysname,
		@currName    sysname,
		@currPos     int

	/* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
	CREATE TABLE #columns_rowset
	(
		TABLE_CATALOG			sysname,
		TABLE_SCHEMA			sysname,
		TABLE_NAME			sysname,
		COLUMN_NAME			sysname,
		COLUMN_GUID			binary(16)		null,
		COLUMN_PROPID			int			null,
		ORDINAL_POSITION		int,
		COLUMN_HASDEFAULT		bit,
		COLUMN_DEFAULT			varchar(255),
		COLUMN_FLAGS			int,
		IS_NULLABLE			bit,
		DATA_TYPE			smallint,
		TYPE_GUID			binary(16)		null,
		CHARACTER_MAXIMUM_LENGTH	int			null,
		CHARACTER_OCTET_LENGTH		int			null,
		NUMERIC_PRECISION		smallint		null,
		NUMERIC_SCALE			smallint		null, 
		DATETIME_PRECISION		int			null,
		CHARACTER_SET_CATALOG		sysname			null,
		CHARACTER_SET_SCHEMA		sysname			null,
		CHARACTER_SET_NAME		sysname			null,
		COLLATION_CATALOG		sysname			null,
		COLLATION_SCHEMA		sysname			null,
		COLLATION_NAME			sysname			null,
		DOMAIN_CATALOG			sysname 		null,
		DOMAIN_SCHEMA			sysname 		null,
		DOMAIN_NAME			sysname 		null,
		DESCRIPTION			varchar(1)
	)		

	/* Declare cursor for fixup */
	DECLARE curColRowset CURSOR
	FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
	    FROM #columns_rowset
	    ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION


	/* Store results into the temp table */
	insert into #columns_rowset
		(
		TABLE_CATALOG,
		TABLE_SCHEMA,
		TABLE_NAME,
		COLUMN_NAME,
		COLUMN_GUID,
		COLUMN_PROPID,
		ORDINAL_POSITION,
		COLUMN_HASDEFAULT,
		COLUMN_DEFAULT,
		COLUMN_FLAGS,
		IS_NULLABLE,
		DATA_TYPE,
		TYPE_GUID,
		CHARACTER_MAXIMUM_LENGTH,
		CHARACTER_OCTET_LENGTH,
		NUMERIC_PRECISION,
		NUMERIC_SCALE,
		DATETIME_PRECISION,
		CHARACTER_SET_CATALOG,
		CHARACTER_SET_SCHEMA,
		CHARACTER_SET_NAME,
		COLLATION_CATALOG,
		COLLATION_SCHEMA,
		COLLATION_NAME,
		DOMAIN_CATALOG,
		DOMAIN_SCHEMA,
		DOMAIN_NAME,
		DESCRIPTION
		)
	select
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		COLUMN_NAME		= c.name,
		COLUMN_GUID		= convert(binary(16),null),
		COLUMN_PROPID		= convert(int,null),
		ORDINAL_POSITION	= convert(int, c.colid),
		COLUMN_HASDEFAULT	= convert(bit, case when m.text is null then 0 else 1 end),
		COLUMN_DEFAULT		= substring(m.text,2,datalength(m.text)-2),
		COLUMN_FLAGS		= convert(int,
						case when d.is_long = 1 
						then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
						else 0
						end
					|	case when d.fixlen is not null
						then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
						else 0
						end
					|	case when c.status&8 = 8
						then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
						else 0
						end
					|	case 
						when d.type_name = 'timestamp'
							then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
						when (c.status&128) != 128
							then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
						else 0 
						end),
		IS_NULLABLE		= convert(bit,c.status&8),
		DATA_TYPE		= d.oledb_data_type,
		TYPE_GUID		= convert(binary(16),null),
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DATETIME_PRECISION	= convert(int,
						case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
							when data_precision = 23 then 3 else 0 end),
		CHARACTER_SET_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'master' 
						else null 
						end),
		CHARACTER_SET_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'dbo' 
						else null 
						end),
		CHARACTER_SET_NAME	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then a_cha.name 
						else null 
						end),
		COLLATION_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'master' 
						else null 
						end),
		COLLATION_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'dbo' 
						else null 
						end),
		COLLATION_NAME		= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then b_cha.name 
						else null 
						end),
		DOMAIN_CATALOG		= case when t.usertype < 256 then null else db_name() end,
		DOMAIN_SCHEMA		= case when t.usertype < 256 then null else user_name(o.uid) end,
		DOMAIN_NAME		= case when t.usertype < 256 then null else t.name end,
		DESCRIPTION		= convert(varchar(1),null)	
	
	from
		syscolumns c,
		syscomments m,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t,
		sysusers u,
		master.dbo.sysconfigures	cfg,
		master.dbo.syscharsets		a_cha, /* charset/1001, not sortorder. */
		master.dbo.syscharsets		b_cha  /* sortorder/2001, not charset. */
	where
		o.name = @table_name
	and 	o.type in ('U','V','S')
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@column_name is null or @column_name = c.name)
	and 	o.id = c.id
	and 	t.type = d.ss_dtype
	and	(t.usertype != 80 or d.type_name='timestamp')
	and 	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and 	c.usertype = t.usertype
	and	c.cdefault *= m.id
	and	m.colid = 1
	and	cfg.comment = 'default sortorder ID' 
	and	a_cha.type = 1001 /* type is charset */
	and	b_cha.type = 2001 /* type is sortorder */
	and	a_cha.id = b_cha.csid
	and 	b_cha.id = cfg.value
	order by 2, 3, c.colid

	/* Fix ordinal positions */
	OPEN curColRowset
	FETCH NEXT FROM curColRowset
	INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
	
	/* Initialize tracking variables */
	SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL

	WHILE @@FETCH_STATUS = 0
	BEGIN
		/* Check to see if the catalog/schema/table */
		/* has changed since last row               */
		IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
		    @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
		BEGIN
			/* Increment ordinal count */
			SELECT @currPos = @currPos + 1
		END
		ELSE
		BEGIN
			/* Set the catalog/schema/table to new values */
			SELECT @currCatalog = @TABLECATALOG,
			       @currSchema  = @TABLESCHEMA,
			       @currName    = @TABLENAME,
			       @currPos     = 1
		END

		/* Check for holes in the ordinal position order */
		if (@currPos < @ORDINALPOSITION)
		BEGIN		
			/* Update the ordinal position */
			UPDATE #columns_rowset
			SET    ORDINAL_POSITION = @currPos
			WHERE  TABLE_CATALOG = @TABLECATALOG
			  AND  TABLE_SCHEMA  = @TABLESCHEMA
			  AND  TABLE_NAME    = @TABLENAME
			  AND  COLUMN_NAME   = @COLUMNNAME
		END

		/* Fetch next result */
		FETCH NEXT FROM curColRowset
		INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
	END

	/* Cleanup cursor */
	CLOSE curColRowset
	DEALLOCATE curColRowset

	set nocount off

	/* Output the result set */
	select *
	from #columns_rowset
	order by 2, 3, 7
go

dump tran master with no_log
go

create procedure sp_columns_rowset;2
	(
	@table_schema 	varchar(255) = null,
	@column_name	varchar(255) = null
	)
as 	
	set nocount on

	/* Declare bindings */
	DECLARE @TABLECATALOG    sysname,
		@TABLESCHEMA     sysname,
		@TABLENAME       sysname,
		@COLUMNNAME      sysname,
		@ORDINALPOSITION int

	/* Declare tracking variables */
	DECLARE @currCatalog sysname,
 		@currSchema  sysname,
		@currName    sysname,
		@currPos     int

	/* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
	CREATE TABLE #columns_rowset
	(
		TABLE_CATALOG			sysname,
		TABLE_SCHEMA			sysname,
		TABLE_NAME			sysname,
		COLUMN_NAME			sysname,
		COLUMN_GUID			binary(16)		null,
		COLUMN_PROPID			int			null,
		ORDINAL_POSITION		int			null,
		COLUMN_HASDEFAULT		bit,
		COLUMN_DEFAULT			varchar(255),
		COLUMN_FLAGS			int,
		IS_NULLABLE			bit,
		DATA_TYPE			smallint,
		TYPE_GUID			binary(16)		null,
		CHARACTER_MAXIMUM_LENGTH	int			null,
		CHARACTER_OCTET_LENGTH		int			null,
		NUMERIC_PRECISION		smallint		null,
		NUMERIC_SCALE			smallint		null, 
		DATETIME_PRECISION		int			null,
		CHARACTER_SET_CATALOG		sysname			null,
		CHARACTER_SET_SCHEMA		sysname			null,
		CHARACTER_SET_NAME		sysname			null,
		COLLATION_CATALOG		sysname			null,
		COLLATION_SCHEMA		sysname			null,
		COLLATION_NAME			sysname			null,
		DOMAIN_CATALOG			sysname 		null,
		DOMAIN_SCHEMA			sysname 		null,
		DOMAIN_NAME			sysname 		null,
		DESCRIPTION			varchar(1)
	)		

	/* Declare cursor for fixup */
	DECLARE curColRowset CURSOR
	FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
	    FROM #columns_rowset
	    ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION

	/* Store results into the temp table */
	insert into #columns_rowset
		(
		TABLE_CATALOG,
		TABLE_SCHEMA,
		TABLE_NAME,
		COLUMN_NAME,
		COLUMN_GUID,
		COLUMN_PROPID,
		ORDINAL_POSITION,
		COLUMN_HASDEFAULT,
		COLUMN_DEFAULT,
		COLUMN_FLAGS,
		IS_NULLABLE,
		DATA_TYPE,
		TYPE_GUID,
		CHARACTER_MAXIMUM_LENGTH,
		CHARACTER_OCTET_LENGTH,
		NUMERIC_PRECISION,
		NUMERIC_SCALE,
		DATETIME_PRECISION,
		CHARACTER_SET_CATALOG,
		CHARACTER_SET_SCHEMA,
		CHARACTER_SET_NAME,
		COLLATION_CATALOG,
		COLLATION_SCHEMA,
		COLLATION_NAME,
		DOMAIN_CATALOG,
		DOMAIN_SCHEMA,
		DOMAIN_NAME,
		DESCRIPTION
		)
	select
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		COLUMN_NAME		= c.name,
		COLUMN_GUID		= convert(binary(16),null),
		COLUMN_PROPID		= convert(int,null),
		ORDINAL_POSITION	= convert(int, c.colid),
		COLUMN_HASDEFAULT	= convert(bit, case when m.text is null then 0 else 1 end),
		COLUMN_DEFAULT		= substring(m.text,2,datalength(m.text)-2),
		COLUMN_FLAGS		= convert(int,
						case when d.is_long = 1 
						then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
						else 0
						end
					|	case when d.fixlen is not null
						then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
						else 0
						end
					|	case when c.status&8 = 8
						then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
						else 0
						end
					|	case 
						when d.type_name = 'timestamp'
							then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
						when (c.status&128) != 128
							then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
						else 0 
						end),
		IS_NULLABLE		= convert(bit,c.status&8),
		DATA_TYPE		= d.oledb_data_type,
		TYPE_GUID		= convert(binary(16),null),
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DATETIME_PRECISION	= convert(int,
						case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
							when data_precision = 23 then 3 else 0 end),
		CHARACTER_SET_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'master' 
						else null 
						end),
		CHARACTER_SET_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'dbo' 
						else null 
						end),
		CHARACTER_SET_NAME	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then a_cha.name 
						else null 
						end),
		COLLATION_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'master' 
						else null 
						end),
		COLLATION_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then 'dbo' 
						else null 
						end),
		COLLATION_NAME		= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then b_cha.name 
						else null 
						end),
		DOMAIN_CATALOG		= case when t.usertype < 256 then null else db_name() end,
		DOMAIN_SCHEMA		= case when t.usertype < 256 then null else user_name(o.uid) end,
		DOMAIN_NAME		= case when t.usertype < 256 then null else t.name end,
		DESCRIPTION		= convert(varchar(1),null)	
	
	from
		syscolumns c,
		syscomments m,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t,
		sysusers u,
		master.dbo.sysconfigures	cfg,
		master.dbo.syscharsets		a_cha, /* charset/1001, not sortorder. */
		master.dbo.syscharsets		b_cha /* sortorder/2001, not charset. */
	where
	 	o.type in ('U','V','S')
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@column_name is null or @column_name = c.name)
	and 	o.id = c.id
	and 	t.type = d.ss_dtype
	and	(t.usertype != 80 or d.type_name='timestamp')
	and 	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and 	c.usertype = t.usertype
	and	c.cdefault *= m.id
	and	 m.colid = 1
	and	cfg.comment = 'default sortorder ID' 
	and	a_cha.type = 1001 /* type is charset */
	and	b_cha.type = 2001 /* type is sortorder */
	and	a_cha.id = b_cha.csid
	and 	b_cha.id = cfg.value
	order by 2, 3, c.colid
	
	/* Fix ordinal positions */
	OPEN curColRowset
	FETCH NEXT FROM curColRowset
	INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
	
	/* Initialize tracking variables */
	SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL

	WHILE @@FETCH_STATUS = 0
	BEGIN
		/* Check to see if the catalog/schema/table */
		/* has changed since last row               */
		IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
		    @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
		BEGIN
			/* Increment ordinal count */
			SELECT @currPos = @currPos + 1
		END
		ELSE
		BEGIN
			/* Set the catalog/schema/table to new values */
			SELECT @currCatalog = @TABLECATALOG,
			       @currSchema  = @TABLESCHEMA,
			       @currName    = @TABLENAME,
			       @currPos     = 1
		END

		/* Check for holes in the ordinal position order */
		if (@currPos < @ORDINALPOSITION)
		BEGIN		
			/* Update the ordinal position */
			UPDATE #columns_rowset
			SET    ORDINAL_POSITION = @currPos
			WHERE  TABLE_CATALOG = @TABLECATALOG
			  AND  TABLE_SCHEMA  = @TABLESCHEMA
			  AND  TABLE_NAME    = @TABLENAME
			  AND  COLUMN_NAME   = @COLUMNNAME
		END

		/* Fetch next result */
		FETCH NEXT FROM curColRowset
		INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
	END

	/* Cleanup cursor */
	CLOSE curColRowset
	DEALLOCATE curColRowset

	set nocount off

	/* Output the result set */
	select *
	from #columns_rowset
	order by 2, 3, 7
go

dump tran master with no_log
go

if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
else
	drop proc sp_columns_rowset
go

/*	Procedure for 7.0 server */
create procedure sp_columns_rowset
	(
   	@table_name	sysname,
	@table_schema 	sysname = NULL,
	@column_name	sysname = NULL
	)
as
	set nocount on

	/* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
	CREATE TABLE #columns_rowset
	(
		TABLE_CATALOG			sysname,
		TABLE_SCHEMA			sysname,
		TABLE_NAME			sysname,
		COLUMN_NAME			sysname,
		COLUMN_GUID			uniqueidentifier	null,
		COLUMN_PROPID			int			null,
		ORDINAL_POSITION		int			IDENTITY(1,1),
		COLUMN_HASDEFAULT		bit,
		COLUMN_DEFAULT			nvarchar(2000),
		COLUMN_FLAGS			int,
		IS_NULLABLE			bit,
		DATA_TYPE			smallint,
		TYPE_GUID			uniqueidentifier	null,
		CHARACTER_MAXIMUM_LENGTH	int			null,
		CHARACTER_OCTET_LENGTH		int			null,
		NUMERIC_PRECISION		smallint		null,
		NUMERIC_SCALE			smallint		null, 
		DATETIME_PRECISION		int			null,
		CHARACTER_SET_CATALOG		sysname			null,
		CHARACTER_SET_SCHEMA		sysname			null,
		CHARACTER_SET_NAME		sysname			null,
		COLLATION_CATALOG		sysname			null,
		COLLATION_SCHEMA		sysname			null,
		COLLATION_NAME			sysname			null,
		DOMAIN_CATALOG			sysname 		null,
		DOMAIN_SCHEMA			sysname 		null,
		DOMAIN_NAME			sysname 		null,
		DESCRIPTION			nvarchar(1)
	)		

	/* Store results into the temp table */
	insert into #columns_rowset
		(
		TABLE_CATALOG,
		TABLE_SCHEMA,
		TABLE_NAME,
		COLUMN_NAME,
		COLUMN_GUID,
		COLUMN_PROPID,
		COLUMN_HASDEFAULT,
		COLUMN_DEFAULT,
		COLUMN_FLAGS,
		IS_NULLABLE,
		DATA_TYPE,
		TYPE_GUID,
		CHARACTER_MAXIMUM_LENGTH,
		CHARACTER_OCTET_LENGTH,
		NUMERIC_PRECISION,
		NUMERIC_SCALE,
		DATETIME_PRECISION,
		CHARACTER_SET_CATALOG,
		CHARACTER_SET_SCHEMA,
		CHARACTER_SET_NAME,
		COLLATION_CATALOG,
		COLLATION_SCHEMA,
		COLLATION_NAME,
		DOMAIN_CATALOG,
		DOMAIN_SCHEMA,
		DOMAIN_NAME,
		DESCRIPTION
		)
	select
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		COLUMN_NAME		= c.name,
		COLUMN_GUID		= convert(uniqueidentifier,null),
		COLUMN_PROPID		= convert(int,null),
		COLUMN_HASDEFAULT	= convert(bit, case when m.text is null then 0 else 1 end),
		COLUMN_DEFAULT		= convert(nvarchar(2000),
						substring(m.text,2,datalength(m.text)/2-2)),
		COLUMN_FLAGS		= convert(int,
						case when d.is_long = 1 
						then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
						else 0
						end
					|	case when d.fixlen is not null
						then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
						else 0
						end
					|	case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
						then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
						else 0
						end
					|	case 
						when d.type_name = 'timestamp'
							then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
						when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
							then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
						else 0 
						end),
		IS_NULLABLE		= convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
		DATA_TYPE		= d.oledb_data_type,
		TYPE_GUID		= convert(uniqueidentifier,null),
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case 
						when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size,c.length/2)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size*2,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DATETIME_PRECISION	= convert(int,
						case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
							when data_precision = 23 then 3 else 0 end),
		CHARACTER_SET_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'master' 
						else null 
						end),
		CHARACTER_SET_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'dbo' 
						else null 
						end),
		CHARACTER_SET_NAME	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then a_cha.name 
						else null 
						end),
		COLLATION_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'master' 
						else null 
						end),
		COLLATION_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'dbo' 
						else null 
						end),
		COLLATION_NAME		= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then b_cha.name 
						else null 
						end),
		DOMAIN_CATALOG		= case when t.usertype < 256 then null else db_name() end,
		DOMAIN_SCHEMA		= case when t.usertype < 256 then null else user_name(o.uid) end,
		DOMAIN_NAME		= case when t.usertype < 256 then null else t.name end,
		DESCRIPTION		= convert(nvarchar(1),null)	

	from
		syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t,
		master.dbo.sysconfigures	cfg,
		master.dbo.syscharsets		a_cha, /* charset/1001, not sortorder. */
		master.dbo.syscharsets		b_cha /* sortorder/2001, not charset. */
	where
		permissions(o.id, c.name) <> 0
	and	o.name = @table_name
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@column_name is null or @column_name = c.name)
	and 	o.id = c.id
	and 	t.xtype = d.ss_dtype
	and 	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and 	c.xusertype = t.xusertype
	and	cfg.comment = 'default sortorder id' 
	and	a_cha.type = 1001 /* type is charset */
	and	b_cha.type = 2001 /* type is sortorder */
	and	a_cha.id = b_cha.csid
	and 	b_cha.id = cfg.value
	order by 2, 3, c.colorder

	set nocount off

	/* Output the result set */
	select *
	from #columns_rowset
	order by 2, 3, 7
go

dump tran master with no_log
go

/*	Procedure for 7.0 server */
create procedure sp_columns_rowset;2
	(
	@table_schema 	sysname = NULL,
	@column_name	sysname = NULL
	)
as 	
	set nocount on

	/* Declare bindings */
	DECLARE @TABLECATALOG    sysname,
		@TABLESCHEMA     sysname,
		@TABLENAME       sysname,
		@COLUMNNAME      sysname,
		@ORDINALPOSITION int

	/* Declare tracking variables */
	DECLARE @currCatalog sysname,
 		@currSchema  sysname,
		@currName    sysname,
		@currPos     int

	/* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
	CREATE TABLE #columns_rowset
	(
		TABLE_CATALOG			sysname,
		TABLE_SCHEMA			sysname,
		TABLE_NAME			sysname,
		COLUMN_NAME			sysname,
		COLUMN_GUID			uniqueidentifier	null,
		COLUMN_PROPID			int			null,
		ORDINAL_POSITION		int			null,
		COLUMN_HASDEFAULT		bit,
		COLUMN_DEFAULT			nvarchar(2000),
		COLUMN_FLAGS			int,
		IS_NULLABLE			bit,
		DATA_TYPE			smallint,
		TYPE_GUID			uniqueidentifier	null,
		CHARACTER_MAXIMUM_LENGTH	int			null,
		CHARACTER_OCTET_LENGTH		int			null,
		NUMERIC_PRECISION		smallint		null,
		NUMERIC_SCALE			smallint		null, 
		DATETIME_PRECISION		int			null,
		CHARACTER_SET_CATALOG		sysname			null,
		CHARACTER_SET_SCHEMA		sysname			null,
		CHARACTER_SET_NAME		sysname			null,
		COLLATION_CATALOG		sysname			null,
		COLLATION_SCHEMA		sysname			null,
		COLLATION_NAME			sysname			null,
		DOMAIN_CATALOG			sysname 		null,
		DOMAIN_SCHEMA			sysname 		null,
		DOMAIN_NAME			sysname 		null,
		DESCRIPTION			nvarchar(1)
	)		

	/* Declare cursor for fixup */
	DECLARE curColRowset CURSOR
	FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
	    FROM #columns_rowset
	    ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION

	/* Store results into the temp table */
	insert into #columns_rowset
		(
		TABLE_CATALOG,
		TABLE_SCHEMA,
		TABLE_NAME,
		COLUMN_NAME,
		COLUMN_GUID,
		COLUMN_PROPID,
		ORDINAL_POSITION,
		COLUMN_HASDEFAULT,
		COLUMN_DEFAULT,
		COLUMN_FLAGS,
		IS_NULLABLE,
		DATA_TYPE,
		TYPE_GUID,
		CHARACTER_MAXIMUM_LENGTH,
		CHARACTER_OCTET_LENGTH,
		NUMERIC_PRECISION,
		NUMERIC_SCALE,
		DATETIME_PRECISION,
		CHARACTER_SET_CATALOG,
		CHARACTER_SET_SCHEMA,
		CHARACTER_SET_NAME,
		COLLATION_CATALOG,
		COLLATION_SCHEMA,
		COLLATION_NAME,
		DOMAIN_CATALOG,
		DOMAIN_SCHEMA,
		DOMAIN_NAME,
		DESCRIPTION
		)

	select
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		COLUMN_NAME		= c.name,
		COLUMN_GUID		= convert(uniqueidentifier,null),
		COLUMN_PROPID		= convert(int,null),
		ORDINAL_POSITION	= convert(int, c.colorder),
		COLUMN_HASDEFAULT	= convert(bit, case when m.text is null then 0 else 1 end),
		COLUMN_DEFAULT		= convert(nvarchar(2000),
						substring(m.text,2,datalength(m.text)/2-2)),
		COLUMN_FLAGS		= convert(int,
						case when d.is_long = 1 
						then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
						else 0
						end
					|	case when d.fixlen is not null
						then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
						else 0
						end
					|	case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
						then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
						else 0
						end
					|	case 
						when d.type_name = 'timestamp'
							then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
						when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
							then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
						else 0 
						end),
		IS_NULLABLE		= convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
		DATA_TYPE		= d.oledb_data_type,
		TYPE_GUID		= convert(uniqueidentifier,null),
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case 
						when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size,c.length/2)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size*2,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DATETIME_PRECISION	= convert(int,
						case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
							when data_precision = 23 then 3 else 0 end),
		CHARACTER_SET_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'master' 
						else null 
						end),
		CHARACTER_SET_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'dbo' 
						else null 
						end),
		CHARACTER_SET_NAME	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then a_cha.name 
						else null 
						end),
		COLLATION_CATALOG	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'master' 
						else null 
						end),
		COLLATION_SCHEMA	= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then N'dbo' 
						else null 
						end),
		COLLATION_NAME		= convert(sysname,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/
						then b_cha.name 
						else null 
						end),
		DOMAIN_CATALOG		= case when t.usertype < 256 then null else db_name() end,
		DOMAIN_SCHEMA		= case when t.usertype < 256 then null else user_name(o.uid) end,
		DOMAIN_NAME		= case when t.usertype < 256 then null else t.name end,
		DESCRIPTION		= convert(nvarchar(1),null)	
	
	from
		syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t,
		master.dbo.sysconfigures	cfg,
		master.dbo.syscharsets		a_cha, /* charset/1001, not sortorder.*/
		master.dbo.syscharsets		b_cha /* sortorder/2001, not charset.*/
	where
		permissions(o.id, c.name) <> 0
	and 	o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@column_name is null or @column_name = c.name)
	and 	o.id = c.id
	and 	t.xtype = d.ss_dtype
	and 	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and 	c.xusertype = t.xusertype
	and	cfg.comment = 'default sortorder id' 
	and 	a_cha.type = 1001 /* type is charset */
	and 	b_cha.type = 2001 /* type is sortorder */
	and	a_cha.id = b_cha.csid
	and 	b_cha.id = cfg.value
	order by 2, 3, c.colorder

	/* Fix ordinal positions */
	OPEN curColRowset
	FETCH NEXT FROM curColRowset
	INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
	
	/* Initialize tracking variables */
	SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL

	WHILE @@FETCH_STATUS = 0
	BEGIN
		/* Check to see if the catalog/schema/table */
		/* has changed since last row               */
		IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
		    @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
		BEGIN
			/* Increment ordinal count */
			SELECT @currPos = @currPos + 1
		END
		ELSE
		BEGIN
			/* Set the catalog/schema/table to new values */
			SELECT @currCatalog = @TABLECATALOG,
			       @currSchema  = @TABLESCHEMA,
			       @currName    = @TABLENAME,
			       @currPos     = 1
		END

		/* Check for holes in the ordinal position order */
		if (@currPos < @ORDINALPOSITION)
		BEGIN		
			/* Update the ordinal position */
			UPDATE #columns_rowset
			SET    ORDINAL_POSITION = @currPos
			WHERE  TABLE_CATALOG = @TABLECATALOG
			  AND  TABLE_SCHEMA  = @TABLESCHEMA
			  AND  TABLE_NAME    = @TABLENAME
			  AND  COLUMN_NAME   = @COLUMNNAME
		END

		/* Fetch next result */
		FETCH NEXT FROM curColRowset
		INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
	END

	/* Cleanup cursor */
	CLOSE curColRowset
	DEALLOCATE curColRowset

	set nocount off

	/* Output the result set */
	select 	*
	from #columns_rowset
	order by 2, 3, 7
go

dump tran master with no_log
go

/*	Procedure for 7.0 server */
create procedure sp_columns_rowset;5
	(
	@table_server		sysname,
	@table_catalog		sysname = null,
	@table_name		sysname = null,
	@table_schema		sysname = null,
	@column_name		sysname = null
	)
as
	select
		TABLE_CATALOG,
		TABLE_SCHEMA,
		TABLE_NAME,
		COLUMN_NAME,
		COLUMN_GUID,
		COLUMN_PROPID,
		ORDINAL_POSITION,
		COLUMN_HASDEFAULT,
		COLUMN_DEFAULT,
		COLUMN_FLAGS,
		IS_NULLABLE,
		DATA_TYPE,
		TYPE_GUID,
		CHARACTER_MAXIMUM_LENGTH,
		CHARACTER_OCTET_LENGTH,
		NUMERIC_PRECISION,
		NUMERIC_SCALE,
		DATETIME_PRECISION,
		CHARACTER_SET_CATALOG,
		CHARACTER_SET_SCHEMA,
		CHARACTER_SET_NAME,
		COLLATION_CATALOG,
		COLLATION_SCHEMA,
		COLLATION_NAME,
		DOMAIN_CATALOG,
		DOMAIN_SCHEMA,
		DOMAIN_NAME,
		DESCRIPTION
	from master.dbo.SYSREMOTE_COLUMNS <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name,
				@column_name > 
	order by 1, 2, 3, 7
go


dump tran master with no_log
go

grant execute on sp_columns_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_columns_rowset' and type = 'P '))
		begin
		drop procedure sp_columns_rowset
		dump tran master with no_log
		end
	end
go

print ''
print 'creating sp_foreign_keys_rowset'
go

/*	Procedure for 6.0 and 6.5 server */
CREATE PROCEDURE sp_foreign_keys_rowset
	(
 	@pk_table_name		varchar(255) = null,
	@pk_table_schema	varchar(255) = null,
	@pk_table_catalog	varchar(255) = null,
	@fk_table_name		varchar(255) = null,
	@fk_table_schema	varchar(255) = null,
	@fk_table_catalog	varchar(255) = null
	)
as
IF @pk_table_name is not null
	BEGIN
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(binary(16),null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(binary(16),null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,1),
		UPDATE_RULE			= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey1
	and	r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey1
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,2),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
 	and	o1.id = c1.id
	and	c1.colid = r.rkey2
	and	r.keycnt >= 2
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey2
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,3),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey3
	and	r.keycnt >= 3
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey3
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,4),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 4
	and	o1.id = c1.id
	and	c1.colid = r.rkey4
	and	r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey4
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,5),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 5
	and	o1.id = c1.id
	and	c1.colid = r.rkey5
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey5
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,6),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 6
	and	o1.id = c1.id
	and	c1.colid = r.rkey6
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey6
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,7),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 7
	and	o1.id = c1.id
	and	c1.colid = r.rkey7
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey7
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,8),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 8
	and	o1.id = c1.id
	and	c1.colid = r.rkey8
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey8
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,9),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 9
	and	o1.id = c1.id
	and	c1.colid = r.rkey9
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey9
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,10),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/	
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 10
	and	o1.id = c1.id
	and	c1.colid = r.rkey10
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey10
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,11),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 11
	and	o1.id = c1.id
	and	c1.colid = r.rkey11
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey11
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,12),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 12
	and	o1.id = c1.id
	and	c1.colid = r.rkey12
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey12
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,13),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 13
	and	o1.id = c1.id
	and	c1.colid = r.rkey13
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey13
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,14),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 14
	and	o1.id = c1.id
	and	c1.colid = r.rkey14
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey14
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,15),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 15
	and	o1.id = c1.id
	and	c1.colid = r.rkey15
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey15
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,16),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 16
	and	o1.id = c1.id
	and	c1.colid = r.rkey16
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey16
	order by 8,9,2,3,13
	END
ELSE IF @fk_table_name is not null
	BEGIN
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(binary(16),null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(binary(16),null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,1),
		UPDATE_RULE			= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	o2.id = c2.id
	and	c2.colid = r.fkey1
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey1
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,2),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 2
	and	o2.id = c2.id
	and	c2.colid = r.fkey2
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey2
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,3),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 3
	and	o2.id = c2.id
	and	c2.colid = r.fkey3
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey3
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,4),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 4
	and	o2.id = c2.id
	and	c2.colid = r.fkey4
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey4
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,5),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 5
	and	o2.id = c2.id
	and	c2.colid = r.fkey5
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey5
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,6),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 6
	and	o2.id = c2.id
	and	c2.colid = r.fkey6
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey6
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,7),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 7
	and	o2.id = c2.id
	and	c2.colid = r.fkey7
	and	r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey7
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,8),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 8
	and	o2.id = c2.id
	and	c2.colid = r.fkey8
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey8
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,9),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 9
	and	o2.id = c2.id
	and	c2.colid = r.fkey9
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey9
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,10),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 10
	and	o2.id = c2.id
	and	c2.colid = r.fkey10
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey10
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,11),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 11
	and	o2.id = c2.id
	and	c2.colid = r.fkey11
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey11
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,12),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 12
	and	o2.id = c2.id
	and	c2.colid = r.fkey12
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey12
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,13),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 13
	and	o2.id = c2.id
	and	c2.colid = r.fkey13
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey13
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,14),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 14
	and	o2.id = c2.id
	and	c2.colid = r.fkey14
	and	r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey14
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,15),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 15
	and	o2.id = c2.id
	and	c2.colid = r.fkey15
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey15
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,16),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 16
	and	o2.id = c2.id
	and	c2.colid = r.fkey16
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey16
	order by 8,9,2,3,13
	END
ELSE
	BEGIN
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(binary(16),null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(binary(16),null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,1),
		UPDATE_RULE			= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey1
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey1
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,2),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 2
	and	o1.id = c1.id
	and	c1.colid = r.rkey2
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey2
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,3),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 3
	and	o1.id = c1.id
	and	c1.colid = r.rkey3
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey3
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,4),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 4
	and	o1.id = c1.id
	and	c1.colid = r.rkey4
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey4
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,5),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 5
	and	o1.id = c1.id
	and	c1.colid = r.rkey5
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey5
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,6),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 6
	and	o1.id = c1.id
	and	c1.colid = r.rkey6
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey6
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,7),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 7
	and	o1.id = c1.id
	and	c1.colid = r.rkey7
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey7
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,8),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 8
	and	o1.id = c1.id
	and	c1.colid = r.rkey8
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey8
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,9),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 9
	and	o1.id = c1.id
	and	c1.colid = r.rkey9
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey9
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,10),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 10
	and	o1.id = c1.id
	and	c1.colid = r.rkey10
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey10
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,11),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 11
	and	o1.id = c1.id
	and	c1.colid = r.rkey11
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey11
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,12),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 12
	and	o1.id = c1.id
	and	c1.colid = r.rkey12
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey12
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,13),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 13
	and	o1.id = c1.id
	and	c1.colid = r.rkey13
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey13
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,14),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 14
	and	o1.id = c1.id
	and	c1.colid = r.rkey14
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey14
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,15),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 15
	and	o1.id = c1.id
	and	c1.colid = r.rkey15
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey15
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,16),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	r.keycnt >= 16
	and	c1.colid = r.rkey16
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey16
	order by 8,9,2,3,13
	END
go
dump tran master with no_log
go
CREATE PROCEDURE sp_foreign_keys_rowset;2
	(
   	@handle			int output,
   	@scrollopt		int output,
	@ccopt			int output,
	@rows			int output,
 	@pk_table_name		varchar(255) = null,
	@pk_table_schema	varchar(255) = null,
	@pk_table_catalog	varchar(255) = null,
	@fk_table_name		varchar(255) = null,
	@fk_table_schema	varchar(255) = null,
	@fk_table_catalog	varchar(255) = null
	)
as
	declare @ret int
	
	SET NOCOUNT ON

	create table #spfkeysrowset1
		(
		PK_TABLE_CATALOG	sysname not null,
		PK_TABLE_SCHEMA		sysname not null,
		PK_TABLE_NAME 		sysname not null,
		PK_COLUMN_NAME 		sysname not null,
		PK_COLUMN_GUID		binary(16) null,
		PK_COLUMN_PROPID	int null,
		FK_TABLE_CATALOG	sysname not null,
		FK_TABLE_SCHEMA		sysname not null,
		FK_TABLE_NAME 		sysname not null,
		FK_COLUMN_NAME 		sysname not null,
		FK_COLUMN_GUID		binary(16) null,
		FK_COLUMN_PROPID	int null,
		ORDINAL				int not null,
		UPDATE_RULE			sysname not null,
		DELETE_RULE 		sysname not null,
		PK_NAME				sysname not null,
		FK_NAME				sysname not null,
		DEFERRABILITY		smallint not null		
		)

IF @pk_table_name is not null
	BEGIN
	insert into #spfkeysrowset1
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,1),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey1
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey1
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,2),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey2
	and	r.keycnt >= 2
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey2
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,3),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey3
	and	r.keycnt >= 3
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey3
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,4),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 4
	and	o1.id = c1.id
	and	c1.colid = r.rkey4
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey4
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,5),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 5
	and	o1.id = c1.id
	and	c1.colid = r.rkey5
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey5
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,6),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 6
	and	o1.id = c1.id
	and	c1.colid = r.rkey6
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey6
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,7),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 7
	and	o1.id = c1.id
	and	c1.colid = r.rkey7
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey7
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,8),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 8
	and	o1.id = c1.id
	and	c1.colid = r.rkey8
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey8
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,9),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 9
	and	o1.id = c1.id
	and	c1.colid = r.rkey9
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey9
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,10),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 10
	and	o1.id = c1.id
	and	c1.colid = r.rkey10
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey10
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,11),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 11
	and	o1.id = c1.id
	and	c1.colid = r.rkey11
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey11
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,12),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 12
	and	o1.id = c1.id
	and	c1.colid = r.rkey12
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey12
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,13),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 13
	and	o1.id = c1.id
	and	c1.colid = r.rkey13
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey13
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,14),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 14
	and	o1.id = c1.id
	and	c1.colid = r.rkey14
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey14
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,15),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 15
	and	o1.id = c1.id
	and	c1.colid = r.rkey15
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey15
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,16),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 16
	and	o1.id = c1.id
	and	c1.colid = r.rkey16
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey16
	order by 8,9,2,3,13
	END
ELSE IF @fk_table_name is not null
	BEGIN
	insert into #spfkeysrowset1
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,1),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	o2.id = c2.id
	and	c2.colid = r.fkey1
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey1
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,2),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 2
	and	o2.id = c2.id
	and	c2.colid = r.fkey2
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey2
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,3),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 3
	and	o2.id = c2.id
	and	c2.colid = r.fkey3
	and	r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey3
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,4),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 4
	and	o2.id = c2.id
	and	c2.colid = r.fkey4
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey4
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,5),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 5
	and	o2.id = c2.id
	and	c2.colid = r.fkey5
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey5
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,6),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 6
	and	o2.id = c2.id
	and	c2.colid = r.fkey6
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey6
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,7),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 7
	and	o2.id = c2.id
	and	c2.colid = r.fkey7
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey7
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,8),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 8
	and	o2.id = c2.id
	and	c2.colid = r.fkey8
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey8
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,9),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 9
	and	o2.id = c2.id
	and	c2.colid = r.fkey9
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey9
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,10),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 10
	and	o2.id = c2.id
	and	c2.colid = r.fkey10
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey10
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,11),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 11
	and	o2.id = c2.id
	and	c2.colid = r.fkey11
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey11
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,12),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 12
	and	o2.id = c2.id
	and	c2.colid = r.fkey12
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey12
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,13),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 13
	and	o2.id = c2.id
	and	c2.colid = r.fkey13
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey13
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,14),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 14
	and	o2.id = c2.id
	and	c2.colid = r.fkey14
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey14
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,15),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 15
	and	o2.id = c2.id
	and	c2.colid = r.fkey15
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey15
	union	all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,16),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 16
	and	o2.id = c2.id
	and	c2.colid = r.fkey16
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey16
	order by 8,9,2,3,13
	END
ELSE
	BEGIN
	insert into #spfkeysrowset1
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,1),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey1
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey1
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,2),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 2
	and	o1.id = c1.id
	and	c1.colid = r.rkey2
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey2
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,3),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 3
	and	o1.id = c1.id
	and	c1.colid = r.rkey3
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey3
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,4),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 4
	and	o1.id = c1.id
	and	c1.colid = r.rkey4
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey4
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,5),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 5
	and	o1.id = c1.id
	and	c1.colid = r.rkey5
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey5
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,6),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 6
	and	o1.id = c1.id
	and	c1.colid = r.rkey6
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey6
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,7),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 7
	and	o1.id = c1.id
	and	c1.colid = r.rkey7
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey7
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,8),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 8
	and	o1.id = c1.id
	and	c1.colid = r.rkey8
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey8
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,9),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 9
	and	o1.id = c1.id
	and	c1.colid = r.rkey9
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey9
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,10),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 10
	and	o1.id = c1.id
	and	c1.colid = r.rkey10
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey10
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,11),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 11
	and	o1.id = c1.id
	and	c1.colid = r.rkey11
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey11
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,12),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 12
	and	o1.id = c1.id
	and	c1.colid = r.rkey12
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey12
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,13),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 13
	and	o1.id = c1.id
	and	c1.colid = r.rkey13
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey13
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,14),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 14
	and	o1.id = c1.id
	and	c1.colid = r.rkey14
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey14
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,15),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 15
	and	o1.id = c1.id
	and	c1.colid = r.rkey15
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey15
	union all
	select
		db_name(r.rkeydbid),
		user_name(o1.uid),
		o1.name,
		c1.name,
		convert(binary(16),null),
		convert(int,null),
		db_name(r.fkeydbid),
		user_name(o2.uid),
		o2.name,
		c2.name,
		convert(binary(16),null),
		convert(int,null),
		convert(int,16),
		'NO ACTION',
		'NO ACTION',
		object_name(s.constid),
		object_name(r.constid),
		convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	r.keycnt >= 16
	and	c1.colid = r.rkey16
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey16
	order by 8,9,2,3,13
	END

	exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
		@scrollopt output, @ccopt output, @rows output

	drop table #spfkeysrowset1
   	return isnull(@ret,0)
go
dump tran master with no_log
go
CREATE PROCEDURE sp_foreign_keys_rowset;3
as
	select
		PK_TABLE_CATALOG	= convert(sysname,' '),
		PK_TABLE_SCHEMA		= convert(sysname,' '),
		PK_TABLE_NAME 		= convert(sysname,' '),
		PK_COLUMN_NAME 		= convert(sysname,' '),
		PK_COLUMN_GUID		= convert(binary(16),null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= convert(sysname,' '),
		FK_TABLE_SCHEMA		= convert(sysname,' '),
		FK_TABLE_NAME 		= convert(sysname,' '),
		FK_COLUMN_NAME 		= convert(sysname,' '),
		FK_COLUMN_GUID		= convert(binary(16),null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,1),
		UPDATE_RULE			= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION',
		PK_NAME				= convert(sysname, ' '),
		FK_NAME				= convert(sysname, ' '),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	where	1=0
go
dump tran master with no_log
go


if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
else
	drop proc sp_foreign_keys_rowset
go


/*	Procedure for 7.0 server */
CREATE PROCEDURE sp_foreign_keys_rowset
	(
	@pk_table_name		sysname,
	@pk_table_schema	sysname = null,
	@fk_table_name		sysname = null,
	@fk_table_schema	sysname = null,
	@fk_table_catalog	sysname = null
	)
as
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,1),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey1
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey1
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,2),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey2
	and	r.keycnt >= 2
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey2
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,3),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey3
	and	r.keycnt >= 3
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey3
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,4),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 4
	and	o1.id = c1.id
	and	c1.colid = r.rkey4
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey4
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,5),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 5
	and	o1.id = c1.id
	and	c1.colid = r.rkey5
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey5
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,6),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 6
	and	o1.id = c1.id
	and	c1.colid = r.rkey6
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey6
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,7),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 7
	and	o1.id = c1.id
	and	c1.colid = r.rkey7
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey7
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,8),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 8
	and	o1.id = c1.id
	and	c1.colid = r.rkey8
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey8
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,9),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 9
	and	o1.id = c1.id
	and	c1.colid = r.rkey9
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey9
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,10),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 10
	and	o1.id = c1.id
	and	c1.colid = r.rkey10
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey10
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,11),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 11
	and	o1.id = c1.id
	and	c1.colid = r.rkey11
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey11
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,12),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 12
	and	o1.id = c1.id
	and	c1.colid = r.rkey12
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey12
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,13),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 13
	and	o1.id = c1.id
	and	c1.colid = r.rkey13
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey13
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,14),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 14
	and	o1.id = c1.id
	and	c1.colid = r.rkey14
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey14
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,15),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 15
	and	o1.id = c1.id
	and	c1.colid = r.rkey15
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey15
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,16),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	o1.name = @pk_table_name
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 16
	and	o1.id = c1.id
	and	c1.colid = r.rkey16
	and r.fkeyid = o2.id
	and	(@fk_table_name is null or @fk_table_name = o2.name)
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey16
	order by 8,9,2,3,13
go
dump tran master with no_log
go
CREATE PROCEDURE sp_foreign_keys_rowset;2
	(
	@fk_table_name		sysname,
	@fk_table_schema	sysname = null,
	@pk_table_name		sysname = null,
	@pk_table_schema	sysname = null,
	@pk_table_catalog	sysname = null
	)
as
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,1),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	o2.id = c2.id
	and	c2.colid = r.fkey1
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey1
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,2),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 2
	and	o2.id = c2.id
	and	c2.colid = r.fkey2
	and	r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey2
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,3),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 3
	and	o2.id = c2.id
	and	c2.colid = r.fkey3
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey3
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,4),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 4
	and	o2.id = c2.id
	and	c2.colid = r.fkey4
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey4
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,5),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 5
	and	o2.id = c2.id
	and	c2.colid = r.fkey5
	and	r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey5
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,6),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 6
	and	o2.id = c2.id
	and	c2.colid = r.fkey6
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey6
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,7),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 7
	and	o2.id = c2.id
	and	c2.colid = r.fkey7
	and	r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey7
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,8),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 8
	and	o2.id = c2.id
	and	c2.colid = r.fkey8
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey8
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,9),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 9
	and	o2.id = c2.id
	and	c2.colid = r.fkey9
	and	r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey9
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,10),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 10
	and	o2.id = c2.id
	and	c2.colid = r.fkey10
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey10
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA 	= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,11),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 11
	and	o2.id = c2.id
	and	c2.colid = r.fkey11
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey11
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,12),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 12
	and	o2.id = c2.id
	and	c2.colid = r.fkey12
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey12
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,13),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 13
	and	o2.id = c2.id
	and	c2.colid = r.fkey13
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey13
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,14),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 14
	and	o2.id = c2.id
	and	c2.colid = r.fkey14
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey14
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,15),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 15
	and	o2.id = c2.id
	and	c2.colid = r.fkey15
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey15
	union	all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,16),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and	o2.name = @fk_table_name
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = r.fkeyid
	and	r.keycnt >= 16
	and	o2.id = c2.id
	and	c2.colid = r.fkey16
	and r.rkeyid = o1.id
	and s.id = o1.id and (s.status & 0xf) = 1
	and	(@pk_table_name is null or @pk_table_name = o1.name)
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = c1.id	
	and	c1.colid = r.rkey16
	order by 8,9,2,3,13
go
dump tran master with no_log
go
CREATE PROCEDURE sp_foreign_keys_rowset;3
	(
	@pk_table_schema	sysname = null,
	@pk_table_catalog	sysname = null,
	@fk_table_schema	sysname = null,
	@fk_table_catalog	sysname = null
	)
as
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,1),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	c1.colid = r.rkey1
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey1
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,2),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 2
	and	o1.id = c1.id
	and	c1.colid = r.rkey2
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey2
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,3),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 3
	and	o1.id = c1.id
	and	c1.colid = r.rkey3
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey3
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,4),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 4
	and	o1.id = c1.id
	and	c1.colid = r.rkey4
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey4
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,5),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 5
	and	o1.id = c1.id
	and	c1.colid = r.rkey5
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey5
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,6),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 6
	and	o1.id = c1.id
	and	c1.colid = r.rkey6
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey6
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,7),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 7
	and	o1.id = c1.id
	and	c1.colid = r.rkey7
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey7
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,8),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 8
	and	o1.id = c1.id
	and	c1.colid = r.rkey8
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey8
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,9),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 9
	and	o1.id = c1.id
	and	c1.colid = r.rkey9
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey9
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,10),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 10
	and	o1.id = c1.id
	and	c1.colid = r.rkey10
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey10
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,11),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 11
	and	o1.id = c1.id
	and	c1.colid = r.rkey11
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey11
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,12),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 12
	and	o1.id = c1.id
	and	c1.colid = r.rkey12
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey12
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,13),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 13
	and	o1.id = c1.id
	and	c1.colid = r.rkey13
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey13
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,14),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 14
	and	o1.id = c1.id
	and	c1.colid = r.rkey14
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey14
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,15),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	r.keycnt >= 15
	and	o1.id = c1.id
	and	c1.colid = r.rkey15
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey15
	union all
	select
		PK_TABLE_CATALOG	= db_name(r.rkeydbid),
		PK_TABLE_SCHEMA		= user_name(o1.uid),
		PK_TABLE_NAME 		= o1.name,
		PK_COLUMN_NAME 		= c1.name,
		PK_COLUMN_GUID		= convert(uniqueidentifier,null),
		PK_COLUMN_PROPID	= convert(int,null),
		FK_TABLE_CATALOG	= db_name(r.fkeydbid),
		FK_TABLE_SCHEMA		= user_name(o2.uid),
		FK_TABLE_NAME 		= o2.name,
		FK_COLUMN_NAME 		= c2.name,
		FK_COLUMN_GUID		= convert(uniqueidentifier,null),
		FK_COLUMN_PROPID	= convert(int,null),
		ORDINAL				= convert(int,16),
		UPDATE_RULE			= N'NO ACTION',
		DELETE_RULE 		= N'NO ACTION',
		PK_NAME				= object_name(s.constid),
		FK_NAME				= object_name(r.constid),
		DEFERRABILITY		= convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r, sysconstraints s
	where	
		(@pk_table_catalog is null or @pk_table_catalog = db_name())
	and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
	and	(@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
	and	o1.id = r.rkeyid
	and s.id = o1.id and (s.status & 0xf) = 1
	and	o1.id = c1.id
	and	r.keycnt >= 16
	and	c1.colid = r.rkey16
	and r.fkeyid = o2.id
	and	(@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
	and	o2.id = c2.id	
	and	c2.colid = r.fkey16
	order by 8,9,2,3,13
go
dump tran master with no_log
go
create procedure sp_foreign_keys_rowset;5
	(
	@server_name		sysname,
	@pk_catalog_name	sysname = null,
	@fk_catalog_name	sysname = null,
	@pk_table_name		sysname = null,
	@pk_table_schema	sysname = null,
	@fk_table_name		sysname = null,
	@fk_table_schema	sysname = null
	)
as
	select
		PK_TABLE_CATALOG,
		PK_TABLE_SCHEMA,	
		PK_TABLE_NAME, 	
		PK_COLUMN_NAME, 	
		PK_COLUMN_GUID,	
		PK_COLUMN_PROPID,
		FK_TABLE_CATALOG,
		FK_TABLE_SCHEMA, 
		FK_TABLE_NAME, 	
		FK_COLUMN_NAME, 	
		FK_COLUMN_GUID,	
		FK_COLUMN_PROPID,
		ORDINAL,		
		UPDATE_RULE,	
		DELETE_RULE
		--PK_NAME,		
		--FK_NAME,
		--DEFERRABILITY	
	from master.dbo.SYSREMOTE_FOREIGN_KEYS <
				@server_name,
				@pk_catalog_name,
				@pk_table_schema,
				@pk_table_name,
				@fk_catalog_name,
				@fk_table_schema,
				@fk_table_name >
	order by 7,8,9,1,2,3,13
go

grant execute on sp_foreign_keys_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_foreign_keys_rowset' and type = 'P '))
		begin
		drop procedure sp_foreign_keys_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_indexes_rowset'
go

/*	6.0 and 6.5 version */
create procedure sp_indexes_rowset
	(
	@table_name	varchar(255), 
	@index_name	varchar(255) = null,
	@table_schema	varchar(255) = null	 
	)
as
	select	TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		INDEX_CATALOG		= db_name(),		
		INDEX_SCHEMA		= user_name(o.uid),
		INDEX_NAME		= x.name,
		PRIMARY_KEY		= convert(bit,(x.status & 0x800)/0x800),
		"UNIQUE"		= convert(bit,(x.status & 2)/2),
		"CLUSTERED"		= convert(bit,(x.status & 16)/16),
		"TYPE"			= convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
		FILL_FACTOR		= convert(int, x.OrigFillFactor),
		INITIAL_SIZE		= convert(int,null),
		NULLS			= convert(int,null),
		SORT_BOOKMARKS		= convert(bit,0),
		AUTO_UPDATE		= convert(bit,1),
		NULL_COLLATION		= convert(int,4 /*DBPROPVAL_NC_LOW*/),
		ORDINAL_POSITION	= convert(int,c.colid),
		COLUMN_NAME		= index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
		COLUMN_GUID		= convert(binary(16),null),
		COLUMN_PROPID		= convert(int,null),
		COLLATION		= convert(smallint, 1 /*DB_COLLATION_ASC*/),
		CARDINALITY		= case when (x.status & 2) = 2 then x.rows else null end,
		PAGES			= convert(int, x.dpages),
		FILTER_CONDITION	= convert(varchar(1),null),
		INTEGRATED		= convert(bit,(x.status & 16)/16)
		
	from	sysobjects o, sysindexes x, syscolumns c, sysusers u
	where	o.type in ('U')
	and 	o.name = @table_name
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@index_name is null or @index_name = x.name)
	and 	x.id = o.id
	and 	x.id = c.id
	and  	c.colid < x.keycnt+(x.status&16)/16
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )
	order by 8 desc, 4, 5, 6, 17
go
dump tran master with no_log
go
create procedure sp_indexes_rowset;2
	(
	@index_name	varchar(255) = null,
	@table_schema	varchar(255) = null	 
	)
as
	select	TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		INDEX_CATALOG		= db_name(),		
		INDEX_SCHEMA		= user_name(o.uid),
		INDEX_NAME		= x.name,
		PRIMARY_KEY		= convert(bit,(x.status & 0x800)/0x800),
		"UNIQUE"		= convert(bit,(x.status & 2)/2),
		"CLUSTERED"		= convert(bit,(x.status & 16)/16),
		"TYPE"			= convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
		FILL_FACTOR		= convert(int, x.OrigFillFactor),
		INITIAL_SIZE		= convert(int,null),
		NULLS			= convert(int,null),
		SORT_BOOKMARKS		= convert(bit,0),
		AUTO_UPDATE		= convert(bit,1),
		NULL_COLLATION		= convert(int,4 /*DBPROPVAL_NC_LOW*/),
		ORDINAL_POSITION	= convert(int,c.colid),
		COLUMN_NAME		= index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
		COLUMN_GUID		= convert(binary(16),null),
		COLUMN_PROPID		= convert(int,null),
		COLLATION		= convert(smallint, 1 /*DB_COLLATION_ASC*/),
		CARDINALITY		= case when (x.status & 2) = 2 then x.rows else null end,
		PAGES			= convert(int, x.dpages),
		FILTER_CONDITION	= convert(varchar(1),null),
		INTEGRATED		= convert(bit,(x.status & 16)/16) 
		
	from	sysobjects o, sysindexes x, syscolumns c, sysusers u
	where	o.type in ('U')
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@index_name is null or @index_name = x.name)
	and 	x.id = o.id
	and 	x.id = c.id
	and  	c.colid < x.keycnt+(x.status&16)/16
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )
	order by 8 desc, 4, 5, 6, 17
go
dump tran master with no_log
go


if (charindex('7.00', @@version) > 0)
	drop procedure sp_indexes_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/* 	7.0 version */
create procedure sp_indexes_rowset
	(
	@table_name	sysname, 
	@index_name	sysname = null,
	@table_schema	sysname = null	 
	)
as
	select	TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		INDEX_CATALOG		= db_name(),		
		INDEX_SCHEMA		= user_name(o.uid),
		INDEX_NAME		= x.name,
		PRIMARY_KEY		= convert(bit,(x.status & 0x800)/0x800),
		"UNIQUE"		= convert(bit,(x.status & 2)/2),
		"CLUSTERED"		= convert(bit,(x.status & 16)/16),
		"TYPE"			= convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
		FILL_FACTOR		= convert(int, x.OrigFillFactor),
		INITIAL_SIZE		= convert(int,null),
		NULLS			= convert(int,null),
		SORT_BOOKMARKS		= convert(bit,0),
		AUTO_UPDATE		= convert(bit,1),
		NULL_COLLATION		= convert(int,4 /*DBPROPVAL_NC_LOW*/),
		ORDINAL_POSITION	= convert(int,c.colid),
		COLUMN_NAME		= index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
		COLUMN_GUID		= convert(uniqueidentifier,null),
		COLUMN_PROPID		= convert(int,null),
		COLLATION		= convert(smallint, 1 /*DB_COLLATION_ASC*/),
		CARDINALITY		= case when (x.status & 2) = 2 then x.rows else null end,
		PAGES			= convert(int, x.dpages),
		FILTER_CONDITION	= convert(nvarchar(1),null),
		INTEGRATED		= convert(bit,(x.status & 16)/16) 
		
	from	sysobjects o, sysindexes x, syscolumns c
	where	o.type in ('U')
	and 	o.name = @table_name
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@index_name is null or @index_name = x.name)
	and 	x.id = o.id
	and 	x.id = c.id
	and  	c.colid < x.keycnt+(x.status&16)/16
	and	index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
	and 	permissions(o.id) <> 0
	and     (x.status&32) = 0  -- No hypothetical indexes
	order by 8 desc, 4, 5, 6, 17
go
dump tran master with no_log
go
create procedure sp_indexes_rowset;2
	(
	@index_name	sysname = null,
	@table_schema	sysname = null	 
	)
as
	select	TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		INDEX_CATALOG		= db_name(),		
		INDEX_SCHEMA		= user_name(o.uid),
		INDEX_NAME		= x.name,
		PRIMARY_KEY		= convert(bit,(x.status & 0x800)/0x800),
		"UNIQUE"		= convert(bit,(x.status & 2)/2),
		"CLUSTERED"		= convert(bit,(x.status & 16)/16),
		"TYPE"			= convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
		FILL_FACTOR		= convert(int, x.OrigFillFactor),
		INITIAL_SIZE		= convert(int,null),
		NULLS			= convert(int,null),
		SORT_BOOKMARKS		= convert(bit,0),
		AUTO_UPDATE		= convert(bit,1),
		NULL_COLLATION		= convert(int,4 /*DBPROPVAL_NC_LOW*/),
		ORDINAL_POSITION	= convert(int,c.colid),
		COLUMN_NAME		= index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
		COLUMN_GUID		= convert(uniqueidentifier,null),
		COLUMN_PROPID		= convert(int,null),
		COLLATION		= convert(smallint, 1 /*DB_COLLATION_ASC*/),
		CARDINALITY		= case when (x.status & 2) = 2 then x.rows else null end,
		PAGES			= convert(int, x.dpages),
		FILTER_CONDITION	= convert(nvarchar(1),null),
		INTEGRATED		= convert(bit,(x.status & 16)/16) 
		
	from	sysobjects o, sysindexes x, syscolumns c
	where	o.type in ('U')
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@index_name is null or @index_name = x.name)
	and 	x.id = o.id
	and 	x.id = c.id
	and  	c.colid < x.keycnt+(x.status&16)/16
	and	index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
	and 	permissions(o.id) <> 0
	and     (x.status&32) = 0  -- No hypothetical indexes
	order by 8 desc, 4, 5, 6, 17
go
dump tran master with no_log
go
create procedure sp_indexes_rowset;5
	(
	@table_server		sysname,
	@table_catalog		sysname = null,
	@table_name		sysname = null,
	@index_name		sysname = null,
	@table_schema		sysname = null
	)
as
	select
		TABLE_CATALOG,	
		TABLE_SCHEMA,	
		TABLE_NAME,	
		INDEX_CATALOG,		
		INDEX_SCHEMA,	
		INDEX_NAME,	
		PRIMARY_KEY,	
		"UNIQUE",	
		"CLUSTERED",	
		"TYPE",		
		FILL_FACTOR,	
		INITIAL_SIZE,	
		NULLS,		
		SORT_BOOKMARKS,	
		AUTO_UPDATE,	
		NULL_COLLATION,	
		ORDINAL_POSITION,
		COLUMN_NAME,	
		COLUMN_GUID,	
		COLUMN_PROPID,	
		COLLATION,	
		CARDINALITY,	
		PAGES,		
		FILTER_CONDITION
	--	INTEGRATED
	from master.dbo.SYSREMOTE_INDEXES <
				@table_server,
				@table_catalog,
				@table_schema,
		                @index_name,
				NULL,			/* TYPE (index type) */
				@table_name >
	order by 8 desc, 4, 5, 6, 17
go

grant execute on sp_indexes_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_indexes_rowset' and type = 'P '))
		begin
		drop procedure sp_indexes_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_primary_keys_rowset'
go

/*	Procedure for 6.0 and 6.5 servers */
create procedure sp_primary_keys_rowset
	(
   	@table_name		varchar(255),
	@table_schema 		varchar(244) = null
	)
as
	select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(o.uid),
			TABLE_NAME		= o.name,	
			COLUMN_NAME		= c.name,
			COLUMN_GUID		= convert(binary(16),null),
			COLUMN_PROPID	= convert(int,null),
			ORDINAL			= convert(int,c1.colid),
			PK_NAME			= i.name
	from	sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
	where	o.type in ('U')
	and 	o.name = @table_name
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	o.id = c.id
	and 	o.id = i.id
	and 	(i.status & 0x800) = 0x800
	and 	c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
	and 	c1.id = c.id
	and 	c1.colid <= i.keycnt	/* create rows from 1 to keycnt */
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )
	order by 2, 3
go
dump tran master with no_log
go
create procedure sp_primary_keys_rowset;2
	(
 	@table_schema 		varchar(244) = null
	)
as
	select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(o.uid),
			TABLE_NAME		= o.name,	
			COLUMN_NAME		= c.name,
			COLUMN_GUID		= convert(binary(16),null),
			COLUMN_PROPID	= convert(int,null),
			ORDINAL			= convert(int,c1.colid),
			PK_NAME			= i.name
	from	sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
	where	o.type in ('U')
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	o.id = c.id
	and 	o.id = i.id
	and 	(i.status & 0x800) = 0x800
	and 	c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
	and 	c1.id = c.id
	and 	c1.colid <= i.keycnt	/* create rows from 1 to keycnt */
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )
	order by 2, 3
go

if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
else
	drop proc sp_primary_keys_rowset
go

/*	Procedure for 7.0 servers */
create procedure sp_primary_keys_rowset
	(
   	@table_name		sysname,
	@table_schema 		sysname = null
	)
as
	select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(o.uid),
			TABLE_NAME		= o.name,	
			COLUMN_NAME		= c.name,
			COLUMN_GUID		= convert(uniqueidentifier,null),
			COLUMN_PROPID	= convert(int,null),
			ORDINAL			= convert(int,c1.colid),
			PK_NAME			= i.name
	from	sysindexes i, syscolumns c, sysobjects o, syscolumns c1
	where	o.type in ('U')
	and 	o.name = @table_name
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	o.id = c.id
	and 	o.id = i.id
	and 	(i.status & 0x800) = 0x800
	and 	c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
	and 	c1.id = c.id
	and 	c1.colid <= i.keycnt	/* create rows from 1 to keycnt */
	and	permissions(o.id) <> 0
	order by 2, 3
go
dump tran master with no_log
go
create procedure sp_primary_keys_rowset;2
	(
 	@table_schema 	sysname = null
	)
as
	select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(o.uid),
			TABLE_NAME		= o.name,	
			COLUMN_NAME		= c.name,
			COLUMN_GUID		= convert(uniqueidentifier,null),
			COLUMN_PROPID	= convert(int,null),
			ORDINAL			= convert(int,c1.colid),
			PK_NAME			= i.name
	from	sysindexes i, syscolumns c, sysobjects o, syscolumns c1
	where	o.type in ('U')
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	o.id = c.id
	and 	o.id = i.id
	and 	(i.status & 0x800) = 0x800
	and 	c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
	and 	c1.id = c.id
	and 	c1.colid <= i.keycnt	/* create rows from 1 to keycnt */
	and	permissions(o.id) <> 0
	order by 2, 3
go
dump tran master with no_log
go
create procedure sp_primary_keys_rowset;3
	(
   	@table_name		sysname = null,
	@table_schema 		sysname = null
	)
as
IF @table_name is not NULL
	BEGIN
	select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(o.uid),
			TABLE_NAME		= o.name,	
			COLUMN_NAME		= c.name,
			COLUMN_GUID		= convert(uniqueidentifier,null),
			COLUMN_PROPID	= convert(int,null),
			ORDINAL			= convert(int,c1.colid),
			PK_NAME			= i.name
	from	sysindexes i, syscolumns c, sysobjects o, syscolumns c1
	where	o.type in ('U')
	and 	o.name = @table_name
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	o.id = c.id
	and 	o.id = i.id
	and 	(i.status & 0x800) = 0x800
	and 	c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
	and 	c1.id = c.id
	and 	c1.colid <= i.keycnt	/* create rows from 1 to keycnt */
	and	permissions(o.id) <> 0
	END
ELSE
	BEGIN
	select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(o.uid),
			TABLE_NAME		= o.name,	
			COLUMN_NAME		= c.name,
			COLUMN_GUID		= convert(uniqueidentifier,null),
			COLUMN_PROPID	= convert(int,null),
			ORDINAL			= convert(int,c1.colid),
			PK_NAME			= i.name
	from	sysindexes i, syscolumns c, sysobjects o, syscolumns c1
	where	o.type in ('U')
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	o.id = c.id
	and 	o.id = i.id
	and 	(i.status & 0x800) = 0x800
	and 	c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
	and 	c1.id = c.id
	and 	c1.colid <= i.keycnt	/* create rows from 1 to keycnt */
	and	permissions(o.id) <> 0
	END
go
dump tran master with no_log
go
create procedure sp_primary_keys_rowset;5
	(
	@table_server		sysname,
	@table_catalog		sysname = null,
	@table_name			sysname = null,
	@table_schema		sysname = null
	)
as
	select
		TABLE_CATALOG,	
		TABLE_SCHEMA,	
		TABLE_NAME,	
		COLUMN_NAME,	
		COLUMN_GUID,	
		COLUMN_PROPID,	
		ORDINAL
		--PK_NAME		
	from master.dbo.SYSREMOTE_PRIMARY_KEYS <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name >
	order by 1,2,3
go

grant execute on sp_primary_keys_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_primary_keys_rowset' and type = 'P '))
		begin
		drop procedure sp_primary_keys_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_provider_types_rowset'
go

/*	Procedure for 6.0 and 6.50 servers */
create proc sp_provider_types_rowset
	(
	@data_type 	smallint = null,
	@best_match	tinyint  = null
	)
as
	select
		TYPE_NAME 		= case when t.usertype = 80 then t.name 
						else d.type_name 
						end,
		DATA_TYPE 		= d.oledb_data_type,							
		COLUMN_SIZE		= case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
							then @@max_precision
							else coalesce(d.column_size,d.data_precision,t.prec) end,
		LITERAL_PREFIX 		= d.literal_prefix,
		LITERAL_SUFFIX 		= d.literal_suffix,
		CREATE_PARAMS 		= convert(varchar(32),e.CREATE_PARAMS),
		IS_NULLABLE		= t.allownulls,
		CASE_SENSITIVE		= d.case_sensitive,
		SEARCHABLE 		= d.searchable,
		UNSIGNED_ATTRIBUTE	= d.unsigned_attribute,
		FIXED_PREC_SCALE	= d.fixed_prec_scale,
		AUTO_UNIQUE_VALUE	= d.auto_unique_value, 
		LOCAL_TYPE_NAME		= case	when t.usertype = 80 then t.name 
						else d.local_type_name
						end,
		MINIMUM_SCALE		= convert(smallint,
						case 
						when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
						else null 
						end),
		MAXIMUM_SCALE		= convert(smallint,
						case 
						when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
						then @@max_precision
						else null 
						end),
		GUID			= convert(binary(16),null),
		TYPELIB			= convert(varchar(1),null),
		VERSION			= convert(varchar(1),null),
		IS_LONG			= d.is_long,
		BEST_MATCH		= case when t.usertype = 80
						then convert(bit,0)
						else d.best_match
						end,
		IS_FIXEDLENGTH		= convert(bit, 
						case when d.fixlen is null then 0 else 1 end)
	from 	master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
	where	d.ss_dtype = t.type 
	and	t.usertype <= 100
	and	t.usertype <> 18 /* sysname */
	and	(case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
			= case when d.type_name = 'timestamp' then 1 else 0 end)
	and 	t.usertype *= e.user_type 
	and 	e.AUTO_INCREMENT = 0
	and 	t.type not in (111,109,38,110,55,63)	/* get rid of nullable types */
	and	(@data_type is null or d.oledb_data_type = @data_type)
	and	(@best_match is null or d.best_match = @best_match)
	order by 2
go
dump tran master with no_log
go

if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
else
	drop proc sp_provider_types_rowset
go

/*	Procedure for 7.0 server */
create proc sp_provider_types_rowset
	(
	@data_type 	smallint = null,
	@best_match	tinyint  = null
	)
as
	select
		TYPE_NAME 		= case	when t.usertype = 80 then t.name 
						else d.type_name 
						end,
		DATA_TYPE 		= d.oledb_data_type,							
		COLUMN_SIZE		= case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
							then @@max_precision
							else coalesce(d.column_size,d.data_precision,t.prec) end,
		LITERAL_PREFIX 		= d.literal_prefix,
		LITERAL_SUFFIX 		= d.literal_suffix,
		CREATE_PARAMS 		= convert(nvarchar(32),e.CREATE_PARAMS),
		IS_NULLABLE		= t.allownulls,
		CASE_SENSITIVE		= d.case_sensitive,
		SEARCHABLE 		= d.searchable,
		UNSIGNED_ATTRIBUTE	= d.unsigned_attribute,
		FIXED_PREC_SCALE	= d.fixed_prec_scale,
		AUTO_UNIQUE_VALUE	= d.auto_unique_value, 
		LOCAL_TYPE_NAME		= case	when t.usertype = 80 then t.name 
						else d.local_type_name
						end,
		MINIMUM_SCALE		= convert(smallint,
						case 
						when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
						else null 
						end),
		MAXIMUM_SCALE		= convert(smallint,
						case 
						when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
						then @@max_precision
						else null 
						end),
		GUID			= convert(uniqueidentifier,null),
		TYPELIB			= convert(nvarchar(1),null),
		VERSION			= convert(nvarchar(1),null),
		IS_LONG			= d.is_long,
		BEST_MATCH		= case	when t.usertype = 80
						then convert(bit,0)
						else d.best_match
						end,
		IS_FIXEDLENGTH		= convert(bit, 
						case when d.fixlen is null then 0 else 1 end)

	from master.dbo.spt_provider_types d
		INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
		LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
			t.xusertype = e.user_type
			and e.AUTO_INCREMENT = 0
	where	(@data_type is null or d.oledb_data_type = @data_type)
	and	(@best_match is null or d.best_match = @best_match)	
	and	t.usertype <= 255
	and	t.usertype <> 18 /* sysname */
	order by 2
go

grant execute on sp_provider_types_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
		where name = 'sp_provider_types_rowset' and type = 'P '))
		begin
		drop procedure sp_provider_types_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_procedure_params_rowset'
go

/*	Procedure for 6.0 and 6.50 servers */
create procedure sp_procedure_params_rowset
	(
   	@procedure_name		varchar(255) = null,
	@group_number		int = null,
	@procedure_schema 	varchar(255) = null,
	@parameter_name		varchar(255) = null
	)
as
IF @procedure_name is not null
	BEGIN
	select
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= c.name,
		ORDINAL_POSITION 	= convert(smallint,c.colid),
		PARAMETER_TYPE 		= convert(smallint, 1+((c.status/64)&1)),
		PARAMETER_HASDEFAULT	= convert(tinyint, 0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,
						case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
						then 0 else 1
						end),
		DATA_TYPE		= d.oledb_data_type,
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= d.type_name,
		LOCAL_TYPE_NAME		= d.local_type_name
	
	from
		syscolumns c,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t
	where
		o.name = @procedure_name
	and	o.type = 'P'							/* Just Procedures */
	and	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	o.id = c.id
	and	c.number = @group_number
	and	c.type = d.ss_dtype
	and	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and	c.usertype = t.usertype
	and	(t.usertype != 80 or d.type_name='timestamp')
	and	(@parameter_name is null or @parameter_name = c.name)
	UNION ALL
	SELECT		   /* return value row*/
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= convert(sysname,'RETURN_VALUE'),
		ORDINAL_POSITION 	= convert(smallint,0),
		PARAMETER_TYPE 		= convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
		PARAMETER_HASDEFAULT	= convert(tinyint,0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,0),
		DATA_TYPE		= convert(smallint, 3 /*DBTYPE_I4*/),
		CHARACTER_MAXIMUM_LENGTH= convert(int,null),
		CHARACTER_OCTET_LENGTH	= convert(int,null),
		NUMERIC_PRECISION	= convert(smallint,10),
		NUMERIC_SCALE		= convert(smallint,null),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= convert(sysname,'int'),
		LOCAL_TYPE_NAME		= convert(sysname,'int')
	from
		syscomments c,
		sysobjects o
	where
		o.name = @procedure_name
	and	o.type = 'P'						/* Just Procedures */
	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	(@parameter_name is null or @parameter_name = 'RETURN_VALUE')
	and	c.id = o.id
	and	c.number = @group_number
	and c.colid = 1
	order by 2, 3, 5
	END
ELSE
	BEGIN
	select
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= c.name,
		ORDINAL_POSITION 	= convert(smallint,c.colid),
		PARAMETER_TYPE 		= convert(smallint, 1+((c.status/64)&1)),
		PARAMETER_HASDEFAULT	= convert(tinyint,0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,
						case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
						then 0 else 1
						end),
		DATA_TYPE		= d.oledb_data_type,
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= d.type_name,
		LOCAL_TYPE_NAME		= d.local_type_name
	
	from
		syscolumns c,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t
	where
		o.type = 'P'							/* Just Procedures */
	and	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	o.id = c.id
	and	c.type = d.ss_dtype
	and	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and	c.usertype = t.usertype
	and	(t.usertype != 80 or d.type_name='timestamp')
	and	(@parameter_name is null or @parameter_name = c.name)
	UNION ALL
	SELECT		   /* return value row*/
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= convert(sysname,'RETURN_VALUE'),
		ORDINAL_POSITION 	= convert(smallint,0),
		PARAMETER_TYPE 		= convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
		PARAMETER_HASDEFAULT	= convert(tinyint,0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,0),
		DATA_TYPE		= convert(smallint, 3 /*DBTYPE_I4*/),
		CHARACTER_MAXIMUM_LENGTH= convert(int,null),
		CHARACTER_OCTET_LENGTH	= convert(int,null),
		NUMERIC_PRECISION	= convert(smallint,10),
		NUMERIC_SCALE		= convert(smallint,null),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= convert(sysname,'int'),
		LOCAL_TYPE_NAME		= convert(sysname,'int')
	from
		syscomments c,
		sysobjects o
	where
		o.type = 'P'						/* Just Procedures */
	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	(@parameter_name is null or @parameter_name = 'RETURN_VALUE')
	and	c.id = o.id
	and 	c.colid = 1
	order by 2, 3, 5
	END
go
dump tran master with no_log
go
create procedure sp_procedure_params_rowset;2
	(
  	@handle			int output,
   	@scrollopt		int output,
	@ccopt			int output,
	@rows			int output,
   	@procedure_name		varchar(255) = null,
	@group_number		int = null,
	@procedure_schema 	varchar(255) = null,
	@parameter_name		varchar(255) = null
	)
as
	declare @ret int


SET NOCOUNT ON

	create table #spprocparamrowset1
		(
		PROCEDURE_CATALOG 	sysname not null,
		PROCEDURE_SCHEMA 	sysname not null,
		PROCEDURE_NAME 		varchar(35) not null,
		PARAMETER_NAME 		sysname not null,
		ORDINAL_POSITION 	smallint not null,
		PARAMETER_TYPE 		smallint null,
		PARAMETER_HASDEFAULT	tinyint null,
		PARAMETER_DEFAULT	varchar(255) null,
		IS_NULLABLE		bit not null,
		DATA_TYPE		smallint null,
		CHARACTER_MAXIMUM_LENGTH int null,
		CHARACTER_OCTET_LENGTH	int null,
		NUMERIC_PRECISION	smallint null,
		NUMERIC_SCALE		smallint null,
		DESCRIPTION		varchar(1) null,
		TYPE_NAME		sysname null,
		LOCAL_TYPE_NAME		sysname null,
		)

IF @procedure_name is not null
	BEGIN
	insert into #spprocparamrowset1
	select
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= c.name,
		ORDINAL_POSITION 	= convert(smallint,c.colid),
		PARAMETER_TYPE 		= convert(smallint, 1+((c.status/64)&1)),
		PARAMETER_HASDEFAULT	= convert(tinyint, 0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,
						case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
						then 0 else 1
						end),
		DATA_TYPE		= d.oledb_data_type,
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= d.type_name,
		LOCAL_TYPE_NAME		= d.local_type_name
	
	from
		syscolumns c,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t
	where
		o.name = @procedure_name
	and	o.type = 'P'							/* Just Procedures */
	and	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	o.id = c.id
	and	c.number = @group_number
	and	c.type = d.ss_dtype
	and	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and	c.usertype = t.usertype
	and	(t.usertype != 80 or d.type_name='timestamp')
	and	(@parameter_name is null or @parameter_name = c.name)
	UNION ALL
	SELECT		   /* return value row*/
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= convert(sysname,'RETURN_VALUE'),
		ORDINAL_POSITION 	= convert(smallint,0),
		PARAMETER_TYPE 		= convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
		PARAMETER_HASDEFAULT	= convert(tinyint,0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,0),
		DATA_TYPE		= convert(smallint, 3 /*DBTYPE_I4*/),
		CHARACTER_MAXIMUM_LENGTH= convert(int,null),
		CHARACTER_OCTET_LENGTH	= convert(int,null),
		NUMERIC_PRECISION	= convert(smallint,10),
		NUMERIC_SCALE		= convert(smallint,null),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= convert(sysname,'int'),
		LOCAL_TYPE_NAME		= convert(sysname,'int')
	from
		syscomments c,
		sysobjects o
	where
		o.name = @procedure_name
	and	o.type = 'P'						/* Just Procedures */
	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	(@parameter_name is null or @parameter_name = 'RETURN_VALUE')
	and	c.id = o.id
	and	c.number = @group_number
	and c.colid = 1
	order by 2, 3, 5
	END
ELSE
	BEGIN
	insert into #spprocparamrowset1
	select
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= c.name,
		ORDINAL_POSITION 	= convert(smallint,c.colid),
		PARAMETER_TYPE 		= convert(smallint, 1+((c.status/64)&1)),
		PARAMETER_HASDEFAULT	= convert(tinyint,0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,
						case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
						then 0 else 1
						end),
		DATA_TYPE		= d.oledb_data_type,
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= d.type_name,
		LOCAL_TYPE_NAME		= d.local_type_name
	
	from
		syscolumns c,
		sysobjects o,
		master.dbo.spt_provider_types d,
		systypes t
	where
		o.type = 'P'							/* Just Procedures */
	and	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	o.id = c.id
	and	c.type = d.ss_dtype
	and	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and	c.usertype = t.usertype
	and	(t.usertype != 80 or d.type_name='timestamp')
	and	(@parameter_name is null or @parameter_name = c.name)
	UNION ALL
	SELECT		   /* return value row*/
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= convert(sysname,'RETURN_VALUE'),
		ORDINAL_POSITION 	= convert(smallint,0),
		PARAMETER_TYPE 		= convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
		PARAMETER_HASDEFAULT	= convert(tinyint,0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit,0),
		DATA_TYPE		= convert(smallint, 3 /*DBTYPE_I4*/),
		CHARACTER_MAXIMUM_LENGTH= convert(int,null),
		CHARACTER_OCTET_LENGTH	= convert(int,null),
		NUMERIC_PRECISION	= convert(smallint,10),
		NUMERIC_SCALE		= convert(smallint,null),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= convert(sysname,'int'),
		LOCAL_TYPE_NAME		= convert(sysname,'int')
	from
		syscomments c,
		sysobjects o
	where
		o.type = 'P'						/* Just Procedures */
	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	(@parameter_name is null or @parameter_name = 'RETURN_VALUE')
	and	c.id = o.id
	and 	c.colid = 1
	order by 2, 3, 5
	END

	exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
		@scrollopt output, @ccopt output, @rows output

	drop table #spprocparamrowset1
   	return isnull(@ret,0)
go
dump tran master with no_log
go
create procedure sp_procedure_params_rowset;3
as
	select
		PROCEDURE_CATALOG 	= convert(sysname, ' '),
		PROCEDURE_SCHEMA 	= convert(sysname, ' '),
		PROCEDURE_NAME 		= convert(varchar(35), ' '),
		PARAMETER_NAME 		= convert(sysname, ' '),
		ORDINAL_POSITION 	= convert(smallint, 0),
		PARAMETER_TYPE 		= convert(smallint, 0),
		PARAMETER_HASDEFAULT	= convert(tinyint, 0),
		PARAMETER_DEFAULT	= convert(varchar(255),null),
		IS_NULLABLE		= convert(bit, 0),
		DATA_TYPE		= convert(smallint, 0),
		CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
		CHARACTER_OCTET_LENGTH	= convert(int, 0),
		NUMERIC_PRECISION	= convert(smallint, 0),
		NUMERIC_SCALE		= convert(smallint, 0),
		DESCRIPTION		= convert(varchar(1),null),
		TYPE_NAME		= convert(sysname,null),
		LOCAL_TYPE_NAME		= convert(sysname,null)
	where	1=0
go
dump tran master with no_log
go


if (charindex('7.00', @@version) > 0)
	drop procedure sp_procedure_params_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/*	Procedure for 7.0 servers */
create procedure sp_procedure_params_rowset
	(
   	@procedure_name		sysname,
	@group_number		int = 1,
	@procedure_schema 	sysname = null,
	@parameter_name		sysname = null
	)
as
	select
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= c.name,
		ORDINAL_POSITION 	= convert(smallint,c.colid),
		PARAMETER_TYPE 		= convert(smallint, 1+c.isoutparam),
		PARAMETER_HASDEFAULT	= convert(tinyint, 0),
		PARAMETER_DEFAULT	= convert(nvarchar(255),null),
		IS_NULLABLE		= convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
		DATA_TYPE		= d.oledb_data_type,
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case 
						when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size,c.length/2)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size*2,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DESCRIPTION		= convert(nvarchar(1),null),
		TYPE_NAME		= d.type_name,
		LOCAL_TYPE_NAME		= d.local_type_name
	
	from
		sysobjects o,
		syscolumns c,
		master.dbo.spt_provider_types d,
		systypes t
	where
		o.name = @procedure_name
	and	o.type = 'P'							/* Just Procedures */
	and	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	o.id = c.id
	and	c.number = @group_number
	and	c.xtype = d.ss_dtype
	and	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and	c.xusertype = t.xusertype
	and	(@parameter_name is null or @parameter_name = c.name)
	UNION ALL
	SELECT		   /* return value row*/
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= convert(sysname,'RETURN_VALUE'),
		ORDINAL_POSITION 	= convert(smallint,0),
		PARAMETER_TYPE 		= convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
		PARAMETER_HASDEFAULT	= convert(tinyint, 0),
		PARAMETER_DEFAULT	= convert(nvarchar(255),null),
		IS_NULLABLE		= convert(bit,0),
		DATA_TYPE		= convert(smallint, 3 /*DBTYPE_I4*/),
		CHARACTER_MAXIMUM_LENGTH= convert(int,null),
		CHARACTER_OCTET_LENGTH	= convert(int,null),
		NUMERIC_PRECISION	= convert(smallint,10),
		NUMERIC_SCALE		= convert(smallint,null),
		DESCRIPTION		= convert(nvarchar(1),null),
		TYPE_NAME		= convert(sysname,N'int'),
		LOCAL_TYPE_NAME		= convert(sysname,N'int')
	from
		sysobjects o,
		syscomments c
	where
		o.name = @procedure_name
	and	o.id = c.id 
	and	c.number = @group_number
	and	c.colid = 1
	and	o.type = 'P'						/* Just Procedures */
	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	(@parameter_name is null or @parameter_name = 'RETURN_VALUE')
	order by 2, 3, 5
go
dump tran master with no_log
go
create procedure sp_procedure_params_rowset;2
	(
	@procedure_schema 	sysname = null,
	@parameter_name		sysname = null
	)
as
	select
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= c.name,
		ORDINAL_POSITION 	= convert(smallint,c.colid),
		PARAMETER_TYPE 		= convert(smallint, 1+c.isoutparam),
		PARAMETER_HASDEFAULT	= convert(tinyint, 0),
		PARAMETER_DEFAULT	= convert(nvarchar(255),null),
		IS_NULLABLE		= convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
		DATA_TYPE		= d.oledb_data_type,
		CHARACTER_MAXIMUM_LENGTH= convert(int,
						case 
						when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size,c.length/2)
						else null 
						end),
		CHARACTER_OCTET_LENGTH	= convert(int,
						case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
							or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
						then coalesce(d.column_size,c.length)
						when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						then coalesce(d.column_size*2,c.length)
						else null 
						end),
		NUMERIC_PRECISION	= convert(smallint,
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
							when d.fixed_prec_scale =1 then d.data_precision else null end),
		NUMERIC_SCALE		= convert(smallint, 
						case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
		DESCRIPTION		= convert(nvarchar(1),null),
		TYPE_NAME		= d.type_name,
		LOCAL_TYPE_NAME		= d.local_type_name
	
	from
		sysobjects o,
		syscolumns c,
		master.dbo.spt_provider_types d,
		systypes t
	where
		o.type = 'P'							/* Just Procedures */
	and	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	o.id = c.id
	and	c.xtype = d.ss_dtype
	and	c.length = case when d.fixlen > 0 then d.fixlen else c.length end
	and	c.xusertype = t.xusertype
	and	(@parameter_name is null or @parameter_name = c.name)
	UNION ALL
	SELECT		   /* return value row*/
		PROCEDURE_CATALOG 	= db_name(),
		PROCEDURE_SCHEMA 	= user_name(),
		PROCEDURE_NAME 		= convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
		PARAMETER_NAME 		= convert(sysname,'RETURN_VALUE'),
		ORDINAL_POSITION 	= convert(smallint,0),
		PARAMETER_TYPE 		= convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
		PARAMETER_HASDEFAULT	= convert(tinyint, 0),
		PARAMETER_DEFAULT	= convert(nvarchar(255),null),
		IS_NULLABLE		= convert(bit,0),
		DATA_TYPE		= convert(smallint, 3 /*DBTYPE_I4*/),
		CHARACTER_MAXIMUM_LENGTH= convert(int,null),
		CHARACTER_OCTET_LENGTH	= convert(int,null),
		NUMERIC_PRECISION	= convert(smallint,10),
		NUMERIC_SCALE		= convert(smallint,null),
		DESCRIPTION		= convert(nvarchar(1),null),
		TYPE_NAME		= convert(sysname,N'int'),
		LOCAL_TYPE_NAME		= convert(sysname,N'int')
	from
		sysobjects o,
		syscomments c
	where
		o.type = 'P'		/* Just Procedures */
	and	o.id = c.id 
	and	c.colid = 1
	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and	(@parameter_name is null or @parameter_name = 'RETURN_VALUE')
	order by 2, 3, 5
go


grant execute on sp_procedure_params_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_procedure_params_rowset' and type = 'P '))
		begin
		drop procedure sp_procedure_params_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_procedures_rowset'
go


/* pre 7.00 version */
create procedure sp_procedures_rowset
	(
	@procedure_name		varchar(255), 
	@group_number		int = 1,
	@procedure_schema	varchar(255) = null
	)    	
as
	select
		PROCEDURE_CATALOG	= db_name(),
		PROCEDURE_SCHEMA	= user_name(o.uid),
		PROCEDURE_NAME		= convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
		PROCEDURE_TYPE		= convert(smallint, 3 /*DB_PT_FUNCTION*/),
		PROCEDURE_DEFINITION	= convert(varchar(1),null),
		DESCRIPTION		= convert(varchar(1),null),
		DATE_CREATED		= o.crdate,
		DATE_MODIFIED		= convert(datetime,null)
	from 	
		sysobjects o, 
		syscomments p,
		sysusers u
	where
	    	o.name = @procedure_name
    	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and 	o.type = 'P'		/* Object type of Procedure */
	and 	p.colid = 1
	and 	p.id = o.id
	and	p.number = @group_number
	and 	u.uid = user_id()	/* constrain sysusers uid for use in subquery */
	and 	(	suser_id() = 1 	/* User is the System Administrator */
		or 	o.uid = user_id()	/* User created the object */
			/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or 	((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			 from sysprotects p
			 where p.id = o.id
			     /*  get rows for public,current user,user's group */
				 and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			     /* check for SELECT,EXECUTE privilege */
			 and (action in (193,224)))&1    /* more magic...normalize GRANT */
			) = 1	 /* final magic...compare Grants	*/
		)
	order by 2, 3
go
dump tran master with no_log
go
create procedure sp_procedures_rowset;2
	(
	@procedure_schema	varchar(255) = null
	)    	
as
	select
		PROCEDURE_CATALOG	= db_name(),
		PROCEDURE_SCHEMA	= user_name(o.uid),
		PROCEDURE_NAME		= convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
		PROCEDURE_TYPE		= convert(smallint, 3 /*DB_PT_FUNCTION*/),
		PROCEDURE_DEFINITION	= convert(varchar(1),null),
		DESCRIPTION		= convert(varchar(1),null),
		DATE_CREATED		= o.crdate,
		DATE_MODIFIED		= convert(datetime,null)
	from 	
		sysobjects o, 
		syscomments p,
		sysusers u
	where
	    	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and 	o.type = 'P'		/* Object type of Procedure */
	and 	p.colid = 1
	and 	p.id = o.id
	and 	u.uid = user_id()	/* constrain sysusers uid for use in subquery */
	and 	(	suser_id() = 1 	/* User is the System Administrator */
		or 	o.uid = user_id()	/* User created the object */
			/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or 	((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			 from sysprotects p
			 where p.id = o.id
			     /*  get rows for public,current user,user's group */
				 and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			     /* check for SELECT,EXECUTE privilege */
			 and (action in (193,224)))&1    /* more magic...normalize GRANT */
			) = 1	 /* final magic...compare Grants	*/
		)
	order by 2, 3
go
dump tran master with no_log
go

if (charindex('7.00', @@version) > 0)
	drop procedure sp_procedures_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/* 7.00 version */
create procedure sp_procedures_rowset
	(
	@procedure_name		sysname, 
	@group_number		int = 1,
	@procedure_schema	sysname = null
	)    	
as
	select
		PROCEDURE_CATALOG	= db_name(),
		PROCEDURE_SCHEMA	= user_name(o.uid),
		PROCEDURE_NAME		= convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
		PROCEDURE_TYPE		= convert(smallint, 3 /*DB_PT_FUNCTION*/),
		PROCEDURE_DEFINITION	= convert(nvarchar(1),null),
		DESCRIPTION		= convert(nvarchar(1),null),
		DATE_CREATED		= o.crdate,
		DATE_MODIFIED		= convert(datetime,null)
	from 	
		sysobjects o, 
		syscomments p
	where
		permissions(o.id) <> 0
	and	o.name = @procedure_name
	and 	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and 	o.type = 'P'		/* Object type of Procedure */
	and 	p.colid = 1
	and 	p.id = o.id
	and	p.number = @group_number
	order by 2, 3
go
dump tran master with no_log
go
create procedure sp_procedures_rowset;2
	(
	@procedure_schema	sysname = null
	)    	
as
	select
		PROCEDURE_CATALOG	= db_name(),
		PROCEDURE_SCHEMA	= user_name(o.uid),
		PROCEDURE_NAME		= convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
		PROCEDURE_TYPE		= convert(smallint, 3 /*DB_PT_FUNCTION*/),
		PROCEDURE_DEFINITION	= convert(nvarchar(1),null),
		DESCRIPTION		= convert(nvarchar(1),null),
		DATE_CREATED		= o.crdate,
		DATE_MODIFIED		= convert(datetime,null)
	from 	sysobjects o, syscomments p
	where
		permissions(o.id) <> 0
	and    	(@procedure_schema is null or @procedure_schema = user_name(o.uid))
	and 	o.type = 'P'		/* Object type of Procedure */
	and p.colid = 1
	and p.id = o.id
	order by 2, 3
go

grant execute on sp_procedures_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_procedures_rowset' and type = 'P '))
		begin
		drop procedure sp_procedures_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_schemata_rowset'
go

/*	6.0 and 6.5 version */
create procedure sp_schemata_rowset
	(
	@schema_name	varchar(90) = null, 
	@schema_owner	varchar(90) = null
	)    	
as
	select	distinct
		CATALOG_NAME			= db_name(),
		SCHEMA_NAME			= user_name(o.uid),	
		SCHEMA_OWNER			= user_name(o.uid),	
		DEFAULT_CHARACTER_SET_CATALOG	= convert(sysname,'master'),
		DEFAULT_CHARACTER_SET_SCHEMA	= convert(sysname,'dbo'),
		DEFAULT_CHARACTER_SET_NAME	= convert(sysname,a_cha.name)
	from	
		sysobjects o,
		master.dbo.sysconfigures	cfg,
		master.dbo.syscharsets		a_cha, /* charset/1001, not sortorder */
		master.dbo.syscharsets		b_cha  /* sortorder/2001, not charset */
	where   
		(@schema_name is null or @schema_name = user_name(o.uid))
	and 	(@schema_owner is null or @schema_owner = user_name(o.uid))
	and	a_cha.type = 1001 /* type is charset */
	and     b_cha.type = 2001 /* type is sortorder */
	and 	a_cha.id = b_cha.csid
	and 	b_cha.id = cfg.value
	order by 2
go
dump tran master with no_log
go

if (charindex('7.00', @@version) > 0)
	drop procedure sp_schemata_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/* 7.00 version */
create procedure sp_schemata_rowset
	(
	@schema_name	sysname = null, 
	@schema_owner	sysname = null
	)    	
as
	select	distinct
		CATALOG_NAME			= db_name(),
		SCHEMA_NAME			= user_name(o.uid),	
		SCHEMA_OWNER			= user_name(o.uid),	
		DEFAULT_CHARACTER_SET_CATALOG	= convert(sysname,N'master'),
		DEFAULT_CHARACTER_SET_SCHEMA	= convert(sysname,N'dbo'),
		DEFAULT_CHARACTER_SET_NAME	= convert(sysname,a_cha.name)
	from	
		sysobjects o,
		master.dbo.sysconfigures	cfg,
		master.dbo.syscharsets		a_cha, /* charset/1001, not sortorder */
		master.dbo.syscharsets		b_cha  /* sortorder/2001, not charset */
	where   
		(@schema_name is null or @schema_name = user_name(o.uid))
	and 	(@schema_owner is null or @schema_owner = user_name(o.uid))
	and	a_cha.type = 1001 /* type is charset */
	and     b_cha.type = 2001 /* type is sortorder */
	and 	a_cha.id = b_cha.csid
	and 	b_cha.id = cfg.value
	order by 2
go
dump tran master with no_log
go
go
/* The following stored procedure is used for Sphinx and Hydra */
create procedure sp_schemata_rowset;3
as
	select
		CATALOG_NAME			= convert(sysname,' '),
		SCHEMA_NAME			= convert(sysname,' '),	
		SCHEMA_OWNER			= convert(sysname,' '),	
		DEFAULT_CHARACTER_SET_CATALOG	= convert(sysname,' '),
		DEFAULT_CHARACTER_SET_SCHEMA	= convert(sysname,' '),
		DEFAULT_CHARACTER_SET_NAME	= convert(sysname,' ')
	where	1=0
go
grant execute on sp_schemata_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_schemata_rowset' and type = 'P '))
		begin
		drop procedure sp_schemata_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_statistics_rowset'
go


/*	6.0 and 6.5 version */
create procedure sp_statistics_rowset
	(
	@table_name	varchar(255),
	@table_schema	varchar(255) = null	 
	)
as
	select	db_name()				as TABLE_CATALOG,		
		user_name(o.uid)			as TABLE_SCHEMA,
		o.name					as TABLE_NAME,
		x.rows					as CARDINALITY
	from	sysobjects o, sysindexes x, sysusers u
	where	o.type in ('U')
	and 	o.name = @table_name
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	x.id = o.id
	and 	x.indid in (0,1)	/*If there are no indexes then table stats are in a row with indid =0 */
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )
	order by 2, 3
go
dump tran master with no_log
go
create procedure sp_statistics_rowset;2
	(
	@table_schema	varchar(255) = null	 
	)
as
	select	db_name()				as TABLE_CATALOG,		
		user_name(o.uid)			as TABLE_SCHEMA,
		o.name					as TABLE_NAME,
		x.rows					as CARDINALITY
	from	sysobjects o, sysindexes x, sysusers u
	where	o.type in ('U')
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	x.id = o.id
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
			) = 1	/* final magic...compare Grants	  */
		   )		
	order by 2, 3
go
dump tran master with no_log
go

if (charindex('7.00', @@version) > 0)
	drop procedure sp_statistics_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/* 	7.0 version */
create procedure sp_statistics_rowset
	(
	@table_name	sysname,
	@table_schema	sysname = null	 
	)
as
	select	db_name()				as TABLE_CATALOG,		
		user_name(o.uid)			as TABLE_SCHEMA,
		o.name					as TABLE_NAME,
		x.rows					as CARDINALITY
	from	sysobjects o, sysindexes x
	where	o.type in ('U')
	and 	o.name = @table_name
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	x.id = o.id
	and 	x.indid in (0,1)	/*If there are no indexes then table stats are in a row with indid =0 */
	and 	permissions(o.id) <> 0
	order by 2, 3
go
dump tran master with no_log
go
create procedure sp_statistics_rowset;2
	(
	@table_schema	sysname = null	 
	)
as
	select	db_name()				as TABLE_CATALOG,		
		user_name(o.uid)			as TABLE_SCHEMA,
		o.name					as TABLE_NAME,
		x.rows					as CARDINALITY
	from	sysobjects o, sysindexes x
	where	o.type in ('U')
	and 	(@table_schema is null or @table_schema = user_name(o.uid))
	and 	x.id = o.id
	and 	x.indid in (0,1)	/*If there are no indexes then table stats are in a row with indid =0 */
	and 	permissions(o.id) <> 0
	order by 2, 3
go

grant execute on sp_statistics_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_statistics_rowset' and type = 'P '))
		begin
		drop procedure sp_statistics_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_tables_rowset'
go


/*	Procedure for 6.50 and earlier servers */
create procedure sp_tables_rowset
	(
	@table_name	varchar(255), 
	@table_schema	varchar(255) = null,	
	@table_type	varchar(255) = null 
	)
as
	select	TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		TABLE_TYPE	= convert(varchar(30),
					case o.type 
					when 'U' then 'TABLE'
					when 'V' then 'VIEW'
					when 'S' then 'SYSTEM TABLE'
					end),
		TABLE_GUID	= convert(binary(16), null),
		DESCRIPTION	= convert(varchar(1), null),
		TABLE_PROPID	= convert(int,null),
		DATE_CREATED	= o.crdate,
		DATE_MODIFIED	= convert(datetime,null)
	from	sysusers u, sysobjects o
	where	o.type in ('U','V','S')
	and 	o.name = @table_name
	and 	(	@table_schema is null
		or	@table_schema = user_name(o.uid)
		)
	and 	(
			@table_type is null
		or	@table_type = case o.type 
					when 'U' then 'TABLE'
					when 'V' then 'VIEW'
					when 'S' then 'SYSTEM TABLE'
					end
		)
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			/* join to correlate with all rows in sysobjects */
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
		    ) = 1	/* final magic...compare Grants	  */
		)		
	order by 4, 2, 3
go
dump tran master with no_log
go
create procedure sp_tables_rowset;2
	(
	@table_schema	varchar(255) = null,	
	@table_type	varchar(255) = null 
	)
as
	select	TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		TABLE_TYPE	= convert(varchar(30),
					case o.type 
					when 'U' then 'TABLE'
					when 'V' then 'VIEW'
					when 'S' then 'SYSTEM TABLE'
					end),
		TABLE_GUID	= convert(binary(16), null),
		DESCRIPTION	= convert(varchar(1), null),
		TABLE_PROPID	= convert(int,null),
		DATE_CREATED	= o.crdate,
		DATE_MODIFIED	= convert(datetime,null)
	from	sysusers u, sysobjects o
	where	o.type in ('U','V','S')
	and 	(	@table_schema is null
		or	@table_schema = user_name(o.uid)
		)
	and 	(
			@table_type is null
		or	@table_type = case o.type 
					when 'U' then 'TABLE'
					when 'V' then 'VIEW'
					when 'S' then 'SYSTEM TABLE'
					end
		)
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			/* join to correlate with all rows in sysobjects */
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
		    ) = 1	/* final magic...compare Grants	  */
		)		
	order by 4, 2, 3
go
dump tran master with no_log
go

if (charindex('7.00', @@version) > 0)
	drop procedure sp_tables_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/*	Procedure for 7.00 server */
create procedure sp_tables_rowset
	(
	@table_name	sysname, 
	@table_schema	sysname = null,	
	@table_type	nvarchar(255) = null 
	)
as
	select	*
	from	(select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(uid),
			TABLE_NAME	= name,
			TABLE_TYPE	= convert(nvarchar(30),
						case type 
						when 'U' then 
							case when ObjectProperty(id, 'IsMSShipped') = 0
							then N'TABLE' else N'SYSTEM TABLE' end
						when 'S' then N'SYSTEM TABLE'
						when 'V' then 
							case when ObjectProperty(id, 'IsMSShipped') = 0
							then N'VIEW' else N'SYSTEM VIEW' end
						end),
			TABLE_GUID	= convert(uniqueidentifier, null),
			DESCRIPTION	= convert(nvarchar(1), null),
			TABLE_PROPID	= convert(int,null),
			DATE_CREATED	= crdate,
			DATE_MODIFIED	= convert(datetime,null)
		from	sysobjects
		where	name = @table_name
		and	type in ('U','V','S')
		and	permissions(id) <> 0
		) as o

	where 	(@table_schema is null or @table_schema = TABLE_SCHEMA)
	and 	(@table_type is null or @table_type = TABLE_TYPE)	
	order by 4, 2, 3
go
dump tran master with no_log
go
create procedure sp_tables_rowset;2
	(
	@table_schema	sysname = null,	
	@table_type	nvarchar(255) = null 
	)
as
	select	*
	from	(select	TABLE_CATALOG	= db_name(),
			TABLE_SCHEMA	= user_name(uid),
			TABLE_NAME	= name,
			TABLE_TYPE	= convert(nvarchar(30),
						case type 
						when 'U' then 
							case when ObjectProperty(id, 'IsMSShipped') = 0
							then N'TABLE' else N'SYSTEM TABLE' end
						when 'S' then N'SYSTEM TABLE'
						when 'V' then 
							case when ObjectProperty(id, 'IsMSShipped') = 0
							then N'VIEW' else N'SYSTEM VIEW' end
						end),
			TABLE_GUID	= convert(uniqueidentifier, null),
			DESCRIPTION	= convert(nvarchar(1), null),
			TABLE_PROPID	= convert(int,null),
			DATE_CREATED	= crdate,
			DATE_MODIFIED	= convert(datetime,null)
		from	sysobjects
		where	type in ('U','V','S')
		and	permissions(id) <> 0
		) as o

	where 	(@table_schema is null or @table_schema = TABLE_SCHEMA)
	and 	(@table_type is null or @table_type = TABLE_TYPE)	
	order by 4, 2, 3
go
dump tran master with no_log
go
create procedure sp_tables_rowset;5
	(
	@table_server		sysname,
	@table_catalog		sysname = null,
	@table_name		sysname = null,
	@table_schema		sysname = null,
	@table_type		sysname = null
	)
as
	select
		TABLE_CATALOG,
		TABLE_SCHEMA,
		TABLE_NAME,
		TABLE_TYPE,
		TABLE_GUID,
		DESCRIPTION
	--	TABLE_PROPID,
	--	DATE_CREATED,
	--	DATE_MODIFIED
	from master.dbo.SYSREMOTE_TABLES <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name,
				@table_type >
	order by 4,1,2,3
go

grant execute on sp_tables_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_tables_rowset' and type = 'P '))
		begin
		drop procedure sp_tables_rowset
		dump tran master with no_log
		end
	end
go



print ''
print 'creating sp_tables_info_rowset'
go


/*	Procedure for 6.50 and earlier servers */
create procedure sp_tables_info_rowset
	(
	@table_name	varchar(255), 
	@table_schema	varchar(255) = null,	
	@table_type	varchar(255) = null 
	)
as
	select	TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		TABLE_TYPE		= convert(varchar(30),
						case o.type 
						when 'U' then 'TABLE'
						when 'V' then 'VIEW'
						when 'S' then 'SYSTEM TABLE'
						end),
		TABLE_GUID		= convert(binary(16), null),
		BOOKMARKS		= convert(bit, 1),
		BOOKMARK_TYPE		= convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
		BOOKMARK_DATATYPE	= convert(smallint, 19 /*DBTYPE_UI4 */),
		BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
		BOOKMARK_INFORMATION	= convert(int, 0),
		TABLE_VERSION		= convert(int, o.schema_ver),
		CARDINALITY		= x.rows,
		DESCRIPTION		= convert(varchar(1), null),
		TABLE_PROPID		= convert(int, null)

	from	sysusers u, 
		sysobjects o,
		sysindexes x

	where	o.type in ('U','V','S')
	and 	o.name = @table_name
	and 	(	@table_schema is null
		or	@table_schema = user_name(o.uid)
		)
	and 	(
			@table_type is null
		or	@table_type = case o.type 
					when 'U' then 'TABLE'
					when 'V' then 'VIEW'
					when 'S' then 'SYSTEM TABLE'
					end
		)
	and	o.id *= x.id
	and	x.indid in (0,1)
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			/* join to correlate with all rows in sysobjects */
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
		    ) = 1	/* final magic...compare Grants	  */
		)		
	order by 4, 2, 3
go
dump tran master with no_log
go
create procedure sp_tables_info_rowset;2
	(
	@table_schema	varchar(255) = null,	
	@table_type	varchar(255) = null 
	)
as
	select	TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(o.uid),
		TABLE_NAME		= o.name,
		TABLE_TYPE		= convert(varchar(30),
						case o.type 
						when 'U' then 'TABLE'
						when 'V' then 'VIEW'
						when 'S' then 'SYSTEM TABLE'
						end),
		TABLE_GUID		= convert(binary(16), null),
		BOOKMARKS		= convert(bit, 1),
		BOOKMARK_TYPE		= convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
		BOOKMARK_DATATYPE	= convert(smallint, 19 /*DBTYPE_UI4 */),
		BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
		BOOKMARK_INFORMATION	= convert(int, 0),
		TABLE_VERSION		= convert(int, o.schema_ver),
		CARDINALITY		= x.rows,
		DESCRIPTION		= convert(varchar(1), null),
		TABLE_PROPID		= convert(int, null)

	from	sysusers u, 
		sysobjects o,
		sysindexes x

	where	o.type in ('U','V','S')
	and 	(	@table_schema is null
		or	@table_schema = user_name(o.uid)
		)
	and 	(
			@table_type is null
		or	@table_type = case o.type 
					when 'U' then 'TABLE'
					when 'V' then 'VIEW'
					when 'S' then 'SYSTEM TABLE'
					end
		)
	and	o.id *= x.id
	and	x.indid in (0,1)
	and 	u.uid = user_id() /* constrain sysusers uid for use in subquery */
	and 	(
		suser_id() = 1	 /* User is the System Administrator */
		or o.uid = user_id()	 /* User created the object */
		/* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
		or (	(select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
			from sysprotects p
			/* join to correlate with all rows in sysobjects */
			where p.id = o.id
			/* get rows for public,current user,user's group */
			and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
			/* check for SELECT,EXECUTE privilege */
			and (action in (193,224)))&1	 /* more magic...normalize GRANT */
		    ) = 1	/* final magic...compare Grants	  */
		)		
	order by 4, 2, 3
go
dump tran master with no_log
go

if (charindex('7.00', @@version) > 0)
	drop procedure sp_tables_info_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/*	Procedure for 7.00 server */
create procedure sp_tables_info_rowset
	(
	@table_name	sysname, 
	@table_schema	sysname = null,	
	@table_type	nvarchar(255) = null 
	)
as
	select	*
	from	(select	TABLE_CATALOG		= db_name(),
			TABLE_SCHEMA		= user_name(o.uid),
			TABLE_NAME		= o.name,
			TABLE_TYPE		= convert(nvarchar(30),
							case o.type 
							when 'U' then 
							    case when ObjectProperty(o.id, 'IsMSShipped') = 0
							    then N'TABLE' else N'SYSTEM TABLE' end
							when 'S' then N'SYSTEM TABLE'
							when 'V' then 
							    case when ObjectProperty(o.id, 'IsMSShipped') = 0
							    then N'VIEW' else N'SYSTEM VIEW' end
							end),
			TABLE_GUID		= convert(uniqueidentifier, null),
			BOOKMARKS		= convert(bit, 1),
			BOOKMARK_TYPE		= convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
			BOOKMARK_DATATYPE	= convert(smallint, 19 /*DBTYPE_UI4 */),
			BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
			BOOKMARK_INFORMATION	= convert(int, 0),
			TABLE_VERSION		= convert(int, o.schema_ver),
			CARDINALITY		= x.rows,
			DESCRIPTION		= convert(nvarchar(1), null),
			TABLE_PROPID		= convert(int, null) 
	
		from	sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
		where	o.name = @table_name
		and 	o.type in ('U','V','S')
		and	permissions(o.id) <> 0) as t

	where 	(@table_schema is null or @table_schema = TABLE_SCHEMA)
	and 	(@table_type is null or	@table_type = TABLE_TYPE)
	order by 4, 2, 3
go
dump tran master with no_log
go
create procedure sp_tables_info_rowset;2
	(
	@table_schema	sysname = null,	
	@table_type	nvarchar(255) = null 
	)
as
	select	*
	from	(select	TABLE_CATALOG		= db_name(),
			TABLE_SCHEMA		= user_name(o.uid),
			TABLE_NAME		= o.name,
			TABLE_TYPE		= convert(nvarchar(30),
							case o.type 
							when 'U' then 
							    case when ObjectProperty(o.id, 'IsMSShipped') = 0
							    then N'TABLE' else N'SYSTEM TABLE' end
							when 'S' then N'SYSTEM TABLE'
							when 'V' then 
							    case when ObjectProperty(o.id, 'IsMSShipped') = 0
							    then N'VIEW' else N'SYSTEM VIEW' end
							end),
			TABLE_GUID		= convert(uniqueidentifier, null),
			BOOKMARKS		= convert(bit, 1),
			BOOKMARK_TYPE		= convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
			BOOKMARK_DATATYPE	= convert(smallint, 19 /*DBTYPE_UI4 */),
			BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
			BOOKMARK_INFORMATION	= convert(int, 0),
			TABLE_VERSION		= convert(int, o.schema_ver),
			CARDINALITY		= x.rows,
			DESCRIPTION		= convert(nvarchar(1), null),
			TABLE_PROPID		= convert(int, null) 
	
		from	sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
		where 	o.type in ('U','V','S')
		and	permissions(o.id) <> 0) as t

	where 	(@table_schema is null or @table_schema = TABLE_SCHEMA)
	and 	(@table_type is null or	@table_type = TABLE_TYPE)
	order by 4, 2, 3
go

grant execute on sp_tables_info_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
		where name = 'sp_tables_info_rowset' and type = 'P '))
		begin
		drop procedure sp_tables_info_rowset
		dump tran master with no_log
		end
	end
go

print ''
print 'creating sp_table_constraints_rowset'
go


/*	Procedure for 6.50 and earlier servers */
create procedure sp_table_constraints_rowset
	(
	@table_name			varchar(255), 
	@table_schema		varchar(255) = null,
	@table_catalog		varchar(255) = null,
	@constraint_name	varchar(255) = null,
	@constraint_schema	varchar(255) = null,
	@constraint_catalog	varchar(255) = null,
	@constraint_type	varchar(255) = null 
	)
as
	select
		CONSTRAINT_CATALOG	= db_name(),
		CONSTRAINT_SCHEMA	= user_name(c_obj.uid),
		CONSTRAINT_NAME		= c_obj.name,
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(t_obj.uid),
		TABLE_NAME			= t_obj.name,
		CONSTRAINT_TYPE		= case (c.status & 0xf)
								when 1 then 'PRIMARY KEY'
								when 2 then	'UNIQUE'
								when 3 then	'FOREIGN KEY'
								when 4 then	'CHECK'
		 						end, 
		IS_DEFERRABLE		= convert(bit, 0),
		INITIALLY_DEFERRED	= convert(bit, 0),
		DESCRIPTION			= convert(varchar(1), null)

	from 
		sysobjects c_obj, sysobjects t_obj, sysconstraints c
	where
		    t_obj.name	= @table_name
		and t_obj.type in ('U','S')
		and (@table_catalog is null or @table_catalog = db_name())
		and	(@table_schema is null or @table_schema = user_name(t_obj.uid))
		and c.id = t_obj.id
		and (c.status & 0xf) between 1 and 4
		and c_obj.id	= c.constid
		and	c_obj.uid	= user_id()
		and (@constraint_name is null or c_obj.name	= @constraint_name)
		and (@constraint_catalog is null or @constraint_catalog = db_name())
		and	(@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
		and (@constraint_type is null
			or (c.status & 0xf)
				= case @constraint_type
					when 'PRIMARY KEY' then 1
					when 'UNIQUE' then 2
					when 'FOREIGN KEY' then 3
					when 'CHECK' then 4
					end)
order by 2,3,5,6,7 
go
dump tran master with no_log
go
create procedure sp_table_constraints_rowset;2
	(
	@table_schema		varchar(255) = null,
	@table_catalog		varchar(255) = null,
	@constraint_name	varchar(255) = null,
	@constraint_schema	varchar(255) = null,
	@constraint_catalog	varchar(255) = null,
	@constraint_type	varchar(255) = null 
	)
as
	select
		CONSTRAINT_CATALOG	= db_name(),
		CONSTRAINT_SCHEMA	= user_name(c_obj.uid),
		CONSTRAINT_NAME		= c_obj.name,
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(t_obj.uid),
		TABLE_NAME			= t_obj.name,
		CONSTRAINT_TYPE		= case (c.status & 0xf)
								when 1 then 'PRIMARY KEY'
								when 2 then	'UNIQUE'
								when 3 then	'FOREIGN KEY'
								when 4 then	'CHECK'
		 						end, 
		IS_DEFERRABLE		= convert(bit, 0),
		INITIALLY_DEFERRED	= convert(bit, 0),
		DESCRIPTION			= convert(varchar(1), null)

	from 
		sysobjects c_obj, sysobjects t_obj, sysconstraints c
	where
			t_obj.type in ('U','S')
		and (@table_catalog is null or @table_catalog = db_name())
		and	(@table_schema is null or @table_schema = user_name(t_obj.uid))
		and c.id = t_obj.id
		and (c.status & 0xf) between 1 and 4
		and c_obj.id	= c.constid
		and	c_obj.uid	= user_id()
		and (@constraint_name is null or c_obj.name	= @constraint_name)
		and (@constraint_catalog is null or @constraint_catalog = db_name())
		and	(@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
		and (@constraint_type is null
			or (c.status & 0xf)
				= case @constraint_type
					when 'PRIMARY KEY' then 1
					when 'UNIQUE' then 2
					when 'FOREIGN KEY' then 3
					when 'CHECK' then 4
					end)
order by 2,3,5,6,7 
go
dump tran master with no_log
go


if (charindex('7.00', @@version) > 0)
	drop procedure sp_table_constraints_rowset
else
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go

/*	Procedure for 7.00 server */
create procedure sp_table_constraints_rowset
	(
	@table_name			sysname, 
	@table_schema		sysname = null,
	@table_catalog		sysname = null,
	@constraint_name	sysname = null,
	@constraint_schema	sysname = null,
	@constraint_catalog	sysname = null,
	@constraint_type	nvarchar(255) = null 
	)
as
	select
		CONSTRAINT_CATALOG	= db_name(),
		CONSTRAINT_SCHEMA	= user_name(c_obj.uid),
		CONSTRAINT_NAME		= c_obj.name,
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(t_obj.uid),
		TABLE_NAME			= t_obj.name,
		CONSTRAINT_TYPE		= case (c.status & 0xf)
								when 1 then N'PRIMARY KEY'
								when 2 then	N'UNIQUE'
								when 3 then	N'FOREIGN KEY'
								when 4 then	N'CHECK'
		 						end, 
		IS_DEFERRABLE		= convert(bit, 0),
		INITIALLY_DEFERRED	= convert(bit, 0),
		DESCRIPTION			= convert(nvarchar(1), null)

	from 
		sysobjects c_obj, sysobjects t_obj, sysconstraints c
	where
		    t_obj.name	= @table_name
		and t_obj.type in ('U','S')
		and (@table_catalog is null or @table_catalog = db_name())
		and	(@table_schema is null or @table_schema = user_name(t_obj.uid))
		and c.id = t_obj.id
		and (c.status & 0xf) between 1 and 4
		and c_obj.id	= c.constid
		and	c_obj.uid	= user_id()
		and (@constraint_name is null or c_obj.name	= @constraint_name)
		and (@constraint_catalog is null or @constraint_catalog = db_name())
		and	(@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
		and (@constraint_type is null
			or (c.status & 0xf)
				= case @constraint_type
					when N'PRIMARY KEY' then 1
					when N'UNIQUE' then 2
					when N'FOREIGN KEY' then 3
					when N'CHECK' then 4
					end)
order by 2,3,5,6,7 
go
dump tran master with no_log
go
create procedure sp_table_constraints_rowset;2
	(
	@table_schema		sysname = null,
	@table_catalog		sysname = null,
	@constraint_name	sysname = null,
	@constraint_schema	sysname = null,
	@constraint_catalog	sysname = null,
	@constraint_type	nvarchar(255) = null 
	)
as
	select
		CONSTRAINT_CATALOG	= db_name(),
		CONSTRAINT_SCHEMA	= user_name(c_obj.uid),
		CONSTRAINT_NAME		= c_obj.name,
		TABLE_CATALOG		= db_name(),
		TABLE_SCHEMA		= user_name(t_obj.uid),
		TABLE_NAME			= t_obj.name,
		CONSTRAINT_TYPE		= case (c.status & 0xf)
								when 1 then N'PRIMARY KEY'
								when 2 then	N'UNIQUE'
								when 3 then	N'FOREIGN KEY'
								when 4 then	N'CHECK'
		 						end, 
		IS_DEFERRABLE		= convert(bit, 0),
		INITIALLY_DEFERRED	= convert(bit, 0),
		DESCRIPTION			= convert(nvarchar(1), null)

	from 
		sysobjects c_obj, sysobjects t_obj, sysconstraints c
	where
			t_obj.type in ('U','S')
		and (@table_catalog is null or @table_catalog = db_name())
		and	(@table_schema is null or @table_schema = user_name(t_obj.uid))
		and c.id = t_obj.id
		and (c.status & 0xf) between 1 and 4
		and c_obj.id	= c.constid
		and	c_obj.uid	= user_id()
		and (@constraint_name is null or c_obj.name	= @constraint_name)
		and (@constraint_catalog is null or @constraint_catalog = db_name())
		and	(@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
		and (@constraint_type is null
			or (c.status & 0xf)
				= case @constraint_type
					when N'PRIMARY KEY' then 1
					when N'UNIQUE' then 2
					when N'FOREIGN KEY' then 3
					when N'CHECK' then 4
					end)
order by 2,3,5,6,7 
go
dump tran master with no_log


grant execute on sp_table_constraints_rowset to public
go

dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
			where name = 'sp_table_constraints_rowset' and type = 'P '))
		begin
		drop procedure sp_table_constraints_rowset
		dump tran master with no_log
		end
	end
go

print ''
print 'creating sp_table_privileges_rowset'
go

/*	Procedure for 6.0 and 6.5 server */
CREATE PROCEDURE sp_table_privileges_rowset
	(
	@table_name	varchar(255) = null,
	@table_schema	varchar(255) = null,
	@grantor	varchar(255) = null,
	@grantee	varchar(255) = null
	)
as
IF @table_name is not null
	BEGIN
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u
	where
		o.name = @table_name
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	o.type in ('U','V','S')
	and	p.id = o.id
	and 	(@grantor is null or @grantor = user_name(p.grantor))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,196,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid)
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u
	where
		o.name = @table_name
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	o.type in ('U','V','S')
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'	/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,196,197)
	and 	not exists (	/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4,5,6,1,2
	END
ELSE
	BEGIN
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and	p.id = o.id
	and 	(@grantor is null or @grantor = user_name(p.grantor))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,196,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid)
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'	/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,196,197)
	and 	not exists (	/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4,5,6,1,2
	END
go
dump tran master with no_log
go
CREATE PROCEDURE sp_table_privileges_rowset;2
	(
   	@handle		int output,
   	@scrollopt	int output,
	@ccopt		int output,
	@rows		int output,
	@table_name 	varchar(255) = null,
	@table_schema	varchar(255) = null,
	@grantor	varchar(255) = null,
	@grantee	varchar(255) = null
	)
as
declare @ret int
SET NOCOUNT ON
create table #sptprivsrowset1
	(
	GRANTOR		sysname not null,
	GRANTEE		sysname not null,
	TABLE_CATALOG	sysname not null,
	TABLE_SCHEMA	sysname not null,
	TABLE_NAME	sysname not null,
	PRIVILEGE_TYPE	sysname not null,
	IS_GRANTABLE	bit not null
	)
	
IF @table_name is not null
	BEGIN
	insert into #sptprivsrowset1
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u
	where
		o.name = @table_name
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	o.type in ('U','V','S')
	and	p.id = o.id
	and 	(@grantor is null or @grantor = user_name(p.grantor))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,196,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid)
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u
	where
		o.name = @table_name
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	o.type in ('U','V','S')
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'	/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,196,197)
	and 	not exists (	/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4,5,6,1,2
	END
ELSE
	BEGIN
	insert into #sptprivsrowset1
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case p.action
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and	p.id = o.id
	and 	(@grantor is null or @grantor = user_name(p.grantor))
			/* expand groups */
	and 	((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,196,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid)
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(varchar(30),
					case v.number
					when 193 then 'SELECT'
					when 195 then 'INSERT'
					when 196 then 'DELETE'
					when 197 then 'UPDATE'
					else 'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'	/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,196,197)
	and 	not exists (	/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4,5,6,1,2
	END

exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
	@scrollopt output, @ccopt output, @rows output

drop table #sptprivsrowset1
return isnull(@ret,0)
go
dump tran master with no_log
go
CREATE PROCEDURE sp_table_privileges_rowset;3
as
	select
		GRANTOR		= convert(sysname, ' '),
		GRANTEE		= convert(sysname, ' '),
		TABLE_CATALOG	= convert(sysname, ' '),
		TABLE_SCHEMA	= convert(sysname, ' '),
		TABLE_NAME	= convert(sysname, ' '),
		PRIVILEGE_TYPE	= convert(varchar(30), ' '),
		IS_GRANTABLE	= convert(bit, 0)
	where	1=0
go

if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
else
	drop proc sp_table_privileges_rowset
go


/*	Procedure for 7.0 server */
CREATE PROCEDURE sp_table_privileges_rowset
	(
	@table_name 		sysname,
	@table_schema		sysname = null,
	@grantor		sysname = null,
	@grantee		sysname = null
	)
as
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case p.action
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 196 then N'DELETE'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, sysmembers m
	where
		o.name = @table_name
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	o.type in ('U','V','S')
	and	p.id = o.id
	and 	(@grantor is null or @grantor = user_name(p.grantor))
		/* expand groups - AKUNDONE: only 1 level of grp unrolling. */
	and 	(u.uid > 0 and u.uid < 16384)
	and 	((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,196,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid)
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case v.number
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 196 then N'DELETE'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u
	where
		o.name = @table_name
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	o.type in ('U','V','S')
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'	/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,196,197)
	and 	not exists (	/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4,5,6,1,2
go
dump tran master with no_log
go
CREATE PROCEDURE sp_table_privileges_rowset;2
	(
	@table_schema		sysname = null,
	@grantor		sysname = null,
	@grantee		sysname = null
	)
as
	select
		GRANTOR		= user_name(p.grantor),
		GRANTEE		= user_name(u.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case p.action
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 196 then N'DELETE'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,case when p.protecttype = 205 then 0 else 1 end)
	from 
		sysprotects p, sysobjects o, sysusers u, sysmembers m
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and	p.id = o.id
	and 	(@grantor is null or @grantor = user_name(p.grantor))
		/* expand groups - AKUNDONE: only 1 level of grp unrolling. */
	and 	(u.uid > 0 and u.uid < 16384)
	and 	((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
	and 	p.protecttype <> 206	/* only grant rows */
	and 	p.action in (26,193,195,196,197)
	and 	o.uid <> u.uid			/* no rows for owner */
	and 	not exists (			/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = p.action
			and 	p1.id = p.id
			and 	p1.uid = u.uid)
	union all
	select	/*	Add rows for table owner */
		GRANTOR		= user_name(u.uid),
		GRANTEE		= user_name(o.uid),
		TABLE_CATALOG	= db_name(),
		TABLE_SCHEMA	= user_name(o.uid),
		TABLE_NAME	= o.name,
		PRIVILEGE_TYPE	= convert(nvarchar(30),
					case v.number
					when 193 then N'SELECT'
					when 195 then N'INSERT'
					when 196 then N'DELETE'
					when 197 then N'UPDATE'
					else N'REFERENCES'
					end),
		IS_GRANTABLE	= convert(bit,1)	
	from 
		sysobjects o, master.dbo.spt_values v, sysusers u
	where
		o.type in ('U','V','S')
	and	(@table_schema is null or @table_schema = user_name(o.uid))
	and	(@grantee is null or @grantee = user_name(u.uid))
	and 	u.suid = 1		/* grantor is dbo of database */
	and	(@grantor is null or @grantor = user_name(u.uid))
	and 	v.type = 'P'	/* cross product to get all exposed privileges */
	and 	v.number in (26,193,195,196,197)
	and 	not exists (	/* exclude revoke'd privileges */
			select 	*
			from 	sysprotects p1
			where	p1.protecttype = 206
			and 	p1.action = v.number
			and 	p1.id = o.id
			and 	p1.uid = o.uid)
	order by 4,5,6,1,2
go
dump tran master with no_log
go
create procedure sp_table_privileges_rowset;5
	(
	@table_server		sysname,
	@table_catalog		sysname = null,
	@table_name		sysname = null,
	@table_schema		sysname = null,
	@grantor		sysname = null,
	@grantee		sysname = null
	)
as
	select
		GRANTOR,		
		GRANTEE,		
		TABLE_CATALOG,	
		TABLE_SCHEMA,	
		TABLE_NAME,	
		PRIVILEGE_TYPE,	
		IS_GRANTABLE	
	from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name,
				@grantor,
				@grantee >
	order by 3,4,5,6,1,2
go

grant execute on sp_table_privileges_rowset to public
go
dump tran master with no_log
go
if (charindex('6.00', @@version) > 0)
	begin
	if (exists (select * from sysobjects
		where name = 'sp_table_privileges_rowset' and type = 'P '))
		begin
		drop proc sp_table_privileges_rowset
		dump tran master with no_log
		end
	end
go


print ''
print 'creating sp_linkedservers_rowset'
go
if (charindex('7.00', @@version) = 0)
begin
	print ''
	print ''
	print 'Warning:'
	print 'you are installing the stored procedures '
	print 'on a pre 7.0 SQL Server.'
	print 'Ignore the following errors.'
end
go
/*	Procedure for 7.0 server */
create proc sp_linkedservers_rowset
	(
	@srvname 	sysname
	)
as
	select
		SVR_NAME 		= srvname,
		SVR_PRODUCT		= srvproduct,
		SVR_PROVIDERNAME	= providername,
		SVR_DATASOURCE		= datasource,
		SVR_PROVIDERSTRING	= providerstring,
		SVR_LOCATION		= location,
		SVR_CATALOG		= catalog
	from master.dbo.sysservers
	where srvname = @srvname and (srvstatus & 128) = 128
	order by 1
go
dump tran master with no_log
go
create proc sp_linkedservers_rowset;2
as
	select
		SVR_NAME 		= srvname,
		SVR_PRODUCT		= srvproduct,
		SVR_PROVIDERNAME	= providername,
		SVR_DATASOURCE		= datasource,
		SVR_PROVIDERSTRING	= providerstring,
		SVR_LOCATION		= location,
		SVR_CATALOG		= catalog
	from master.dbo.sysservers
	where (srvstatus & 128) = 128
	order by 1
go

grant execute on sp_linkedservers_rowset to public
go

/*---------------------------- END OLEDB CATALOG PROCS ------------------------*/


dump tran master with no_log
go




if (exists (select * from sysobjects
		where name = 'sp_configure' and type = 'P '))
	begin
		exec sp_configure 'allow updates',0
		reconfigure with override
	end
go

exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */

go

if exists (select * from sysobjects where name = 'sp_check_objects'
			and type = 'P ')
	begin
		/* Only supported on 6.0 servers */
		print ''
		print 'Checking objects created by instcat.sql.'

		exec sp_check_objects 'catalog'
	end
go

print ''
print 'instcat.sql completed successfully.'
go

set quoted_identifier off
go

dump tran master with no_log
go
checkpoint
go
/**/
