Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query syntax error

Posted on 2011-03-09
4
Medium Priority
?
203 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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 61

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

885 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