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

  • Home
  • SEARCH
  • 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 1069667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:27:01+00:00 2026-05-16T20:27:01+00:00

Has anyone managed to create a CTE in SQL Server’s T-SQL that also includes

  • 0

Has anyone managed to create a CTE in SQL Server’s T-SQL that also includes a WITH XMLNAMESPACES declaration?

It seems both WITH keywords insist on being the “first in the T-SQL batch”, and that doesn’t really work….

I tried:

WITH XMLNAMESPACES('http://schemas.myself.com/SomeSchema' as ns)
WITH CTEQuery AS
(
SELECT (list of fields)
    FROM dbo.MyTable
    WHERE (conditions)
)
SELECT * FROM CTEQuery

Didn’t work 🙁 (syntax errors)

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword ‘WITH’.
Msg 319, Level 15, State 1, Line 2
Incorrect syntax near the
keyword ‘with’. If this statement is a
common table expression, an
xmlnamespaces clause or a change
tracking context clause, the previous
statement must be terminated with a
semicolon.

So I tried prepending the second WITH with a semicolon:

WITH XMLNAMESPACES('http://schemas.myself.com/SomeSchema' as ns)
;WITH CTEQuery AS
(
SELECT (list of fields)
    FROM dbo.MyTable
    WHERE (conditions)
)
SELECT * FROM CTEQuery

and got this:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ‘;’.

and then I tried putting the WITH XMLNAMESPACES into the CTE:

WITH CTEQuery AS
(
   WITH XMLNAMESPACES('http://schemas.myself.com/SomeSchema' as ns)
   SELECT (list of fields)
      FROM dbo.MyTable
      WHERE (conditions)
)
SELECT * FROM CTEQuery

and got this:

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword
‘WITH’.
Msg 319, Level 15, State 1, Line 4
Incorrect syntax near the
keyword ‘with’. If this statement is a
common table expression, an
xmlnamespaces clause or a change
tracking context clause, the previous
statement must be terminated with a
semicolon.
Msg 102, Level 15, State 1, Line 21
Incorrect syntax near ‘)’.

So how the heck do I do this??

  • 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-16T20:27:01+00:00Added an answer on May 16, 2026 at 8:27 pm

    Use a comma instead of the second WITH, e.g.

    WITH XMLNAMESPACES('http://schemas.myself.com/SomeSchema' as ns)
    ,CTEQuery AS
    (
    SELECT (list of fields)
        FROM dbo.MyTable
        WHERE (conditions)
    )
    SELECT * FROM CTEQuery
    

    The same if you want multiple CTE expressions. You only need to specify WITH once, and then all other WITH blocks just use a comma instead of the keyword.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone tried installing SQL Server 2008 Developer on a machine that already has
Has anyone managed to use ItemizedOverlays in Android Beta 0.9? I can't get it
Has anyone managed to get subsonic or a variant working on Windows Mobile? We
Has anyone managed to activate Gzip compression on a website in IIS6 ? We're
Has anyone worked much with Microsoft's Managed Extensibility Framework (MEF)? Kinda sounds like it's
Has anyone ever set up Cruise Control to build an OS X Cocoa/Objective-C project?
Has anyone looked at Yahoo's ASTRA ? It's fairly nifty, but I had some
Has anyone run into this error message before when using a timer on an
Has anyone been able to get an NHibernate-based project up and running on a
Has anyone built a website with IronPython and ASP.NET. What were your experiences and

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.