	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		
		)

IF @pk_table_name is not null
	BEGIN
	insert into #spfkeysrowset1
	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,2),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,3),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,4),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,5),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,6),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,7),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,8),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,9),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,10),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,11),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,12),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,13),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,14),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,15),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,16),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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
		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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,2),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,3),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,4),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,5),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,6),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,7),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,8),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,9),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,10),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,11),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,12),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,13),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,14),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,15),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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(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,16),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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
		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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,2),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,3),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,4),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,5),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,6),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,7),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,8),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,9),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,10),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,11),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,12),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,13),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,14),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,15),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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(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,16),
		UPDATE_RULE		= 'NO ACTION',
		DELETE_RULE 		= 'NO ACTION'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	(@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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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'		
	from
		sysobjects o1, sysobjects o2,
		syscolumns c1, syscolumns c2,
		sysreferences r
	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	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 	
	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
	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
	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)
	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)
	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)
	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)
	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)
	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)
	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		
	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		= coalesce(d.column_size,d.data_precision,t.prec),
		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 d.data_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		= coalesce(d.column_size,d.data_precision,t.prec),
		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 d.data_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 model.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, null),
		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 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	(@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,null),
		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,null),
		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.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,null),
		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 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	(@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,null),
		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,null),
		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, null),
		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 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	(@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,null),
		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,null),
		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.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,null),
		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 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	(@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,null),
		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,null),
		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, null),
		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, null),
		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*/
							or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						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 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, null),
		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,null),
		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.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, null),
		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*/
							or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
						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 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, null),
		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,null),
		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	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					end),
		TABLE_GUID	= convert(uniqueidentifier, null),
		DESCRIPTION	= convert(nvarchar(1), null),
		TABLE_PROPID	= convert(int,null),
		DATE_CREATED	= o.crdate,
		DATE_MODIFIED	= convert(datetime,null)
	from	sysobjects o
	where	o.name = @table_name
	and 	o.type in ('U','V','S')
	and	permissions(o.id) <> 0
	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					end
		)	
	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	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					end),
		TABLE_GUID	= convert(uniqueidentifier, null),
		DESCRIPTION	= convert(nvarchar(1), null),
		TABLE_PROPID	= convert(int,null),
		DATE_CREATED	= o.crdate,
		DATE_MODIFIED	= convert(datetime,null)
	from	sysobjects o
	where	o.type in ('U','V','S')
	and	permissions(o.id) <> 0
	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					end
		)	
	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	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					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
	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					end
		)	
	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	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					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
	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 N'TABLE'
					when 'V' then N'VIEW'
					when 'S' then N'SYSTEM TABLE'
					end
		)	
	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_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

dump tran master with no_log
print ''
go

/*-----------------------------------------------------------------------------*/
/*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
/*-----------------------------------------------------------------------------*/

if object_id('sp_linkedservers', 'P') is not null
	drop proc sp_linkedservers
go
raiserror(15339,-1,-1,'sp_linkedservers')
go
create proc sp_linkedservers as
	select
		SRV_NAME = srvname,
		SRV_PROVIDERNAME = providername,
		SRV_PRODUCT = srvproduct,
		SRV_DATASOURCE = datasource,
		SRV_PROVIDERSTRING = providerstring,
		SRV_LOCATION = location,
		SRV_CAT = catalog
	from master.dbo.sysservers
	order by 1
go
grant execute on sp_linkedservers to public
go


if object_id('sp_catalogs', 'P') is not null
	drop proc sp_catalogs
go
raiserror(15339,-1,-1,'sp_catalogs')
go
create procedure sp_catalogs(
	@server_name		sysname)
as
	select
        CATALOG_NAME,
		convert (nvarchar(255),DESCRIPTION)
	from master.dbo.SYSREMOTE_CATALOGS < @server_name >
	order by CATALOG_NAME
go
grant execute on sp_catalogs to public
go


if object_id('sp_tables_ex', 'P') is not null
	drop proc sp_tables_ex
go
raiserror(15339,-1,-1,'sp_tables_ex')
go
create procedure sp_tables_ex(
	@table_server		sysname,
	@table_name			sysname = null,
	@table_schema		sysname = null,
	@table_catalog		sysname = null,
	@table_type			sysname = null)
as
	if ((isnull(charindex('%', @table_name),0) = 0) and
		(isnull(charindex('[', @table_name),0) = 0) and
		(isnull(charindex('_', @table_name),0) = 0))
	begin	/*	If no wild carding */
		select
			TABLE_CAT = TABLE_CATALOG,
			TABLE_SCHEM = TABLE_SCHEMA,
			TABLE_NAME = TABLE_NAME,
			TABLE_TYPE = TABLE_TYPE,
			REMARKS = convert(nvarchar(255),DESCRIPTION)
		from master.dbo.SYSREMOTE_TABLES <
					@table_server,
					@table_catalog,
					@table_schema,
					@table_name,
					@table_type >
		order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
	end
	else
	begin	/*	If wild carding */
		select
			TABLE_CAT = TABLE_CATALOG,
			TABLE_SCHEM = TABLE_SCHEMA,
			TABLE_NAME = TABLE_NAME,
			TABLE_TYPE = TABLE_TYPE,
			REMARKS = convert(nvarchar(255),DESCRIPTION)
		from master.dbo.SYSREMOTE_TABLES <
					@table_server,
					@table_catalog,
					@table_schema,
					NULL,
					@table_type >
		where TABLE_NAME like @table_name
		order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
	end
go
grant execute on sp_tables_ex to public
go


if object_id('sp_columns_ex', 'P') is not null
	drop proc sp_columns_ex
go
raiserror(15339,-1,-1,'sp_columns_ex')
go
create procedure sp_columns_ex(
	@table_server		sysname,
	@table_name			sysname = null,
	@table_schema		sysname = null,
	@table_catalog		sysname = null,
	@column_name		sysname = null,
	@ODBCVer			int = 2)
