Search code examples
sqlt-sqltriggerssql-server-2000

Validate new Create Trigger for SQL Server 2000


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

Solution

  • 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.