I need to update an oracle clob column value to null. I tried to below but that didn't work. Any ideas?
update table_name
set CONTENT_TEMPLATE=empty_clob()
where table_key=12345;
Name Null Type
-------------------- -------- -------------
TEST_KEY NOT NULL NUMBER(10)
TEST_ID NOT NULL VARCHAR2(100)
TEST_TYPE NOT NULL VARCHAR2(30)
TEMPLATE_ID VARCHAR2(100)
ROUTE VARCHAR2(100)
MEDIUM VARCHAR2(100)
BEGIN_EFFECTIVE_DATE NOT NULL DATE
END_EFFECTIVE_DATE NOT NULL DATE
CUSTOMER_ID NOT NULL VARCHAR2(30)
VAR_ID NOT NULL VARCHAR2(30)
CREATION_DATE NOT NULL DATE
LAST_UPDATED_DATE NOT NULL DATE
LAST_UPDATED_BY NOT NULL VARCHAR2(30)
CONTENT_TEMPLATE CLOB
SUBJECT_TEMPLATE CLOB
UNIT_TYPE VARCHAR2(30)
OVERLOADED_ALERT_KEY NUMBER(10)
ALERT_TYPE_ALIAS VARCHAR2(100)
update table_name
set column_name = null
where table_key=12345;
NULL - Absense of data
empty_clob() - CLOB is initialized and empty, not same as NULL.
If you still get ORA-01407 with this column then that means there is a NOT NULL constraint on the column which is of CLOB datatype.
UPDATE:
Since the CLOB column is not NOT NULL setting it to CONTENT_TEMPLATE = NULL should work. CONTENT_TEMPLATE = empty_clob() will also work, but bear in mind that it is not same as NULL.
Related
create table EMPLOYEES
(
EMPLOYEE_ID NUMBER(6) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(25),
EMAIL VARCHAR2(25),
PHONE_NUMBER VARCHAR2(20) DEFAULT NULL,
HIRE_DATE DATE(7),
JOB_ID VARCHAR2(10),
SALARY NUMBER(8,2) DEFAULT NULL,
COMISSION_PCT NUMBER(2,2) DEFAULT NULL,
MANAGER_ID NUMBER(6) DEFAULT NULL,
DEPARTMENT_ID NUMBER(4) DEFAULT NULL
);
This is what I want to create (constraints and other information)
date datatype doesn't accept the length so no need to write date(7) just simply write date and after you run the command desc EMPLOYEES; it will show you that the length of date is by default 7.
create table EMPLOYEES(
EMPLOYEE_ID NUMBER(6) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(25) NOT NULL,
EMAIL VARCHAR2(25) NOT NULL,
PHONE_NUMBER VARCHAR2(20) DEFAULT NULL,
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2) DEFAULT NULL,
COMISSION_PCT NUMBER(2,2) DEFAULT NULL,
MANAGER_ID NUMBER(6) DEFAULT NULL,
DEPARTMENT_ID NUMBER(4) DEFAULT NULL
);
Create table emo(
Emo_id number(10) NOT NULL,
Name varchar2(100) NOT NULL,
Band var char2(2),
Emp_type varchar2(5) DEFAULT "FTE" NOT NULL
);
Should be
CREATE TABLE emo
(
Emo_id NUMBER (10) NOT NULL,
Name VARCHAR2 (100) NOT NULL,
Band VARCHAR2 (2),
Emp_type VARCHAR2 (5) DEFAULT 'FTE' NOT NULL
);
So:
not double but single quotes for column's default value
not var char2 but varchar2
I want to create a stored procedure where I want to check the below condition.
In above two columns, RJ_SAPID and RJ_COLO_SAPID in first row the value I-GJ-JMLU-ENB-6011 is common.
So except that value i want to show the other two values in third column by using stored procedure.
I tried using REPLACE function but couldn't succeed. Below is the code
SELECT REPLACE(RJ_SAPID, RJ_COLO_SAPID, '') FROM NE_STRUCTURES;
Please suggest how to proceed.
table definition
OBJECTID NUMBER
RJ_SAPID VARCHAR2(20)
RJ_COLO_SAPID VARCHAR2(200)
RJ_NETWORK_ENTITY_ID VARCHAR2(30)
STRUCTURE_NAME VARCHAR2(200)
INVENTORY_STATUS_CODE VARCHAR2(4)
RJ_MAINTENANCE_ZONE_CODE VARCHAR2(20)
RJ_SITE_NAME VARCHAR2(200)
RJ_SITE_ADDRESS VARCHAR2(500)
RJ_STRUCTURE_TYPE VARCHAR2(20)
TYPE_NAME VARCHAR2(20)
RJ_LAST_MODIFIED_BY VARCHAR2(50)
RJ_LAST_MODIFIED_DATE DATE
RJ_STATUS VARCHAR2(200)
RJ_CITY_CODE VARCHAR2(10)
RJ_R4G_STATE_CODE VARCHAR2(10)
RJ_DISTRICT_CODE VARCHAR2(20)
RJ_TALUK_CODE VARCHAR2(20)
RJ_JC_CODE VARCHAR2(20)
RJ_JIOPOINT_SAPCODE VARCHAR2(20)
RJ_COMPANY_CODE_1 VARCHAR2(20)
RJ_COMPANY_CODE_2 VARCHAR2(20)
NE_STATUS VARCHAR2(20)
You could use REPLACE(RJ_COLO_SAPID, RJ_SAPID, '') and for leading comma, use LTRIM
SELECT RJ_SAPID, RJ_COLO_SAPID,
REPLACE(RTRIM(LTRIM(REPLACE(RJ_COLO_SAPID, RJ_SAPID, ''), ','), ','), ',,', '') AS ThirdColumn
FROM NE_STRUCTURES
Just switch your arguments and you will be fine:
SELECT REPLACE(RJ_COLO_SAPID, RJ_SAPID, '') FROM NE_STRUCTURES
And if you want to strip unneeded commata use:
SELECT REGEXP_REPLACE(REPLACE(RJ_COLO_SAPID, RJ_SAPID, ''),'^,+|,+$|(,),+','\1') FROM NE_STRUCTURES
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.
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;