?
Solved

SQL Update statement using nested sub select

Posted on 2008-10-23
3
Medium Priority
?
4,192 Views
Last Modified: 2013-12-18
I am trying to bind the data in the top row of a sub select to the column in an update statement.  I've attached the PL/SQL code snippet.

I get the following error:  ORA-00904:  "TMP"."ACCT_ID": invalid identifier

I believe this is due to the fact that the sub query isn't directly linked to the main update statement's table but I don't know how to make this connection using an update statement.

Any help would be greatly appreciated.
UPDATE CM_CO$DLQCS TMP
SET MASTER_SA_ID = (  SELECT SA_ID 
                      FROM (  SELECT SA.SA_ID
                              FROM CI_SA SA
                                , CI_SA_TYPE SA_TYPE
                              WHERE SA.ACCT_ID = TMP.ACCT_ID
                                AND SA.SA_TYPE_CD = SA_TYPE.SA_TYPE_CD
                                AND SA_TYPE.SUB_SA_SW = 'N'
                                AND SA_TYPE.SVC_TYPE_CD = 'E'
                                AND SA.SA_STATUS_FLG < 40
                              ORDER BY SA.START_DT DESC
                           )
                      WHERE ROWNUM = 1
                   );

Open in new window

0
Comment
Question by:InnovaDale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:jamesgu
ID: 22792834
try this
UPDATE CM_CO$DLQCS TMP
SET MASTER_SA_ID = ( select sa_id from 
										 (	SELECT ACCT_ID, SA_ID 
                      FROM (  SELECT SA.ACCT_ID, SA.SA_ID
                              FROM CI_SA SA
                                , CI_SA_TYPE SA_TYPE
                              WHERE SA.SA_TYPE_CD = SA_TYPE.SA_TYPE_CD
                                AND SA_TYPE.SUB_SA_SW = 'N'
                                AND SA_TYPE.SVC_TYPE_CD = 'E'
                                AND SA.SA_STATUS_FLG < 40
                              ORDER BY SA.START_DT DESC
                           )
                      WHERE ROWNUM = 1
                     ) BB
                     where TMP.ACCT_ID = BB.ACCT_ID
                   );

Open in new window

0
 
LVL 27

Expert Comment

by:sujith80
ID: 22794760
Try this, should work
UPDATE CM_CO$DLQCS TMP
SET MASTER_SA_ID = (select SA_ID from (
select ACCT_ID , SA_ID
FROM (
SELECT SA.ACCT_ID , SA.SA_ID, row_number() over(partition by SA.ACCT_ID order by SA.START_DT desc) rn
FROM CI_SA SA , CI_SA_TYPE SA_TYPE
WHERE 
SA.SA_TYPE_CD = SA_TYPE.SA_TYPE_CD
AND SA_TYPE.SUB_SA_SW = 'N'
AND SA_TYPE.SVC_TYPE_CD = 'E'
AND SA.SA_STATUS_FLG < 40 ) 
where rn = 1 ) A
where A.ACCT_ID = TMP.ACCT_ID;

Open in new window

0
 
LVL 27

Accepted Solution

by:
sujith80 earned 2000 total points
ID: 22794766
Missed a closing bracket.
UPDATE CM_CO$DLQCS TMP
SET MASTER_SA_ID = (
		select SA_ID from (
			select ACCT_ID , SA_ID
			FROM 
				(
				SELECT SA.ACCT_ID , SA.SA_ID, row_number() over(partition by SA.ACCT_ID order by SA.START_DT desc) rn
				FROM CI_SA SA , CI_SA_TYPE SA_TYPE
				WHERE 
				SA.SA_TYPE_CD = SA_TYPE.SA_TYPE_CD
				AND SA_TYPE.SUB_SA_SW = 'N'
				AND SA_TYPE.SVC_TYPE_CD = 'E'
				AND SA.SA_STATUS_FLG < 40 
				) 
			where rn = 1 ) A
		where A.ACCT_ID = TMP.ACCT_ID);

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to recover a database from a user managed backup
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question