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 6926359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:54:30+00:00 2026-05-27T10:54:30+00:00

Possible Duplicate: Incorrect syntax near the keyword 'with'…previous statement must be terminated with a

  • 0

Possible Duplicate:
Incorrect syntax near the keyword 'with'…previous statement must be terminated with a semicolon

I want to select hierachical data and insert it in a table.
Therefore i need to use the WITH statment in my insert.

This works fine:

create table test_table
(
 id int
)   

with t_table 
as
(select 12345 wert)
insert into test_table (id)
select wert from t_table

But this generates a “wrong syntax near WITH keyword” error:

CREATE PROCEDURE p_insert_test
AS
BEGIN

with t_table 
as
(select 12345 wert)
insert into test_table (id)
select wert from t_table

END

I guess T-SQL doesn’t like the WITH keyword before the INSERT keyword.
How can i do this kind of insert in a stored procedure?

Thank you!

  • 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-27T10:54:31+00:00Added an answer on May 27, 2026 at 10:54 am

    The CTE declaration needs to be the first command in the batch.

    Just put a semicolon before the WITH and you should be fine:

    ;WITH t_Table...

    EDIT:

    Just to clarify why this is the case, the WITH keyword is used for query hints as well, such as WITH RECOMPILE, WITH (NOLOCK), etc. The query engine needs to know the intent of the WITH keyword, and the only way to signify that it represents a CTE is to ensure that the CTE declarations are the beginning of the batch.

    Otherwise, you could have something ambiguous like:

    SELECT Field
    FROM MyTable
    WITH CteName
    As
    (Select 1)
    

    Without the statement terminator, CteName will be inappropriately read as a query hint applied to MyTable.

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

Sidebar

Related Questions

Possible Duplicate: What is your longest-held programming assumption that turned out to be incorrect?
Possible Duplicate: C++ Overriding… overwriting? What's the difference between override and overwrite ? I've
Possible Duplicate: Why does ASP.NET auto-generated .designer code have the incorrect type? On my
Possible Duplicate: How to edit incorrect commit message in Mercurial? Can i rename commit
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: Ruby block and unparenthesized arguments I'm not sure I understand this syntax
Possible Duplicate: How to store an IP in mySQL I want to get the
Possible Duplicate: running sql statement from excel vba Here's the excerpt that's causing the
Possible Duplicate: What's the difference between dot syntax and square bracket syntax? I've tried
Possible Duplicate: "CSRF token missing or incorrect" while post parameter via AJAX in Django

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.