Executing an oracle .sql file with php - oracle

I'm trying to execute a simple .sql file that resets my database. It works fine when I run it in sqlplus with #, but returns ORA-00922: missing or invalid option, when I run it using php like that:
$query = file_get_contents($path);
$stid = oci_parse($con, $query);
$r = oci_execute($stid);
Here is the .sql file itself, it's quite simple and I simply can't find anything that is wrong with the commands:
ALTER SESSION SET NLS_LANGUAGE = AMERICAN;
ALTER SESSION SET NLS_TERRITORY = AMERICA;
DROP TABLE users;
DROP TABLE friends;
DROP TABLE replies;
DROP TABLE tweets;
DROP TABLE retweets;
DROP TABLE mentions;
DROP TABLE hashtags;
CREATE TABLE users (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
name VARCHAR2(40) NOT NULL,
screen_name VARCHAR2(20) NOT NULL,
followers NUMBER(9) NOT NULL,
friends NUMBER(9) NOT NULL
);
CREATE TABLE friends (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT friendship PRIMARY KEY (id1_str, id2_str)
);
CREATE TABLE replies (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT reply PRIMARY KEY (id1_str, id2_str)
);
CREATE TABLE tweets (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
user_id_str VARCHAR2(20) NOT NULL,
created DATE NOT NULL
);
CREATE TABLE retweets (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT retweet PRIMARY KEY (user_id_str, tweet_id_str)
);
CREATE TABLE hashtags (
tweet_id_str VARCHAR2(20) NOT NULL,
text VARCHAR2(140) NOT NULL,
CONSTRAINT hashtag PRIMARY KEY (tweet_id_str, text)
);
CREATE TABLE mentions (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT mention PRIMARY KEY (user_id_str, tweet_id_str)
);
COMMIT;
I know it's probably silly, but I've spent hours looking for the solution to no avail and would appreciate any tips (even a different method than loading this file).

The problem is simple. Your file contains multiple commands, and oci_parse is meant to work with single statements.
You've got two options:
Read the file, parse it for semi-colons, and pass each resulting string (minus the semi-colon) to oci_parse/oci_execute, or
Change your file so it's a valid PL/SQL block, with BEGIN at the beginning and END; at the end, with each of the DDL statements you want to execute in an EXECUTE IMMEDIATE as shown below:
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_LANGUAGE = AMERICAN';
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = AMERICA';
EXECUTE IMMEDIATE 'DROP TABLE users';
EXECUTE IMMEDIATE 'DROP TABLE friends';
EXECUTE IMMEDIATE 'DROP TABLE replies';
EXECUTE IMMEDIATE 'DROP TABLE tweets';
EXECUTE IMMEDIATE 'DROP TABLE retweets';
EXECUTE IMMEDIATE 'DROP TABLE mentions';
EXECUTE IMMEDIATE 'DROP TABLE hashtags';
EXECUTE IMMEDIATE 'CREATE TABLE users (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
name VARCHAR2(40) NOT NULL,
screen_name VARCHAR2(20) NOT NULL,
followers NUMBER(9) NOT NULL,
friends NUMBER(9) NOT NULL
)';
EXECUTE IMMEDIATE 'CREATE TABLE friends (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT friendship PRIMARY KEY (id1_str, id2_str)
)';
EXECUTE IMMEDIATE 'CREATE TABLE replies (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT reply PRIMARY KEY (id1_str, id2_str)
)';
EXECUTE IMMEDIATE 'CREATE TABLE tweets (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
user_id_str VARCHAR2(20) NOT NULL,
created DATE NOT NULL
)';
EXECUTE IMMEDIATE 'CREATE TABLE retweets (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT retweet PRIMARY KEY (user_id_str, tweet_id_str)
)';
EXECUTE IMMEDIATE 'CREATE TABLE hashtags (
tweet_id_str VARCHAR2(20) NOT NULL,
text VARCHAR2(140) NOT NULL,
CONSTRAINT hashtag PRIMARY KEY (tweet_id_str, text)
)';
EXECUTE IMMEDIATE 'CREATE TABLE mentions (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT mention PRIMARY KEY (user_id_str, tweet_id_str)
)';
END;

Related

automatic partition naming for range partitioning

