ORA-00936: expression absente + ORA-02063: précédant line de LIENBD_S1 Oracle - oracle

Can you help me find the error here? I do not understand why it is not working..
CREATE TABLE Commande2 AS (SELECT * FROM Commande#lienBD_S1 WHERE numF NOT IN (SELECT numF FROM Fournisseur1#lienBD_S1));
When I execute the query without the CREATE TABLE like this
SELECT * FROM Commande#lienBD_S1 WHERE numF NOT IN (SELECT numF FROM Fournisseur1#lienBD_S1);
it works.

I found an other way to make it work !
I created a table Fournisseur2 That contain the the lines that are NOT IN Fournisseur1 and used it to create my table Commande2 like this :
CREATE TABLE Fournisseur2 AS (SELECT * FROM Fournisseur#lienBD_S1 WHERE ville <> 'Paris');
CREATE TABLE Commande2 AS (SELECT * FROM Commande#lienBD_S1 WHERE numF IN (SELECT numF FROM Fournisseur2));

Related

Count of rows from all views in Oracle with a condition

I am trying to get count of all rows from views in oracle schema and my code is working fine. But when i try to add a condition like where actv_ind = 'Y', i am unable to get it working. Any suggestions on how to modify the code to make it working?
SELECT view_name,
TO_NUMBER(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) cnt from '||owner||'.'||view_name||
'where'||view_name||'.'||'actv_ind= Y')),'/ROWSET/ROW/CNT')) as VIEW_CNT
FROM all_views
WHERE owner = 'ABC' AND view_name not in ('LINK$')
I am getting the error ACTV_IND : Invalid Identifier.
The error messages from DBMS_XMLGEN are not very helpful so you need to be very careful with the syntax of the SQL statement. Add a space before and after the where, and add quotation marks around the Y:
SELECT view_name,
TO_NUMBER(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) cnt from '||owner||'.'||view_name||
' where '||view_name||'.'||'actv_ind= ''Y''')),'/ROWSET/ROW/CNT')) as VIEW_CNT
FROM all_views
WHERE owner = 'ABC' AND view_name not in ('LINK$');
The query still assumes that every view contains the column ACTV_IND. If that is not true, you might want to base the query off of DBA_TAB_COLUMNS WHERE COLUMN_NAME = 'ACTV_IND'.
Here's a simple sample schema I used for testing:
create user abc identified by abc;
create or replace view abc.view1 as select 1 id, 'Y' actv_ind from dual;
create or replace view abc.view2 as select 2 id, 'N' actv_ind from dual;

varchar in execute immediate

I'm using the following script pl/sql:
EXECUTE IMMEDIATE 'insert into TAB1(ID, CODE, TYPE, ORDRE)
select ''KEY'', ''KEY_LIB'', ''TYP_KEY'', 3 FROM dual
where not exists(
select ID,CODE,TYPE,ORDRE
FROM TAB1
where TYPE=''TYP_KEY''
AND CODE =''KEY_LIB''
)';
And I'm getting the following error :
00000 - "FROM keyword not found where expected
The error seems to be in the second line but I can't figure out what's wrong.
Can anyone help please ?
Type is a keyword in Oracle. So, try with tablename.Type when type is a column name. Here is your revised query:
EXECUTE IMMEDIATE 'insert into TAB1(ID, CODE, TYPE, ORDRE)
select ''KEY'', ''KEY_LIB'', ''TYP_KEY'', 3 FROM dual
where not exists(
select ID,CODE,TYPE,ORDRE
FROM TAB1
where TAB1.TYPE=''TYP_KEY''
AND CODE =''KEY_LIB''
)';
You should better try it like this:
EXECUTE IMMEDIATE 'insert into TAB1(ID, CODE, "TYPE", ORDRE)
select :KEY, :KEY_LIB, :TYP_KEY, 3 FROM dual
where not exists(
select ID,CODE,"TYPE",ORDRE
FROM TAB1
where "TYPE" = :TYP_KEY
AND CODE = :KEY_LIB
)'
USING 'KEY', 'KEY_LIB', 'TYP_KEY', 'TYP_KEY', 'KEY_LIB';
You can use Alternative Quoting Mechanism (''Q'') for String Literals, then you don't need double quotes. With this technique it is much easier to test queries manually (if it is needed). Btw, your query works fine.
BEGIN
EXECUTE IMMEDIATE
q'[INSERT INTO tab1(id, code, type, ordre)
SELECT 'KEY', 'KEY_LIB', 'TYP_KEY', 3
FROM dual
WHERE NOT EXISTS(
SELECT 1
FROM tab1
where tab1.type = 'TYP_KEY'
AND code = 'KEY_LIB'
)]';
END;
/

