From which table does for all enteries makes a select? - sql

I have two standard tables, VBAP and MAKT.
I want elements of MAKT-MAKTX, where MAKT-MATNR = VBAP - MATNR.
SELECT MATNR FROM VBAP INTO IT_VBAP.
SELECT MATNR MAKTX FROM MAKT INTO IT_MAKT FOR ALL ENTRIES WHERE MATNR = IT_VBAP.
Does this code choose values in it_makt where only those matnr fields are equal?
What exactly does for all entries does.
Thank you.

select field_1 field_2 field_n from table into internal_table for all
entries in another_internal_table where some_field_in_table = another_internal_table-same_field_in_another_internal_table.
You use for all entries when you want to fill an internal table according to values in another internal table.
You can try using the following
select matnr from vbap into it_vbap.
select maktx from makt into it_makt for all entries in it_vbap where matnr = it_vbap-matnr.

For all entries is the the exact thing, You might think, as You read it. It is like this:
"For all entries in" allows You to use a subset of an internal table as where condition in a select statement by the possibility to provide the fields of this internal table as where condition-parameters in the select statement.
So, internally it is also only a loop, You can use the sql-monitor transaction, and mostly will see, that for all entries really behaves like a select inside a loop if I remember correctly. It is quite helpful, and usually used relatively often.
If the subset-table, which is used as parameter-table is empty, then You perform a full table select (full table scan), if You only use the tables fields as where parameters. So always pay attention to the select which fills Your where-table or check its contents with if lines( the_table ) > 0. Nevertheless take a look at vwegert's link, it describes the pros and cons of for all entries. Do You need more info ?

Related

use SELECT on internal table (ABAP)

I am still very inexperienced with SAP ABAP.
I have an internal table that I want to filter further and further based on whether data is present.
I have tried the following, but unfortunately I cannot apply a SELECT to an internal table.
How can I solve this problem?
Hope I have explained my problem clearly enough!
"Here I'm getting the hole database into my internal table
SELECT * FROM TABLE
INTO CORRESPONDING FIELDS OF TABLE #itab.
"This should be my first filter if iv_name is not initial
IF iv_name IS NOT INITIAL.
SELECT * FROM itab
WHERE NAME = #iv_name
INTO CORRESPONDING FIELDS OF TABLE #itab.
ENDIF.
"This should be my second filter if iv_age is not initial
IF iv_age IS NOT INITIAL.
SELECT * FROM itab
WHERE AGE = #iv_age
INTO CORRESPONDING FIELDS OF TABLE #itab.
ENDIF.
There are several ways in ABAP to achieve your goal.
You can use the DELETE keyword to filter the data in an internal table:
IF iv_name IS NOT INITIAL
DELETE itab WHERE name NE iv_name.
ENDIF.
Another possibility is to use the FILTER keyword, but the prerequisite is, that the internal table is TYPE SORTED or HASHED:
itab = FILTER #( itab WHERE name = iv_name ).

How to subscript a Postgres column

I have a Postgres query:
SELECT main
FROM (
SELECT
CASE WHEN 1=1 THEN (col_a, col_b)
END as main
FROM "table1"
LIMIT 100) inner_t
Which returns a single column of values in the format (value_a, value_b) in each row. I want the outer query to format those values so that all the value_a's and value_b's are in their own separate columns.
Is there an easy way to do this?
Output screenshot:
http://example.com/path-to-ghosts.jpg
You can abuse row_to_json to do this, but it is probably best to avoid anonymous record types in the first place.
SELECT row_to_json(main)->>'f1', row_to_json(main)->>'f2'
FROM (
SELECT
CASE WHEN 1=1 THEN (col_a, col_b)
END as main
FROM "table1"
LIMIT 100) inner_t
To give a concrete example (after running pgbench -i):
SELECT row_to_json(main)->>'f1', row_to_json(main)->>'f2'
FROM (
SELECT
CASE WHEN 1=1 THEN (aid, bid)
END as main
FROM pgbench_accounts
LIMIT 100) inner_t;
But it only works in v10 and up.
This is more of an explanation than an actual answer. But it won't fit into a comment.
The thing is, SQL is a strictly typed language. Postgres demands to know the number and data types in the SELECT list at call time. The *-expansion in SELECT * FROM .. is based on registered types. Postgres knows the columns of a table because the structure is saved in the catalog tables.
The expression nested in your construct (col_a, col_b) is short for ROW(col_a, col_b) and a ROW constructor creates an anonymous record. The manual:
By default, the value created by a ROW expression is of an anonymous
record type. If necessary, it can be cast to a named composite type —
either the row type of a table, or a composite type created with
CREATE TYPE AS.
Postgres does not know how to expand an anonymous record. *-expansion does not work.
You could cast like the manual says. But that's only an option if the type is stable, i.e. you always put in the same number of columns with the same data types. And that still would not preserve column names.
So, for the best solution, first define:
Obviously you want to preserve column vales.
Do you also want to preserve column names?
Do you also want to preserve column types?
And:
Is the number of columns in the expression always the same?
Are data types always the same?
The CASE condition is stable or based on other columns?
If the true aim of the game is to fit multiple values in a single CASE expression, you only care about values, create a text array instead:
SELECT main[1] AS col_a, main[2] AS col_b
FROM (
SELECT CASE WHEN true THEN ARRAY[col_a::text, col_b::text] END AS main
FROM table1
LIMIT 100
) inner_t;
You lose name and type. You can cast and add aliases if you know name & type.
Else you have to describe your use case more closely - in the question.
Try Below query
SELECT split_part(main,',',1) as Val1,split_part(main,',',2) as Val2
FROM (
SELECT
CASE WHEN 1=1 THEN (col_a, col_b)
END as main
FROM "table1"
LIMIT 100) inner_t

