Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 BCP and Parent/child tables sequence

Author  Topic 

erncelen
Starting Member

15 Posts

Posted - 2005-06-05 : 06:09:37
Hi.......

I am going to develop a BCP procedure in a large DB SQLServer2000 with more 300 tables.

Myquestion is :

Because of Primary Key and Foreign Key, there are Parent and childs tables, I cannot run BCP on tables randomly or by alphabetic order , but a correct order to keep the constranits between tables.

What are the best way to identify on which table must be run first the BCP utility.

Anyone can suggest me some tools or other utility (eg MS store procedure...) to solve my problem?

Any help will be appreciated.....

Thank in advance



Kristen
Test

22859 Posts

Posted - 2005-06-05 : 06:59:49
I haven't tested this, but its culled from some code we use to do a similar job.

First build a temporary table with the table names (taken from sysobjects), decrement a "Level" count according to the number of referencing foreign keys, and then you have an "order" to process the tables (you will need to sort ASC or DESC according to what you are trying to do - deleting child-first, or inserting parent-first

This will NOT work [fully] for any circular relationships.

Kristen

create table #kbm_SP_XferTable_03
(
dsTable varchar(255),
dsTID int, -- sysobjects ID
dsLevel int NULL default 50 -- Dependency level
)

INSERT INTO #kbm_SP_XferTable_03
(
dsTable,
dsTID
)
SELECT ... e.g. name, id FROM dbo.sysobjects WHERE type = 'U'

select @strSQL = 'UPDATE DSc '
+ 'SET DSc.dsLevel = DSp.dsLevel-1 '
+ 'from dbo.sysforeignkeys FK '
+ 'join #kbm_SP_XferTable_03 DSp on DSp.dsTID = FK.rkeyid '
+ 'join #kbm_SP_XferTable_03 DSc on DSc.dsTID = FK.fkeyid '
+ 'where DSc.dsLevel >= DSp.dsLevel'

set @intLoop = 10
while @intLoop > 0
BEGIN
set @intLoop = @intLoop - 1
exec (@strSQL)
END

select dsTable,
dsLevel
FROM #kbm_SP_XferTable_03
ORDER BY dsLevel DESC, dsTable
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-06-05 : 09:36:33
We had a similar requirement, here is our solution:
(it will take care of selfreferences and circular relationships(by ignoring them))
create function dbo.fnNestLevel(@object_id int) returns int as
begin
/* returns the "nesting" of an object
0 = the object is a "top level" object, ie. it has no parents
n = the object has n parents, ie how deeply it is nested
example usage:

select object_name(id) as table_name ,dbo.fnNestLevel(id) as nestlevel
from sysobjects
where type = 'u' order by 2, 1 --(specify 2 desc or 2 asc)

*/
declare @nestlevel int; set @nestlevel = -1
declare @nest table(nestlevel int not null, objid int)
insert @nest(nestlevel,objid)
select @nestlevel,@object_id
while @@rowcount <> 0
begin
set @nestlevel = @nestlevel + 1
insert @nest(nestlevel,objid)
select @nestlevel, s.rkeyid
from sysreferences s join @nest n
on s.fkeyid = n.objid
where n.nestlevel = @nestlevel - 1
and s.fkeyid <> s.rkeyid -- remove selfreferences
and s.rkeyid not in(select objid from @nest) -- remove circular relationships
end
return @nestlevel
end
GO

select object_name(id) as table_name ,dbo.fnNestLevel(id) as nestlevel
from sysobjects
where type = 'u' order by 2 /*desc*/ , 1
GO

drop function dbo.fnNestLevel
GO


rockmoose
Go to Top of Page

erncelen
Starting Member

15 Posts

Posted - 2005-06-05 : 16:50:38
Thank to everybody for your suggestion.....

Go to Top of Page
   

- Advertisement -