INSERT INTO with JOINS in Oracle - sql

Hi I try to adapt this blog post to my needs using Oracle to model a Data Vault architecture.
With the following code I try to build a link between two hubs referencing on the original source table "orders".
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders as src
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order AS dest
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
However I receive following error.
Error at Command Line : 192 Column : 17
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Any help or hint much appreciated!

There are two places where you have used the as keyword. For giving alias to the table you just need to write the name of the alias after table name. as should not be used there.
Try the following code: (changes are mentioned inline)
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders src -- removed AS from here
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order dest -- removed AS from here
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
Cheers!!

Remove the as in your query i.e. instead of
FROM orders as src
use
FROM orders src
Oracle doesn't support the as alias, unless it is/will be added in most current versions

Related

Outer join for Alias name and column name -Oracle

I had a working sample query earlier in my code as mentioned below.
SELECT DISTINCT
nombre_aplicacion,
APLICACION,
NOMBRE_APLCODE,
DESCRIPCION,
AREAFUNC
FROM (
select **CODAPLICATION nombre_aplicacion**,
APLICACION,
NOMBRE_APLCODE,
DESCRPTION,
AREAFUNC
from admin.VW_APLICACIONES#dblink,
admin.VW_PRODUCTOS#dblink
where **nombre_aplicacion (+) = CODAPLICATION**
)
WHERE 1=1
ORDER BY nombre_aplicacion ASC;
When I try similar type of query with different tables I was getting error as invalid ORA-00904: "NOMBRE_APLICACION": invalid identifier.
If I remove nombre_aplicacion (+) = CODAPLICATION in where condition query is fetching the result. Can any one suggest why I was facing error as its working earlier with sample query and I was getting error? Is this join is valid?
The query is not valid as:
In the inner sub-query you select areafunc and in the outer query you use area which does not appear in the inner sub-query so will not be available.
In the inner sub-query, you define CODAPLICATION to have the alias nombre_aplicacion and then you try to use that alias in the WHERE clause as a join condition; that will not work.
You have not described which column belongs to which table but you want something like:
SELECT DISTINCT
a.codaplication AS nombre_aplicacion,
a.aplicacion,
a.nombre_aplcode,
p.descrption,
p.areafunc
from APLICACIONES a
LEFT OUTER JOIN PRODUCTOS p
ON (a.primary_key_column = p.foreign_key_column)
ORDER BY nombre_aplicacion ASC;
Note: you are going to have to correct the code to give the correct table aliases for each column and give the correct columns for the join condition.

Using UDTF on Join clause

I'm trying to use UDTF on a join and for some reason I'm receiving an error which says that the UDTF is not recognizing the 1st table on the statement:
SHOW TRANSACTIONS IN ACCOUNT;
CREATE OR REPLACE TEMPORARY TABLE UTIL_DB.PUBLIC.TRANSACTIONS AS
SELECT * from table(result_scan(last_query_id()));
SELECT *
FROM UTIL_DB.PUBLIC.TRANSACTIONS AS T
JOIN table(information_schema.QUERY_HISTORY_BY_SESSION(T."id")) Q
WHERE Q.EXECUTION_STATUS = 'RUNNING'
AND T."id" != CURRENT_SESSION();
ERROR:
SQL compilation error: error line 3 at position 55
invalid identifier 'T."id"'
The motivation for this query is getting the sql text of the active transaction
Can someone please advise how can I resolve it?
Thanks
It seems that the issue is when trying to pass the identifier instead of "constant" into information_schema.query_history_by_session().
As an alternative information_schema.query_history() could be used:
SELECT *
FROM tab AS T
join table(information_schema.query_history()) Q
ON T."id" = Q.session_id -- here the join instead of correlation
WHERE Q.EXECUTION_STATUS = 'RUNNING'
AND T."id" != CURRENT_SESSION();

SQL command not properly ended in update statement with multiple sets

