CREATE TRIGGER tg_insert_employees
ON dbo.employees
FOR INSERT
AS 
BEGIN
DECLARE cu CURSOR FOR SELECT bh FROM inserted
DECLARE @bh int,@cnt int
OPEN cu
FETCH cu INTO @bh
WHILE @@fetch_status=0
BEGIN
SELECT @cnt=count(*) FROM dbo.t_employee
WHERE bh=@bh
IF @cnt>0 
BEGIN
   INSERT INTO dbo.t_employee(bh,in_service) VALUES(@bh,1)
END
ELSE
BEGIN
  UPDATE dbo.t_employee SET in_service=1 WHERE bh=@bh
END
FETCH cu INTO @bh
END CLOSE cu
DEALLOCATE cuEND
GO

解决方案 »

  1.   

    IF EXISTS (SELECT name
               FROM   sysobjects
               WHERE  name = 'Update_in_service' AND
                      type = 'TR')
    DROP TRIGGER Update_in_service
    GOCREATE TRIGGER Update_in_service
    ON employee
    FOR INSERT
    AS
    declare @rc int
    select @rc=0
    declare @hd intbegin
    declare @cnt int
    select @hd=hd from insered
    SELECT @cnt=count(*) FROM   in_service
    where hb=@hd 
     if @cnt=1
    begin
    select @hd=hd from insered
    update in_service set in_service=1 where hd=@hd
    end
       else
    begin
    select @hd=hd from inserted
    insert into in_service(hd,in_service)
    values(@hd,1)  
    end
    end
    GO
      

  2.   

    1st Use Cursor , 
    2nd Didn't Use ......我好像在说费话