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 9126827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:08:17+00:00 2026-06-17T07:08:17+00:00

I have been told to create a trigger for inserts on our SQL Server

  • 0

I have been told to create a trigger for inserts on our SQL Server 2000.

I’ve never written a trigger before, and our old server does not appear to have any triggers defined on it.

Following the Triggers in SQL Server tutorial, I have created this trigger that I have not executed yet:

create trigger trgAfterMachine1Insert on Test_Results
after insert
as
  declare @sn varchar(20), @sysID varchar(50),
          @opID varchar(50), @testResult varchar(255)
  select @sn=Serial_Number from inserted
  select @sysID=System_ID from inserted
  select @opID=Op_ID from inserted
  select @testResult=Test_Result from inserted

  exec sp1_AddSnRecord(@sn, @sysID, @opID, @testResult)

  print 'Machine1 After Insert Trigger called AddSnRecord'

go

First, notice that I have written a stored procedure called sp1_AddSnRecord to insert this data into a new table (so I do not mess up the existing table). I certainly hope a stored procedure can be called on a trigger, because it performs data validation and enumeration on the data before inserting anything into the other tables.

I really don’t see a way in SQL Server 2000 to test to see if this will work, and I’m a bit nervous about just hitting that Execute button in Management Studio.

So, I’ve been looking at this for a while and trying to read up on some other SO techniques.

From Aaron Bertrand‘s example HERE, it looks like I can combine all of my select calls into one line:

create trigger trgAfterMachine1Insert on Test_Results
after insert

as

  declare @sn varchar(20), @sysID varchar(50),
          @opID varchar(50), @testResult varchar(255)

  select @sn=Serial_Number, @sysID=System_ID,
         @opID=Op_ID, @testResult=Test_Result 
  from inserted

  exec sp1_AddSnRecord(@sn, @sysID, @opID, @testResult)

  print 'Machine1 After Insert Trigger called AddSnRecord'

go

Otherwise, I don’t see anything more enlightening anywhere or see anyone asking about techniques to test triggers before creating them.

One of my colleges here at work does more SQL work than I do, but he admits that he has never written triggers. All he was able to tell me was, “Man, if you screw that up, you could cause a lot of problems on the server!” All that did was make me nervous, which is why I am here. (98% of what I do is write C# code for Windows Forms and old Windows Mobile devices).

So, how would I verify that this trigger is valid and will not cause any issues on the Server before creating? I’ve got a local SQL Server Express on my machine, but it is much newer than SQL 2000 and does not have the live data running on it from our Production floor.

If the trigger proves to be faulty afterwards, would I be able to remove it with a simple delete trigger trgAfterMachine1Insert? My search for “delete trigger” seems to have returned mostly triggers for AFTER DELETE.

Thanks in advance.

UPDATE: Including the stored procedure at Martin’s request:

ALTER PROCEDURE [dbo].[sp1_AddSnRecord](
    @serial_Number varchar(20), 
    @system_ID varchar(50), 
    @op_ID varchar(50), 
    @test_Result varchar(255)) as begin
  set NOCOUNT ON;
  declare @sn as VarChar(20);
  set @sn=dbo.fn_ValidSN(@serial_Number);
  if (7<Len(@sn)) begin
    declare @badge varchar(50), @result varchar(50), @sysID varchar(50);
    set @badge=dbo.fn_GetBadge(@op_ID);
    set @result=dbo.fn_GetTestResult(@test_Result);
    set @sysID=dbo.fn_GetSysType(@system_ID);
    if ((0<Len(@badge)) and (0<Len(@result)) and (0<Len(@sysID))) begin
      declare @id int;
      select @id=ID from Serial_Numbers where Serial_Number=@sn;
      if (@id<1) begin -- this serial number has not been entered
        insert into Serial_Numbers (Serial_Number) values (@sn);
        select @id=@@IDENTITY from Serial_Numbers;
      end
      if (0<@id) begin -- now insert into SN_Records
        insert into SN_Records (SN_ID, SYS_ID, OP_ID, Date_Time, Test_Result)
          values (@id, @sysID, @badge, GetDate(), @result);
      end
    end
  end
end
  • 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-06-17T07:08:19+00:00Added an answer on June 17, 2026 at 7:08 am

    So, let me re-phrase what you are saying:

    • you have no experience writing triggers
    • there is no one else in the company with experience to write triggers
    • you only have a production environment and no other place to test you code
    • management is telling you to get this done by tonight

    This is a sure recipe for disaster.

    First you need to stand up against requests where your only option is to fail. Tell management that their data is too important to do something like this without proper testing.

    Then get an appropriate testing environment. If your company is a MSDN subscriber you will have access to a copy of SQL Server 2000 Developer Edition that you can install on you laptop or better in some virtual machine.

    While you are waiting for that install read about professional behavior in software development. Start with http://en.wikipedia.org/wiki/Robert_Cecil_Martin and then go to software craftsmanship.


    But, as I know that won’t happen tonight, you can do this in the meantime:

    1) Create a new database on the production server

    2) Copy the table in question: SELECT TOP(10) * INTO NewDb.dbo.Table FROM OldDb.dbo.Table;
    You don’t need more data as this is an insert trigger

    3) Copy the other tables you need in the same way

    4) apply your trigger to the table in NewDb

    5) test

    6) fix and go back to 5

    7) if you are satisfied, copy the trigger to OldDb

    Some things to consider:

    • Make sure you test inserts of more than one row
    • Don’t call a procedure in the trigger. Not that that is wrong in it self, but you won’t be able to get multi row inserts working with it
    • do not ever use @@IDENTITY. That’s an order. (reasons and solutions are here: http://sqlity.net/en/351/identity-crisis/ )

    After all that start looking into TDD in the database here: tSQLt.org
    (Most ideas work in SQL 2000, however the framework does not.)

    Hope that helps.

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

Sidebar

Related Questions

I have been told to create forum functionality in our website..I have done it
In our company's coding standard, we have been told to be aware of the
I have previously been told that I should always use Randomize() before I use
I've been told that if I foreign key two tables, that SQL Server will
I have been told to create a query that will show the top 10
I have been told to create a website using a non standard font. The
I have been told I will be let go at the end of the
i have been told to use 'when' statement to make multiplexer but not use
I have been told to edit this file in Sharepoint Designer: /_layouts/KWizCom_WikiPlus/CreateNew.aspx I found
I have been told that Eclipse is a good tool for programming in certain

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.