I have following Oracle update statement
UPDATE details
SET details.ISO_BNK_TX_CODE_PRTRY_CODE = details.TX_TYP_CODE,
details.ISO_BNK_TX_CODE_PRTRY_ISSUER = 'BAI'
FROM AS_ACCT_STAT_DET details
JOIN AS_ACCT_STAT statements ON details.ACCT_STAT_ID = statements.ID
JOIN MSG_FDEF fdef ON statements.W_R_SOURCE_FORMAT_ID = fdef.ID
WHERE fdef.CODE = 'BAI2'
/
I made the original script in SQL Server. But I thought this code would work for both. But I receive following error message:
Error at Command Line : 4 Column : 1 Error report - SQL Error:
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
I think something is missing between the last set and the FROM statement.
Anyone?
Try this query
UPDATED
UPDATE (
Select
details.ISO_BNK_TX_CODE_PRTRY_CODE ,details.TX_TYP_CODE,
details.ISO_BNK_TX_CODE_PRTRY_ISSUER
FROM
AS_ACCT_STAT_DET details
INNER JOIN
AS_ACCT_STAT statements ON details.ACCT_STAT_ID = statements.ID
INNER JOIN
MSG_FDEF fdef ON statements.W_R_SOURCE_FORMAT_ID = fdef.ID
WHERE
fdef.CODE = 'BAI2'
) d
SET
d.ISO_BNK_TX_CODE_PRTRY_CODE = d.TX_TYP_CODE,
d.ISO_BNK_TX_CODE_PRTRY_ISSUER = 'BAI'
Oracle doesn't allow a join in an update statement, without a subquery anyway. You may be able to use an updatable view here, but you don't need to as you aren't getting the new values from one of the joined tables.
So it looks like you can just do:
UPDATE details
SET details.ISO_BNK_TX_CODE_PRTRY_CODE = details.TX_TYP_CODE,
details.ISO_BNK_TX_CODE_PRTRY_ISSUER = 'BAI'
WHERE EXISTS (
SELECT null
FROM AS_ACCT_STAT statements
JOIN MSG_FDEF fdef ON statements.W_R_SOURCE_FORMAT_ID = fdef.ID
WHERE statements.ID = details.ACCT_STAT_ID
AND fdef.CODE = 'BAI2'
)
/
That will only update rows where there is matching data in the other two tables, from the exists() condition; that subquery is correlated with the table being updated via its where clause.

How to copy column from one table to another which can be joined by ID?

UPDATE s
SET s.TECH_NAME = r1.TECH_NAME
FROM **ERRMSG** s INNER JOIN **RAWDATA** r1 on
s.id = r1.id;
I want to update tech_name in table ERRMSG from table RAWDATA.Joining condition is ID.Whats wrong in the above query.Im getting the following error
Error at Command Line : 62 Column : 1
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
This sounds like a job for the MERGE statement documented here:
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm
This is more in the spirit of what you were trying to do, it avoids correlated subqueries, and it gives you more flexibility.
merge into errmsg s
using rawdata r1
on (s.id = r1.id)
when matched then update set s.tech_name = r1.tech_name
If needed, you can use the other capabilities of MERGE as well; for example, when NOT matched, the id in the errmsg table does not have a corresponding id in the rawdata table. Would you like to take an action in that case as well? You could, for example, set the tech_name to NULL or some default value or delete the row altogether - you can do all these things in one MERGE statement. See the documentation I provided.
What is wrong is that you are using syntax that Oracle does not support. Use a correlated subquery instead:
UPDATE errmsg s
SET s.TECH_NAME = (SELECT r1.TECH_NAME FROM rawdata r1 WHERE s.id = r1.id)
WHERE EXISTS (SELECT 1 FROM rawdata r1 WHERE s.id = r1.id)

command error in SQL Query, not sure why?

I'm new to SQL and I'm currently writing a query and I got this error. Any help will be appreciated.
ORA-00933: SQL command not properly ended
My Query below:
CREATE VIEW moscow_paris_overlap(SSN) AS
SELECT t1.SSN
FROM assign AS T1
INNER JOIN assign AS T2
ON T1.SSN = T2.SSN
WHERE T1.EndYear = T2.StartYear
AND T1.CityName = 'Moscow'
AND T2.CityName = 'Paris';
SELECT DISTINCT emp.* FROM emp INNER JOIN moscow_paris_overlap ON emp.SSN = moscow_paris_overlap.SSN;
First, you need to separate the CREATE VIEW from the query which uses it. If you're using SQL*Plus or something similar you can do this by putting a / on a separate line between the two. This will cause the CREATE VIEW to be executed first.
Second, AS can't be used in a FROM or INNER JOIN when defining a table alias. Change the FROM clause in your view creation to FROM ASSIGN T1. Similarly, the INNER JOIN should be INNER JOIN ASSIGN T2.