I need to update newly created column in my oracle table. To do so I need to use existing values in row to decide how to populate this column, I am getting error:
java.lang.NullPointerException -> See Debug Output for details
This is my query:
UPDATE
SCHEMA_NAME.TABLE_NAME
SET
OCO= IF CO= 'Y' AND COM='Y' THEN
{
'Y'
} ELSE
{
'N'
}
END IF;
Any suggestions on syntax?
You could use CASE expression in the SET clause.
For example,
UPDATE table
SET schema.column = CASE
WHEN CO= 'Y' AND COM='Y' THEN
'Y'
ELSE
'N'
END
Related
I am confused with writing Postgresql UPDATE query with CASE (or IF) condition.
Expected behavior: I have to raise an exception and do not do update if one specific field is not null.
Current behavior: I tries CASE and IF conditions, but every time I got syntax error: SQL Error [42601]: ERROR: syntax error at or near "exception".
Thoughts: maybe there is a way to achieve same result with adding constraint to table?
UPDATE public.SomeTable
SET Title = (
case when EndDate IS null
THEN 'new title'
ELSE raise exception 'Cannot update entity if EndDate is not null'
end),
Description = (
case when EndDate IS null
THEN 'new description'
ELSE raise exception 'Cannot update entity if EndDate is not null'
end)
WHERE Id=4
Version:
PostgreSQL 11.5 (Debian 11.5-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
The only relatively convenient way I can think to do this uses updatable views:
create view vw_sometable as
select t.*
from sometable t
where endDate is null
with check option;
You would then insert into the view, rather than into the underlying table.
You can further refine this by giving update permissions on the view but not on the underlying table, so all updates need to go through the view.
Well, another method would not return an error, but it would not update the rows. Just include the condition in the where clause:
UPDATE public.SomeTable
SET Title = 'new title',
Description = 'new description'
WHERE Id = 4 AND EndDate is null';
You could add subsequent logic to check if any rows are updated and raise the error there.
remove case after end
UPDATE public.SomeTable
SET Title = (
case when EndDate IS null
THEN 'new title'
ELSE 'Cannot update entity if EndDate is not null'
end ),
Description = (
case when EndDate IS null
THEN 'new description'
ELSE 'Cannot update entity if EndDate is not null'
end )
WHERE Id=4
I am running SQL Server and am needing to update hundreds of entries. Column A has unique values and based on that I need to update column B of the same table, all with different values. Essentially I am looking for a way to perform the following but in a bulk manner:
UPDATE table
set column B = 'value'
where column A = 'unique value'
Is this what you are expecting,
UPDATE table
set column B = case when column A = 5 then 'unique string' end;
How about:
update table set
columnB = concat('unique ', columnA)
you may, or may not, need a where clause, depending on your needs.
With hundreds of values you better have the {unique value - update value} mapping defined in a separate table as TT is asking. But otherwise, if you are okay with hard-coding those mappings in your UPDATE statement:
UPDATE t
SET B = CASE A
WHEN 'unique value1' THEN 'value1'
WHEN 'unique value2' THEN 'value2'
WHEN 'unique value3' THEN 'value3'
ELSE 'Unknown'
END
FROM table t
BTW, the above is only for 3 mappings
Here, you need to write cursor to update a column value in the same table
BEGIN
DECLARE c_a, c_b TEXT;
DECLARE c_ab CURSOR FOR
SELECT column_a,column_b FROM c_table;
OPEN c_ab;
LOOP
FETCH c_ab into c_ca, c_cb;
IF c_ca = "0" THEN
update c_table SET column_b = "No" WHERE
column_a = c_ca;
END IF;
IF c_ca = "1" THEN
update c_table SET column_b = "Yes" WHERE
column_a = c_ca;
END IF;
END LOOP;
CLOSE c_ab;
END
Working and tested code [please refer some cursor tutorials to update according to your condition] and with this you can update table in bulk and speedy
Thanks #Jim Macaulay. That did it. Thank you everyone else for your input.
I want to update a column 'XYZ_RCVD' to 'Y' if there is a value in the row, if not i want to update column 'XYZ_RCVD' to 'N'. I started with this simple script that worked but I had to modify it for the 'N' case. How would i merge both of them into one? I tried some IF-THEN-else but that didn't work too well with the INSERT statement.
Here is what i have so far.
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'Y'
WHERE XYZ_NAME IS NOT NULL;
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'N'
WHERE XYZ_NAME IS NULL;
The solution below by Habib worked perfectly.
I believe you are looking for CASE WHEN like:
UPDATE TEST_SURVEY
SET XYZ_RCVD = CASE WHEN XYZ_NAME IS NOT NULL THEN 'Y' ELSE 'N' END;
I am using PL/sql on APEX.
I have a simple table, where one of the column I need to update the field using Case or if condition.
Just can't this right.
e.g.
Update TableAA
set column1 =
Select case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end as column1
from TableAA;
Since you are updating records from same table, why not try:
Update TableAA set column1 =
case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end;
I am running an SQL query and have just two parenthesis but I still get the error "missing parenthesis". The code is shown below:
Update ALEKWE_CUSTOMER C
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE C.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
If this is for sql-server, then this might be the answer:
Update ALEKWE_CUSTOMER
set recently_purchased = (CASE WHEN EXISTS(
SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D WHERE customer_id=D.customer_id) THEN 'Y'
ELSE 'N' END);
You need to put the whole CASE structure in parentheses too.
Next to that, you can't use an alias for the table which will be updated.
You cannot have the alias C at this position, and neither do you need it:
Update ALEKWE_CUSTOMER
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE ALEKWE_CUSTOMER.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
You don't need to put the case into paranteses either.
You would need:
Update ALEKWE_CUSTOMER
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE C.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
FROM ALEKWE_CUSTOMER C
for your alias to work
Edit:
My altered statements work fine on Microsoft SQL-Server.
Apparently the problem is specific to Oracle SQL.
After reading your comments, I added Oracle to your tags, since you say this nowhere.