--函数,实现 AA->AB,AZ->BA create function f_char( @str varchar(10) )returns varchar(10) as begin return( left(@str,len(@str)-2) +char((ascii(right(@str,1))-64)/26+ascii(right(@str,2))) +char((ascii(right(@str,1))-64)%26+65)) end go--调用 select dbo.f_char('AKBZ'),dbo.f_char('AKBY'),dbo.f_char('AY') go--删除测试 drop function f_char/*--测试结果---------- ---------- ---------- AKCA AKBZ AZ(所影响的行数为 1 行) --*/
不想用到麻烦的计算的话,你可以建一个辅助表: id int , lbl char(2) 1 AA 2 AB ...... 26 AZ 27 BA ......这样,你的T1表中只存数字就行了。需要的时候再从上面的表里查找对应的字符串。
to: victorycyz(中海) 不想再建表,后面的都已经行了, 只是在函数里面,我怎么不能查找表及删除表及更新表!
to: zjcxc(邹建)这个表只存最新的编码,所以更改之后,把最新的放进去。以前的删掉就行了。我只是把它作为一个临时表,存放最新的编码。省得每次要从资料表里去查找最新的编码,如此而已
where NewCode like @参数+'%'
--初步判断就只能这样理解 create proc p_process @参数 varchar(2) as declare @NewCode varchar(10) select @NewCode=@参数 +char((ascii(right(NewCode,1))-64)/26+ascii(right(NewCode,2))) +char((ascii(right(NewCode,1))-64)%26+65) from T1 where NewCode like @参数+'%' if @@rowcount=0 --没有就插入 insert T1(NewCode) values(@参数+'AA') else --有就更新,这样就保持只有一条了 update T1 set NewCode=@NewCode where NewCode like @参数+'%'
to: zjcxc(邹建)好像不太行,我的t1表里只能是最新的一条,四个字符的值。 结果也不对
--晕,今天怎么这么多人说不行,一定要我写测试代码测试证明行才服啊--测试表的 create table T1(NewCode char(4)) go--初步判断就只能这样理解 create proc p_process @参数 varchar(2) as declare @NewCode varchar(10) select @NewCode=@参数 +char((ascii(right(NewCode,1))-64)/26+ascii(right(NewCode,2))) +char((ascii(right(NewCode,1))-64)%26+65) from T1 where NewCode like @参数+'%' if @@rowcount=0 --没有就插入 insert T1(NewCode) values(@参数+'AA') else --有就更新,这样就保持只有一条了 update T1 set NewCode=@NewCode where NewCode like @参数+'%' goexec p_process 'AA' exec p_process '01' exec p_process '01' exec p_process 'BB' exec p_process 'BB' exec p_process 'BB' exec p_process 'AA'select * from T1 go--删除测试 drop table T1 drop proc p_process/*--测试结果NewCode ------- AAAB 01AB BBAC(所影响的行数为 3 行) --*/
------------------------------数据全部删除吗?那是不是意味着表中始终只有一条记录?
如果查到表中的值后两位为ZZ呢?怎么处理?
create function f_char(
@str varchar(10)
)returns varchar(10)
as
begin
return(
left(@str,len(@str)-2)
+char((ascii(right(@str,1))-64)/26+ascii(right(@str,2)))
+char((ascii(right(@str,1))-64)%26+65))
end
go--调用
select dbo.f_char('AKBZ'),dbo.f_char('AKBY'),dbo.f_char('AY')
go--删除测试
drop function f_char/*--测试结果---------- ---------- ----------
AKCA AKBZ AZ(所影响的行数为 1 行)
--*/
id int , lbl char(2)
1 AA
2 AB
......
26 AZ
27 BA
......这样,你的T1表中只存数字就行了。需要的时候再从上面的表里查找对应的字符串。
只是在函数里面,我怎么不能查找表及删除表及更新表!
create proc p_process
@参数 varchar(2)
as
declare @NewCode varchar(10)
select @NewCode=@参数
+char((ascii(right(NewCode,1))-64)/26+ascii(right(NewCode,2)))
+char((ascii(right(NewCode,1))-64)%26+65)
from T1 where NewCode like @参数+'%'
if @@rowcount=0 --没有就插入
insert T1(NewCode) values(@参数+'AA')
else --有就更新,这样就保持只有一条了
update T1 set NewCode=@NewCode where NewCode like @参数+'%'
结果也不对
create table T1(NewCode char(4))
go--初步判断就只能这样理解
create proc p_process
@参数 varchar(2)
as
declare @NewCode varchar(10)
select @NewCode=@参数
+char((ascii(right(NewCode,1))-64)/26+ascii(right(NewCode,2)))
+char((ascii(right(NewCode,1))-64)%26+65)
from T1 where NewCode like @参数+'%'
if @@rowcount=0 --没有就插入
insert T1(NewCode) values(@参数+'AA')
else --有就更新,这样就保持只有一条了
update T1 set NewCode=@NewCode where NewCode like @参数+'%'
goexec p_process 'AA'
exec p_process '01'
exec p_process '01'
exec p_process 'BB'
exec p_process 'BB'
exec p_process 'BB'
exec p_process 'AA'select * from T1
go--删除测试
drop table T1
drop proc p_process/*--测试结果NewCode
-------
AAAB
01AB
BBAC(所影响的行数为 3 行)
--*/