触发问题!
问候 StackOverflow 社区!
对于这个新项目,我需要编写一个触发器,当将一行插入其中一个表时触发该触发器。
我有一个名为 Questions 的表,其中包含以下字段:
- ID - int
- Datecreated -smalldatetime
- Category_ID - int
- Value - ntext
- Timelimit -smalldatetime
- helper - ntext (可为空)
,另一个表 User_Questions 包含以下字段:
- ID - int
- Question_ID - int
- User_ID - int
- Datecreated - 小日期时间
- 助手 - ntext(可为空)。
现在我想我可以编写一个触发器,从 Questions 表中提取 Datecreated 和 ID 字段,并将它们添加到 Users_Questions 表中的新行中。您能否建议我如何获取 User_ID 字段的值?
我们将不胜感激。
预先非常感谢您!
Greetings StackOverflow community!
For this new project I'm required to write a trigger which fires when a row is inserted into one of the tables.
I have a table called Questions which contains the following fields:
- ID - int
- Datecreated - smalldatetime
- Category_ID - int
- Value - ntext
- Timelimit - smalldatetime
- helper - ntext (nullable)
and another table User_Questions which contains the following fields:
- ID - int
- Question_ID - int
- User_ID - int
- Datecreated - smalldatetime
- helper - ntext (nullable).
Now I think i can write a trigger which extracts the Datecreated and ID fields from Questions table and adds them to a new row in the Users_Questions table. Could you please advice me on how to get the value for the User_ID field?
That would be greatly appreciated.
Thank you very much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,我更喜欢的一种选择是创建一个存储过程,该存储过程采用必要的输入参数,然后将两个输入添加到事务中的两个表中。这样你就可以控制正在发生的事情:(
这假设 SQL Server 2005,并且 ID 字段是 INT IDENTITY - 正确吗?)
众所周知,触发器很难正确使用,它们的扩展性不是很好 - 我会尝试避免触发器,如果永远可能(并不总是可能,但通常是)-
One option that I would prefer in this case would be to create a stored procedure that takes the necessary input parameters, and then does your two inputs into the two tables in a transaction. That way you control what's going on:
(this assumes SQL Server 2005, and the ID fields are INT IDENTITY - correct??)
Triggers are notoriously hard to get right, they don't scale very well - I would try to avoid triggers if ever possible (not always possible, but often, it is) –