最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

oracle database - How to ignore skiping the null values in my plsql query - Stack Overflow

matteradmin7PV0评论

I have the query inside ' ' :

X:=NULL;
v_sql:='insert into users(id) values(NVL('||X||',NULL))';

The result I got is:

insert into users(id) values(NVL(,NULL))

Why the null value is skipped? And how can I fix this?

I have the query inside ' ' :

X:=NULL;
v_sql:='insert into users(id) values(NVL('||X||',NULL))';

The result I got is:

insert into users(id) values(NVL(,NULL))

Why the null value is skipped? And how can I fix this?

Share Improve this question edited Nov 16, 2024 at 20:38 jps 22.7k16 gold badges88 silver badges107 bronze badges asked Nov 16, 2024 at 17:24 mmll llmmmmll llmm 111 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 1

How to fix this ? Avoid it by not contatening. It's not good practice. Always try to use bind variables and then you won't run into such issues. For your use case, no dynamic sql is needed. This works too and is a lot simpler:

DECLARE
    l_x VARCHAR2(1);
BEGIN
    l_x := NULL;
    INSERT INTO users ( id ) VALUES ( nvl(l_x,NULL) );
END;
/

PL/SQL procedure successfully completed.

If EXECUTE IMMEDIATE is needed (for some other reason you're not mentioning) , then use it with bind variables:

DECLARE
    l_x VARCHAR2(1);
    l_sql VARCHAR2(100);
BEGIN
    l_x := NULL;
    l_sql  := q'!INSERT INTO users ( id ) VALUES ( nvl(:x,NULL) )!';
    EXECUTE IMMEDIATE l_sql USING l_x;
END;
/

PL/SQL procedure successfully completed.

Actually, it is not skipped but it causes the concatenation to fail silently.

  1. X is NULL.

  2. When you do '|| X ||', the NULL value of X is not treated as a string but as a missing value.

  3. String concatenation with NULL ('|| NULL ||') results in the entire concatenation expression becoming NULL.

    Thus, the v_sql assignment evaluates to NULL, and you see NVL(,NULL) in your result

    If you want the literal text NULL to appear in the SQL query when X is NULL, you need to explicitly handle this substitution. For example:

    v_sql := 'insert into users(id) values(NVL(' || CASE WHEN X IS NULL THEN 'NULL' ELSE X END || ', NULL))';

Post a comment

comment list (0)

  1. No comments so far