• OK, this is an ugly kludge for SQL Server 2000.

    Replace the snippet for creating the preorder table with the one below.

    I can't think of a better way to do this.

    Glen

    -- Create a preorder table using the database

    -- relationships between tables. By definition,

    -- a table A will be 'less than' a table B if

    -- B has a foreign key pointing to A.

    -- SQL Server 2000 version.

    SELECT A, B

    INTO tblPreorder

    FROM

    (

    SELECT

    -- May be multiple relationships between two

    -- tables but we only need one

    DISTINCT

    -- Get name of parent table pointing to foreign table

    so2.name as B,

    -- Get name of foreign table by deleting

    -- name of parent table (along with punctuation)

    -- from name of foreign key constraint

    RIGHT(so1.name,LEN(so1.name) - (LEN(so2.name)+4)) as A

    FROM

    sysobjects so1

    INNER JOIN

    sysobjects so2

    ON

    so1.parent_obj = so2.id

    WHERE

    -- Restrict sysobjects to foreign key constraints

    so1.xtype='F'

    AND

    -- Make sure name of foreign table represents

    -- a real table because sysobjects will append

    -- 1, 2, etc. to foreign key constraint name if

    -- multiple relationships exist between a pair of them

    RIGHT(so1.name,LEN(so1.name) - (LEN(so2.name)+4)) IN (SELECT NAME FROM sysobjects WHERE xtype = 'U')

    ) derived

    ORDER BY A, B