create table 表(id varchar(6) default(dbo.f_xqID())CREATE function dbo.f_xqID()
returns varchar(6)
as
begin
declare @maxID int,@newID varchar(20),@strLen int,@ssLen int,@listLen int
set @ssLen=6
if not exists(select 1 from 表 where id='000001')
set @newID='000001'
else
begin
select @maxID=max(cast(id as int)) from 表
set @maxID=@maxID+1
set @newID=cast(@maxID as varchar(6))
set @strLen=len(@newID)
set @listLen=@ssLen-@strLen
while @listLen>0
begin
set @newID='0'+@newID
set @listLen=@listLen-1
end
end
return (@newID)
end
returns varchar(6)
as
begin
declare @maxID int,@newID varchar(20),@strLen int,@ssLen int,@listLen int
set @ssLen=6
if not exists(select 1 from 表 where id='000001')
set @newID='000001'
else
begin
select @maxID=max(cast(id as int)) from 表
set @maxID=@maxID+1
set @newID=cast(@maxID as varchar(6))
set @strLen=len(@newID)
set @listLen=@ssLen-@strLen
while @listLen>0
begin
set @newID='0'+@newID
set @listLen=@listLen-1
end
end
return (@newID)
end
解决方案 »
- where 如何写
- SQL Serve出现两个用户,有一个还启动不了
- 一条SQL语句读取两个分类表~
- 请问存储过程里的两条默认语句的意义?
- 从服务器接受结果时发生传输级错误provider:TCP提供程序,error:0指定网络名不可用
- MS-SQL Server 2000 如何自动备份?
- 数据库中的一个整型字段值为NULL,将这个值取出放到整型变量中,通过整型变量输出到界面表格中,我怎么才能通过整型变量判断数据库中得值
- SQL2008建表后,management studio左边看不到表,刷新也没用
- 紧急求救!我使用聚合函数时候遇到的问题!
- 如何在SQL Server 2000中创建链接服务器?
- 再问inner join和left join
- 为什么查询大的数据量的时候用游标比较快?
create function getmaxcode
return varchar(5)
AS
declare @result varchar(5)
select @result=isnull(max(no),1)+1 from yourtable--编号所在的表
return right('0000'+@result,5)
--再在需要设定的字段的默认值中填上“getmaxcode()”
create table test(id varchar(18), --流水号,日期(8位)+时间(4位)+流水号(4位)
name varchar(10) --其他字段
)go
--创建生成流水号的触发器
create trigger t_insert on test
INSTEAD OF insert
as
declare @id varchar(18),@id1 int,@head varchar(12)
select * into #tb from inserted
set @head=convert(varchar,getdate(),112)+replace(convert(varchar(5),getdate(),108),':','')
select @id=max(id) from test where id like @head+'%'
if @id is null
set @id1=0
else
set @id1=cast(substring(@id,13,4) as int)
update #tb set @id1=@id1+1
,id=@head+right('0000'+cast(@id1 as varchar),4)
insert into test select * from #tb
go
--插入数据,进行测试
insert into test(name)
select 'aa'
union all select 'bb'
union all select 'cc'--修改系统时间,再插入数据测试一次
insert into test(name)
select 'aa'
union all select 'bb'
union all select 'cc'--显示测试结果
select * from test
--删除测试环境
drop table test/*--测试结果
id name
------------------ ----------
2004022720430001 aa
2004022720430002 bb
2004022720430003 cc
2004022720430004 aa
2004022720430005 bb
2004022720430006 cc(所影响的行数为 6 行)
--*/
returns varchar(6)
as
begin
declare @maxID int,@newID varchar(20),@strLen int,@ssLen int,@listLen int
set @ssLen=6
if not exists(select 1 from 表22 where id='000001')
set @newID='000001'
else
begin
select @maxID=max(cast(id as int)) from 表22
set @maxID=@maxID+1
set @newID=cast(@maxID as varchar(6))
set @strLen=len(@newID)
set @listLen=@ssLen-@strLen
while @listLen>0
begin
set @newID='0'+@newID
set @listLen=@listLen-1
end
end
return (@newID)
end--------创建表--------------create table 表22(id varchar(6) default(dbo.f_xqID2()),name varchar(10))----------测试------------insert 表22(name) select 'a'
insert 表22(name) select 'b'
----------测试结果----------- id name
000001 a
000002 b