Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4620214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:31:50+00:00 2026-05-22T02:31:50+00:00

Greetings StackOverflow community! For this new project I’m required to write a trigger which

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:31:50+00:00Added an answer on May 22, 2026 at 2:31 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings to all! This is my first question here on stackoverflow. I have a
Greetings everyone. This is my first question here at stackoverflow so please bear with
Greetings to the stackoverflow community, I am currently following a bioinformatics module as part
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
Greetings all, I have posted this on the MSDN managed news groups as well
Greetings, I need to store the filename of a log into a variable so
Greetings! As part of Ruby learning, I am working on a project that involves
Greetings, I have a complicated scenario to handle. I have a wsdl file which
Greetings, I am changing the width of an element which serves as a bar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.