在 UPDATE 触发器中 - 获取不带列名称的主键
我需要在不知道主键列名称的情况下更新 DateModified 列。
基本上,我有一个像这样的简单 UPDATE 触发器:
CREATE TRIGGER updated_SCHEMA_TABLE
ON [SCHEMA].[TABLE]
AFTER UPDATE AS
BEGIN
SET NOCOUNT ON;
UPDATE [SCHEMA].[TABLE]
SET DateModified = getdate()
WHERE [PRIMARYKEY]
IN (SELECT [PRIMARYKEY]
FROM Inserted)
END
但不知道主键的列名称,因为触发器将以编程方式生成(请参阅此问题了解原因)。
这可能吗?
I need to update the DateModified Column without knowing the name of the Primary Key Column.
Basically, I've got a plain-jane UPDATE trigger like this:
CREATE TRIGGER updated_SCHEMA_TABLE
ON [SCHEMA].[TABLE]
AFTER UPDATE AS
BEGIN
SET NOCOUNT ON;
UPDATE [SCHEMA].[TABLE]
SET DateModified = getdate()
WHERE [PRIMARYKEY]
IN (SELECT [PRIMARYKEY]
FROM Inserted)
END
but won't know the primary key's column name because the trigger will be generated programmatically (see this question as to why).
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,也许我把这部分作为上一个问题中的“练习”有点不公平。
这适用于具有单列 PK 的表。从这些开始,然后返回并通过复合 PK 手动调整这些可能是最简单的。
OK, perhaps I was a bit unfair leaving this part as an "exercise" in the previous question.
This would work for tables with a single column PK. It might be easiest to start with these and then go back and manually adjust those with a composite PK.
如果您能够从标识列构建所有主键:
否则,您将必须使用所有 sys 表来查看索引(干得好,乔)。
If you were able to build all of your primary keys from identity columns:
otherwise, you will have to look through the indexes using all of the sys tables (good work Joe).
好吧,我只是快速浏览了一些系统视图,但没有看到任何内容告诉您每个表的主键是什么。你可能只需要手工完成即可。
well, i just took a quick gander through some of the system views, and i'm not seeing anything that tells you what the primary keys are for each table. you just might have to do it by hand.