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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:06:52+00:00 2026-05-24T01:06:52+00:00

WITH emp_CTE AS ( SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS IdentityId, * FROM

  • 0
WITH emp_CTE AS (
    SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS IdentityId, *
    FROM dbo.employee )
SELECT * FROM emp_CTE

This works fine

If the same query is written like this.

WITH emp_CTE AS (
    SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS IdentityId, *
    FROM dbo.employee )
SELECT * FROM EMPLOYEES    
SELECT * FROM emp_CTE

it gives a message telling emp_CTE does not exist.

Is there any way we can overcome this issue?

thanks
Prince

  • 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-24T01:06:53+00:00Added an answer on May 24, 2026 at 1:06 am

    The CTE is part of the subsequent statement only.

    The subsequent statement can be a single SELECT/INSERT/UPDATE/DELETE, or a compound (with UNION, INTERSECT etc)

    For example:

    ;WITH cte1 AS
    (
       select ...
    ), cte2 AS
    (
        select ...
    )
    SELECT ...
    UNION 
    SELECT ...;
    

    The rule of thumb is that the scope is until where next ; would be. A semi-colon terminates any statement but is optional unfortunately.

    Your failing code above is actually this

    ...;
    WITH emp_CTE AS (
        SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS IdentityId, *
        FROM dbo.employee )
    SELECT * FROM EMPLOYEES; 
    SELECT * FROM emp_CTE;
    

    So the CTE is only in scope up until ...EMPLOYEES;

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

Sidebar

Related Questions

When I run this query : select emp.emp_name as name from emp order by
SELECT * FROM emp FOR XML AUTO , ROOT ('Employees') , ELEMENTS this query
I have sql something like this: SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED >=
String query = select email from emp_select; Statement stmt; try { DB db=new DB();
I am trying the following query if exists (select 1 from emp where eid
select NAME from Temp_EMP where name not in select NAME from EMPLOYEE and deptid
Example: select ename from emp where hiredate = todate('01/05/81','dd/mm/yy') and select ename from emp
Suppose I have a dataset with those two immortal tables: Employee & Order Emp
ALTER PROCEDURE dbo.StoredProcedure8 @emp_code bigint, @co_id bigint, @p decimal(8,2) output AS SELECT @p =
Table name: Employee Column Name: Emp_name Emp_name has this contents: xx\rama, xx\rajesh, xx\vignesh I

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.