SELECT FROM multiple tables INTO one internal Table - select

My db tables:
db_1
db_2
db_3
My internal table:
it_comb
it_comb has a structure with some fields from db_1, db_2, db_3.
All db tables have different structures.
I want to select everything from db_1, db_2, db_3 into the correct fields of it_comb with a where condition.
I would like to do something like this: (This doesn't work)
SELECT * From db_1, db_2, db_3 into CORRESPONDING FIELDS OF TABLE it_comb WHERE db_1-MATNR LIKE db_2-MATNR AND db_1-MATNR LIKE db_3-MATNR.
Obviously, this doesn't work because I can't use ',' like that. How do I write this in ABAP? So that it_comb is filled with data from db_1, db_2 and db_3.
Another problem is that every time I select something into it_comb, my previous data gets overwritten.
Code example would be appreciated for ABAP-Beginner.

You can use inner join -
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE it_comb
FROM db_1 AS a
INNER JOIN db_2 AS b
ON a~matnr = b~matnr
INNER JOIN db_3 AS c
ON a~matnr = c~matnr
WHERE (Your any other condition).
APPENDING won't overwrite the previous record from internal table it_comb.
Warning: Use APPENDING if internal table is TYPE STANDARD otherwise you'll get dump.
Also check the SELECT - JOIN documentaion

One other thing you can do in the newer ABAP versions is
select * from mara inner join mvke on mvke~matnr = mara~matnr into table #data(lt_combined).
loop at lt_combined into data(ls_combined).
write: / ls_combined-mara-matnr, ls_combined-mvke-vkorg.
endloop.
this will define and populate your internal table in one step without the need for a separate "data" statement.
Note that in a join with *, you will get an internal table with substructures based on the table names -- since the structure is implied in the field-list of the select, you can also do something like this for a more efficient database query (so it doesn't need to return all the fields) which also eliminates the substructures:
select mara~matnr, mvke~vkorg from mara inner join mvke on mvke~matnr = mara~matnr into table #data(lt_combined).
loop at lt_combined into data(ls_combined).
write: / ls_combined-matnr, ls_combined-vkorg.
endloop.
Hope this helps!

Without JOIN I executed SELECT statements one by one as follows
data it_comb type TABLE OF vbak.
select * APPENDING CORRESPONDING FIELDS OF TABLE it_comb FROM vbak UP TO 10 ROWS.
select * APPENDING CORRESPONDING FIELDS OF TABLE it_comb FROM vbrk UP TO 10 ROWS.
select * APPENDING CORRESPONDING FIELDS OF TABLE it_comb FROM likp UP TO 10 ROWS.

Related

How to do a Select * followed by a join SEA-ORM

I want to do a join with another table. I followed the tutorial on the site and the my code compiles but it's not performing the join and instead just selects the first table.
SELECT
"table1.col1"
"table1.col2"
"table1.col3"
FROM
"table1"
JOIN "table2" ON "table1"."col1" = "table2"."col1"
LIMIT
1
It is only returning the data from table1 and not concatenating the columns where the condition for table1 and table2 is met.
I execute the query using the following code:
Entity::find()
.from_raw_sql(Statement::from_string(DatabaseBackend::Postgres, query.to_owned()))
.all(&self.connection)
.await?
That returns a Vec<Model>. Is this the correct way? Also, how can I build a SQL statement using an Entity as the base which looks like SELECT * from "table1".
After 'SELECT' (and before 'FROM') you are specifying which columns
to include in the output,
and you are selecting only three columns from table1 in your code.
Add the columns you want to include from table2 here, and you may get
the results you want.

Update or insert with outer join in postgres

Is it possible to add a new column to an existing table from another table using insert or update in conjunction with full outer join .
In my main table i am missing some records in one column in the other table i have all those records i want to take the full record set into the maintable table. Something like this;
UPDATE maintable
SET all_records= othertable.records
FROM
FULL JOIN othertable on maintable.col = othertable.records;
Where maintable.col has same id a othertable.records.
I know i could simply join the tables but i have a lot of comments in the maintable i don't want to have to copy paste back in if possible. As i understand using where is equivalent of a left join so won't show me what i'm missing
EDIT:
What i want is effectively a new maintable.col with all the records i can then pare down based on presence of records in other cols from other tables
Try this:
UPDATE maintable
SET all_records = o.records
FROM othertable o
WHERE maintable.col = o.records;
This is the general syntax to use in postgres when updating via a join.
HTH
EDIT
BTW you will need to change this - I used your example, but you are updating the maintable with the column used for the join! Your set needs to be something like SET missingcol = o.extracol
AMENDED GENERALISED ANSWER (following off-line chat)
To take a simplified example, suppose that you have two tables maintable and subtable, each with the same columns, but where the subtable has extra records. For both tables id is the primary key. To fill maintable with the missing records, for pre 9.5 versions of Postgres you must use the following syntax:
INSERT INTO maintable (SELECT * FROM subtable s WHERE NOT EXISTS
(SELECT 1 FROM maintable m WHERE m.id = s.id));
Since 9.5 there is a (preferred) alternative:
INSERT INTO maintable (SELECT * FROM subtable) ON CONFLICT DO NOTHING;
This is preferred because (apart from being simpler) it avoids the situation that has been known to arise in the former, where a race condition is created between the INSERT and the sub-SELECT.
Obviously when the columns are different, you need to specify in the INSERT statement which columns are inserted from which. Something like:
INSERT INTO maintable (id, ColA, ColB)
(SELECT id, ColE, ColG FROM subtable ....)
Similarly the common field might not be id in both tables. However, the simplified example should be enough to point you in the right direction.

are INTO, FROM an JOIN the only ways to get a table?

I'm currently writing a script which will allow me to input a file (generally .sql) and it'll generate a list of every table that's used in that file. the process is simple as it opened the input file, checks for a substring and if that substring exists outputs the line to the screen.
the substring that being checked is tsql keywords that is indicative of a selected table such as INTO, FROM and JOIN. not being a T-SQL wizard those 3 keywords are the only ones i know of that are used to select a table in a query.
So my question is, in T-SQL are INTO, FROM an JOIN the only ways to get a table? or are these others?
There're many ways to get a table, here're some of them:
DELETE
FROM
INTO
JOIN
MERGE
OBJECT_ID (N'dbo.mytable', N'U') where U is the object type for table.
TABLE, e.g. ALTER TABLE, TRUNCATE TABLE, DROP TABLE
UPDATE
However, by using your script, you'll not only get real tables, but maybe VIEW and temporary table. Here're 2 examples:
-- Example 1
SELECT *
FROM dbo.myview
-- Example 2
WITH tmptable AS
(
SELECT *
FROM mytable
)
SELECT *
FROM tmptable

Postgresql get references from a dictionary

I'm trying to build a request to get the data from a table, but some of those columns have foreign keys I would like to replace by the associated keyword in one request.
Basically there's
table A with column 1:PKA-ID and column 2:name.
table B with column 1:PKB-ID, column 2:FKA-ID, column 3:amount.
I want to get all the lines in table B but with all foreign keys replaced by the associated names in table A.
I started building a request with a subrequest + alias to get that, but ofc I have more than one result per subrequest, yet I can't find a way to link that subrequest to the ID of table B [might be exhausted, dumb or both] from the main request. I did something like that:
SELECT (SELECT "NAME" FROM A JOIN B ON ID = FKA-ID) AS name, amount FROM TABLEB;
it feels so simple of a request yet...
You don't need a join in the subselect.
SELECT pkb_id,
(SELECT name FROM a WHERE a.pka_id = b.fka_id),
amount
FROM b;
(See it live in SQL Fiddle).
The subselect query runs for each and every row of its parent select and has the parent row available from the context.
You can also use a simple join.
SELECT b.pkb_id, a.name, b.amount
FROM b, a
WHERE a.pka_id = b.fka_id;
Note that the join version puts less restrictions on the PostgreSQL query optimizer so in some cases the join version might work faster. (For example, in PostgreSQL 9.6 the join might utilize multiple CPU units, cf. Parallel Query).

How to get list of constraints of a table along with their respective column names in SQL Server 2008 R2

I want to list all constraint names with their respective column name for a table. I am using the below mentioned code :
select *
from sys.objects
where parent_object_id = object_id('qw') --this gives me the constraints lists but does not give me columns in which they are applied.
select *
from sys.columns
where object_id = object_id('qw') -- this gives me the column list of the table.
My problem is that I am not able to join these two queries to get columns along with their constraints.
Sounds like you need CONSTRAINT_COLUMN_USAGE.