Posts

Showing posts with the label Plsql

Check If A Variable Is Null In Plsql

Answer : if var is NULL then var :=5; end if; Use: IF Var IS NULL THEN var := 5; END IF; Oracle 9i+: var = COALESCE(Var, 5) Other alternatives: var = NVL(var, 5) Reference: COALESCE NVL NVL2 In PL/SQL you can't use operators such as '=' or '<>' to test for NULL because all comparisons to NULL return NULL . To compare something against NULL you need to use the special operators IS NULL or IS NOT NULL which are there for precisely this purpose. Thus, instead of writing IF var = NULL THEN... you should write IF VAR IS NULL THEN... In the case you've given you also have the option of using the NVL built-in function. NVL takes two arguments, the first being a variable and the second being a value (constant or computed). NVL looks at its first argument and, if it finds that the first argument is NULL , returns the second argument. If the first argument to NVL is not NULL , the first argument is returned. So you c...