新贴求解
现在有表tab,里边是产品检验记录,有初检和复检的情况,也就是说同一个样品号有可能检验两次,但是一个产品检验多次的情况是这样的:如果样品号是T07-001,那么第一次检验的样品录入时的样品号xuhao是T07-001-1,第二次检验的是T07-001-2,但为了统计方便我又认为增加了一个字段,将T07-001-1和T07-001-2都统一成了T07-001,也就是里边的yanpinhao(这个是由触发器根据xuhao自动生成的),也就是有一个初检标记字段flag,是为了统计需要而增加的,默认的情况是 'Y ',也就是都为初检记录,但如果同一个样品号有初检记录时,新增加的同一个样品号的记录的flag就要设为 'N ',我想用触发器实现 
 简化后的表结构及数据如下: 
 drop   table   tab 
 CREATE   TABLE   [dbo].[tab]   ( 
 [riqi]   [datetime]   , 
 [xuhao]   [char]   (10)   , 
 [yangpinhao]   [char]   (9)   , 
 [flag]   [char]   (1)   default    'Y ', 
 [panding]   [char]   (6)   )    
 我生成yangpinhao的触发器是这样的: 
 update   tab   set   yangpinhao=substring(a.xuhao,1,charindex( '- ',a.xuhao)-1)+ '- '+substring(a.xuhao, 
 charindex( '- ',a.xuhao)+1, 
 case   when   charindex( '- ',substring(a.xuhao,charindex( '- ',a.xuhao)+1,5))> 0 
    then   charindex( '- ',substring(a.xuhao,charindex( '- ',a.xuhao)+1,5))-1   else   5   end) 
    from   inserted   a,tab   d 
 where   a.riqi=d.riqi      
 测试数据如下: 
 insert         into         tab    
                                              select       '2007-02-01   10:45:20 ', 'T07-003-2 ', ' ', ' ', '合格 '                
    union      all         select          '2007-02-01   10:48:20 ', 'T07-002-1 ', ' ', ' ',    '不合格 ' 
       union      all         select          '2007-02-01   10:55:20 ', 'T07-002-2 ', ' ', ' ',    '合格 ' 
       union      all         select          '2007-02-01   11:25:20 ', 'T07-003-1 ', ' ', ' ',    '不合格 ' 
------解决方案--------------------感觉思路不对,应该在插入前判断是否存在xuhao如果存在插入时直接改为N 
 可写一插入的存储过程。 
 CREATE PROCEDURE prtest 
 	-- Add the parameters for the stored procedure here 
 	@riqi datetime,  
 	@xuhao varchar(50), 
 	@panding varchar(50) 
 AS 
 BEGIN 
 	-- SET NOCOUNT ON added to prevent extra result sets from 
 	-- interfering with SELECT statements. 
 	SET NOCOUNT ON;   
     DECLARE @CNT INT 
 	SELECT @CNT=COUNT(1) FROM [dbo].[tab] WHERE xuhao=@xuhao 
     IF @CNT> 0 
        insert   into   tab(riqi,xuhao,panding,[flag]) VALUES @riqi,@xuhao,@panding, 'N ' 
     ELSE 
        insert   into   tab(riqi,xuhao,panding) VALUES @riqi,@xuhao,@panding 
     END 
 END
------解决方案--------------------只需要將我給的觸發器代碼加在你的觸發器裡面就可以的。   
 drop table tab 
 CREATE TABLE [dbo].[tab] ( 
 [riqi] [datetime] , 
 [xuhao] [char] (10) , 
 [yangpinhao] [char] (9) , 
 [flag] [char] (1) default  'Y ', 
 [panding] [char] (6) )  
 GO 
 Create Trigger TR_Tab 
 On Tab 
 After Insert 
 As 
 	update tab set yangpinhao=substring(a.xuhao,1,charindex( '- ',a.xuhao)-1)+ '- '+substring(a.xuhao, 
 	charindex( '- ',a.xuhao)+1, 
 	case when charindex( '- ',substring(a.xuhao,charindex( '- ',a.xuhao)+1,5))> 0 
 	 then charindex( '- ',substring(a.xuhao,charindex( '- ',a.xuhao)+1,5))-1 else 5 end) 
 	 from inserted a,tab d 
 	where a.