Solved

SQL Update statement using nested sub select

Posted on 2008-10-23
3
4,179 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
  • 2
3 Comments
 
LVL 9

Expert Comment

by:jamesgu
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now