How can place multiple "Begins" in a procedure to drop and create tables?
My attempt -
CREATE PROCEDURE procCreateCarTable
IS
BEGIN
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE CARS';
END;
COMMIT;
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE CARS (ID VARCHAR2(1), NAME VARCHAR2(10), TITLE VARCHAR2(10))';
COMMIT;
END;
END;
One by one the executes work but when creating this procedure I get the following error -
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "END"
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Here is the SQL procedure that I got to work as mentioned in my comment below -
CREATE PROCEDURE procCreateCarTable
IS
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE CARS';
EXCEPTION WHEN OTHERS THEN NULL;
EXECUTE IMMEDIATE 'CREATE TABLE CARS (ID VARCHAR2(1), NAME VARCHAR2(10), TITLE
VARCHAR2(10))';
COMMIT;
END;
I added an EXCEPTION in case table CARS does not exist, but now my latest procedure below fails -
CREATE PROCEDURE procCreateCarTable
IS
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE CARS';
EXCEPTION WHEN OTHERS THEN NULL;
EXECUTE IMMEDIATE 'CREATE TABLE CARS (ID VARCHAR2(1), NAME VARCHAR2(10), TITLE
VARCHAR2(10))';
EXECUTE IMMEDIATE 'DROP TABLE TRUCKS';
EXCEPTION WHEN OTHERS THEN NULL;
EXECUTE IMMEDIATE 'CREATE TABLE TRUCKS (ID VARCHAR2(1), NAME VARCHAR2(10), TITLE
VARCHAR2(10))';
COMMIT;
END;
The only reason I could think of as to why my latest procedure failed is because of the other 'EXCEPTION'. I do want the other one there so the procedure can still create the TRUCKS table. Any suggestions?
Through trial and error I created the procedure:
CREATE PROCEDURE procCreateCarTable
IS
BEGIN
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE CARS';
EXCEPTION WHEN OTHERS THEN NULL;
EXECUTE IMMEDIATE 'CREATE TABLE CARS (ID VARCHAR2(1), NAME VARCHAR2(10), TITLE
VARCHAR2(10))';
END;
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE TRUCKS';
EXCEPTION WHEN OTHERS THEN NULL;
EXECUTE IMMEDIATE 'CREATE TABLE TRUCKS (ID VARCHAR2(1), NAME VARCHAR2(10), TITLE
VARCHAR2(10))';
END;
END;
Related
I am trying to create a table in Oracle DB and I am getting this error:
PLS-00103: Encountered the symbol "(" when expecting one of the following:
. , # in <an identifier>
<a double-quoted delimited-identifier> partition subpartition
The create statement is the following:
set echo on
set term on
set pause off
set pages 5000
set serveroutput on size 99999
spool sqlResult.lst
declare
--
wcont number :=0;
begin
CREATE TABLE schema.TABLE
(ID NUMBER GENERATED BY DEFAULT AS IDENTITY,
DOCUMENT1 VARCHAR2(50),
DOCUMENT2 VARCHAR2(50),
ID_CLIENT1 NUMBER,
ID_CLIENT2 NUMBER,
BDATEO DATE,
FDATE DATE,
ID_PROCESS VARCHAR2(100),
TYPE VARCHAR2(50),
CONSTRAINT tmp_pk PRIMARY KEY(ID));
wcont:=wcont+ sql%ROWCOUNT;
if sql%ROWCOUNT > 1
then
RAISE_APPLICATION_ERROR(-20010,'ERROR');
end if;
--
commit;
--
dbms_output.put_line('SUCCESS');
--
exception
when others then
rollback;
dbms_output.put_line(sqlerrm);
end;
/
spool off
exit
I can't see where is the error because the only "(" are after new table name and I think it's mandatory.
Best regards!
CREATE OR REPLACE PROCEDURE P_DUP_DEL_CREATE_TAB IS
BEGIN
CREATE OR REPLACE TABLE T_DUPLICATE_TABLE (
F_NUMBER NUMBER(2),
S_NUMBER NUMBER(1),
CONSTRAINT ID PRIMARY KEY (F_NUMBER, S_NUMBER)
);
END P_DUP_DEL_CREATE_TAB;
I don't know why it gives me this error:
Error(3,3): PLS-00103: Found the symbol "CREATE" instead of one of the following: ( begin case declare exit for goto if ...
I tried using the backslash, but it doesn't solve the problem, any help?
You cannot use DDL statements in PL/SQL. You need to use EXECUTE IMMEDIATE and cannot use CREATE OR REPLACE:
CREATE OR REPLACE PROCEDURE P_DUP_DEL_CREATE_TAB
IS
TABLE_DOES_NOT_EXIST EXCEPTION;
PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942);
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE T_DUPLICATE_TABLE';
EXCEPTION
WHEN TABLE_DOES_NOT_EXIST THEN
EXECUTE IMMEDIATE 'CREATE TABLE T_DUPLICATE_TABLE (
F_NUMBER NUMBER(2),
S_NUMBER NUMBER(1),
CONSTRAINT ID PRIMARY KEY (F_NUMBER, S_NUMBER)
)';
END P_DUP_DEL_CREATE_TAB;
/
db<>fiddle here
Here are my 2 tables that I am using
create table EMPLOYEE (
Eid number(5) primary key,
Ename char(30),
Designation char(30));
create table Attendance (
Eid number(5) references EMPLOYEE(Eid),
tdate date,
attendance char(2) check(attendance in('P','A')));
Here is the Procedure I am using to take attendance
create or replace PROCEDURE take_attendance(
id IN number,
attendance IN char) IS
begin
insert into ATTENDANCE Values(id, sysdate, attendance);
end;
Here is the Trigger I am using
create or replace TRIGGER to_take_attendance
before insert or update of eid on ATTENDANCE
for each row
declare
CURSOR c(n number) is select * from EMPLOYEE where eid = :new.eid;
e c%rowtype;
invalid_id EXCEPTION;
begin
open c(:new.eid);
fetch c into e;
if c%NOTFOUND then
raise invalid_id;
else
NULL;
end if;
EXCEPTION
when invalid_id then
close c;
raise_application_error(-20001,'Invalid ID');
close c;
end;
Here are the Errors I am getting
begin
*
ERROR at line 1:
ORA-20001: Invalid ID
ORA-06512: at "101503028.TO_TAKE_ATTENDANCE", line 17
ORA-04088: error during execution of trigger '101503028.TO_TAKE_ATTENDANCE'
ORA-06512: at "101503028.TAKE_ATTENDANCE", line 5
ORA-06512: at line 2
I am not sure why I am getting those errors. I am new to PL/SQL is my question my be very stupid.
Thanks!
EDIT - Removed the others in exception.
You are getting errors because your trigger works as expected.
You are trying to insert to ATTENDANCE, ID that does not exist in EMPLOYEE.
Exception defined in your code:
raise_application_error(-20001,'Invalid ID');
Error received:
ORA-20001: Invalid ID
I have set of sql statements (create table, views, sequences), Where my schema name changes all the time and rest of the sql be the same.
Even in schema name a part of it has to change, for example:
I have a schema name ABC_XYZ, i would like to change this schema name to ABC_DEF_XYZ.
For this i tried to insert a variable in the schema name like ABC_&var1_XYZ. If i pass the variable in the schema name as shown here (ABC_&var1_XYZ) and pass the value to the variable, It ask me to declare the vaue of the variable.
I have given the error and the code i use below:
Error report -
ORA-06550: line 5, column 52:
PLS-00201: identifier 'REL4' must be declared
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
ORA-06550: line 7, column 51:
PLS-00201: identifier 'REL4' must be declared
here is my create statement:
set echo off
set verify off
undefine mySchemaPart
declare
vSQL varchar2(32767);
begin
vSQL:= 'begin EXECUTE IMMEDIATE alter table ABC_'||&&mySchemaPart||'_OWNER.TEST drop constraint EMPLOYEE_ID_FK; EXCEPTION when others then if (SQLCODE != -02443 and SQLCODE != -942) then RAISE; end if; end';
execute immediate vSQL;
vSQL:= 'begin EXECUTE IMMEDIATE drop table ABC_'||&mySchemaPart||'_OWNER.TEST cascade constraints PURGE; EXCEPTION when others then if SQLCODE != -942 then RAISE; end if; end';
execute immediate vSQL;
vSQL:= 'create table ABC_'||&mySchemaPart||'OWNER.TEST
(
EMPLOYEE_ID NUMBER(19) not null,
LAST_UPDT_DT DATE not null,
)';
execute immediate vSQL;
vSQL:= 'CREATE OR REPLACE SYNONYM ABC_USER.TEST FOR ABC_'||&mySchemaPart||'OWNER.TEST';
execute immediate vSQL;
vSQL:= 'begin EXECUTE IMMEDIATE alter table ABC_'||&mySchemaPart||'OWNER.TEST
add constraint EMPLOYEE_ID_FK foreign key (EMPLOYEE_ID)
references ABC_OWNER.GDSD (EMPLOYEE_ID); EXCEPTION when others then if (SQLCODE != -02443 and SQLCODE != -02275) then RAISE; end if; end';
execute immediate vSQL;
end;
/
Is there any other way i can try to insert my value in this schema.
You can use dynamic SQL to handle a varying schema in your DDL; for example:
accept mySchemaPart PROMPT "Schema part: "
declare
vMySchema varchar2(30) := 'ABC_' || '&mySchemaPart' || '_XYZ';
vSQL varchar2(32767);
begin
vSQL := 'create table ' || vMySchema || '.tableName (...)';
execute immediate vSQL;
end;
/
Pass it as 'ABC_||'&var1||'_XYZ'
An example of insert statement;
insert into test (a)
select 'ABC_'||&var1||'_XYZ' from dual ;
On execution it will ask for prompt.
As a block you can use this:
declare
var varchar2(100);
begin
var:='create table ABC_'||&var1||'_XYZ.tablnam(a number)'; --add your columns
dbms_output.put_line(var);
execute immediate var;
end;
As per #bin request his/her statments:
declare
var varchar2(500);
begin
var:='create table ABC_'||&var||'_OWNER.TEST(EMPLOYEE_ID NUMBER(19) not null, LAST_UPDT_DT DATE not null )';
--Table Created
dbms_output.put_line(var);
execute immediate var;
var:='alter table ABC_'||&var1||'_OWNER.TEST add constraint EMPLOYEE_ID_FK foreign key (EMPLOYEE_ID) references ABC_OWNER.GDSD(EMPLOYEE_ID)';
--Constraint Added
dbms_output.put_line(var);
execute immediate var;
var:='CREATE OR REPLACE SYNONYM ABC_USER.TEST FOR ABC_'||&var1||'_OWNER.TEST';
--Synonym Created
dbms_output.put_line(var);
execute immediate var;
var:='alter table ABC_'||&var1||'_OWNER.TEST drop constraint EMPLOYEE_ID_FK';
--Constraint Dropped
dbms_output.put_line(var);
execute immediate var;
var:='drop table ABC_'||&var1||'_OWNER.TEST cascade constraints PURGE';
--Table Dropped
dbms_output.put_line(var);
execute immediate var;
Exception
WHEN others then
dbms_output.put_line('Error--'||sqlerrm||'-'||sqlcode);
end;
Output:
SQL> declare
var varchar2(100);
begin
var:='create table ABC_'||&var1||'OWNER.TEST(EMPLOYEE_ID NUMBER(19) not null, LAST_UPDT_DT DATE not null )';
'
'
'
dbms_output.put_line(var);
execute immediate var;
end;
Enter value for var1: 'REL4'
create table ABC_REL4_OWNER.TEST(EMPLOYEE_ID NUMBER(19) not null, LAST_UPDT_DT DATE not null )
Table created.
alter table ABC_REL4_OWNER.TEST add constraint EMPLOYEE_ID_FK foreign key (EMPLOYEE_ID) references ABC_OWNER.GDSD(EMPLOYEE_ID)
Table altered.
CREATE OR REPLACE SYNONYM ABC_USER.TEST FOR ABC_REL4_OWNER.TEST
Synonym created.
alter table ABC_REL4_OWNER.TEST drop constraint EMPLOYEE_ID_FK
Table altered.
drop table ABC_REL4_OWNER.TEST cascade constraints PURGE
Table dropped.
Wrote this script:
static void schema()
{
Sql.newInstance(*DB_CONFIG, DB_DRIVER).execute('''
drop table if exists post;
drop table if exists author;
create table author(
id integer primary key,
name varchar(500)
);
create table post(
id integer primary key,
title varchar(500),
text longvarchar,
author integer not nul,
foreign key(author) references author(id)
);
''')
}
and after start, I see this:
"WARNING: Failed to execute:
because: ORA-00933: SQL command not properly ended"
I'm using Oracle 11g 2 database and oracle jdbc driver.
drop table if exists post;
drop table if exists author;
It is not a valid Oracle syntax. You could do it in the following way -
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE post';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE author';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
There is another syntax error -
author integer not nul,
Correct it to NOT NULL.