Error in select no header

I want make select from EPREI to ET_DATA ET_DATA have prices prices is structure what i want fill it from EPREIH...
*"----------------------------------------------------------------------
*"*"Lokálne rozhranie:
*" IMPORTING
*" REFERENCE(I_PREIS) TYPE ZRT_IWP_CPREISTYP OPTIONAL
*" EXPORTING
*" REFERENCE(ET_DATA) TYPE ZTT_IWPIFC_PRICE
*" EXCEPTIONS
*" NO_DATA_SELECTED
*"----------------------------------------------------------------------
field-symbols: <lfs_ET_DATA> like line of ET_DATA.
refresh ET_DATA.
SELECT *
FROM EPREI
into CORRESPONDING FIELDS OF TABLE ET_DATA.
loop at ET_DATA assigning <lfs_ET_DATA>.
select *
from EPREIH
into CORRESPONDING FIELDS OF TABLE ET_DATA-prices
where PREIS = <lfs_ET_DATA>-PREIS
and PREISTYP <= <lfs_ET_DATA>-PREISTYP.
endloop.
it shows error:
"ET_DATA" is table without hedaer line and therefore have not component call prices.
How can i make this select correctly? Sorry for my english but i am so tired...
As the previous answer said, ET_DATA is a reference to a table type and does not have a work area.
So if you really want to add a list of EPREIH into ET_DATA-PRICE you have to point to the work area which is assigned to your field symbol.
Something like this:
field-symbols: <lfs_ET_DATA> like line of ET_DATA.
refresh ET_DATA.
SELECT *
FROM EPREI
into CORRESPONDING FIELDS OF TABLE ET_DATA.
loop at ET_DATA assigning <lfs_ET_DATA>.
select *
from EPREIH
into CORRESPONDING FIELDS OF TABLE <lfs_ET_DATA>-prices
where PREIS = <lfs_ET_DATA>-PREIS
and PREISTYP <= <lfs_ET_DATA>-PREISTYP.
endloop.
extra tip: try using pretty printer with "keywords uppercase" and avoid "select *" if you are not using all fields from those tables.
I'm not entirely sure what you are trying, but it looks like you want to update the field "prices" of table ET_DATA.
If you have 1 record in EPREIH for each record in EPREI, your select should look like this :
loop at ET_DATA assigning <lfs_ET_DATA>.
select single prices
from EPREIH
into <lfs_ET_DATA>-prices
where
PREIS = <lfs_ET_DATA>-PREIS
and PREISTYP <= <lfs_ET_DATA>-PREISTYP.
endloop.
You are using a field (ET_DATA-prices) as a table (INTO CORRESPONDING FIELDS OF). That will not work.
Firstly, the reason you are getting the syntax error is that you would need to update ET_DATA with a work area (structure declared as a line of the same type as the table).
What your code will end up doing though is replacing records in ET_DATA on the second select, on each iteration of your loop, so that will accomplish nothing.
You could most like select everything in one SELECT by using a JOIN:
SELECT E1~FIELD1 E2~FIELD2 (etc.)
FROM EPREI as E1
JOIN EPREIH as E2
ON E2~PREIS = E1~PREIS AND
E2~PREISTYP = E2-PREISTYP
into CORRESPONDING FIELDS OF TABLE ET_DATA.
You will just need to change the field specifications in the SELECT clause above to match the fields in your table (and you could leave out the CORRESPONDING).
And you should probably limit the selection on EPREI with a WHERE clause.

How to Unselect The Field in select Query using sql

hi in my database i am store more than 50 field with primarykey (Auto increment) i am not sure about the fields name but i wants to select the entire data in that table , i am using
SELECT * FROM tablename
i want to select all the fields except that ID but this query populate the entire table so is there is possible to unselect the particular field in the select query. Can anyone have an idea please guide me. Thanks in Advance
The * indicates that you want to select ALL fields from a given table. If you want to select only a few fields, or all but one, then you will need to specify the ones you want manually:
select field1,field2,field3 from tablename
The SQL standard does not offer an "except" notation. It would be neat if we could
select t.* -t.ID
from some_table t
/
but it is not supported.
On the other hand, SELECT * is a dangerous construct. It is always better to explicitly list the columns we want in any given situation.

how to convert result of an select sql query into a new table in ms access

how to convert result of an select sql query into a new table in msaccess ?
You can use sub queries
SELECT a,b,c INTO NewTable
FROM (SELECT a,b,c
FROM TheTable
WHERE a Is Null)
Like so:
SELECT *
INTO NewTable
FROM OldTable
First, create a table with the required keys, constraints, domain checking, references, etc. Then use an INSERT INTO..SELECT construct to populate it.
Do not be tempted by SELECT..INTO..FROM constructs. The resulting table will have no keys, therefore will not actually be a table at all. Better to start with a proper table then add the data e.g. it will be easier to trap bad data.
For an example of how things can go wrong with an SELECT..INTO clause: it can result in a column that includes the NULL value and while after the event you can change the column to NOT NULL the engine will not replace the NULLs, therefore you will end up with a NOT NULL column containing NULLs!
Also consider creating a 'viewed' table e.g. using CREATE VIEW SQL DDL rather than a base table.
If you want to do it through the user interface, you can also:
A) Create and test the select query. Save it.
B) Create a make table query. When asked what tables to show, select the query tab and your saved query.
C) Tell it the name of the table you want to create.
D) Go make coffee (depending on taste and size of table)
Select *
Into newtable
From somequery