I am attempting to write a generic trigger that can inspect each tuple that is to be inserted into the database. In this inspection I want to replace characters that are of interest to me, such as the curly quotation mark (MS word) with the regular quotation mark (“). Since I do not want to have to write two replace calls for each column that stores data that the user supplies, nor do I want to do this in the application for a myriad of reasons. pseudocode as follows:
create or replace trigger changeChars
before insert
on @tableName
reference new as var
for each row
Begin
loop
:var.@column := replace(:var.@column,'”', '"');
end
Why do you want to replace these characters? A lot of folks have problems with characters like the Microsoft curly quotes because those characters are not supported by their database character set which, by default, causes character set conversion to replace those characters with replacement characters like ‘?’. If that is the problem that you’re trying to solve, database code can’t help because the character set conversion happens at the network layer before the data even gets to the database. Potentially, you could use Oracle Locale Builder to build a custom locale for your client machines that would allow you to specify different replacement characters (i.e. double-quotes rather than Microsoft curly quotes).
If your problem is not character set related, there is no way to create this sort of dynamic trigger. You could write a bit of dynamic SQL that created a trigger for every table that called
REPLACEon everyVARCHAR2column in the table. Of course, you’d need to maintain the trigger every time an additionalVARCHAR2column was added (either by changing the trigger or re-running the PL/SQL block).Untested, and I’m sure the DDL generated isn’t correct, but you’d want something like