Update statement with inner join issues
I have searched for the answer on this and not really 100%....so figured I would ask...be nice :)
what have I done wrong? Or am I just going about this the wrong way? I have looked at the oracle docs and I can't find an example showing me this. I do see where you SET values based on the select query results. I want to update the result of a query based on the join with static values....
UPDATE table.a
SET table.a_STATUS=9,table.a.INDEX = 'N'
WHERE (SELECT table.a INNER JOIN table.b ON (table.a.COMPANY = table.b.COMPANY) AND (table.a.PO_NUMBER =table.b.PO_NUMBER) AND (table.a.PO_RELEASE =table.b.PO_RELEASE) AND (table.a.PO_CODE =table.b.PO_CODE) AND (table.a_STATUS=1) AND (table.b.CLOSED_FL = 'Y'));
Hi,
Welcome to the forum!
user11360811 wrote:
I have searched for the answer on this and not really 100%....so figured I would ask...be nice :)
what have I done wrong? Or am I just going about this the wrong way? I have looked at the oracle docs and I can't find an example showing me this. I do see where you SET values based on the select query results. I want to update the result of a query based on the join with static values....
UPDATE table.aThat's updating a table called A in a schema called TABLE (which is not a good name for any user-named object). Are those really your table and schema names? Perhaps you meant to have an underscore instead of a dot:
UPDATE table_ais much, much more reasonable. It means the table name is TABLE_A (a perfectly good name) in the current schema.
SET table.a_STATUS=9,table.a.INDEX = 'N'
WHERE ( ...There's a syntax error. You can't just say
"WHERE (sub-query)"; it has to be
"WHERE EXISTS (sub-suery)" or
"WHERE (sub-query) = some_value" or
"WHERE some_value IN (sub_query)", or something similar. WHERE can never be used without some kind of comparison operator, such as EXISTS, = or IN.
SELECT table.a INNER JOIN table.b ON ...Here are some more syntax errors. The correct syntax for any query is
SELECT column_list
FROM table_name ...If table.a is your (first) table name, then you're missing the list of columns to SELECT, and the mandatory keyword FROM.
(table.a.COMPANY = table.b.COMPANY) AND (table.a.PO_NUMBER =table.b.PO_NUMBER) AND (table.a.PO_RELEASE =table.b.PO_RELEASE) AND (table.a.PO_CODE =table.b.PO_CODE) AND (table.a_STATUS=1) AND (table.b.CLOSED_FL = 'Y'));Some general advice about UPDATE:
If it's not obvious how to use UPDATE to do what you want, then there's a good chance that UPDATE is the wrong tool for the job. MERGE might be much simpler, and more efficient as well. This is especially likely if you need to join the table that's being updated to some other table.
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved. That way, the people who want to help you can re-create the problem and test their ideas.
Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
Simplify the problem as much as possible. Remove all tables and columns that play no role in this problem.
If you're asking about a DML statement, such as UPDATE, the CREATE TABLE and INSERT statements should re-create the tables as they are before the DML, and the results will be the contents of the changed table(s) when everything is finished.
Always say which version of Oracle you're using (for example, 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
Edited by: Frank Kulash on Jan 17, 2013 4:58 PM
Similar Messages
-
Update statement with inner join
Hello everyone. I am am trying to do an update statement with an inner join. I have found several examples of SQL statements that work with Sql server and mysql but they don't work in Oracle. Does anyone know the proper way in Oracle 10G? I am trying to update all fields in one table from fields in another table.
for example:
UPDATE table3
SET
TL3.name = TL2.name,
TL3.status = TL2.status,
TL3.date = TL2.date
FROM table3 TL3 JOIN table2 TL2
ON (TL3.unique_id = TL2.unique_id);
any help will be appreciated.Hi,
You can also use MERGE, like this:
MERGE INTO table3 dst
USING (
SELECT unique_id
, name
, status
, dt -- DATE is not a good column name
FROM table2
) src
ON (dst.unique_id = src.unique_id)
WHEN MATCHED THEN UPDATE
SET dst.name = src.name
, dst.status = src.status
, dst.dt = src.dt
;Unlike UPDATE, this lets you avoid essentially doing the same sub-query twice: once in the SET clause and then again in the WHERE clause.
Like UPDATE, you don't acutally join the table being changed (table3 in this case) to the other table(s); that is, the FROM clause of the suib-query does not include table3.
Riedelme is right; you'll get better response to SQL questions like this in the SQL and PL/SQL forum:
PL/SQL -
Syntax errors in update query with inner joins and sub query.
Below is the query:
UPDATE sp_CFQ_Coord_Corrections
INNER JOIN (CFQ_Coord_Corrections
INNER JOIN CFQ_Referrals ON CFQ_Coord_Corrections.CorrID = CFQ_Referrals.RecID)
ON sp_CFQ_Coord_Corrections.ID = CFQ_Referrals.RecID
SET CFQ_Coord_Corrections.MatchFound = 1,
CFQ_Coord_Corrections.RecTblID = [CFQ_Referrals].[RecTblID],
sp_CFQ_Coord_Corrections.MatchFound = 1
WHERE (((CFQ_Coord_Corrections.MatchFound)=0)
AND ((sp_CFQ_Coord_Corrections.MatchFound)=0)
AND ((CFQ_Coord_Corrections.RecImported)=1)
AND ((CFQ_Referrals.RecFileName)='COORDCORR_SPOINT')
AND ((CFQ_Referrals.RecCombKey)='No.Match')
AND ((sp_CFQ_Coord_Corrections.RecImported)=1));
Error messages seen when executed:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'INNER'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'CFQ_Coord_Corrections'.
Please help.....Below is the query:
UPDATE sp_CFQ_Coord_Corrections
INNER JOIN (CFQ_Coord_Corrections
INNER JOIN CFQ_Referrals ON CFQ_Coord_Corrections.CorrID = CFQ_Referrals.RecID)
ON sp_CFQ_Coord_Corrections.ID = CFQ_Referrals.RecID
SET CFQ_Coord_Corrections.MatchFound = 1,
CFQ_Coord_Corrections.RecTblID = [CFQ_Referrals].[RecTblID],
sp_CFQ_Coord_Corrections.MatchFound = 1
WHERE (((CFQ_Coord_Corrections.MatchFound)=0)
AND ((sp_CFQ_Coord_Corrections.MatchFound)=0)
AND ((CFQ_Coord_Corrections.RecImported)=1)
AND ((CFQ_Referrals.RecFileName)='COORDCORR_SPOINT')
AND ((CFQ_Referrals.RecCombKey)='No.Match')
AND ((sp_CFQ_Coord_Corrections.RecImported)=1));
Error messages seen when executed:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'INNER'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'CFQ_Coord_Corrections'.
Please help.....
sp_CFQ_Coord_Corrections is a table and not a stored procedure.
are these both tables "sp_CFQ_Coord_Corrections" and "CFQ_Coord_Corrections" different ?? -
Trying to convert SELECT query to Update query with INNER JOINS
Assalam O Alaikum!
I've tried to convert this query of mine but failed.
I wish to use it for update datasource in data GridView. I'm fetching data with it but converting it to update is not helping giving multiple errors.
I tried to share the pic but they don't let me do so. Its actually a gridView with check boxes. check the item and update it..
Here is the query. Please help me with that.
<pre>
SELECT [rightsId], [saveRights], [updateRights],
[viewRights], [deleteRights], [printRights],
[processRights], [verifyRights], [unProcessRights],
[unVerifyRights], CONVERT(VARCHAR(100),tblGroup.groupId)as groupId, convert(varchar(100),tblmenu.[menuId])as menuid
FROM [tblRights] inner join tblMenu ON
tblMenu.menuId=tblRights.menuId INNER JOIN
tblGroup ON tblGroup.groupId=tblRights.rightsId
</pre>code is fine the above query works fine with the fetching(select) but when I try to write it with update it doesn't. Here is the asp code. I'm doing nothing with c# or vb.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
DataSourceID="ratGrid" AutoGenerateColumns="False"
CssClass="GridViewStyle" Width="100%" AllowSorting="True" AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<Columns>
<asp:BoundField DataField="rightsId" HeaderText="rightsId" ItemStyle-Width="75px" SortExpression="rightsId" Visible="False">
<ItemStyle Width="75px" />
</asp:BoundField>
<asp:CheckBoxField DataField="saveRights" HeaderText="Save" SortExpression="saveRights" />
<asp:CheckBoxField DataField="updateRights" HeaderText="Update" SortExpression="updateRights" />
<asp:CheckBoxField DataField="viewRights" HeaderText="View" SortExpression="viewRights" />
<asp:CheckBoxField DataField="deleteRights" HeaderText="Delete" SortExpression="deleteRights" />
<asp:CheckBoxField DataField="printRights" HeaderText="Print" SortExpression="printRights" />
<asp:CheckBoxField DataField="processRights" HeaderText="Process" SortExpression="processRights" />
<asp:CheckBoxField DataField="verifyRights" HeaderText="Verify" SortExpression="verifyRights" />
<asp:CheckBoxField DataField="unProcessRights" HeaderText="UnProcess" SortExpression="unProcessRights" />
<asp:CheckBoxField DataField="unVerifyRights" HeaderText="UnVerify" SortExpression="unVerifyRights" />
<asp:BoundField DataField="groupId" HeaderText="groupId" ReadOnly="True" SortExpression="groupId" Visible="False" />
<asp:BoundField DataField="menuid" HeaderText="menuid" SortExpression="menuid" ReadOnly="True" Visible="False" />
</Columns>
<RowStyle CssClass="RowStyle" BackColor="#FFF7E7" ForeColor="#8C4510" />
<PagerStyle CssClass="PagerStyle" ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle CssClass="SelectedRowStyle" BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle CssClass="HeaderStyle" BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle CssClass="AltRowStyle" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:SqlDataSource ID="ratGrid"
runat="server"
ConnectionString="<%$ ConnectionStrings:Conn_Str %>"
SelectCommand="SELECT [rightsId], [saveRights], [updateRights],
[viewRights], [deleteRights], [printRights],
[processRights], [verifyRights], [unProcessRights],
[unVerifyRights], CONVERT(VARCHAR(100),tblGroup.groupId)as groupId, convert(varchar(100),tblmenu.[menuId])as menuid
FROM [tblRights] inner join tblMenu ON
tblMenu.menuId=tblRights.menuId INNER JOIN
tblGroup ON tblGroup.groupId=tblRights.rightsId"
FilterExpression="menuId like '{0}%' and [groupId] like '{1}%'" >
<FilterParameters>
<asp:ControlParameter ControlID="ddlmenu" Name="menu"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ddlgroup" Name="Country"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
Your table needs key(s) and you need to assign the key as DataKeys property for the GridView to make your UpDate and Delete work without coding. -
Dear Experts,
Update Employee set desicode = o.dcod from employee e inner join operator o on
e.employee_id = o.employe_id
waiting for suggestions,The other thread from yesterday that Satyaki mentioned Update.
William:
The SQL Server syntax from the OP is their version of an updateable join view. One interesting quirk of the implementation is that it does not require a unique key in the joined table, and will not error even if there are multiple rows that could update a row in the main table. I am still trying to figure out how it decides which row to use for the update, or if it actually updates multiple times and last update wins.
John -
Need help with update statement with multiple joins
I've got the following select statement that is pulling 29 records:
SELECT
PPA.PROJECT_ID,
PPA.SEGMENT1,
peia.expenditure_item_id,
peia.expenditure_type,
pec.expenditure_comment
FROM PA.PA_PROJECTS_ALL PPA,
pa.pa_expenditure_items_all peia,
pa.pa_expenditure_comments pec
where PPA.segment1 < '2008' and
PPA.project_id = 52 and -- just run for project # 20077119 for testing
peia.expenditure_type = 'PAYROLL' and
peia.project_id = ppa.project_id and
PEC.EXPENDITURE_ITEM_ID = PEIA.EXPENDITURE_ITEM_ID;
I need to update the pec.expenditure_comments to a static field for those 29 records. I assume I should start with the following, but not sure how to complete the where:
update
pa.pa_expenditure_comments pec
set pec.expenditure_comment = 'REFERENCE HD#728'
where
First time that we've ever needed to update, so any and all help appreciated.Try using exists:
update pa.pa_expenditure_comments pec
set pec.expenditure_comment = 'REFERENCE HD#728'
where exists ( select null
from pa.pa_projects_all ppa
, pa.pa_expenditure_items_all peia
, pa.pa_expenditure_comments pec2
where ppa.segment1 < '' -- not sure what you posted here, so for next time:
-- please put your examples between the code tags.
and ppa.project_id = 52 -- just run for project # 20077119 for testing
and peia.expenditure_type = 'PAYROLL'
and peia.project_id = ppa.project_id
and pec2.expenditure_item_id = peia.expenditure_item_id
and pec2.expenditure_item_id = pec.expenditure_item_id
); -
hi friends,
can we use where clause in select statement with inner join.
i am using following code which gives error (The column name "WERKS" has two meanings . .).
SELECT AMATNR ACHARG AERSDA BMENGE B~MEINS
FROM MCHA AS A INNER JOIN CHVW AS B
ON ACHARG = BCHARG
INTO TABLE T_OUTPUT1
WHERE WERKS = PLANT AND CHARG = P_CHARG.
thanks.Yes u can use where clause in select statement with inner join.
SELECT AMATNR ACHARG AERSDA BMENGE B~MEINS
FROM MCHA AS A INNER JOIN CHVW AS B
ON ACHARG = BCHARG
INTO TABLE T_OUTPUT1
WHERE WERKS = PLANT AND CHARG = P_CHARG.
" mention the table name in where condition also like awerks or bwerks.
egards
Rajendra -
Update statement with joining other tables
Hi ,
I have two table one is containing xml file , basically i need to read from those xml file then update to another table based on some condition.
UPDATE TRCB_XBRL_STG_2 STG
SET PROFIT =
case when xbrl.isconsolidatedacc='Y' and EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..."') is not null
THEN EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..."')
WHEN XBRL.ISCONSOLIDATEDACC='N' AND EXTRACTVALUE(XBRL.XBRLFILE,'//PROFIT ', 'xmlns:acra="..') IS NOT NULL
THEN extractValue(XBRL.xbrlfile,'//PROFIT ', 'xmlns:acra=".."')
ELSE STG.PROFIT
END,
SET REVENUE=
case when xbrl.isconsolidatedacc='Y' and EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE', 'xmlns:acra="..."') is not null
THEN EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE.', 'xmlns:acra="..."')
WHEN XBRL.ISCONSOLIDATEDACC='N' AND EXTRACTVALUE(XBRL.XBRLFILE,'//REVENUE', 'xmlns:acra="..') IS NOT NULL
THEN extractValue(XBRL.xbrlfile,'//REVENUE', 'xmlns:acra="REVENUE"')
ELSE STG.REVENUE
END,
... (around 100 columns)
FROM TRCB_XBRL xbrl ,TRCB_XBRL_STG_2 STG
WHERE STG.XBRL_ID = XBRL.XBRL_ID Number of columns are around 100 , please anyone suggest how to use update statement with joining two tables.Hi,
If all the values needed to update a given row of table_x are coming from the same row of table_y (or from the same row of a result set of a query involving any number of tables), then you can do something like this:
UPDATE table_x x
SET (col1, col2, col3, ...)
= (
SELECT NVL (y.col1, x.col1)
, NVL (y.col2, x.col2)
, NVL (y.col3, x.col3)
FROM table_y y
WHERE x.pkey = y.expr
AND ...
WHERE ...
;If the WHERE clause depends on the same row of table_y, then it will probably be simpler and more efficient to use MERGE instead of UPDATE.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
In the case of a DML operation (such as UPDATE) the sample data should show what the tables are like before the DML, and the results will be the contents of the changed table(s) after the DML.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002} -
SUBQUERY with INNER JOIN in ABAP
Hi Expert
How Can I write SUBQUERY with INNER JOIN in ABAP.
thanksThanks Sidhharth for your Respose
Actually I need data from VBAP, VBAK, VBUP, VBUK and VBKD with all functional validation satisfied for a sales order item data. The requirement is to report all item data with other information from table VBAK, VBUP, VBUK and VBKD in a corresponding record with ALL data in VBAP.
Your help is appriciated.
see the below query for reference
select alias_vbap~vbeln from vbap as
alias_vbap inner join vbak as alias_vbak
on alias_vbapvbeln eq alias_vbakvbeln
"and alias_vbak~bukrs_vf eq '2400'
inner join vbuk as alias_vbuk
on alias_vbukvbeln = alias_vbapvbeln
inner join vbup as alias_vbup
on alias_vbupvbeln = alias_vbapvbeln
and alias_vbupposnr = alias_vbapposnr
left OUTER join veda as alias_veda
on alias_vedavbeln = alias_vbapvbeln
and alias_vedavposn = alias_vbapposnr
left outer join vbkd as alias_vbkd
on alias_vbkdvbeln = alias_vbapvbeln
and alias_vbkdposnr = alias_vbapposnr -
Need help to write a query for Update statement with join
Hi there,
The following update statement gives me error as the given table in set statement is invalid. But its the right table .
Is the statement correct? Please help .
update (
select distinct(vpproadside.VEHICLE_CRED_OVERRIDE.vin)
from vpproadside.VEHICLE_CRED_OVERRIDE
join vpproadside.vpp_vehicle
on vpproadside.vpp_vehicle.vin = vpproadside.VEHICLE_CRED_OVERRIDE.vin
where VPP_CARRIER_SEQ_NUMBER = 90
and EXPIRY_DATE = '17-MAR-10'
set vpproadside.VEHICLE_CRED_OVERRIDE.EXPIRY_DATE = '15-SEP-10';Edited by: Indhu Ram on Mar 12, 2010 1:00 PM
Edited by: Indhu Ram on Mar 12, 2010 1:22 PM
Edited by: Indhu Ram on Mar 12, 2010 2:35 PM
Edited by: Indhu Ram on Mar 15, 2010 8:04 AM
Edited by: Indhu Ram on Mar 15, 2010 8:06 AM
Edited by: Indhu Ram on Mar 15, 2010 8:28 AMAsk Tom has very good discussion about this, if UPDATE does not work for PK issue, you can use MERGE
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:760068400346785797 -
I need to create the following SQL query in Toplinks, but cannot figure out how to do it. I just want to get all Contacts that have their InitialEventID set to some integer, or have a mapping in the ContactEvents table set to that integer. The mapping between Contacts and ContactEvents is @OnetoMany.
Select DISTINCT Contacts.* from Contacts JOIN ContactEvents ON (Contacts.ID = ContactEvents.ContactID) WHERE (ContactEvents.EventID = 34 OR Contacts.InitialEventID = 34);
I'm trying the following, but it obviouly doesn't work.
SELECT DISTINCT c FROM Contacts c INNER JOIN c.contactEventsCollection ce WHERE ce.eventID = 34
What should I do?Other than the query missing the 'or' clause, why isn't the query working, and what SQL gets generated? You can turn TopLink logging to Fine or Finest to get the SQL logged. You might also want to use OUTER JOIN intead since I assume that Contacts with an InitialEventID=34 may or may not have any ContactEvents associated with it:
SELECT DISTINCT c FROM Contacts c OUTER JOIN c.contactEventsCollection ce WHERE ( (ce.eventID = 34) OR (c.InitialEventID = 34) )
Best Regards,
Chris -
Hi all, consider the tables and data below
CREATE TABLE table1 (id NUMBER, a NUMBER, b NUMBER) ;
CREATE TABLE table2 (id NUMBER, c NUMBER, d NUMBER);
INSERT INTO table1 VALUES(111,2,0);
INSERT INTO table1 VALUES(111,1,2);
INSERT INTO table1 VALUES(111,1,3);
INSERT INTO table1 VALUES(222,1,3);
INSERT INTO table2 VALUES(111,5,8);
INSERT INTO table2 VALUES(222,6,7);
what i want to do is write a UPDATE STATEMENT that joins the two tables BY id
and update table1 rows. i want to include the following CASE statement
UPDATE COLUMN a intable1 according to this logic
case
WHEN b >0
THEN nvl(c,b)
ELSE
d
END
so table1 after the update should look like this
id a b
111 8 0
111 5 2
111 5 3
222 6 3can somebody help write a update statement that update table1 according to case statement and joins both tables to get the values necessary? thanksHooray for sample tables!
SQL> alter table table2 add constraint table2_pk primary key (id);
Table altered.
SQL> update
2 (select t1.a
3 ,case when t1.b > 0 then nvl(t2.c, t1.b)
4 else t2.d
5 end new_value
6 from table1 t1
7 join table2 t2
8 on t1.id = t2.id
9 )
10 set a = new_value;
4 rows updated.
SQL> select * from table1;
ID A B
111 8 0
111 5 2
111 5 3
222 6 3 -
How to re-write this big SELECT Query with INNER JOINs?
Hi Experts
I have a performance killer SELECT query with an inner join of 3 tables u2013 VBAP, VBAK and VBEP together, which populates records to an internal table INT_COLL_ORD. Based on these records selected, in another SELECT query, records are fetched from VBUK table to the internal table INT_VBUK.
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV D~AUART E~ETTYP E~EDATU
INTO TABLE INT_TAB_RES
FROM VBAP AS A INNER JOIN VBAK AS D
ON D~VBELN EQ A~VBELN AND D~MANDT EQ A~MANDT
INNER JOIN VBEP AS E
ON E~VBELN EQ A~VBELN AND E~POSNR EQ A~POSNR AND E~MANDT EQ A~MANDT
WHERE A~VBELN IN s_VBELN AND
D~auart in s_auart AND
D~vkorg in s_vkorg AND
D~vbtyp eq 'C' AND
( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
A~ABGRU EQ SPACE AND
A~MTVFP IN R_MTVFP AND
A~PRCTR IN R_PRCT AND
E~ETENR EQ '1'.
SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
DELETE ADJACENT DUPLICATES FROM INT_TAB_RES COMPARING VBELN POSNR.
CHECK NOT INT_TAB_RES [] IS INITIAL.
SELECT VBELN UVALL CMGST INTO TABLE INT_VBUK
FROM VBUK FOR ALL ENTRIES IN INT_TAB_RES
WHERE VBELN = INT_TAB_RES-VBELN AND UVALL NE 'A'.
Now, the requirement is:
I want to split this query. Like, first join VBAK and VBUK first. With this selection, go to the inner join of VBAP and VBEP (on key VBELN) to get the results. How can I re-write this Query?
Please help.
Thx n RgdsHi Nagraj
As of your suggestion, I have re-written the query as below:
* Declarations
TYPES: BEGIN OF TYP_COLL_ORD,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBUP-POSNR,
MATNR LIKE VBAP-MATNR,
KWMENG LIKE VBAP-KWMENG,
KBMENG LIKE VBAP-KBMENG,
ERDAT LIKE VBAK-ERDAT,
ERZET LIKE VBAK-ERZET,
PSTYV LIKE VBAP-PSTYV,
AUART LIKE VBAK-AUART, u201Calready exists in type
ETTYP LIKE VBEP-ETTYP,
EDATU LIKE VBEP-EDATU.
TYPES: END OF TYP_COLL_ORD.
DATA: INT_COLL_ORD TYPE TABLE OF TYP_COLL_ORD WITH HEADER LINE.
TYPES: BEGIN OF TYP_VBUK,
AUART LIKE VBAK-AUART, u201Chave added this field
VBELN LIKE VBUK-VBELN,
UVALL LIKE VBUK-UVALL,
CMGST LIKE VBUK-CMGST.
TYPES: END OF TYP_VBUK.
DATA: INT_VBUK TYPE TABLE OF TYP_VBUK WITH HEADER LINE.
*QUERY#1 u2013 for VBAK & VBUK Join
SELECT A~AUART B~VBELN B~UVALL B~CMGST
INTO TABLE INT_VBUK
FROM VBAK AS A INNER JOIN VBUK AS B
ON A~VBELN EQ B~VBELN
WHERE A~VBELN IN s_VBELN AND
A~auart in s_auart AND
A~vkorg in s_vkorg AND
A~vbtyp eq 'C' AND
B~UVALL NE 'A'.
IF NOT INT_VBUK[] IS INITIAL.
SORT INT_VBUK BY VBELN.
DELETE ADJACENT DUPLICATES FROM INT_VBUK COMPARING VBELN.
*QUERY#2 u2013 for VBAP & VBEP Join
SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV B~ETTYP B~EDATU
INTO TABLE INT_COLL_ORD
FROM VBAP AS A INNER JOIN VBEP AS B
ON B~VBELN EQ A~VBELN AND B~POSNR EQ A~POSNR AND B~MANDT EQ A~MANDT
FOR ALL ENTRIES IN INT_VBUK
WHERE A~VBELN = INT_VBUK-VBELN AND
( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
A~ABGRU EQ SPACE AND
A~MTVFP IN R_MTVFP AND
A~PRCTR IN R_PRCT AND
B~ETENR EQ '1'.
ENDIF.
SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
DELETE ADJACENT DUPLICATES FROM INT_COLL_ORD
COMPARING VBELN POSNR.
CHECK NOT INT_COLL_ORD[] IS INITIAL.
LOOP AT INT_COLL_ORD.
CLEAR: L_MTART,L_ATPPR,L_ETTYP.
IF L_PREVIOUS_ETTYP NE INT_COLL_ORD-ETTYP OR
L_PREVIOUS_AUART NE INT_COLL_ORD-AUART.
READ TABLE INT_OVRCTL WITH KEY AUART = INT_COLL_ORD-AUART ETTYP = INT_COLL_ORD-ETTYP.
CHECK SY-SUBRC NE 0.
Now, the issue is:
Please note that declaration for INT_COLL_ORD has a field AUART, which is used in further parts of program (see the statement just above)
But, since neither VBAP nor VBEP contains AUART field, it cannot be fetched through the QUERY#2. So this value is not populated into INT_COLL_ORD through SELECT Query.
Since this field is used in later part of program & that the internal table has no value for this field, it dumps!!
How to include this value into the INT_COLL_ORD?
Plz suggest.... -
Dear Experts,
I have writen a inner join code with MKPF and MSEG which taking too much time, while I have used index.
Indexes are:
MSEG
MATNR
WERKS
LGORT
BWART
SOBKZ
MKPF
BUDAT
MBLNR
My Select Query is :
SELECT B~MATNR
B~MAT_KDAUF
B~MAT_KDPOS
B~BWART
B~MENGE
B~MEINS
B~AUFNR
INTO TABLE IT_MSEG
FROM MKPF AS A
INNER JOIN MSEG AS B
ON AMBLNR EQ BMBLNR
AND AMJAHR EQ BMJAHR
WHERE A~BUDAT IN BUDAT
AND B~MATNR IN MATNR
AND B~WERKS IN WERKS
AND B~LGORT IN LGORT
AND B~BWART IN BWART.hi,
you can use for all entries it will work faster then joins
About it:*
FOR ALL ENTRIES WHERE
Syntax
... FOR ALL ENTRIES IN itab WHERE ... col operator itab-comp ...
Effect
If the addition FOR ALL ENTRIES is specified before the language element WHERE, then the components comp of the internal table itab can be used as operands when comparing with relational operators.
The internal table itab must have a structured line type and the component comp must be compatible with the column col.
The logical expression sql_cond of the WHERE condition can comprise various logical expressions by using AND and OR. However, if FOR ALL ENTRIES is specified, there must be at least one Comparison with a column of the internal table itab, which can be specified either statistically or dynamically (Release 6.40 and higher). In a statement with a SELECTstatement with FOR ALL ENTRIES, the addition ORDER BY can only be used with the addition PRIMARY KEY.
The whole logical expression sql_cond is evaluated for each individual line of the internal table itab. The resulting set of the SELECT statement is the union of the resulting sets from the individual evaluations. Duplicate lines are automatically removed from the resulting set. If the internal table itab is empty, the whole WHERE statement is ignored and all lines in the database are put in the resulting set.
Notes
In Release 6.10 and higher, the same internal table can be specified after FOR ALL ENTRIES and after INTO.
The addition FOR ALL ENTRIES is only possible before WHERE conditions of the SELECT statement.
Example
Exporting all flight data for a specified departure city. The relevant airlines and flight numbers are first put in an internal table entry_tab, which is evaluated in the WHERE condition of the subsquent SELECT statement.
PARAMETERS p_city TYPE spfli-cityfrom.
TYPES: BEGIN OF entry_tab_type,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
END OF entry_tab_type.
DATA: entry_tab TYPE TABLE OF entry_tab_type,
sflight_tab TYPE SORTED TABLE OF sflight
WITH UNIQUE KEY carrid connid fldate.
SELECT carrid connid
FROM spfli
INTO CORRESPONDING FIELDS OF TABLE entry_tab
WHERE cityfrom = p_city.
SELECT carrid connid fldate
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE sflight_tab
FOR ALL ENTRIES IN entry_tab
WHERE carrid = entry_tab-carrid AND
connid = entry_tab-connid.
hope it will help you
Rahul sharma
Edited by: RAHUL SHARMA on Sep 11, 2008 8:10 AM -
Performance problem with inner join
Hi
With this Query
SELECT *
FROM TABLE1
INNER JOIN TABLE2
ON TABLE1.Col1 = TABLE2.Col2 AND TABLE1.Col2 = TABLE2.Col2
WHERE TABLE1.Col3 = 'AB' AND
TABLE2.Col4 = 'ZZ';
TABLE1 have 6,000,000 of records
TABLE2 have 9,000,000 of records
when I run this query the result take 30 secs
If I change the Optimizer_mode for 'RULE'
the result take 4 secs
All statistics is ok
Primary Key exists for TABLE1 and TABLE2 base-on Col1 and Col2
And index on Table1.Col3,Table1.Col4
somebody can help me ?Analyze tables compute statistics
and analyze index compute statistics
I join explain plan
STATEMENT_ID TIMESTAMP REMARKS OPERATION OPTIONS OBJECT_NODE OBJECT_OWNER OBJECT_NAME OBJECT_INSTANCE OBJECT_TYPE OPTIMIZER SEARCH_COLUMNS ID PARENT_ID POSITION COST CARDINALITY BYTES
OTHER_TAG PARTITION_START PARTITION_STOP PARTITION_ID OTHER DISTRIBUTION CPU_COST IO_COST TEMP_SPACE
WithRule1 2004-01-19 16:28:43 SELECT STATEMENT RULE 0
WithRule1 2004-01-19 16:28:43 SORT AGGREGATE 1 0 1
WithRule1 2004-01-19 16:28:43 NESTED LOOPS 2 1 1
WithRule1 2004-01-19 16:28:43 TABLE ACCESS BY INDEX ROWID MICRODEV T088_INSC 1 ANALYZED 3 2 1
WithRule1 2004-01-19 16:28:43 INDEX RANGE SCAN MICRODEV IX4_T088_INSC NON-UNIQUE ANALYZED 2 4 3 1
WithRule1 2004-01-19 16:28:43 TABLE ACCESS BY INDEX ROWID MICRODEV T090_CPOS_DEM 2 ANALYZED 5 2 2
WithRule1 2004-01-19 16:28:43 INDEX UNIQUE SCAN MICRODEV PK_T090_CPOS_DEM UNIQUE ANALYZED 2 6 5 1
7 rows selected.
STATEMENT_ID TIMESTAMP REMARKS OPERATION OPTIONS OBJECT_NODE OBJECT_OWNER OBJECT_NAME OBJECT_INSTANCE OBJECT_TYPE OPTIMIZER SEARCH_COLUMNS ID PARENT_ID POSITION COST CARDINALITY BYTES
OTHER_TAG PARTITION_START PARTITION_STOP PARTITION_ID OTHER DISTRIBUTION CPU_COST IO_COST TEMP_SPACE
WithChoose1 2004-01-19 16:29:37 SELECT STATEMENT CHOOSE 0 21680 21680 1 40
21680
WithChoose1 2004-01-19 16:29:37 SORT AGGREGATE 1 0 1 1 40
WithChoose1 2004-01-19 16:29:37 HASH JOIN 2 1 1 21680 59050 2362000
21680 3875000
WithChoose1 2004-01-19 16:29:37 TABLE ACCESS BY INDEX ROWID MICRODEV T088_INSC 1 ANALYZED 3 2 1 7031 96727 2708356
7031
WithChoose1 2004-01-19 16:29:37 INDEX RANGE SCAN MICRODEV IX4_T088_INSC NON-UNIQUE ANALYZED 2 4 3 1 232 96727
232
WithChoose1 2004-01-19 16:29:37 TABLE ACCESS FULL MICRODEV T090_CPOS_DEM 2 ANALYZED 5 2 2 11224 5662693 67952316
11224
6 rows selected.
Maybe you are looking for
-
Adobe Air App in First Lady Michelle Obama's "Apps for Healthy Kis" Contest
Break pal - Fitness at Your Desk (http://www.breakpal.com). An online workplace wellness application built using Adobe Air and Drupal has been accepted into First Lady Michelle Obama's " Apps for healthy kids competition. The competition is to insp
-
How to pass querystring value to swfobject and set it in adobe flash
Hi, I must tell that I have not much knowledge about flash. I have a flash slideshow on my homepage which displays news by a xml file under http://bit.ly/q48UmE and I am using slideshowpro for it. That slideshow xml file path must be set within adobe
-
Purchase order 4500003445 has no item error is comin
hi all, created one PO, then created Invoice with refrence to PO, After doing MIRO, I am trying to do GR with refrence to PObut system is showing error purchase order 4500007834 has no item. Gr based invoice is uncheck. vendor aknowledgement is unche
-
Enhancements in ASN Search Query - additional selection parameter
Hi, I am working on adding an additional search parameter in ASN Overview screen in supplier view. The search is POWL based and in the "Maintain Search Criteria" screen I have been able to add a custom field named "ZTEST_DATE" of type DATS. To get th
-
VLC - audio and video over aiplay
I tried using airplay mirrorring from my Mac running VLC to my AppleTV to watch on the computer. However I see that the audio from the VLC app still plays through the desktop and not the TV. If I increase/decrease volume, that is heard on the TV but