PL/SQL Oracle error when writing an insert statement with subquery - plsql

Here is my INSERT statement:
INSERT INTO customer_payment (payment_type_id, PAYMENT_METHOD, PAYMENT_STATUS, sql_sequence)
((SELECT emcpm.payment_method_type_id,
epmt.description, ecba.mandate_status
FROM cust_pay_map emcpm, payment_method_type epmt, customer_bank_account ecba
WHERE emcpm.payment_method_type_id = ecba.payment_method_type_id), MY_SEQ.nextval);
I get the error
ORA-00907: missing right parenthesis
when I run it. Please help me correct the mistake.

Add the sequence in the select statement like this:
INSERT
INTO customer_payment
(
payment_type_id,
PAYMENT_METHOD,
PAYMENT_STATUS,
sql_sequence
)
SELECT emcpm.payment_method_type_id,
epmt.description,
ecba.mandate_status ,
MY_SEQ.nextval
FROM cust_pay_map emcpm,
payment_method_type epmt,
customer_bank_account ecba
WHERE emcpm.payment_method_type_id = ecba.payment_method_type_id;

Related

Liquibase : Expected something between the "TABLE" keyword and the "IF" keyword

I am trying to run the below sql by liquibase and I am getting an error expected something between "TABLE" and the keyword "IF" keyword .This is for teradata database
CREATE MULTISET TABLE IF NOT EXISTS SCHEMA_NAME.TABLE_NAME, NO, FALLBACK,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO AS ( I TRIED WITHOUT "AS")
(
COL1 INTEGER,
COL1 INTEGER....ETC)
PRIMARY INDEX( COL1,COL2);
REPLACE PROCEDURE <database_name>.drop_if_exists( in_object varchar(50)) begin IF EXISTS(
SELECT 1
FROM dbc.tables
WHERE tablename = in_object
and databasename='<database_name>') THEN
CALL DBC.SysExecSQL('DROP TABLE ' || in_object);
END IF; END;
Followed by the regular create table statment.

How can drop table if table exists in oracle?

I am trying to create database by my java application using my generated schema file. In schema I have included drop query also. But I want to do some improvements for DROP QUERY. So I want to check the existence of db objects before running drop query and drop only when if it exists.
I googled for it and found some oracle link, Some link suggest following syntax and some mentioned that ORACLE does not support such syntax.
SYNTAX A:
IF EXISTS DROP TABLE TABLE_NAME
SYNTAX B:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
I also tried following queries:-
IF EXISTS (SELECT * FROM dba_objects WHERE OBJECT_NAME = 'BBB' )
DROP TABLE [BBB]
but it was giving error:-
Error starting at line 2 in command:
DROP TABLE [BBB]
Go
Error report:
SQL Error: ORA-00903: invalid table name
00903. 00000 - "invalid table name"
*Cause:
*Action:
Error starting at line 1 in command:
IF EXISTS (SELECT * FROM dba_objects WHERE OBJECT_NAME = 'BBB' ) DROP TABLE [BBB]
Error report:
Unknown Command
I refered following links:-
https://community.oracle.com/thread/2421779?tstart=0
Please suggest me if there any other queries to drop table with condition if table exists.
Drop table with no check. If any error exists you'll never know when something went wrong.
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE my_table';
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
Or you can search in Oracle dictionary.
DECLARE
l_cnt NUMBER;
BEGIN
SELECT count(*)
INTO l_cnt
FROM user_tables
WHERE table_name = 'MY_TABLE';
IF l_cnt = 1 THEN
EXECUTE IMMEDIATE 'DROP TABLE my_table';
END IF;
END;
If you run following code you do not have to check if table exists and in case of errors (table is locked with now wait or any other you will know about it)
begin
for c1 in (select owner,table_name from dba_tables where table_name='MY_TABLE') loop
execute immediate 'drop table '||c1.owner||'.'||c1.table_name||'';
end loop;
end;
Try this : It will drop table 'table_name' if it is present .
declare
a varchar2(700) ;
begin
execute immediate ' SELECT CASE WHEN tab = 1
THEN ''DROP TABLE TABLE_NAME''
ELSE ''select 1 from dual''
END
FROM ( SELECT sum(case when table_name = ''TABLE_NAME'' then 1 else 0 end ) as tab FROM user_tables)' into a;
EXECUTE IMMEDIATE a;
end;

Inserting into table using data from two other tables, using PL/SQL on SQLDeveloper

