How to avoid hardcoding an Id in a Sql Update Trigger

寵の児 提交于 2019-12-14 03:26:23

问题


I have an Update Trigger that logs table changes to a log table.

In the log table, there is a column which stores the type of change made, ie, an Insert or an Update.

I then have a LogType table with three rows, one row for 'Update' and one for 'Insert' and one for 'Deleted' with Id's of 1, 2, 3.

In the update trigger, and hardcode the value of 2 when I insert into the log table. But this seems to the coder in me a poor practice.

The only alternate I can think of to this is having a "Key" column in the LogType table, and then having the 'Key' of 'UPDATE' Then this would get hardcoded into the Updated Trigger.

But is there any advantage to this? Is there any other way to resolve this? As a C# programmer, I would use an enum, but this is no such concept in Sql.

Greg


回答1:


I don't see a problem in what you are proposing, I use triggers to maintain a history table but instead if a numerical value I use a single char: 'I', 'U' or 'D' as then there's no need to have a lookup/enum to remember which value is which operation.



来源:https://stackoverflow.com/questions/19078240/how-to-avoid-hardcoding-an-id-in-a-sql-update-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!