2005 Jul 28 3:28 PM
Hi guys,
i have a big problem with left outer join and the where condition:
3 tables
table1: IDCUST NAME TEL
table2: IDCUST CURRENCY
table3: ID3 IDCUST PRODUCTNAME
So one Customer can have more than one Product (1 - n)
So my select looks like this:
Select
a~name
a~tel
b~currency
c~productname
from
table1 As a
inner join table2 as b
on aidcust = bidcust
left outer join table3 as c !!!!!
on aidcust = cidcust
Where (condition-string).
Now the Error:
my condition-string is dynamic.
so if i want to have productname="prod1" an all customers with this product i get an error.
but if i want to have all customers where tel number = "123"
i get a result with all customers tel=123 AND their products! (that's correct).
So how can i get a result with using a where condition including table 3 without having an error????
Please help me!!!
2005 Jul 28 3:34 PM
2005 Jul 28 3:44 PM
Thanks for this answer,
so it isnt possible doing where condition on left outer join? i tested in on access (very stupid i know) but in this case it worked.
So i thought that it may work in OpenSQL also.
Regards,
Marcus
2005 Jul 28 3:41 PM
Hi,
You can put your filter after <b>ON</b>:
Example:
data lang type sy-langu.
lang = 'E'.
select aSAKNR bTXT50 into corresponding fields of table test
from SKA1 as a left join SKAT as b
on aSAKNR = bSAKNR and <b>b~SPRAS = lang</b>
where a~KTOPL = 'BULG'.
Svetlin
2005 Jul 28 3:47 PM
2005 Jul 28 6:10 PM
But how can i do a dynamic "on condition" ?
vardynamic = 'AND c~prod = ''test'' '.
... on aidcust = cidcust (vardynamic) ?
2005 Jul 28 6:32 PM
2005 Jul 28 6:58 PM
Here is some sample code to show you how to generate a subroutine at runtime with dynamic code.
report zrich_0001
no standard page heading.
types: t_source(72).
data: routine(32) value 'TEMP_ROUTINE',
program(8),
message(128),
line type i.
data: isource type table of t_source,
xsource type t_source.
data: it001 type table of t001.
data: xt001 type t001.
start-of-selection.
* Here is the source code for the temporary subroutine
* with you select statement
xsource = 'REPORT ZTEMP_REPORT.'.
insert xsource into isource index 1.
xsource = 'FORM & tables itab.'.
replace '&' with routine into xsource.
insert xsource into isource index 2.
xsource = 'select * into corresponding fields of table itab'.
append xsource to isource.
xsource = 'from t001'.
append xsource to isource.
xsource = 'where bukrs <> space.'.
append xsource to isource.
xsource = 'ENDFORM.'.
append xsource to isource.
* Generate the subroutine
generate subroutine pool isource name program
message message
line line.
* Call it and get your internal back with data.
if sy-subrc = 0.
perform (routine) in program (program) tables it001.
loop at it001 into xt001.
write:/ xt001-bukrs, xt001-BUTXT.
endloop.
else.
write:/ message.
endif.
Regards,
Rich Heilman
2005 Jul 29 5:55 AM
I did not know that i could do subroutine in an BSP event handler!
Is this right?
Regards Marcus
2005 Jul 29 6:53 AM
Hi,
i tested ur solution but it didnt work as i imagine:
select aSAKNR bTXT50 into corresponding fields of table test
from SKA1 as a left outer join SKAT as b
on aSAKNR = bSAKNR and b~SPRAS = lang
where a~KTOPL = 'BULG'.
I got only results where a~KTOPL = 'BULG' !
The condition of b~SPRAS = lang did not work.
Regards,
Marcus
2005 Jul 29 7:05 AM
Hi,
Try this.
select aSAKNR bTXT50 into corresponding fields of table test
from SKA1 as a left outer join SKAT as b
on aSAKNR = bSAKNR
where a~KTOPL = 'BULG'
<b>and b~SPRAS = lang</b>.
2005 Jul 29 7:41 AM
Hi,
look at my first posting!
This is the problem, that the where condition used within left outer join is not allowed!
Regards,
Marcus
2005 Aug 01 12:44 PM
Hi,
types : begin of ty,
saknr type ska1-saknr,
txt50 type skat-txt50,
spras type skat-spras,
end of ty.
data : test type standard table of ty,
wa type ty.
select aSAKNR bTXT50 b~spras into corresponding fields of table test
from ( SKA1 as a left outer join SKAT as b
on aSAKNR = bSAKNR )
where a~KTOPL = 'BULG'.
Loop at test into wa.
if wa-spras ne lang.
delete itab index sy-tabix.
endif.
endloop.
2005 Jul 29 9:15 AM
2005 Aug 01 12:27 PM
2005 Aug 01 5:43 PM
Marcus, hi
You are not going to be able to achieve your requirement the way you want to. Left outer joined tables cannot have clause(s) in the WHERE condition and dynamic clauses are only allowed in the WHERE condition. The two are mutually exclusive.
The following links to the official help back this up,
from the first link http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb39c4358411d1829f0000e829fbfe/content.htm scroll down to 'Specifying Two or More Database tables as a Left Outer Join' and it states 'The WHERE clause may not contain any comparisons with columns from <dbtab>. All comparisons using columns from <dbtab> must appear in the condition <cond>.' (Where <dbtab> is the left outer joined table.)
From the second link http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3a1f358411d1829f0000e829fbfe/content.htm scroll down to 'Dynamic Conditions' and it states 'You may only use dynamic conditions in the WHERE clause of the SELECT statement.'
I realise this hasn't helped resolve your problem but it does clearly indicate you need to use an alternative technique like dynamically generated subroutine pools, which Rich Heilman outlined above, or further processing the data in a loop once you've retrieved it, as outlined by Jayanthi Jayarman.
Although you can use dynamic clauses in the HAVING addition, you face the same restriction as with the WHERE clause in that you cannot reference the left outer joined table.
I hope this helps and allows you to proceed to come at the problem from a different perspective.
Kind regards
Mark