0

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.

3
  • Please update your code so we can understand better what does your SP look like. Commented Mar 21, 2016 at 14:44
  • Are you setting parameters as optional? Commented Mar 21, 2016 at 14:46
  • Where in the Oracle manual did you find the syntax to prefix variables with @? Commented Mar 23, 2016 at 11:19

3 Answers 3

4

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)
Sign up to request clarification or add additional context in comments.

Comments

3

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

4 Comments

Oracle does not have an ISNULL function: ORA-00904: "ISNULL": invalid identifier. See @Niall answer for some Oracle functions that do the same thing.
You may either use NVL() or use the more verbose but common CASE WHEN...THEN..ELSE...END construct
I am using CASE WHEN... with named query but I am getting an error ORA-00932: inconsistent datatypes
I#d checking the types of the THEN and ELSE legs. But it may be worth an explicit question.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.