♣
数据库字段问题
我有一两字段.id(增量+1) noid(要求为0000+id即id为11的时候,noid=0011而不是000011).请问如何实现呢?
· 网友精彩回答:
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
--用触发器来实现自动更新.
--创建测试表
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 王五
- 更多问题:
- · 怎么在ASP中实现暂停功能?
- · 如何将下面这个客户端程序在JBuilderX中运行???
- · 请问:with nocheck 在这里是什么意思啊?????????????????
- · 防毒软件猛于虎!----半夜来骂来了,顺便求解决方案
- · 我对微软出的那套技术内幕非常不欣赏,反而喜欢wrox的书
- · 防毒软件猛于虎!----半夜来骂来了,顺便求解决方案
- · 50分
- · 我想买个移动硬盘,不知道哪款好,请教大家二个问题! 谢谢!
- · 100分求 :如何检测一个URL是否可用??
- · 虚拟路径的表示问题?
- · TAudioMixer v1.15 这个东西在XP下回出错?
- · 用JavaMail发邮件却在目标邮箱找不到
- · Access2003打不开数据表提示出现未知错误
- · 如何将excel表格做成的数据表转化为数据库表?
- · 类似于这种网站是怎么做出来的,有没有源程序下载。能不能告诉我,谢谢!!!
- · 为什么protected 定义后的有些内容不能在页面中显示出来

