- 论坛徽章:
- 0
|
pb 要跟sybase 连接时,出现以下问题。。
create procedure sp_pb60index
@objname varchar(92) /* the table to check for indexes */
as
declare @objid int /* the object id of the table */
declare @indid int /* the index id of an index */
declare @key1 varchar(30) /* first key */
declare @key2 varchar(30) /* second key */
declare @key3 varchar(30) /* third key */
declare @key4 varchar(30) /* fourth key */
declare @key5 varchar(30) /* ... */
declare @key6 varchar(30)
declare @key7 varchar(30)
declare @key8 varchar(30)
declare @key9 varchar(30) /* ... */
declare @key10 varchar(30)
declare @key11 varchar(30)
declare @key12 varchar(30)
declare @key13 varchar(30) /* ... */
declare @key14 varchar(30)
declare @key15 varchar(30)
declare @key16 varchar(30)
declare @unique smallint /* index is unique */
declare @clustered smallint /* index is clustered */
declare @isolevel int /* ptrack 325579 isolation level */
/*
** Check to see the the table exists and initialize @objid.
*/
select @objid = object_id(@objname)
/*
** Table doesn't exist so return.
*/
if @objid is null
begin
return
end
/*
** See if the object has any indexes.
** Since there may be more than one entry in sysindexes for the object,
** this select will set @indid to the index id of the first index.
*/
select @indid = min(indid)
from sysindexes
where id = @objid
and indid >; 0
and indid < 255
/*
** If no indexes, return.
*/
if @indid is null
begin
return
end
/* ptrack 325579 override isolation level 0 default */
select @isolevel = @@isolation
if @isolevel = 0
begin
set transaction isolation level 1
end
/*
** Now check out each index, figure out it's type and keys and
** save the info in a temporary table that we'll print out at the end.
*/
create table #spindtab
(
index_name varchar(30),
index_num int,
index_key1 varchar(30) null,
index_key2 varchar(30) null,
index_key3 varchar(30) null,
index_key4 varchar(30) null,
index_key5 varchar(30) null,
index_key6 varchar(30) null,
index_key7 varchar(30) null,
index_key8 varchar(30) null,
index_key9 varchar(30) null,
index_key10 varchar(30) null,
index_key11 varchar(30) null,
index_key12 varchar(30) null,
index_key13 varchar(30) null,
index_key14 varchar(30) null,
index_key15 varchar(30) null,
index_key16 varchar(30) null,
index_unique smallint,
index_clustered smallint,
index_status smallint,
index_status2 int
)
while @indid != null
begin
/*
** First we'll figure out what the keys are.
*/
declare @i int
declare @thiskey varchar(30)
declare @lastindid int
select @i = 1
set nocount on
while @i <= 16
begin
select @thiskey = index_col(@objname, @indid, @i)
if @thiskey = null
begin
goto keysdone
end
if @i = 1
begin
select @key1 = index_col(@objname, @indid, @i)
end
else
if @i = 2
begin
select @key2 = index_col(@objname, @indid, @i)
end
else
if @i = 3
begin
select @key3 = index_col(@objname, @indid, @i)
end
else
if @i = 4
begin
select @key4 = index_col(@objname, @indid, @i)
end
else
if @i = 5
begin
select @key5 = index_col(@objname, @indid, @i)
end
else
if @i = 6
begin
select @key6 = index_col(@objname, @indid, @i)
end
else
if @i = 7
begin
select @key7 = index_col(@objname, @indid, @i)
end
else
if @i = 8
begin
select @key8 = index_col(@objname, @indid, @i)
end
else
if @i = 9
begin
select @key9 = index_col(@objname, @indid, @i)
end
else
if @i = 10
begin
select @key10 = index_col(@objname, @indid, @i)
end
else
if @i = 11
begin
select @key11 = index_col(@objname, @indid, @i)
end
else
if @i = 12
begin
select @key12 = index_col(@objname, @indid, @i)
end
else
if @i = 13
begin
select @key13 = index_col(@objname, @indid, @i)
end
else
if @i = 14
begin
select @key14 = index_col(@objname, @indid, @i)
end
else
if @i = 15
begin
select @key15 = index_col(@objname, @indid, @i)
end
else
if @i = 16
begin
select @key16 = index_col(@objname, @indid, @i)
end
/*
** Increment @i so it will check for the next key.
*/
select @i = @i + 1
end
/*
** When we get here we now have all the keys.
*/
keysdone:
set nocount off
/*
** Figure out if it's a clustered or nonclustered index.
*/
if @indid = 1
select @clustered = 1
if @indid >; 1
select @clustered = 0
/*
** Now we'll check out the status bits for this index
*/
/*
** See if the index is unique (0x02).
*/
if exists (select *
from master.dbo.spt_values v, sysindexes i
where i.status & v.number = v.number
and v.type = 'I'
and v.number = 2
and i.id = @objid
and i.indid = @indid)
select @unique = 1
else
select @unique = 0
/*
** Now we have all the needed info for the index so we'll add
** the goods to the temporary table.
*/
insert into #spindtab
select name, @i - 1, @key1, @key2, @key3, @key4,
@key5, @key6, @key7, @key8, @key9,
@key10, @key11, @key12, @key13, @key14,
@key15, @key16, @unique, @clustered, status, status2
from sysindexes
where id = @objid
and indid = @indid
/*
** Now move @indid to the next index.
*/
select @lastindid = @indid
select @indid = null
select @indid = min(indid)
from sysindexes
where id = @objid
and indid >; @lastindid
and indid < 255
end
/*
** Now print out the contents of the temporary index table.
*/
select index_name, index_num, index_key1, index_key2,
index_key3, index_key4, index_key5, index_key6,
index_key7, index_key8, index_key9, index_key10,
index_key11, index_key12, index_key13, index_key14,
index_key15, index_key16, index_unique, index_clustered,
index_status, index_status2
from #spindtab
drop table #spindtab
/* ptrack 325579 reset isolation 0 after we're done with procedure */
if @isolevel = 0
begin
set transaction isolation level 0
end
****************************************************************************************************
create proc sp_pb60primarykey
@objname varchar(92)
as
declare @objid int /* the object id of the table */
declare @keyname varchar(30) /* name of primary key */
declare @indid int /* the index id of the index */
declare @keycnt smallint /* number of columns in pk */
select @objid = object_id(@objname)
if @objid is null
begin
return 1
end
select @keyname = name,
@indid = indid,
@keycnt = keycnt
from dbo.sysindexes
where id = object_id(@objname) and
indid >; 0 and /* make sure it is an index */
(status2 & 2) = 2 and /* make sure declaritive constraint */
(status & 204 = 2048 /* make sure it is primary key */
if @keycnt is null
begin
return 1
end
/* keycnt contains #clustered key columns but it contains #keys + 1 */
/* for non-clustered indexes. */
if @indid >; 1
begin
select @keycnt = @keycnt - 1
end
if @keycnt = 0
begin
return 1
end
select @keyname, @keycnt,
index_col(@objname, @indid, 1),
index_col(@objname, @indid, 2),
index_col(@objname, @indid, 3),
index_col(@objname, @indid, 4),
index_col(@objname, @indid, 5),
index_col(@objname, @indid, 6),
index_col(@objname, @indid, 7),
index_col(@objname, @indid, ,
index_col(@objname, @indid, 9),
index_col(@objname, @indid, 10),
index_col(@objname, @indid, 11),
index_col(@objname, @indid, 12),
index_col(@objname, @indid, 13),
index_col(@objname, @indid, 14),
index_col(@objname, @indid, 15),
index_col(@objname, @indid, 16)
*************************************************
*************************************************
create proc sp_pb60foreignkey
@objname varchar(92)
as
declare @objid int /* the object id of the fk table */
declare @keyname varchar(30) /* name of foreign key */
declare @constid int /* the constraint id in sysconstraints */
declare @keycnt smallint /* number of columns in pk */
declare @stat int
declare @isolevel int /* ptrack 325579 isolation level */
select @objid = object_id(@objname)
if (@objid is null)
begin
return (1)
end
select @stat = sysstat2
from dbo.sysobjects
where id = @objid and
(sysstat2 & 2) = 2
if (@stat is null)
begin
return (1)
end
/* Now I know this table has one or more foreign keys. */
/* ptrack 325579 override isolation level 0 default */
select @isolevel = @@isolation
if @isolevel = 0
begin
set transaction isolation level 1
end
select o1.name, r.keycnt, o2.name, user_name(o2.uid),
r.fokey1, r.fokey2, r.fokey3, r.fokey4, r.fokey5, r.fokey6,
r.fokey7, r.fokey8, r.fokey9, r.fokey10, r.fokey11, r.fokey12,
r.fokey13, r.fokey14, r.fokey15, r.fokey16
from dbo.sysconstraints c, dbo.sysobjects o1,
dbo.sysreferences r, dbo.sysobjects o2
where c.tableid = @objid and
c.status = 64 and
c.constrid = o1.id and
o1.type = 'RI' and
c.constrid = r.constrid and
r.reftabid = o2.id
if @isolevel = 0
begin
set transaction isolation level 0
end |
|