I have a table like this
CREATE TABLE data_audit(
id_col NUMBER(*,0) NOT NULL ENABLE,
col_2 VARCHAR2(10) NOT NULL ENABLE,
col_3 NUMBER(*,0) NOT NULL ENABLE,
col_4 VARCHAR2(10) NOT NULL ENABLE,
col_5 VARCHAR2(10) NOT NULL ENABLE,
created_at TIMESTAMP (3) DEFAULT current_timestamp,
CONSTRAINT DATA_AUDIT_PK PRIMARY KEY (col_2,col_3,col_4)
USING INDEX ENABLE
)
PARTITION BY RANGE(created_at)
(
PARTITION p2022_jan
VALUES LESS THAN (TO_DATE('01-jan-2022')),
PARTITION p2022_feb
VALUES LESS THAN (TO_DATE('01-feb-2022')),
PARTITION p2022_mar
VALUES LESS THAN (TO_DATE('01-mar-2022'))
)
Here - in above example - i need to explicitly mention the name in DDL
I want to create new partition automatically for every month data
How can i achieve it naming it automatically - i am ok with any random name of partition
I am using oracle - Oracle Database 19c Enterprise Edition Release 19.0.0.0
Instead of a RANGE partition I would suggest an INTERVAL partition:
CREATE TABLE DATA_AUDIT (
id_col NUMBER(*,0) NOT NULL ENABLE,
col_2 VARCHAR2(10) NOT NULL ENABLE,
col_3 NUMBER(*,0) NOT NULL ENABLE,
col_4 VARCHAR2(10) NOT NULL ENABLE,
col_5 VARCHAR2(10) NOT NULL ENABLE,
CREATED_AT TIMESTAMP (3) default current_timestamp ,
CONSTRAINT DATA_AUDIT_PK PRIMARY KEY (col_2,col_3,col_4) USING INDEX ENABLE
)
partition by range (CREATED_AT) INTERVAL (INTERVAL '1' MONTH)
(PARTITION p2021_dec VALUES LESS THAN (TIMESTAMP '2022-01-01 00:00:00'));
Then Oracle will create new partition automatically every months.
For renaming you can runs this procedure by daily scheduler job as proposed by Barbaros Özhan.
PROCEDURE RenamePartitions IS
ts TIMESTAMP;
newName VARCHAR2(30);
CURSOR TabPartitions IS
SELECT TABLE_NAME, PARTITION_NAME, HIGH_VALUE
FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = 'DATA_AUDIT'
ORDER BY 1,2;
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET DDL_LOCK_TIMEOUT = 180';
FOR aPart IN TabPartitions LOOP
EXECUTE IMMEDIATE 'BEGIN :ret := '||aPart.HIGH_VALUE||'; END;' USING OUT ts;
ts := ADD_MONTHS(ts, -1);
newName := 'p'||TO_CHAR(ts,'yyyy_mon', 'NLS_DATE_LANGUAGE = american');
IF aPart.PARTITION_NAME <> newName THEN
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' RENAME PARTITION '||aPart.PARTITION_NAME||' TO '||newName;
END IF;
END LOOP;
END RenamePartitions;
Or see a more generic one: Partition table rename automatically in ORACLE
Assume that the table only has the first partition(p2022_jan) and you'll add new partitions to the table every month, then firstly create a stored procedure such as
CREATE OR REPLACE PROCEDURE Pr_Add_Part_to_Data_Audit is
v_ddl VARCHAR2(32767);
v_date DATE := TO_DATE(TO_CHAR(sysdate,'yyyy-mm-')||'01','yyyy-mm-dd');
BEGIN
v_ddl :='ALTER TABLE data_audit ADD PARTITION p'||TO_CHAR(v_date,'yyyy')||'_'||TO_CHAR(v_date,'mon')||' VALUES LESS THAN ('''||v_date||''')';
EXECUTE IMMEDIATE v_ddl;
END;
/
then, call that from a scheduler at the beginning of the upcoming months such as
DECLARE
v_job_name VARCHAR2(32) := 'jb_add_part_data';
BEGIN
DBMS_SCHEDULER.CREATE_JOB(job_name => v_job_name,
job_type => 'STORED_PROCEDURE',
job_action => 'Pr_Add_Part_to_Data_Audit',
start_date => TO_DATE('01-02-2021 01:00:10',
'DD-MM-YYYY HH24:MI:SS'),
repeat_interval => 'FREQ=MONTHLY; BYHOUR=1;',
auto_drop => false,
comments => 'Adds a new partition every month');
DBMS_SCHEDULER.ENABLE(v_job_name);
END;
/
Here the answergiven by Wernfried Domscheit - is correct
thank you for your Answer - it showed the way of controlling partition name - in my case i didn't need to care about partition name - i just needed to make sure - new partition is created with new name - so i used first half of your comment -
to verify it - i used it with DAY - partitioning
CREATE TABLE DATA_AUDIT (
id_col NUMBER(*,0) NOT NULL ENABLE,
col_2 VARCHAR2(10) NOT NULL ENABLE,
col_3 NUMBER(*,0) NOT NULL ENABLE,
col_4 VARCHAR2(10) NOT NULL ENABLE,
col_5 VARCHAR2(10) NOT NULL ENABLE,
CREATED_AT TIMESTAMP (3) default current_timestamp ,
CONSTRAINT DATA_AUDIT_PK PRIMARY KEY (col_2,col_3,col_4) USING INDEX ENABLE
)
partition by range (CREATED_AT) INTERVAL (INTERVAL '1' DAY)
(PARTITION p2021_dec VALUES LESS THAN (TIMESTAMP '2022-01-01 00:00:00'));
and then it created partitions like
partition name - table name
P2021_DEC - DATA_PURGE_AUDIT
SYS_P8592 - DATA_PURGE_AUDIT

PL/SQL block to populate table

I have been tasked to populate a table called Sales_Facts with a PL/SQL block but I return 0 results. The procedure is executed with out error and I run my script to populate my table but my SELECT COUNT script returns nothing. I cannot see what I am doing wrong.
Here's what I have:
CREATE TABLE Sales (
sale_ID VARCHAR2(10) NOT NULL,
salesperson_ID VARCHAR2(10) NOT NULL,
cust_ID VARCHAR2(10) NOT NULL,
sale_date DATE,
VIN VARCHAR2(20) NOT NULL,
mileage INT,
vehicle_status VARCHAR2(15),
gross_sale_price NUMBER(8,2) NOT NULL,
PRIMARY KEY (sale_ID),
CONSTRAINT FK_Customer_ID FOREIGN KEY (cust_ID) REFERENCES Customers(cust_ID),
CONSTRAINT FK_VIN_ID FOREIGN KEY (VIN) REFERENCES Sale_Vehicles(VIN));
CREATE TABLE Times (
sale_day DATE NOT NULL, --populated from Sales sale_date
day_type VARCHAR2(50) NOT NULL,
PRIMARY KEY (sale_day));
CREATE TABLE Vehicles (
vehicle_Code VARCHAR2(10),
description VARCHAR2(100),
PRIMARY KEY (vehicle_Code));
Vehicles is populated with this:
CREATE SEQUENCE veh_code_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
COMMIT;
--PL/SQL Block
SET SERVEROUTPUT ON
DECLARE
vehType VARCHAR2(50);
v_make OLTP_Vehicles.make%type;
v_model OLTP_Vehicles.model%type;
CURSOR v_type IS SELECT DISTINCT make, model FROM OLTP_Vehicles;
BEGIN
OPEN v_type;
LOOP
FETCH v_type INTO v_make, v_model;
vehType := v_make || ', ' || v_model;
INSERT INTO Vehicles (vehicle_Code, description)
VALUES (veh_code_seq.NEXTVAL, vehType);
EXIT WHEN v_type%notfound;
END LOOP;
CLOSE v_type;
END;
/
CREATE TABLE Financing_Plans (
plan_ID VARCHAR2(10) NOT NULL,
institution VARCHAR2(25) NOT NULL,
loan_type VARCHAR2(15) NOT NULL,
percentage DECIMAL(4,2) NOT NULL,
min_down NUMBER(8,2) NOT NULL,
max_loan_amt NUMBER(8,2) NOT NULL,
max_term INT NOT NULL,
PRIMARY KEY (plan_ID));
CREATE TABLE Dealerships (
dealer_ID VARCHAR2(5) NOT NULL,
location VARCHAR(30) NULL,
region_ID VARCHAR(5) NULL,
street_address VARCHAR2(100) NOT NULL,
city VARCHAR2(25) NOT NULL,
state VARCHAR2(15) NOT NULL,
zip VARCHAR2(5) NOT NULL,
phone VARCHAR2(10) NOT NULL,
sqft NUMERIC(8,2) NULL,
opened_date DATE,
manager VARCHAR2(100) NULL,
district_ID VARCHAR2(5) NOT NULL,
PRIMARY KEY (dealer_ID),
CONSTRAINT UC_Dealership UNIQUE (dealer_ID,district_ID));
CREATE TABLE Sales_Facts (
sale_day DATE NOT NULL,
vehicle_Code VARCHAR2(10) NOT NULL,
plan_ID VARCHAR2(10) NOT NULL,
dealer_ID VARCHAR2(5) NOT NULL,
vehicles_sold NUMBER(8,2) NOT NULL,
gross_sales_amt NUMBER(8,2) NOT NULL,
CONSTRAINT PK_Sales_Facts PRIMARY KEY (sale_day, vehicle_Code, plan_ID, dealer_ID),
CONSTRAINT FK_Sale_Day FOREIGN KEY(sale_day) References Times(sale_day),
CONSTRAINT FK_Vehicle_Code FOREIGN KEY(vehicle_Code) References Vehicles(vehicle_Code),
CONSTRAINT FK_Fin_Plan_ID FOREIGN KEY(plan_ID) References Financing_Plans(plan_ID),
CONSTRAINT FK_Dealer_ID FOREIGN KEY(dealer_ID) References Dealerships(dealer_ID));
And here is my procedure that is not returning any results:
CREATE OR REPLACE PROCEDURE Populate_Sales_Facts
AS
l_sale_day DATE;
l_vehicle_Code VARCHAR2(10);
l_plan_ID VARCHAR2(10);
l_dealer_ID VARCHAR2(5);
l_vehicles_sold NUMBER(8,2);
l_gross_sales_amt NUMBER(8,2);
CURSOR c1 IS SELECT sale_day,vehicle_Code,fp.plan_ID,d.dealer_ID,
COUNT (*) AS vehicles_sold,
SUM (s.gross_sale_price) AS gross_sales_amt
FROM Times t, Sales s, Financing_Plans fp, Dealerships d, Vehicles v
WHERE t.sale_day = s.sale_date
GROUP BY sale_day, vehicle_Code, fp.plan_ID, d.dealer_ID;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO l_sale_day, l_vehicle_Code, l_plan_ID, l_dealer_ID, l_vehicles_sold, l_gross_sales_amt;
EXIT WHEN c1%NOTFOUND;
IF l_vehicles_sold <> 0 THEN
INSERT INTO SALES_FACTS (sale_day,vehicle_Code,plan_ID,dealer_ID,vehicles_sold, gross_sales_amt)
VALUES (l_sale_day,l_vehicle_Code,l_plan_ID,l_dealer_ID,l_vehicles_sold,l_gross_sales_amt);
END IF;
END LOOP;
CLOSE c1;
END;
/
BEGIN
Populate_Sales_Facts;
END;
/
I was given the fields to populate the Sales table and they cannot be changed
per my requirements but I did fix my WHERE statement to pull sale_day from the Times table where equal to the sale_date in the Sales table because those are the only fields that linked. So I was able to get the table to populate but now instead of getting no more than 200 rows, I am getting 61065 rows of data. Here are my requirements: get every possible combination of the dimension tables’ primary keys and then the total vehicles sold and gross sales amount for each combination. If these values for Total_Vehicles_Sold and Gross_Sales_Amount for a combination are zero then don’t INSERT a row into the SALES_FACT table. Only insert rows for combinations of the four foreign key columns where there
were some vehicles sold. Maybe I am just misunderstanding the task but I feel like I am getting too many rows now.

PL/SQL Syntax problems

I am working on online platform to make an ERD and to get the PL/SQL code of it but I get the following code (see below) but I am not sure if it is PL/SQL.
I need to verify if this code is PL/SQL or not:
CREATE TABLE "CATEGORY" (
"ID" NUMBER(10) PRIMARY KEY,
"THLEVEL" NUMBER(10) NOT NULL
);
CREATE SEQUENCE "CATEGORY_SEQ" NOCACHE;
CREATE TRIGGER "CATEGORY_BI"
BEFORE INSERT ON "CATEGORY"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "CATEGORY_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "REPORT" (
"ID" NUMBER(10) PRIMARY KEY,
"CLIENT" CLOB NOT NULL,
"VERSION" NUMBER(10) NOT NULL
);
CREATE SEQUENCE "REPORT_SEQ" NOCACHE;
CREATE TRIGGER "REPORT_BI"
BEFORE INSERT ON "REPORT"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "REPORT_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "ASSET" (
"ID" NUMBER(10) PRIMARY KEY,
"REPORT" NUMBER(10) NOT NULL,
"IP" VARCHAR2(1000 CHAR) NOT NULL,
"NAME" VARCHAR2(1000 CHAR)
);
CREATE INDEX "IDX_ASSET__REPORT" ON "ASSET" ("REPORT");
ALTER TABLE "ASSET" ADD CONSTRAINT "FK_ASSET__REPORT" FOREIGN KEY ("REPORT") REFERENCES "REPORT" ("ID");
CREATE SEQUENCE "ASSET_SEQ" NOCACHE;
CREATE TRIGGER "ASSET_BI"
BEFORE INSERT ON "ASSET"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "ASSET_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "SOLUTION" (
"ID" NUMBER(10) PRIMARY KEY,
"IMPLEVEL" VARCHAR2(1000 CHAR) NOT NULL,
"DIFFICULTY" VARCHAR2(1000 CHAR) NOT NULL,
"DESCRIPTION" CLOB NOT NULL
);
CREATE SEQUENCE "SOLUTION_SEQ" NOCACHE;
CREATE TRIGGER "SOLUTION_BI"
BEFORE INSERT ON "SOLUTION"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "SOLUTION_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "VULNERABILITY" (
"ID" VARCHAR2(1000 CHAR) PRIMARY KEY,
"ASSET" NUMBER(10) NOT NULL,
"SOLUTION" NUMBER(10) NOT NULL,
"CATEGORY" NUMBER(10) NOT NULL,
"CVE" VARCHAR2(1000 CHAR),
"DATE" TIMESTAMP NOT NULL,
"LOCATION" CLOB NOT NULL
);
CREATE INDEX "IDX_VULNERABILITY__ASSET" ON "VULNERABILITY" ("ASSET");
CREATE INDEX "IDX_VULNERABILITY__CATEGORY" ON "VULNERABILITY" ("CATEGORY");
CREATE INDEX "IDX_VULNERABILITY__SOLUTION" ON "VULNERABILITY" ("SOLUTION");
ALTER TABLE "VULNERABILITY" ADD CONSTRAINT "FK_VULNERABILITY__ASSET" FOREIGN KEY ("ASSET") REFERENCES "ASSET" ("ID");
ALTER TABLE "VULNERABILITY" ADD CONSTRAINT "FK_VULNERABILITY__CATEGORY" FOREIGN KEY ("CATEGORY") REFERENCES "CATEGORY" ("ID");
ALTER TABLE "VULNERABILITY" ADD CONSTRAINT "FK_VULNERABILITY__SOLUTION" FOREIGN KEY ("SOLUTION") REFERENCES "SOLUTION" ("ID");
CREATE TABLE "EXPLOIT" (
"ID" NUMBER(10) PRIMARY KEY,
"VULNERABILITY" VARCHAR2(1000 CHAR) NOT NULL,
"NAME" VARCHAR2(1000 CHAR) NOT NULL,
"TYPE" CLOB NOT NULL,
"DESCRIPTION" CLOB NOT NULL
);
CREATE INDEX "IDX_EXPLOIT__VULNERABILITY" ON "EXPLOIT" ("VULNERABILITY");
ALTER TABLE "EXPLOIT" ADD CONSTRAINT "FK_EXPLOIT__VULNERABILITY" FOREIGN KEY ("VULNERABILITY") REFERENCES "VULNERABILITY" ("ID");
CREATE SEQUENCE "EXPLOIT_SEQ" NOCACHE;
CREATE TRIGGER "EXPLOIT_BI"
BEFORE INSERT ON "EXPLOIT"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "EXPLOIT_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;
So is this PL/SQL or is somethin else from Oracle? How this will sound in PL/SQL?
Well that is PL/SQL and it will work in Oracle if you remove the double semi-colon you have on create trigger.
https://dba.stackexchange.com/questions/1121/how-to-differentiate-between-sql-and-pl-sql
That is NOT PL/SQL. That is DDL (Data Definition Language). Specifically it is Oracle DDL.
These are DDL statements in which we can create, alter tables ,PL/SQL is a Procedural language SQL in which we use DML Statements for any Process.
These are a set of DDL commands and not PL/SQL as such. However if you incorporate these commands in a nice stored procedure or package, it might be termed as PL/SQL (procedural part of it) but not currently in its current shape.

Getting Invalid Character when attempting insert on Oracle Express 10g

I used the below create statement in Oracle Express 10g.
CREATE TABLE Employee( EmpNo VARCHAR2(8) NOT NULL,
EmpName VARCHAR2(30), Department VARCHAR2(30) NOT NULL,
Email VARCHAR2(30) NOT NULL, Phone VARCHAR2(15) NOT NULL,
CONSTRAINT PK_EMPNO PRIMARY KEY (EmpNo) );
And then used the following insert statement.
Insert into EMPLOYEE (EMPNO,EMPNAME,DEPARTMENT,EMAIL,PHONE)
values('E100','Chuck Coordinator','Administration','chuck#colorado.edu','3-1111');
Why am I getting?
"Ivalid Character"

Oracle: Dynamically set all NOT NULL columns in a Table to allow NULL

I have a table with 75+ columns in it.
Almost all of the columns have the NOT NULL constraint.
If do a giant alter table modify statement (with every column in there), I get an error saying something along the lines of "You can't set this field to NULL, because it already is NULL"
I have to do this for several tables, and so would prefer to have a dynamic solution.
Can I dynamically find all of the columns that are NOT NULL, and set them to NULL?
I've seen several similar questions like this, but can't find a solution for Oracle SQL.
Modify all columns in a table to 'not null' no matter what
Here is a test table, with two not null columns, and one null column:
create table zzz_mark_test_me (
cust_id varchar2(20) not null,
cust_name varchar2(20) null,
cust_phone varchar2(20) not null
);
table ZZZ_MARK_TEST_ME created.
desc zzz_mark_test_me
Name Null Type
---------- -------- ------------
CUST_ID NOT NULL VARCHAR2(20)
CUST_NAME VARCHAR2(20)
CUST_PHONE NOT NULL VARCHAR2(20)
Now invoke this SQL:
select 'alter table ' || table_name ||
' modify (' || column_name || ' null );'
from user_tab_columns
where table_name='ZZZ_MARK_TEST_ME' and nullable='N'
order by column_id;
Which yields this:
alter table ZZZ_MARK_TEST_ME modify (CUST_ID null );
alter table ZZZ_MARK_TEST_ME modify (CUST_PHONE null );
Copy/paste the output into SQL*Plus etc. and invoke:
alter table ZZZ_MARK_TEST_ME modify (CUST_ID null );
table ZZZ_MARK_TEST_ME altered.
alter table ZZZ_MARK_TEST_ME modify (CUST_PHONE null );
table ZZZ_MARK_TEST_ME altered.
And now, no more NOT NULL:
desc zzz_mark_test_me
Name Null Type
---------- ---- ------------
CUST_ID VARCHAR2(20)
CUST_NAME VARCHAR2(20)
CUST_PHONE VARCHAR2(20)
You can use this procedure. You can first comment out line containing "execute immediate" to see what it executes, before running.
First parameter is schema_name, second is table_name.
create or replace procedure proc_null(t_owner in varchar2, t_name in varchar2) as
v_exec_imm varchar2(1000);
begin
for o in (select owner, column_name from all_tab_cols where owner=t_owner and table_name=t_name and nullable = 'N')
loop
v_exec_imm := 'alter table '||t_owner||'.'||t_name||' modify ('||o.column_name||' null) ';
execute immediate v_exec_imm; -- comment this line if You want, modifies table
dbms_output.put_line( v_exec_imm );
end loop;
end proc_null;

Resources