我可以使用更新触发器在外键字段上进行部分级联反应吗?
挑战是,当我更改名称时,我想要的一家零售店 sales 表:
retailname 保持删除名称(我的意思是更改名称)用于记录随着前几个月的销售日期和 retailname ,将是本月以来的新插入名称。这样做的原因是,当我将来会搜索销售表时,当时我会以零售的名称而不是新名称,也是出于其他一些原因。
我想到在表 myretails 上使用而不是更新触发器来重复记录并更改重复记录上的零售名称值为新的RetailName,而不是在用户尝试更新然后使用同一触发器时进行更新通过销售表循环以使外国密钥 retailname 更改为新创建的记录,其销售日期=本月,但不幸的是我无法编写T-SQL为此。 。 。还要将一个零售业的两个名称与两个零售商店相关联,我想到要添加一个字段,该字段将包含将2个记录分类在一起的值。
有人可以帮助解决这个问题吗?
nb:不要怀疑为什么我将零售名称用作PK而不是零售,这是因为某些原因是这样的。
The Challenge is that, when I change the name of One of My Retail Shop I want for the Sales Table:
RetailName to stay the Deleted name (I mean the Changed Name) for records with previous months Sales Date and RetailName to be the New Inserted Name for Records from this Month Onward. The reason for doing this is that when I will be searching Sales Table in the future I will be doing so by the Name of the Retail at that time not the new name and also for some other reasons.
I thought of Using an INSTEAD OF UPDATE trigger on table MyRetails to Duplicate the Record and change RetailName value on the duplicated record to the New RetailName instead of Updating when a user tries to Update it then using the same trigger to Loop through Sales table so as to change the foreign Key RetailName to the newly created One for those records whose Sale Date = This Month, But unfortunately I was Unable to write the T-SQL for that . . . Also to relate the 2 Names for the One Retail and not to be confused as two Retail Shops I thought of adding a field which will contain values that will categorize the 2 Records together.
Can Somebody help to tackle this problem?
NB: Don't wonder why I Used RetailName As PK instead of RetailID, it was meant to be like that for some reasons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将为您提供答案,但首先让我澄清一下:
您选择的零售名称而不是某些ID的原因是错误 ,您可能会在将来为此选择付出高昂的代价< /strong>。
但是人们有时需要学习艰难的方式,所以这里是:
您的想法(除了早期的观点)是合理的。但是,无需触发而不是触发;后触发器将正常工作。
添加一个字段来连接它们似乎是听起来的,让我们称它们为[myretailsgroup]并填充它:
扳机应该看起来像这样:
I will provide an answer for you, but let me first clarify:
The reasons you have chosen RetailName instead of some ID are wrong, and you probably will pay dearly in the future for that choice.
But people need to learn the hard way sometimes, so here goes:
Your thoughts (apart from the earlier points) are sound. No need for an instead-of trigger, though; an AFTER trigger will work fine.
Adding a field to connect them seems sound, let's call them [MyRetailsGroup] and populate it:
The trigger sould look something like this: