Skip to content

Question about Rendering with NULLs #34

Closed

Description

@xendren

I'm struggling with a template that queries rows from a table and generates an anonymous PL/SQL block with the query results as INSERT statements. One or more of the columns in the table can be NULL, so I need the template output to render NULL when the value is null and a single-quoted string when it is not.

          <% for c1 in (select NULL as a_varchar_col, 
                             from dual) loop %>          
   INSERT INTO my_table (process_id,
                                        a_varchar_col)
        VALUES (1,
                '<%= NVL(c1.a_varchar_col, 'NULL') %>');
       <% end loop; %>     

The above works, but it surrounds the NULL with single quotes because of the outer quotes. If I remove the outer quotes, NULL displays fine, but changing the column to return an actual string will not have quotes. I have made several attempts to conditionally force single-quotes around the column value inside the <%=...%>, but the single-quotes get parsed incorrectly and are usually removed.

Any help would be appreciated.

Activity

  1. osalvador commented on Mar 10, 2019

    @osalvador
    Owner

    Hi @xendren ,

    This template:

    <% for c1 in (select null as a_varchar_col from dual) loop %>          
       INSERT INTO my_table (process_id, a_varchar_col)
            VALUES (1,'<%= NVL(c1.a_varchar_col, null) %>');
    <% end loop; %> 

    Generate this output:

       INSERT INTO my_table (process_id, a_varchar_col)
            VALUES (1,'');
    

    And this other template:

    <% for c1 in (select 'Something' as a_varchar_col from dual) loop %>          
       INSERT INTO my_table (process_id, a_varchar_col)
            VALUES (1,'<%= NVL(c1.a_varchar_col, null) %>');
    <% end loop; %> 

    output this

       INSERT INTO my_table (process_id, a_varchar_col)
            VALUES (1,'Something');
    

    I see everything right. What are you trying to do exactly?

  2. MikeKutz commented on Mar 10, 2019

    @MikeKutz
    Collaborator

    @xendren

    For Oracle, NULL and '' are the same thing.

    If you want the word NULL, then you need to do a CASE statement. IF ... END IF doesn't work within <%= .. %> tags.

    Example template:

    <% for c1 in (select NULL as a_varchar_col 
                                 from dual
                                 union all
                                 select 'Something' from dual) loop %>          
       INSERT INTO my_table (process_id, a_varchar_col)
            VALUES (1, <%= case
                               when c1.a_varchar_col is not null
                               then
                                 '''' || c1.a_varchar_col || ''''
                               else 'NULL'
                             end %>);
           <% end loop; %>

    Produces output:

       INSERT INTO my_table (process_id, a_varchar_col)
            VALUES (1, NULL);
       INSERT INTO my_table (process_id, a_varchar_col)
            VALUES (1, 'Something');
    
  3. xendren commented on Mar 10, 2019

    @xendren
    Author

    Thank you both for the quick responses. Oracle does treat '' the same as NULL for VARCHAR, but not for CHAR, so our coding standards prohibit the use of '' for NULL. My apologies, I should have stated that in the initial post.

    Mike solved the problem for me. I had tried the case statement inside the expression clause, but it looks like I did not have the correct number of single-quotes to make it work. Using Mike's example with 4 single-quotes surrounding the value did work as expected.

    Thanks again for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions