数据库字段问题

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

我有一两字段.id(增量+1)   noid(要求为0000+id即id为11的时候,noid=0011而不是000011).请问如何实现呢?

· 网友精彩回答:

发表者:lim_zjg

create   table   tb1  
  (  
    id   int   identity(1,1)   primary   key,  
    noid   varchar(20)  
   
  )  
   
  reate   trigger   autoadd  
  on   tb1  
  for   insert  
  as  
    declare   @noid   varchar(20),@id   int,@strid   varchar(10)  
    set   @id=(select   id   from   inserted)  
    set   @strid=cast(@id   as   varchar(10))  
    set   @noid=substring(0000,1,4-len(@strid))   +   @strid  
    print   @noid  
    update   tb1   set   noid=@noid   where   id=@id    
 

发表者:mschen

--用触发器来实现自动更新.  
   
  --创建测试表  
  create   table   test(id   int   identity(1,1),noid   varchar(4),[name]   varchar(100))  
  --创建触发器  
  create   trigger   dbo.tri_test  
  on   test   for   insert  
  as  
  update   test  
  set   noid=right(10000+t.id,4)  
  from   test   t   join   inserted   i  
  on   t.id=i.id  
   
  go  
  --插入数据  
  insert   test([name])  
  select   张三  
  union   all   select   李四  
  union   all   select   王五  
  --结果  
  /*  
  id                     noid   name                                                                                                                                                                                                    
   
  1                       0002   张三  
  2                       0003   李四  
  3                       0004   王五  
   
   
   
 

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved