Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:18:38+00:00 2026-05-28T08:18:38+00:00

I have a cte that loops through the records creates a list and I

  • 0

I have a cte that loops through the records creates a list and I pass a parameter of @ParamterID in a select statement but the set operations says incorrect syntax near Set and in that select it gets the path where I can do a search for the paths that are LIKE ‘%’ + @Path + ‘%’ and those are the items I want to delete from the table called Post. could someone help me complete this please and correct my SET @Path syntax

DELCARE @ParmeterID int = 3
DECLARE @Path nvarchar(MAX) 

WITH cte(Url, Level, Path, PostID, ParentPostID)
AS (

SELECT REPLACE(LOWER(Title), ' ', '-'), 1 AS Level, CAST(PostID as nvarchar(MAX)) as Path,
PostID, ParentPostID
FROM Post
WHERE ParentPostID IS NULL

UNION ALL

SELECT cte.Url, cte.Level + 1,
LOWER(cte.Path + '-' +  CAST(P.PostID AS VARCHAR(MAX))),
P.PostID,  P.ParentPostID 
FROM Post P 
INNER JOIN cte ON P.ParentPostID = cte.PostID
)
-- says incorrect sybnatx near Set @Path
SET @Path = (SELECT Path FROM cte WHERE PostID = @ParemeterID); 


-- list i want to delete --
SELECT * FROM cte WHERE Path LIKE '%' + @Path + '%'

delete -- list -- FROM Post --
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:18:38+00:00Added an answer on May 28, 2026 at 8:18 am

    I’m not sure what exactly you are trying to do (I don’t know your table schema) but you can try this:

    DECLARE @ParmeterID int = 3
    
    WITH cte(Url, Level, Path, PostID, ParentPostID)
    AS (
        SELECT REPLACE(LOWER(Title), ' ', '-'), 1 AS Level, CAST(PostID as nvarchar(MAX)) as Path,
        PostID, ParentPostID
        FROM Post
        WHERE ParentPostID IS NULL
    
        UNION ALL
    
        SELECT cte.Url, cte.Level + 1,
        LOWER(cte.Path + '-' +  CAST(P.PostID AS VARCHAR(MAX))),
        P.PostID,  P.ParentPostID 
        FROM Post P 
        INNER JOIN cte ON P.ParentPostID = cte.PostID
    )
    
    delete p
    from post p
    join cte on p.postId = cte.postId and 
        cte.path like '%' + (select top 1 [path] from cte where postId = @ParmeterID) + '%'
    

    Addedd: Ordering by path, please mind that this is a quick and dirty way.

    WITH cte(Url, Level, Path, PostID, ParentPostID, Sequence)
    AS (
        SELECT REPLACE(LOWER(Title), ' ', '-'), 1 AS Level, CAST(PostID as nvarchar(MAX)) as Path,
        PostID, ParentPostID, right(replicate('0', 8) + cast(PostID as varchar(max)), 8)
        FROM Post
        WHERE ParentPostID IS NULL
    
        UNION ALL
    
        SELECT cte.Url, cte.Level + 1,
        LOWER(cte.Path + '-' +  CAST(P.PostID AS VARCHAR(MAX))),
        P.PostID,  P.ParentPostID, cte.sequence + right(replicate('0', 8) + cast(p.PostID as varchar(max)), 8)
        FROM Post P 
        INNER JOIN cte ON P.ParentPostID = cte.PostID
    )
    select Url, Level, Path, PostID, ParentPostID
    from cte
    order by Sequence
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that CTE are better than cursor. But I am unable to
I have a CTE as below (logic removed) ;with cte_a as ( select ID,
I have written a table-valued UDF that starts by a CTE to return a
I have a CTE as follows WITH details AS ( SELECT FldId ,Rev ,Words
I have a query that creates several temporary tables and then inserts data into
I have a CTE that's doing some recursion. The easiest way to imagine the
Say that have the following CTE that returns the level of some tree data
I have a problem with recursive CTE query Let's say that I have that
I have a CTE query that seems to be causing an awful lot of
I'd like (using cte) to count children in table in that way to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.