Solved

sql query syntax error

Posted on 2011-03-09
4
197 Views
Last Modified: 2012-05-11
I attached some code:  When it gets run I get a error

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'Category_ID'.

Why?


WITH CTE AS(
SELECT Category_ID, Category_Name, Parent_Category_ID, convert(varchar(max),Category_Name) Tree
  FROM Categories
 WHERE Parent_Category_ID is null
 UNION ALL
SELECT c.Category_ID, c.Category_Name, c.Parent_Category_ID, p.Tree + ' / ' + convert(varchar(max),c.Category_Name)
  FROM Categories c
  JOIN CTE p
    ON c.Parent_Category_ID = p.Category_ID

Open in new window

0
Comment
Question by:vbnetcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35084614
this should do:
WITH CTE AS(
SELECT Category_ID, Category_Name, Parent_Category_ID, convert(varchar(max),Category_Name) Tree
  FROM Categories
 WHERE Parent_Category_ID is null
 UNION ALL
SELECT c.Category_ID, c.Category_Name, c.Parent_Category_ID, p.Tree + ' / ' + convert(varchar(max),c.Category_Name)
  FROM Categories c
  JOIN CTE p
    ON c.Parent_Category_ID = p.Category_ID
)
select * from data

Open in new window

0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35084632
I believe it may be because you have not closed your parenthesis.
0
 

Author Closing Comment

by:vbnetcoder
ID: 35084684
ty
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 35084758
fixing syntax will not help you... what are you trying to achieve here...

try fixing one by one

with cte as (
....  -- first run this part to see if you get desired values
)

then run

with cte as (...)
select * from categories c join cte ON cteParent_Category_ID = c.Category_ID

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySql Workbench outputting "OK" as result of queries. 5 30
Search query matching words 20 38
SQL - Simple Pivot query 8 27
How can I use this function? 3 32
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question