Solved

sql query syntax error

Posted on 2011-03-09
4
196 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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