as

	set nocount on
	declare
	@DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),	@DBCOLUMNFLAGS_ISLONG	binary(1),
	@DBCOLUMNFLAGS_ISROWVER	binary(2)

	select
	@DBCOLUMNFLAGS_ISFIXEDLENGTH	= 0x10,	@DBCOLUMNFLAGS_ISLONG	= 0x80,
	@DBCOLUMNFLAGS_ISROWVER	= 0x0200

	declare	
	@DBTYPE_I2 smallint,			@DBTYPE_I4 smallint,
	@DBTYPE_R4 smallint,			@DBTYPE_R8 smallint,
	@DBTYPE_CY smallint,			@DBTYPE_DATE smallint,
	@DBTYPE_BSTR smallint,			@DBTYPE_BOOL smallint,
	@DBTYPE_VARIANT smallint,		@DBTYPE_DECIMAL smallint,
	@DBTYPE_UI1 smallint,			@DBTYPE_BYREF smallint,
	@DBTYPE_I1 smallint,			@DBTYPE_UI2 smallint,
	@DBTYPE_UI4 smallint,			@DBTYPE_I8 smallint,
	@DBTYPE_UI8 smallint,			@DBTYPE_GUID smallint,
	@DBTYPE_BYTES smallint,			@DBTYPE_STR smallint,
	@DBTYPE_WSTR smallint,			@DBTYPE_NUMERIC smallint,
	@DBTYPE_DBDATE smallint,		@DBTYPE_DBTIME smallint,
	@DBTYPE_DBTIMESTAMP smallint

	select
	@DBTYPE_I2			= 2,	@DBTYPE_I4			= 3,
	@DBTYPE_R4			= 4,	@DBTYPE_R8			= 5,
	@DBTYPE_CY			= 6,	@DBTYPE_DATE		= 7,
	@DBTYPE_BSTR		= 8,	@DBTYPE_BOOL		= 11,
	@DBTYPE_VARIANT		= 12,	@DBTYPE_DECIMAL		= 14,
	@DBTYPE_UI1			= 17,	@DBTYPE_BYREF		= 16384,
	@DBTYPE_I1			= 16,	@DBTYPE_UI2			= 18,
	@DBTYPE_UI4			= 19,	@DBTYPE_I8			= 20,
	@DBTYPE_UI8			= 21,	@DBTYPE_GUID		= 72,
	@DBTYPE_BYTES		= 128,	@DBTYPE_STR			= 129,
	@DBTYPE_WSTR		= 130,	@DBTYPE_NUMERIC		= 131,
	@DBTYPE_DBDATE		= 133,	@DBTYPE_DBTIME		= 134,	
	@DBTYPE_DBTIMESTAMP	= 135

	declare	
	@ODS_SMALLINT_NOT_NULL tinyint,	@ODS_INT_NULL tinyint,
	@ODS_INT_NOT_NULL tinyint,		@ODS_TINYINT_NOT_NULL tinyint,
	@ODS_REAL_NOT_NULL tinyint,		@ODS_FLOAT_NULL tinyint,
	@ODS_FLOAT_NOT_NULL tinyint,	@ODS_MONEY_NULL tinyint,
	@ODS_MONEY_NOT_NULL tinyint,	@ODS_DATETIME_NULL tinyint,
	@ODS_DATETIME_NOT_NULL tinyint,	@ODS_TEXT tinyint,
	@ODS_BIT tinyint,				@ODS_DECIMAL tinyint,
	@ODS_NUMERIC tinyint,			@ODS_GUID tinyint,
	@ODS_IMAGE tinyint,				@ODS_TIMESTAMP tinyint,
	@ODS_BINARY_NULL tinyint,		@ODS_BINARY_NOT_NULL tinyint,
	@ODS_VARBINARY tinyint,			@ODS_CHAR_NULL tinyint,
	@ODS_CHAR_NOT_NULL tinyint,		@ODS_VARCHAR tinyint

	select 	
	@ODS_IMAGE = 34,			@ODS_TEXT = 35,
	@ODS_GUID = 36,				@ODS_BINARY_NULL = 37,			
	@ODS_VARBINARY = 37,		@ODS_INT_NULL = 38,
	@ODS_CHAR_NULL = 39,		@ODS_VARCHAR = 39,
	@ODS_TIMESTAMP = 45,		@ODS_BINARY_NOT_NULL = 45,
	@ODS_CHAR_NOT_NULL = 47,	@ODS_TINYINT_NOT_NULL = 48,
	@ODS_BIT = 50,				@ODS_SMALLINT_NOT_NULL = 52,	
	@ODS_INT_NOT_NULL = 56,		@ODS_REAL_NOT_NULL = 59,		
	@ODS_MONEY_NOT_NULL = 60,	@ODS_DATETIME_NOT_NULL = 61,	
	@ODS_FLOAT_NOT_NULL = 62,	@ODS_DECIMAL = 106,
	@ODS_NUMERIC = 108,			@ODS_FLOAT_NULL = 109,
	@ODS_MONEY_NULL = 110,		@ODS_DATETIME_NULL = 111

	declare 
	@ODBC_GUID smallint,		@ODBC_NTEXT smallint,
	@ODBC_NVARCHAR smallint,	@ODBC_NCHAR smallint,
	@ODBC_BIT smallint,			@ODBC_TINYINT smallint,
	@ODBC_IMAGE smallint,		@ODBC_VARBINARY smallint,
	@ODBC_TIMESTAMP smallint,	@ODBC_BINARY smallint,
	@ODBC_TEXT smallint,		@ODBC_CHAR smallint,
	@ODBC_NUMERIC smallint,		@ODBC_DECIMAL smallint,
	@ODBC_MONEY smallint,		@ODBC_INT smallint,		
	@ODBC_SMALLINT smallint,	@ODBC_FLOAT smallint,	
	@ODBC_REAL smallint,		@ODBC_VARCHAR smallint,
	@ODBC_DATETIME smallint

	select
	@ODBC_GUID		= -11,	@ODBC_NTEXT		= -10,
	@ODBC_NVARCHAR	= -9,	@ODBC_NCHAR		= -8,
	@ODBC_BIT		= -7,	@ODBC_TINYINT	= -6,
	@ODBC_IMAGE		= -4,	@ODBC_VARBINARY	= -3,
	@ODBC_TIMESTAMP	= -2,	@ODBC_BINARY	= -2,
	@ODBC_TEXT		= -1,	@ODBC_CHAR		= 1,
	@ODBC_NUMERIC	= 2,	@ODBC_DECIMAL	= 3,
	@ODBC_MONEY		= 3,	@ODBC_INT		= 4,		
	@ODBC_SMALLINT	= 5,	@ODBC_FLOAT		= 6,	
	@ODBC_REAL		= 7,	@ODBC_VARCHAR	= 12,
	@ODBC_DATETIME	=	
	case @ODBCVer
		when 2 then 11
		else 93
	end

	create table #tmp_columns
	(	TABLE_CAT nvarchar(127),
		TABLE_SCHEM nvarchar(127),
		TABLE_NAME nvarchar(127),
		COLUMN_NAME nvarchar(127),
		DATA_TYPE smallint,
        TYPE_NAME nvarchar(127) NULL,
		COLUMN_SIZE int NULL,
		BUFFER_LENGTH int NULL,
		DECIMAL_DIGITS smallint NULL,
		NUM_PREC_RADIX smallint NULL,
        NULLABLE smallint, 
		REMARKS nvarchar(255) NULL,
        COLUMN_DEF nvarchar(127) NULL,
        SQL_DATA_TYPE smallint null,
        SQL_DATETIME_SUB smallint NULL,
		CHAR_OCTET_LENGTH int NULL,
		ORDINAL_POSITION smallint,
		IS_NULLABLE char(4),
		SS_DATA_TYPE tinyint null,
		COLUMN_FLAGS int)	

	insert into #tmp_columns
	select
		TABLE_CAT = c.TABLE_CATALOG,
		TABLE_SCHEM = c.TABLE_SCHEMA,
		TABLE_NAME = c.TABLE_NAME,
		COLUMN_NAME = c.COLUMN_NAME,
		DATA_TYPE = 
		case c.DATA_TYPE & ~@DBTYPE_BYREF
			when @DBTYPE_I2				then @ODBC_SMALLINT
			when @DBTYPE_I4				then @ODBC_INT
			when @DBTYPE_R4				then @ODBC_REAL
			when @DBTYPE_R8				then @ODBC_FLOAT
			when @DBTYPE_CY				then @ODBC_MONEY
			when @DBTYPE_DATE			then @ODBC_DATETIME 
			when @DBTYPE_DBDATE			then @ODBC_DATETIME	
			when @DBTYPE_DBTIME			then @ODBC_DATETIME	
			when @DBTYPE_DBTIMESTAMP	then @ODBC_DATETIME
			when @DBTYPE_BOOL			then @ODBC_BIT
			when @DBTYPE_VARIANT		then @ODBC_NVARCHAR
			when @DBTYPE_DECIMAL		then @ODBC_DECIMAL
			when @DBTYPE_UI1			then @ODBC_TINYINT
			when @DBTYPE_I1				then @ODBC_NUMERIC
			when @DBTYPE_UI2			then @ODBC_NUMERIC
			when @DBTYPE_UI4			then @ODBC_NUMERIC
			when @DBTYPE_I8				then @ODBC_NUMERIC
			when @DBTYPE_UI8			then @ODBC_NUMERIC
			when @DBTYPE_NUMERIC		then @ODBC_NUMERIC
			when @DBTYPE_GUID			then @ODBC_GUID
			when @DBTYPE_BYTES then  
				case 
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
							(c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
							 p.COLUMN_SIZE = 8
						then @ODBC_TIMESTAMP
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
						then @ODBC_BINARY
					else @ODBC_VARBINARY
				end
			 when @DBTYPE_STR  then
				case
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
					else @ODBC_VARCHAR
				end
			when @DBTYPE_WSTR then
				case
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
					else @ODBC_NVARCHAR
				end
			when @DBTYPE_BSTR then
				case
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
					when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
					else @ODBC_NVARCHAR
				end
		end,
        TYPE_NAME = NULL,
		COLUMN_SIZE = 
			COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),		
		BUFFER_LENGTH = isnull(c.CHARACTER_OCTET_LENGTH,0),
		DECIMAL_DIGITS = c.NUMERIC_SCALE,
        NUM_PREC_RADIX = NULL,
		NULLABLE = convert(smallint, c.IS_NULLABLE),
		REMARKS = convert(nvarchar(255),c.DESCRIPTION),
        COLUMN_DEF = c.COLUMN_DEFAULT,
        SQL_DATA_TYPE = NULL,
        SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
		CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
		ORDINAL_POSITION = c.ORDINAL_POSITION,
		IS_NULLABLE = 
		case c.IS_NULLABLE
			when 1 then 'YES'
			else 'NO'
		end,
		SS_DATA_TYPE = NULL,
		COLUMN_FLAGS = c.COLUMN_FLAGS
	from master.dbo.SYSREMOTE_COLUMNS <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name,
				@column_name > c
		LEFT OUTER JOIN
         master.dbo.SYSREMOTE_PROVIDER_TYPES <
                @table_server > p
				/* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
		ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1


	update #tmp_columns
	set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
		TYPE_NAME = 
		case 
			when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
					(COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
					BUFFER_LENGTH = 8
				then 'timestamp'
			when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
				then 'binary'
			else spt_dt.TYPE_NAME
		end,
		DECIMAL_DIGITS = COALESCE(DECIMAL_DIGITS,spt_dt.numeric_scale ),
		NUM_PREC_RADIX = spt_dt.RADIX,
		SS_DATA_TYPE = 
		case
			when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
				and #tmp_columns.NULLABLE = 1
					then @ODS_INT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
				then @ODS_TINYINT_NOT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT	and #tmp_columns.NULLABLE = 0
				then @ODS_SMALLINT_NOT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
				then @ODS_INT_NOT_NULL
			when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
				then @ODS_FLOAT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
				then @ODS_REAL_NOT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
				then @ODS_FLOAT_NOT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
				then @ODS_MONEY_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
				then @ODS_MONEY_NOT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
				then @ODS_DATETIME_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
				then @ODS_DATETIME_NOT_NULL
			when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
			when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
			when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL	
			when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
			when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
			when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
			when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
				and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
					(COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
					BUFFER_LENGTH = 8
				then @ODS_TIMESTAMP
			when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =	1
				and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
				then  @ODS_BINARY_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =	0
				and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
				then @ODS_BINARY_NOT_NULL
			when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
				then @ODS_VARBINARY	
			when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
				then @ODS_CHAR_NULL
			when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
				then @ODS_CHAR_NOT_NULL
			when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
			else null
		end
		from master.dbo.spt_datatype_info spt_dt
		where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
			and spt_dt.ODBCVer in (null,@ODBCVer)
			and spt_dt.AUTO_INCREMENT in ( 0, null)
			and not spt_dt.TYPE_NAME = 'smalldatetime'

		select
			TABLE_CAT,			TABLE_SCHEM,		TABLE_NAME,
			COLUMN_NAME,		DATA_TYPE,			TYPE_NAME,
			COLUMN_SIZE,		BUFFER_LENGTH,		DECIMAL_DIGITS,
			NUM_PREC_RADIX,		NULLABLE,			REMARKS,
			COLUMN_DEF,			SQL_DATA_TYPE,		SQL_DATETIME_SUB,
			CHAR_OCTET_LENGTH,	ORDINAL_POSITION,	IS_NULLABLE,
			SS_DATA_TYPE
		from #tmp_columns
		order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION

		drop table #tmp_columns

go
grant execute on sp_columns_ex to public
go


if object_id('sp_table_privileges_ex', 'P') is not null
	drop proc sp_table_privileges_ex
go
raiserror(15339,-1,-1,'sp_table_privileges_ex')
go
create procedure sp_table_privileges_ex(
	@table_server		sysname,
	@table_name			sysname = null,
	@table_schema		sysname = null,
	@table_catalog		sysname = null)
as
	select
		TABLE_CAT = TABLE_CATALOG,
		TABLE_SCHEM = TABLE_SCHEMA,
		TABLE_NAME = TABLE_NAME,
        GRANTOR = GRANTOR,
        GRANTEE = GRANTEE,
        PRIVILEGE = PRIVILEGE_TYPE,
        IS_GRANTABLE = 
		case IS_GRANTABLE
			when 1 then 'YES'
			when 0 then 'NO'
			else null
		end
	from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name >
	order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
go
grant execute on sp_table_privileges_ex to public
go


if object_id('sp_column_privileges_ex', 'P') is not null
	drop proc sp_column_privileges_ex
go
raiserror(15339,-1,-1,'sp_column_privileges_ex')
go
create procedure sp_column_privileges_ex(
	@table_server		sysname,
	@table_name			sysname = null,
	@table_schema		sysname = null,
	@table_catalog		sysname = null,
	@column_name		sysname = null)
as
	select
		TABLE_CAT = TABLE_CATALOG,
		TABLE_SCHEM = TABLE_SCHEMA,
		TABLE_NAME = TABLE_NAME,
        COLUMN_NAME = COLUMN_NAME,
        GRANTOR = GRANTOR,
        GRANTEE = GRANTEE,
        PRIVILEGE = PRIVILEGE_TYPE,
        IS_GRANTABLE = 
		case IS_GRANTABLE
			when 1 then 'YES'
			when 0 then 'NO'
			else null
		end
	from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name,
				@column_name >
	order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
go
grant execute on sp_column_privileges_ex to public
go


if object_id('sp_indexes', 'P') is not null
	drop proc sp_indexes
go
raiserror(15339,-1,-1,'sp_indexes')
go
create procedure sp_indexes(
	@table_server		sysname,
	@table_name			sysname = null,
	@table_schema		sysname = null,
	@table_catalog		sysname = null,
	@index_name			sysname = null,
    @is_unique          bit = null)
as
	select
		TABLE_CAT = TABLE_CATALOG,
		TABLE_SCHEM = TABLE_SCHEMA,
		TABLE_NAME = TABLE_NAME,
        NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
		INDEX_QUALIFIER = TABLE_NAME,
		INDEX_NAME = INDEX_NAME,
        TYPE = 
		case [CLUSTERED]
			when 1 then 1
			else 3
		end,
		ORDINAL_POSITION = ORDINAL_POSITION,
        COLUMN_NAME = COLUMN_NAME,
		ASC_OR_DESC = 
		case [COLLATION]
			when 1 then 'A'
			when 2 then 'D'
			else null
		end,
        CARDINALITY = CARDINALITY,
        PAGES = 
		case [CLUSTERED]
			when 1 then PAGES
			else NULL
		end,
        FILTER_CONDITION = FILTER_CONDITION
	from master.dbo.SYSREMOTE_INDEXES <
				@table_server,
				@table_catalog,
				@table_schema,
                @index_name,
				NULL,			/* TYPE (index type) */
				@table_name >
    where @is_unique is null or @is_unique = [UNIQUE]
	order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
