Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update query if statement for Oracle

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?

like image 621
Jakub Zak Avatar asked Feb 18 '15 10:02

Jakub Zak


People also ask

Can we use if statement in update query?

Yes! This works because MySQL doesn't update the row, if there is no change, as mentioned in docs: If you set a column to the value it currently has, MySQL notices this and does not update it. Yes!

Can we use if condition in Oracle SQL query?

In Oracle, the IF-THEN-ELSE statement is used to execute code when a condition is TRUE, or execute different code if the condition evaluates to FALSE.

Can we use update statement in function in Oracle?

The stored function may not modify database tables. It cannot execute an INSERT, DELETE, or UPDATE statement. A stored function that is called remotely or through a parallelized action may not read or write the values of package variables. The Oracle Server does not support side effects that cross user sessions.

How to use update statement in Oracle?

Oracle UPDATE statement is used to update existing values in a table. ... table_name : It’s the name of table within which we want to update values. SET column1=value1 : Here, we define the column name and new value that will replace existing value. The new value can be a literal value or a subquery which ultimately returns a single value.

What is the correct syntax for updating a table in Oracle?

Syntax. The syntax for the UPDATE statement when updating one table in Oracle/PLSQL is: UPDATE table SET column1 = expression1, column2 = expression2, ... column_n = expression_n [WHERE conditions]; OR. The syntax for the Oracle UPDATE statement when updating one table with data from another table is:

How do you update a table in SQL?

Let’s examine the UPDATE statement in detail. First, you specify the name of the table which you want to update. Second, you specify the name of the column whose values are to be updated and the new value. If you update more than two columns, you separate each expression column = value by a comma.

How to perform a basic UPDATE statement with a where clause?

This example shows how to perform a basic UPDATE statement with a WHERE clause controlling the record that is updated. A check query can be used to show that the TerritoryID for record with BusinessEntityID = 285 has been set to 1. USE [AdventureWorks] GO --1) Update one column one row. UPDATE [dbo].


1 Answers

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
like image 63
Lalit Kumar B Avatar answered Nov 16 '22 03:11

Lalit Kumar B