views:

50

answers:

3

I have a column that I need to be able to guarantee never gets set to anything other than "N" - I thought a trigger would be the perfect solution for this, but I can't seem to figure out how to make it so that anytime the column gets set to something other than "N" I reset it back to "N"

Any pointers?

EDIT: I wouldn't want to do a constraint because the application that will potentially change it to Y is outside of my control and I don't want it to be getting errors when it sets it to Y, I just want to passively set it back to N without fanfare.

+2  A: 

I would recommend a column constraint instead of a trigger. Easier to track down and debug. But I assume that you mean it could never be set to any value other than N or null, right? If not, why bother storing it if the value is always 'N'?

MJB
The application is out of our control and we can only customize workflow through the database so for our purposes we want it to _always_ be N, and nothing else, not even null.
Jose Jose
OK. Against my better judgment, but I guess you could do a trigger that sets :new.AnnoyingColumn = 'N' every time a record is updated or inserted. Sounds really strange though, and I can't imagine what kind of trouble that would cause. What difficulty are you having?
MJB
It is a long story, it is basically a huge propietary piece of software we use and have absolutely no control over the code that makes it run. For our company we don't ever want this particular column to be set to Y but the application doesn't give us this option so we're just trying to enforce it this way. I have unfortunately arrived at a trigger that does it, my question is now how to _write_ said trigger as I don't have much experience in the matter.
Jose Jose
OK, I will show you a sample. I have to do it as an answer though, instead of a comment, because formatting sucks in comments.
MJB
A: 

The easiest way (if the option is open to you to create views) is to provide a view on your table, and set the value of the column in question to a static value of "N" within the view - i.e. persist the value in the view.

davek
The application that is outside of my control makes decisions based on the value of this column, so making a view that has it set to 'N' always will do me no good because I can't make the application use that view.
Jose Jose
is it out of the question to name your view to the name of the table, and to rename your table to something else? Your app doesn't need to know if it is looking at a table or a view under the covers.
davek
Although that is an option, we would much rather just do a trigger that does it.
Jose Jose
+1  A: 

Something like this perhaps. I know it is a bad idea, and it will come back to haunt you. Also, I have not done Oracle in a few years, but this should get you started:

create or replace trigger trg_special_col before update on AnnoyingTable
for each row
begin
  :new.special_col := 'N';
end;
MJB
Similar for before insert
Gary
Trust me, it is the only way to do what I want to do. The way my office works it will be fine. Thanks.
Jose Jose
@Gary - thanks for making my answer more complete.
MJB