SQL小看点。。。帮我再做一个和这个效果一样的自定义函数,将数据库中的表格student的code自加1

发布时间:2024-11-06 07:34 发布:上海旅游网

问题描述:

create function getnextcode()
--declare @code varchar(20)
returns varchar(20)
as
begin
declare @cmp varchar(20)
declare @a int
declare @b int
declare @c int
declare @new varchar(20)
--set @code='AB0006'
--print @code

declare @code varchar(20)
select @code=code from stud where len(code) = (select max(len (code)) from stud)
--print @code
set @a=1
set @c=1

while @c<100
begin
select @new=substring (@code,@a,1)
if @new not like'[0,1,2,3,4,5,6,7,8,9]'
begin
set @a=@a+1
set @c=@c+1
-- print @a
end
else break
end
select @new=substring (@code,@a,len(@code)-@a+1)
--print @new
select @cmp =@new

select @new = cast(@new as int)
select @new = @new +1
if len(@new)=len(@cmp)+1
begin
select @new = cast(@new as varchar(20))
select @new = substring (@code,1,len(@code)-len(@new)+1)+@new
end
else
begin
select @new = cast(@new as varchar(20))
select @new = substring (@code,1,len(@code)-len(@new))+@new
end
return @new
--完成code加1
end

问题解答:

你不是已经好了吗
还做?改一下名字就行

热点新闻