Select from where in ( ) table? - abap

I want a select with dynamic where conditions in ABAP Syntax.
An SQL Statement would look like this:
SELECT * FROM MCH1 WHERE MATNR IN (...) AND CHARG IN (...)
My approach was to add 2 structures ZMATN_STR and ZCHARG_STR to the dictionary with associated components as line (MATNR, CHARG).
Then create 2 table types with associated line types.
Now im stucked in ABAP because I don't know how to write the where clause.
That's what I have so far:
SELECT *
FROM
mch1
FOR ALL ENTRIES IN #matnrs
WHERE
matnr = #matnrs-matnr
INTO TABLE #DATA(lt_result).
It works for either matnr or charg but not with both of them.
Additional Info
This select happens in a function module where 2 import parameter exists (the 2 table types) - so I cannot just write where in ('xxx', 'yyy')

data lr_matnr type range of matnr.
data lr_charg type range of MCH1-charg.
"Fill lr_matnr and lr_charg...
SELECT * FROM MCH1 WHERE MATNR IN #lr_matnr AND CHARG IN #lr_charg
INTO TABLE #data(lt_result).

Related

Creating a list in SQL and iterating through the list after the FROM line

I want to access some table like Toyota_Corolla, Toyota_Camry, Toyota_Prius, Toyota_Rav4
Instead of typing out multiple SELECT statements like the following:
SELECT * FROM Toyota_Corolla;
SELECT * FROM Toyota_Camry;
SELECT * FROM Toyota_Prius;
SELECT * FROM Toyota_Rav4;
Is there a way to create a list of strings like ['Corolla', 'Camry', 'Prius', Rav4'] and iterate through the list after the FROM line to something similar to:
SELECT * FROM 'Toyota_'` + 'some loop to iterate the list of car model'
I know for my example, it's easier to just type out the whole thing, but what about the situation when Toyota has hundred of models?
This is MS SQL Server DBMS
No. First, you should fix your data model so you have a single table with an additional column for the Toyota model. That is the right way to store the data.
With the data you have, you can emulate this with a view:
create view vw_toyota as
select 'Corolla' as toyota_model, t.* from Toyota_Corolla t union all
select 'Camry' as toyota_model, t.* from Toyota_Camry t union all
select 'Prius' as toyota_model, t.* from Toyota_Prius t union all
select 'Rav4' as toyota_model, t.* from Toyota_Rav4 t;
This also adds the source table information.
And then do:
select *
from vw_toyota;

ABAP LIKE with select option

I have the following abap program that looks as following:
TABLES lfa1.
DATA gt_lfa1 TYPE SORTED TABLE OF lfa1 WITH UNIQUE DEFAULT KEY.
SELECT-OPTIONS sl_lifnr FOR lfa1-lifnr.
SELECT-OPTIONS sl_name FOR lfa1-name1.
START-OF-SELECTION.
SELECT * FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE gt_lfa1
WHERE lifnr IN sl_lifnr
AND name1 LIKE sl_name.
Searching for vendors that name starts with:
I've got no results, but it exists vendors with this pattern.
It's not necessary to use LIKE. You can use IN instead.
I ran your code with IN in the SQL and I have results in the table.
if you use a select option you have to use the statement IN not like:
s_matnr = *3578 *
select options s_matnr type mara-matnr.
select bismt
from mara
into lv_bismt
where matnr in s_matnr
If you use s/4hana you can use even fuzzy
You need to replace * with %, if you are using LIKE operand. https://help.sap.com/doc/abapdocu_750_index_htm/7.50/de-DE/abenwhere_logexp_like.htm
REPLACE ALL OCCURENCES OF '*' in sl_lifnr WITH '%'
REPLACE ALL OCCURENCES OF '*' in sl_name WITH '%'
SELECT * FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE gt_lfa1
WHERE lifnr LIKE sl_lifnr
AND name1 LIKE sl_name.

SQL query with regex

I have a table vehicles has a column 'name'
Values are stored like car/tesla, car/honda, truck/daimler (each value is stored as type/brand)
I want to query the table using only brand. If I look up tesla, it should return the row corresponding to car/tesla. How do I do it? I'm using postgres.
There is no need in regex in your case. Just use old good like:
select name
from vehicles
where name like '%/tesla'
2 solutions are available a select query with LIKE operand or a select query with contains operand.
select * from vehicles where name LIKE '%tesla%'
Select * from vehicles where Contains(name, "tesla");

SQL Query to match if data is not present in another table

I have two tables named tblStockManagement and tblFolding in my database. i have column Name in tblFolding table and column FoldingID as Foreign Key in tblStockManagement table. now i have comboBox in my Winform and i want Names of Items in combobox from tblFolding Table but only those items that are not in tblStockManagement Table.
(because i dont want to select data again if it is already in tblStockManagement table . instead i will update the quantity later).
these are the screenshots of both of tables. please tell me how can i do that
NOT EXISTS version:
select *
from tblFolding f
where not exists (select * from tblStockManagement SM
where sm.FoldingID = f.FoldingID)
NOT EXISTS is "NULL safe", which NOT IN isn't.
This is you need.Basically a sub query which gets all folding id and using not in operator I exclude those matching sets.
SELECT Name
FROM tblFolding
WHERE FoldingID NOT IN (
SELECT FoldingID
FROM tblStockManagement
)
;
You can use SQL NOT condition
Select Name
From tblFolding
Where FoldingId Not In (Select FoldingId From tblStockManagement)
Order By Name

Set column type with a select ... into statement

I'm trying to create a table using a SELECT ... INTO statement. The table is created and populates properly, but I want to change the data type of two of the columns.
SELECT DISTINCTROW
AR_Server_Pre.OrderID,
AR_Server_Pre.LineTotal,
AR_Server_Pre.[Total payments],
AR_Server_Pre.ShipDate,
(AR_Server_Pre.LineTotal-nz(AR_Server_Pre.[Total Payments])) AS AmountDue
INTO AR_Final
FROM AR_Server_Pre
WHERE
((([AR_Server_Pre].[LineTotal]-nz([AR_Server_Pre].[Total Payments]))>0.5)
AND
((AR_Server_Pre.ShipDate)<Date()));
I want to assign column type of currency to LineTotal and AmountDue. AR_Server_Pre is a Select Query, so the simple solution of "Change it at that table) won't work.
You can wrap the specified fields in a CCur() function to force them to be Currency, e.g.,
SELECT DISTINCTROW
AR_Server_Pre.OrderID,
CCur(AR_Server_Pre.LineTotal) AS LineTotal,
...