Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql query syntax error

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

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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