触发问题!

发布于 2024-11-07 11:36:28 字数 625 浏览 0 评论 0原文

问候 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2024-11-14 11:36:28

在这种情况下,我更喜欢的一种选择是创建一个存储过程,该存储过程采用必要的输入参数,然后将两个输入添加到事务中的两个表中。这样你就可以控制正在发生的事情:(

这假设 SQL Server 2005,并且 ID 字段是 INT IDENTITY - 正确吗?)

 CREATE PROCEDURE dbo.InsertQuestion
    @Datecreated SMALLDATETIME, @Category_ID INT,
    @Question NVARCHAR(MAX), @Timelimit SMALLDATETIME,
    @helper NVARCHAR(MAX)
 AS BEGIN
    -- start transaction and a TRY..CATCH block
    BEGIN TRANSACTION
    BEGIN TRY
        -- insert values into "Questions" table
        INSERT INTO 
           dbo.Questions(DateCreated, Category_ID, Question, TimeLimit, Helper)
        VALUES
            (@DateCreated, @Category_ID, @Question, @TimeLimit, @Helper)

        -- retrieve the ID of the newly inserted row
        DECLARE @QuestionID INT
        SET @QuestionID = SCOPE_IDENTITY()

        -- determine the user ID from SQL Server
        DECLARE @UserID INT
        SET @UserID = SUSER_ID() 

        -- insert values into "User_Questions" table
        INSERT INTO 
           dbo.UserQuestions(QuestionID, UserID, DateCreated, Helper)
        VALUES
            (@QuestionID, @UserID, @DateCreated, @Helper)

        -- commit transaction, if everything went well
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
       -- handle your error, e.g. by logging to a table or something.....
       ROLLBACK TRANSACTION
    END CATCH
 END

众所周知,触发器很难正确使用,它们的扩展性不是很好 - 我会尝试避免触发器,如果永远可能(并不总是可能,但通常是)-

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??)

 CREATE PROCEDURE dbo.InsertQuestion
    @Datecreated SMALLDATETIME, @Category_ID INT,
    @Question NVARCHAR(MAX), @Timelimit SMALLDATETIME,
    @helper NVARCHAR(MAX)
 AS BEGIN
    -- start transaction and a TRY..CATCH block
    BEGIN TRANSACTION
    BEGIN TRY
        -- insert values into "Questions" table
        INSERT INTO 
           dbo.Questions(DateCreated, Category_ID, Question, TimeLimit, Helper)
        VALUES
            (@DateCreated, @Category_ID, @Question, @TimeLimit, @Helper)

        -- retrieve the ID of the newly inserted row
        DECLARE @QuestionID INT
        SET @QuestionID = SCOPE_IDENTITY()

        -- determine the user ID from SQL Server
        DECLARE @UserID INT
        SET @UserID = SUSER_ID() 

        -- insert values into "User_Questions" table
        INSERT INTO 
           dbo.UserQuestions(QuestionID, UserID, DateCreated, Helper)
        VALUES
            (@QuestionID, @UserID, @DateCreated, @Helper)

        -- commit transaction, if everything went well
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
       -- handle your error, e.g. by logging to a table or something.....
       ROLLBACK TRANSACTION
    END CATCH
 END

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) –

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文