如何避免在 Sql 更新触发器中对 Id 进行硬编码 [英] How to avoid hardcoding an Id in a Sql Update Trigger

查看:28
本文介绍了如何避免在 Sql 更新触发器中对 Id 进行硬编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更新触发器,可以将表更改记录到日志表中.

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.

然后我有一个包含三行的 LogType 表,一行用于更新",一行用于插入",另一行用于删除",ID 为 1、2、3.

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.

在更新触发器中,当我插入到日志表时,硬编码 2 的值.但这对我的编码人员来说似乎是一种糟糕的做法.

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.

我能想到的唯一替代方法是在 LogType 表中有一个键"列,然后有更新"的键"然后这将被硬编码到更新的触发器中.

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.

但这有什么好处吗?有没有其他方法可以解决这个问题?作为 C# 程序员,我会使用枚举,但在 Sql 中没有这样的概念.

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.

格雷格

推荐答案

我看不出你提出的问题有什么问题,我使用触发器来维护历史表,但如果是数值,我使用单个字符:'I'、'U' 或 'D',因为不需要查找/枚举来记住哪个值是哪个操作.

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.

这篇关于如何避免在 Sql 更新触发器中对 Id 进行硬编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