Solved

sql query syntax error

Posted on 2011-03-09
4
194 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
4 Comments
 
LVL 142

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:HainKurt
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now