Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Syntax with 'WITH' 1

Status
Not open for further replies.
Oct 11, 2006
300
US
What is the syntax error near the WITH statement?

Thanks.

Code:
with organization (depth, emp, boss, chain)
as
( select 1 as depth,
         emp,
         boss,
         cast(rtrim(emp) as varchar (400))
    from tree
  where boss is null
  union all
  select a.depth + 1 as depth,
         b.emp,
         b.boss,
         cast (a.chain + ', ' + rtrim(b.emp) as varchar(400))
    from organization a
   inner join tree b
      on a.emp = b.boss
)

Error I get is:

Incorrect syntax near the keyword 'with'.
 
this should work, keep in mind that this will work on SQl server 2005 only and not on 2000

Code:
with organization (depth, emp, boss, chain)
as
( select 1 as depth,
         emp,
         boss,
         cast(rtrim(emp) as varchar (400))
    from tree
  where boss is null
  union all
  select a.depth + 1 as depth,
         b.emp,
         b.boss,
         cast (a.chain + ', ' + rtrim(b.emp) as varchar(400))
    from organization a
   inner join tree b
      on a.emp = b.boss
)

select * from organization


2000 syntax is like this
Code:
select * from 
( select 1 as depth,
         emp,
         boss,
         cast(rtrim(emp) as varchar (400))
    from tree
  where boss is null
  union all
  select a.depth + 1 as depth,
         b.emp,
         b.boss,
         cast (a.chain + ', ' + rtrim(b.emp) as varchar(400))
    from organization a
   inner join tree b
      on a.emp = b.boss
) organization

Denis The SQL Menace
SQL blog:
 
Thanks. Changes from one version to another can be painful in the syntax changes as well. I work SQL Server 2000.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top