PL SQL - Stored procedure update column if parameter is not NULL - oracle

I've written a stored procedure that updates a table.
But I would like to take into account where one or more of the parameters are NULL.
In such an instance, I don't want to update the column, I want to leave the existing value as is.
I've tried to use:
UPDATE
VS_USER_T
SET
USR_FIRST_NAME = ISNULL(p_NewUsrFName, #p_NewUsrFName)
WHERE
USR_ID = lv_Num_UsrId;
But I get an error on the '#', I'm using Oracle 12c.
This is the procedure call
PROCEDURE UpdateUser
( p_UserId IN VS_USER_T.USR_ID%TYPE,
p_NewUsrFName IN VS_USER_T.USR_FIRST_NAME%TYPE,
p_NewUsrLName IN VS_USER_T.USR_LAST_NAME%TYPE,
p_NewUsrname IN VS_USER_T.USR_LOGIN%TYPE)
Please advise how my UPDATE statement should look like, when 'p_NewUsrname ' can be NULL, in which case I want to leave the existing value as is.
Thanks in advance.

To keep the existing value you need to refer to the existing column value:
USR_FIRST_NAME = ISNULL(p_NewUsrFName, USER_FIRST_NAME)
or you could use:
USR_FIRST_NAME = CASE WHEN p_NewUsrFName is null THEN USER_FIRST_NAME ELSE NewUsrFName END

ISNULL() is not yet a standard Oracle function (at least in the Oracle 12c version that you say you are using). If is of course possible to write a PL/SQL function called ISNULL() and use that.
For a standard Oracle 12c installation, try using NVL or COALESCE instead.
USR_FIRST_NAME = NVL(p_NewUsrFName, USR_FIRST_NAME)
or
USR_FIRST_NAME = COALESCE(p_NewUsrFName, USR_FIRST_NAME)

You could use a decode statement e.g.
update my_table t
set username = decode(p_NewUsrname, NULL, t.username, p_NewUsrname)
where t.id = p_UserId;

Related

Oracle APEX 5 ITEM value

It's possible to get a Oracle APEX 5 item value inside SQL Developer ?
I know it'possible to use something like that:
SELECT *
FROM apex_050100.wwv_flow_data d
inner join apex_050100.wwv_flow_sessions$ s on d.flow_instance = s.id;
where d.flow_instance = <session_id_from_url>;
But i want to use the V function:
select v('ITEM') FROM DUAL;
Or event better to set this item value like this:
APEX_UTIL.SET_SESSION_STATE (
p_name IN VARCHAR2 DEFAULT NULL,
p_value IN VARCHAR2 DEFAULT NULL);
You should use bind variables instead of the v function. Rather than this:
select *
from table
where column = v('PX_ITEM_NAME');
Do this instead:
select *
from table
where column = :PX_ITEM_NAME;
This is safer (not vulnerable to SQL injection), more performant (avoids hard parses and uses shared cursors), and more convenient in that you can copy this over to SQL Developer.
When you run this in SQL Developer, you will be prompted for the values before the query is executed.

Retrieving data from oracle table using scala jdbc giving wrong results

I am using scala jdbc to check whether a partition exists for an oracle table. It is returning wrong results when an aggregate function like count(*) is used.
I have checked the DB connectivity and other queries are working fine. I have tried to extract the value of count(*) using an alias, But it failed. Also tried using getString. But it failed.
Class.forName(jdbcDriver)
var connection = DriverManager.getConnection(jdbcUrl,dbUser,pswd)
val statement = connection.createStatement()
try{
val sqlQuery = s""" SELECT COUNT(*) FROM USER_TAB_PARTITIONS WHERE
TABLE_NAME = \'$tableName\' AND PARTITION_NAME = \'$partitionName\' """
val resultSet1 = statement.executeQuery(sqlQuery)
while(resultSet1.next())
{
var cnt=resultSet1.getInt(1)
println("Count="+cnt)
if(cnt==0)
// Code to add partition and insert data
else
//code to insert data in existing partition
}
}catch(Exception e) { ... }
The value of cnt always prints as 0 even though the oracle partition already exists. Can you please let me know what is the error in the code? Is this giving wrong results because I am using scala jdbc to get the result of an aggregate function like count(*)? If yes, then what would be the correct code? I need to use scala jdbc to check whether the partition already exists in oracle and then insert data accordingly.
This is just a suggestion or might be the solution in your case.
Whenever you search the metadata tables of the oracle always use UPPER or LOWER on both side of equal sign.
Oracle converts every object name in to the upper case and store it in the metadata unless you have specifically provided the lower case object name in double quotes while creating it.
So take an following example:
-- 1
CREATE TABLE "My_table_name1" ... -- CASE SENSISTIVE
-- 2
CREATE TABLE My_table_name2 ... -- CASE INSENSITIVE
In first query, we used double quotes so it will be stored in the metadata of the oracle as case sensitive name.
In second query, We have not used double quotes so the table name will be converted into the upper case and stored in the metadata of the oracle.
So If you want to create a query against any metadata in the oracle which include both of the above cases then you can use UPPER or LOWER against the column name and value as following:
SELECT * FROM USER_TABLES WHERE UPPER(TABLE_NAME) = UPPER('<YOUR TABLE NAME>');
Hope, this will help you in solving the issue.
Cheers!!

if Statement SAP HANA Views

Add following filter on a column in SAP HANA Analytical view using if statement
if(Col1='a') col2=Col2
else if(Col2='b') col2=col2*1
Can someone help to give me syntax for HANA IF statement for following logic?
Why not using the documentation at the first place?
Not really clear what you are trying to do here. Look's like you are calculating something using col2 based on comparison on col1. As View will not allow you to update the value in the column, you will need to create col3 and put there the following:
if("Col1" = 'a',"Col2", if("Col1" = 'b',"Col2" * 1,'not a not b') )
BTW, do you think col2=col2*1 makes any sense?
Is it possible that you (or Shidai) are confusing the IF-Function with the IF-Statement? Both are working differently:
SELECT IF("Col1"=='a', 'aaahhh', 'uhhhhh') FROM DUMMY;
This works just like in an Excel: If Col1 is 'a' then the first value is returned, otherwise the second.
DECLARE x VARCHAR(100);
IF "Col1"='a'
THEN
x := "Col2";
ELSEIF "Col2"='b'
THEN
x := "Col2" * 1;
END IF
This is a control structure and only allowed in a SQLScript block, e.g. a stored procedure or anonymous block. You cannot use it in a simple SELECT statement.
It's not so clear what you are trying to do with assigning to col2, so I used x instead.
Also note:
HANA is case-sensitive. If you want to use the column Col1 you must write "Col1".
There is also CASE, which works similar to the IF-Function.

ADODB.Recordset addnew with an Oracle package field

Hy guys.
I have a visual basic 6 program with an Oracle DB. When i add a record to table i do:
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("Title") = "Titolo"
Adodc1.Recordset.Fields("Author") = "Autore"
Adodc1.Recordset.Fields("IP") = "10.0.1.1"
Adodc1.Recordset.Update
This routine is ok, but i want to set IP with an oracle function: sys_context('USERENV','IP_ADDRESS')
When i have modified my routine in
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("Title") = "Titolo"
Adodc1.Recordset.Fields("Author") = "Autore"
Adodc1.Recordset.Fields("IP") = "sys_context('USERENV','IP_ADDRESS')"
Adodc1.Recordset.Update
the field of table is always null.
There are workaround for this problem?
Thanks in advance
I think the simple answer is you cannot do it that way. As it is written you are assigning the fixed string 'sys_context('USERENV','IP_ADDRESS')' into the 'IP' field.
To use that function you will have to construct an INSERT statement and execute it directly. Somethink like
INSERT INTO your_table ( Title, Author, IP )
SELECT 'Titolo', 'Autore', sys_context('USERENV','IP_ADDRESS')
Alternatively you could create a stored procedure and execute that (which might be better in the long run)

Setting the value of a form field from a Query

I have a form field where one of the values has a default value defined in a an application settings table. The user would see the default value upon display of the create form, but could change it to another value if they wanted prior to saving the new row.
I don't see any way in the field defaults to specify that the default value is the result of an SQL query (e.g. select default_rate from app_defaults where row_key = 1).
Surely this has to be possible, but how?
As posted to Jeffrey above, final solution can be done completely within APEX but using the Default Value Type = PL/SQL Function Body on the page item.
DECLARE default_value number(9,2);
BEGIN
SELECT deflt_rate INTO default_value FROM app_defaults WHERE row_key = 1;
RETURN default_value;
END;
You can use the SQL query in a PL/SQL block to assign it directly, e.g.
SELECT default_rate
INTO :myblock.rate
FROM app_defaults
WHERE row_key = 1;

Resources