go
grant execute on sp_indexes to public
go


if object_id('sp_foreignkeys', 'P') is not null
	drop proc sp_foreignkeys
go
raiserror(15339,-1,-1,'sp_foreignkeys')
go
create procedure sp_foreignkeys(
	@table_server		sysname,
    @pktab_name         sysname = null,
    @pktab_schema       sysname = null,
    @pktab_catalog      sysname = null,
    @fktab_name         sysname = null,
    @fktab_schema       sysname = null,
    @fktab_catalog      sysname = null)
as
	select
		PKTABLE_CAT = PK_TABLE_CATALOG,
		PKTABLE_SCHEM = PK_TABLE_SCHEMA,
		PKTABLE_NAME = PK_TABLE_NAME,
        PKCOLUMN_NAME = PK_COLUMN_NAME,
		FKTABLE_CAT = FK_TABLE_CATALOG,
		FKTABLE_SCHEM = FK_TABLE_SCHEMA,
		FKTABLE_NAME = FK_TABLE_NAME,
        FKCOLUMN_NAME = FK_COLUMN_NAME,
        KEY_SEQ = ORDINAL,
        UPDATE_RULE = 
		case UPDATE_RULE
			when 'CASCADE' then 0
			when 'NO ACTION' then 1
			when 'SET NULL' then 2
			else null
		end,
        DELETE_RULE = 
		case DELETE_RULE
			when 'CASCADE' then 0
			when 'NO ACTION' then 1
			when 'SET NULL' then 2
			else null
		end,
        FK_NAME = convert(sysname, NULL),
        PK_NAME = convert(sysname, NULL),
		DEFERRABILITY = convert(smallint, null)

	from master.dbo.SYSREMOTE_FOREIGN_KEYS <
				@table_server,
				@pktab_catalog,
				@pktab_schema,
				@pktab_name,
				@fktab_catalog,
				@fktab_schema,
				@fktab_name >
	order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
go
grant execute on sp_foreignkeys to public
go


if object_id('sp_primarykeys', 'P') is not null
	drop proc sp_primarykeys
go
raiserror(15339,-1,-1,'sp_primarykeys')
go
create procedure sp_primarykeys(
	@table_server		sysname,
    @table_name         sysname = null,
    @table_schema       sysname = null,
    @table_catalog      sysname = null)
as
	select
		TABLE_CAT = TABLE_CATALOG,
		TABLE_SCHEM = TABLE_SCHEMA,
		TABLE_NAME = TABLE_NAME,
        COLUMN_NAME = COLUMN_NAME,
        KEY_SEQ = ORDINAL,
        PK_NAME = convert(sysname, NULL)
	from master.dbo.SYSREMOTE_PRIMARY_KEYS <
				@table_server,
				@table_catalog,
				@table_schema,
				@table_name >
	order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
go
grant execute on sp_primarykeys 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
/**/
