You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 (selectNULLas 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.
<% for c1 in (selectnullas 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?
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 (selectNULLas a_varchar_col
from dual
union allselect'Something'from dual) loop %>INSERT INTO my_table (process_id, a_varchar_col)
VALUES (1, <%= case
when c1.a_varchar_colis 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');
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.
Activity
osalvador commentedon Mar 10, 2019
Hi @xendren ,
This template:
Generate this output:
And this other template:
output this
I see everything right. What are you trying to do exactly?
MikeKutz commentedon Mar 10, 2019
@xendren
For Oracle,
NULLand''are the same thing.If you want the word
NULL, then you need to do aCASEstatement.IF ... END IFdoesn't work within<%= .. %>tags.Example template:
Produces output:
xendren commentedon Mar 10, 2019
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.