Oracle Invalid Identifier (with Inner Join)

I'm having an "Invalid Identifier" in Oracle because of the "B.username" (username column does exist in USER table). When i remove this, it's working fine. How to resolve this issue? I came from a MySQL background.
SELECT * FROM (SELECT qNA.assignment, qNA.regDate, B.username, (
SELECT DISTINCT NVL(idx, 0)
FROM EK_USERGRADE
WHERE year = (SELECT DISTINCT userGradeNo FROM EK_USER WHERE ID = qNA.userIdx)
) AS userGradeIdx
FROM EK_NEWTESTAPPLICANT qNA
WHERE IDX = :idx ) A
INNER JOIN EK_USER B ON (A.userIdx = B.ID)
Let's try this with a simplified version of your query:
-- test tables
create table NEWTESTAPPLICANT as select 1 useridx from dual ;
create table B as select 1 id, 'name1' username from dual ;
-- query
select *
from (
select B.username
from NEWTESTAPPLICANT qNA
) A join B on A.useridx = B.id ;
-- ORA-00904: "B"."USERNAME": invalid identifier
There's no "username" column in the NEWTESTAPPLICANT table, which causes the error. A LATERAL inline view (examples see here) may do the trick ...
-- query
select
*
from B, lateral (
select B.username
from NEWTESTAPPLICANT qNA
) A ;
-- result
ID USERNAME USERNAME
1 name1 name1
This works with Oracle 12c.
The problem is, that both your virtual table A and users B have the same column name "username". Specify alias in the main select, like "Select A.* , B.* from(...".
Is it ORA-00903?
User is a reserved word are you sure you created this table? Table name cannot be a reserved word.

Select query succeeds however Insert using same query fails with ORA-00979 (not a GROUP BY expression)

I am having a hard time finding why the below insert would fail while the select alone executes successfully.
INSERT INTO I6_POC_ADJ_OUTPUT
(LOADID,ADJ_ID,ADJ_CATEGORY,ADJ_COMMENT,ITEM,CHANNEL,FDATE,ADJ_TOT_QTY,FQTY)
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY) ADJ_TOT_QTY,
SUM(ADJ.TP_SPLIT) FQTY
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE;
Select and Insert Executions
Update:
Tried with a simple CTAS and it works.
CREATE TABLE TEST_ADJ_OUTPUT AS
SELECT * FROM (
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY),
SUM(ADJ.FQTY*ADJ.TP_SPLIT)
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE
);
Truncated the same table and tried inserting into the same but if fails with the same error. "SQL Error: ORA-00979: not a GROUP BY expression"
TRUNCATE TABLE TEST_ADJ_OUTPUT;
INSERT INTO TEST_ADJ_OUTPUT
SELECT * FROM (
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY),
SUM(ADJ.FQTY*ADJ.TP_SPLIT)
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE
);
Appreciate your help.
Thanks,
Manoj

How to use not exists with insert?

I'm trying to frame the below query but it always gives me the error "SQL command not properly ended". How do i use it??
INSERT INTO PROGRAM_KPI (AMSPROGRAMID,MASTER_KPI_ID,LASTUPDATEDBYDATALOAD)
(SELECT 'PRG-026',MASTER_KPI_ID,to_char(sysdate,'dd-mon-yy hh.mi.ss') from kpi_master)
WHERE NOT EXISTS(select * from insight_master
where amsprogramid = V_PROGRAMID
and inamsscope = 1
and tickettype = 'INCIDENT'
and TICKETSUBMITDATE is not null);
Please try this..(Removing the brackets and formating the code)
INSERT INTO program_kpi
(amsprogramid, master_kpi_id, lastupdatedbydataload)
SELECT 'PRG-026', master_kpi_id, TO_CHAR (SYSDATE, 'dd-mon-yy hh.mi.ss')
FROM kpi_master
WHERE NOT EXISTS (
SELECT *
FROM insight_master
WHERE amsprogramid = v_programid AND inamsscope = 1
AND tickettype = 'INCIDENT'
AND ticketsubmitdate IS NOT NULL);
But What is the relation between table program_kpi and insight_master ?
There seems to be no join between the inner and outer subquery.

Resources