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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:42:04+00:00 2026-05-27T15:42:04+00:00

I have the statement below when creating a Oracle view. The statement is to

  • 0

I have the statement below when creating a Oracle view. The statement is to return a particular value as MYVALUE column.

(SELECT myval 
  FROM (SELECT myval 
          FROM mytable 
         WHERE primary_key = /*CS.primary_key*/ 12345 
         ORDER BY table_primary_key ASC) 
 WHERE ROWNUM < 2) AS MYVALUE,

The inner query can return more than one row. I am only interested in the 1st record and it must be ordered by the table_primary_key, hence the use of the sub query to allow the ROWNUM selection.

When I create the query in its current state above, the view is created successfully. When I uncomment CS.primary_key and remove the hardcoded 12345, the view creation fails, with no description of why

(SQLDeveloper 2.1): “Failed: Warning: execution completed with warning”.

Also, just to try and narrow down the problem, I removed the ORDER BY, and ROWNUM as below, and the same error occurs

(SELECT myval 
  FROM (SELECT myval 
          FROM mytable 
         WHERE primary_key = CS.primary_key) 
) AS MYVALUE,

Lastly, I know CS.primary_key is a valid reference, as I use in in other parts of my view without issues.

Any idea why the reference is valid, or how to get a more detailed error message?

edit: updated starting opening bracket

EDIT2: Thanks for the responses so far. Here is a summary of the problem, I think the CS.PRIMARY key should be in scope, as I use it in other places in my query. The code below works, but if I replace the hardcoded 1 to CS.primary_key, it fails:

drop view myview;
drop table mytable;
drop table mytable_parent;
drop table proof_table;

-- ISSUE TABLES
create table mytable_parent ( primary_key number primary key );
create table mytable ( primary_key number, myval varchar(255), parent_primary_key number);
insert into mytable_parent values (1);
insert into mytable_parent values (2);
insert into mytable values (1, 'myval1-1', 1);
insert into mytable values (2, 'myval1-2', 1);
insert into mytable values (3, 'myval2-1', 2);

-- EXAMPLE TABLE TO PROVE CS.* WORKS
create table proof_table ( primary_key number primary key, parent_primary_key number, any_old_value varchar(255));
insert into proof_table values (1, 1, 'proofval1-1');
insert into proof_table values (2, 2, 'proofval1-2');

-- VIEW
CREATE OR REPLACE FORCE VIEW myview AS 
  SELECT 

  -- PROOF STATEMENT USING CS.primary_key SUCCESSFULLY
  (SELECT any_old_value FROM proof_table WHERE parent_primary_key IN 
    (SELECT primary_key FROM proof_table WHERE parent_primary_key = 

    -- USING CS REFERENCE, NO PROBLEM
    CS.primary_key)

  ) AS PROOF_VALUE,

  -- PROBLEM STATEMENT
  (SELECT myval FROM (SELECT myval FROM mytable 
         WHERE parent_primary_key = /*CS.primary_key*/ 1
         ORDER BY primary_key ASC) 
  WHERE ROWNUM < 2) AS MYVALUE  

  -- DEFINE CS
  FROM mytable_parent CS;
  • 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-27T15:42:04+00:00Added an answer on May 27, 2026 at 3:42 pm

    From the limited information you have supplied I’d suggest that CS.PRIMARY_KEY is out of scope for your subquery. Hence it compiles OK when you use a literal and doesn’t compile when it has to resolve the reference to CS.PRIMARY_KEY.

    Include whichever table CS refers to in the subquery (with the relevent criteria) to check that this is the case.
    If it is then you’ll need to rewrite your query to ensure the CS table is in scope for all its dependencies.

    Hope it helps…

    See the details in this link: http://etutorials.org/SQL/Mastering+Oracle+SQL/Chapter+5.+Subqueries/5.4+Inline+Views/

    You will see that inline views are executed prior to the outer query and nesting the reference to the CS table too deeply will cause it to become out of scope.

    Replace the nested subquery where you do your ordering:

    (SELECT myval 
       FROM (SELECT myval 
               FROM mytable
              WHERE parent_primary_key = CS.primary_key
              ORDER BY primary_key ASC)
       WHERE ROWNUM < 2) AS MYVALUE
    

    with an analytic function or other way of limiting your rows and it will work fine

    This is untested but replacing the above code with:

    (SELECT DISTINCT 
            FIRST_VALUE(myval) OVER (PARTITION BY parent_primary_key ORDER BY primary_key) 
       FROM mytable
      WHERE parent_primary_key = CS.primary_key) AS MYVALUE 
    

    might be close to what you want and the reference to CS is in scope.

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

Sidebar

Related Questions

i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I have LINQ statement that looks like this: return ( from c in customers
If you have the select statement below where the PK is the primary key:
In SQL server 2008, I have select statement that spits below text output. 'text1d','text2','text3'
I have a problem when trying to execute this update statement (below) using C#
I have a problem with the SQL statement detailed below. The query returns the
I have this if statement that tests for the 2 conditions below. The second
I have the below String assignment statement String items[] = line.split(\,\,15); String fileNamet =
I have following statement for query articles from some sections Article.all(:joins => :sections, :conditions
I have a statement like the below. The order returned is 1,4,5. My code

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.