I'm writing a PL/SQL procedure, and I need to insert into a Table, based on an equality of two columns from two differents tables.
Here is my code:
create or replace PROCEDURE insertSomething
IS
BEGIN
INSERT INTO MYDBP ( ZIP )
SELECT POSTCODE
FROM ZIPDBP
WHERE ZIPDBP.ZIP = OTHERDBP.ZIP;
COMMIT;
END;
I'm getting an error saying OTHERDBP.ZIP is an invalid identifier. What is the issue?
EDIT:
To get the output I expected I need another equality statement between two of the tables ID, but again I'm getting invalid identifier again, this time for DBP_CLIENTS.ID. Here is the code
INSERT INTO DBP_CLIENTS ( POSTCODE )
SELECT POSTCODE
FROM DBP_POSTCODE, HELENS_DATA
WHERE DBP_POSTCODE.LOCALITY = HELENS_DATA.SUBURB
AND DBP_POSTCODE.STATE = 'NSW'
AND DBP_CLIENTS.ID = HELENS_DATA.ID;
COMMIT;
Try this:
create or replace PROCEDURE insertSomething
IS
BEGIN
INSERT INTO MYDBP ( ZIP )
SELECT POSTCODE
FROM ZIPDBP, OTHERDBP
WHERE ZIPDBP.ZIP = OTHERDBP.ZIP;
COMMIT;
END;
You have to add otherdbp to from section. And you don't need to use () in procedure declaration.
Moreover, insert is reserverd word in pl/sql, so procedure must have different name
You have to add DBP_CLIENTS in the FROM clause:
INSERT INTO DBP_CLIENTS ( POSTCODE )
SELECT POSTCODE
FROM DBP_POSTCODE, HELENS_DATA, DBP_CLIENTS
WHERE DBP_POSTCODE.LOCALITY = HELENS_DATA.SUBURB
AND DBP_CLIENTS.ID = HELENS_DATA.ID
AND DBP_POSTCODE.STATE = 'NSW';
COMMIT;

Insert and delete in oracle 11g procedure

I need to write a procedure with input parameters like groupid and item ids(array)
I need to insert into a table with values(groupid,itemid[0]) all items like (1,11),(1,12),(1,13),etc. I have table with two columns (groupid,itemid). please help me to sort out this problem. I am trying to write merge but getting error
CREATE OR REPLACE PROCEDURE update_product_group_prc
(
in_product_group_key IN INT,
in_product_key_array IN dbms_utility.maxname_array
)
AS
indx pls_integer := in_product_key_array.FIRST;
BEGIN
WHILE(INDX IS NOT NULL)
LOOP
MERGE INTO DIM_PRODUCT_X_GROUP PXG
USING(SELECT IN_PRODUCT_GROUP_KEY ,
IN_PRODUCT_KEY_ARRAY(INDX) IN_PRODUCT_KEY_ARRAY
FROM DUAL) IN_TBL
ON(IN_TBL.IN_PRODUCT_GROUP_KEY=PXG.PRODUCT_GROUP_KEY)
WHEN MATCHED THEN
-- DELETE --need to delete source records which r not here in array.
-- WHERE
WHEN NOT MATCHED THEN
INSERT (PRODUCT_GROUP_KEY,PRODUCT_KEY)
VALUES (IN_TBL.IN_PRODUCT_GROUP_KEY,IN_TBL.IN_PRODUCT_KEY_ARRAY);`
INDX := IN_PRODUCT_KEY_ARRAY.NEXT(INDX);
END LOOP;
END update_product_group_prc;
Error(16,41): PL/SQL: ORA-00936: missing expression
You have a syntax error. Comment the "WHEN MATCHED THEN" fragment.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF55028

I am getting an error when I execute a delete query

I am executing the following query
DELETE FROM List,Tree WHERE List.CatID = Tree.CatID AND List.ID = '1' AND Tree.Cat = '332'
but I run into following error
near ",": syntax error
Correct syntax for delete statement is this
DELETE FROM table_name WHERE somecolumn=somevalue
So you cannot use 2 tables in a single delete query by separating them with a comma..
You need to do something like
DELETE something FROM table_name INNER JOIN...
You can delete only from one table at a time, and you have to rewrite the join as a subquery:
DELETE FROM List
WHERE ID = '1'
AND CatID IN (SELECT CatID
FROM Tree
WHERE Cat = '332')

Resources