我想实现这样的效果

jsidiot 2003-05-14 03:51:26
当在数据库中有新的数据insert的时候
我想用数据库来控制maxid就象使用identity类型一样
这个id是这样的
051400002
05是现在的五月
14是今天的日期
00002是本月的第二个编号
一共是九位数,不够用零代替
我现在如何用sql得到下一个051400003的编号呢
因为我本来是用程序控制的,但是当多用户使用的时候。会发生数据的重复,
我的意思不知道你们懂了没有
谢谢
...全文
77 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsidiot 2003-05-16
  • 打赏
  • 举报
回复
不好意思
由于时间比较急
我现在无心研究上面的代码,只有以后在看了。而且我无法调用这个存储过程,因为表中各列在同时插入的时候如何实现我不是很清楚。谢谢楼上的回答。
我上面的过程已经实现了自动生成编号
我现在想知道如果在insert的时候该列为空则触发该时间
否则不出发该事件的写法
leimin 2003-05-16
  • 打赏
  • 举报
回复
drop proc usp_getnewid
go
CREATE proc usp_getnewid
@newdate datetime =null,
@varNewID varchar(9) output
as
set nocount on
begin
Declare @theM char(2),
@theD char(2),
@varMaxID char(10)


Declare @rc int

select @rc=0

if isnull(@newdate,0)=0
select @newdate=getdate()

if isdate(@newdate)=0
begin
select @rc=-2
return @rc
end

--get the maxid from the test table
if @rc=0
begin
select @varMaxID=(select isnull(max(cast(substring(number_order,5,5) as int)),'0')+1
from test with (rowlock)
where cast(substring(number_order,1,2) as int)=month(@NewDate))

select @rc=@@error
if @rc<>0 or isnull(@varMaxID,0)=0
begin
select @rc=-2
return @rc
end

else
select @varMaxID='00001'
end

select @varMaxID=left('00000',5-len(@varMaxID))+@varMaxID

select @theM=Month(@Newdate)
select @theD=Day(@NewDate)
select @theM=left('00',2-Len(@theM))+@theM
select @theD=left('00',2-Len(@theD))+@theD
select @varNewID=@theM+@theD+@varMaxID

end

run the stored procedure:
declare @date datetime
declare @newid varchar(9)


exec usp_getnewid '2003-4-1',@varnewid=@newid output

print @newid
jsidiot 2003-05-16
  • 打赏
  • 举报
回复
我再推一下哦
joygxd 2003-05-16
  • 打赏
  • 举报
回复
declare @date datetime
set @date=getdate()
set @date=substring(@date,7,4)+left(@date,2)+substring(@date,4,2)
select cast(max(id) as int)+1
from table
where left(id,8)=@date
liuyun2003 2003-05-16
  • 打赏
  • 举报
回复
如果要判断某列是否为空。在触发器上加入这段代码。
………………
declare @variable_name type
select @variable_name=你要判断的列 from inserted
if @variable_name is null
begin
处理代码
end
else
begin
处理代码
end
………………
wuqiuzhi 2003-05-16
  • 打赏
  • 举报
回复
1:我先前比较忙,没有仔细验证,原来的程序修改如下(经验证,没有错误)
declare @m int,@d int
select @m=month(getdate()),@d=day(getdate())
declare @Str1 varchar(20),@Str2 varchar(20)
set @Str1=case when @m>9 then cast(@m as varchar(2))
else '0'+cast(@m as char(1)) end +
case when @d>9 then cast(@d as varchar(2))
else '0'+cast(@d as char(1)) end
--然后找出这天的最大id号
declare @id int
set @Str2='%'+@Str1+'%'
select @id=max(cast(substring(rtrim(lTrim(DateId)),5,5) as int))
from Table_DateID where DateId like @Str2
--设置现在的id号
set @id=isnull(@id,0)+1
--把新的id号转化成字符串
declare @i int,@idpref int
set @i=0
set @Str2=''
while @i<5
begin
set @idpref=(@id/power(10,@i)) % 10
set @i=@i+1
set @Str2=cast(@idpref as char(1))+isnull(@Str2,'')
end
set @Str1=@Str1+@Str2
select @Str1
2:在insert的时候该列为空则触发该时间
否则不出发该事件的写法:
在过程中加入一个判断
if field is not null--不为空
return--退出本过程
if field is null--为空
yourproc
jsidiot 2003-05-15
  • 打赏
  • 举报
回复
我现在实现了这个过程
如下
Declare @theM char(2),@theD char(2),@theN char(5),@maxid char(10),@xxx char(9)
set @maxid=(select isnull(max(cast(substring(number_order,5,5) as int)),'0')+1 from test where
cast(substring(number_order,1,2) as int)=month(getdate()))

if not exists (select max(cast(substring(number_order,5,5) as int)) from test where
cast(substring(number_order,1,2) as int)=month(getdate()))
set @maxid='00001'
else
-- print '000000'
set @maxid=left('00000',5-len(@maxid))+@maxid
print @maxid
set @theM=Month(GetDate())
set @theD=Day(GetDate())
set @theM=left('00',2-Len(@theM))+@theM
set @theD=left('00',2-Len(@theD))+@theD
set @xxx=@theM+@theD+@maxid
print @xxx
insert into test (number_order) values (@xxx)
但是我又遇到一个新得问题
就是我现在需要得到得效果是
当INSERT得number_order字段得数值为空得时候触发该事件,不然不触发该事件
谢谢
对了。为什么这几天都没有看到大力啊
jsidiot 2003-05-15
  • 打赏
  • 举报
回复
再推一下
我比较急真的
a1n1 2003-05-15
  • 打赏
  • 举报
回复
可以,但要保证id不要重复。
jsidiot 2003-05-15
  • 打赏
  • 举报
回复
@theM=left('00',2-Len(@theM))+@theM 这句话老是出错

@theD=left('00',2-Len(@theD))+@theD 这句话老是出错


能维一确定你的插入记录的条件(使用Inserted) 这句话不懂是什么意思一
我用select max(id)可以吗?
nik_Amis 2003-05-15
  • 打赏
  • 举报
回复
up
jsidiot 2003-05-15
  • 打赏
  • 举报
回复
为什么case 语句总是出现问题啊/
请教高手
triout 2003-05-15
  • 打赏
  • 举报
回复
Create Trigger InsRow On 你的表名
For Insert
As
Declare @theM char(2),@theD char(2),@theN char(5)
Select @theM=Month(GetDate())
@theM=left('00',2-Len(@theM))+@theM
Select @theD=Day(GetDate())
@theD=left('00',2-Len(@theD))+@theD
Declare cursor cursor1 for select count(*)+1 from 你的表名 where left(maxid,4)=@theM+@theD
cursor1.open
fetch cursor1 into @theN
cursor1.close
@theN=left('00000',5-Len(@theN))+@theN
Update 你的表名 Set MaxID=@theM+@theD+@theN Where 能维一确定你的插入记录的条件(使用Inserted)
triout 2003-05-15
  • 打赏
  • 举报
回复
创建一个触发器:
Create Trigger InsRow On 你的表名
For Insert
As
Declare @theM char(2),@theD char(2),@theN char(5)
Select @theM=Month(GetDate())
Case Len(@theM)
When 1 Then @theM='0'+@theM
End
Select @theD=Day(GetDate())
Case Len(@theD)
When 1 Then @theD='0'+@theD
End
Declare cursor cursor1 for select count(*)+1 from 你的表名 where left(maxid,4)=@theM+@theD
cursor1.open
fetch cursor1 into @theN
cursor1.close
Case Len(@theN)
When 1 Then @theN='0000'+@theN
When 2 Then @theN='000'+@theN
When 3 Then @theN='00'+@theN
When 4 Then @theN='0'+@theN
End
Update Inserted Set MaxID=@theM+@theD+@theN
jsidiot 2003-05-15
  • 打赏
  • 举报
回复
to wuqiuzhi(孜孜)
你的方法好像不行
有错误
而且没有得到结果
jsidiot 2003-05-14
  • 打赏
  • 举报
回复
先谢过
leimin 2003-05-14
  • 打赏
  • 举报
回复
还是写一个UDF比较方便,已可以通过一个表来读取,不过要注意LOCK的问题,避免重复ID.
wuqiuzhi 2003-05-14
  • 打赏
  • 举报
回复
修改一下:

--设置现在的id号
set @id=@id+1

改为:
set @id=isnull(@id,0)+1
以便适应当天是第一条的情况
wuqiuzhi 2003-05-14
  • 打赏
  • 举报
回复
这样吧,方法可能麻烦:
--先得出日期的字段:
declare @m int,@d int
select @m=month(getdate()),@d=day(getdate())
declare @Str1 char(20),@Str2 char(20)
set @Str1=case when @m>9 then case(@m as char(2))
else '0'+case(@m as char(1)) end +
case when @d>9 then case(@d as char(2))
else '0'+case(@d as char(1)) end
--然后找出这天的最大id号
declare @id int
select @id=max(cast(substring(Fieldname,5,5) as int))
from yourtable where fieldname like @Str1
--设置现在的id号
set @id=@id+1
--把新的id号转化成字符串
declare @i int,@idpref int
set @i=0
while @i<5
begin
set @idpref=(@id/power(10,i))mod 10
set @i=@i+1
set @Str2=cast(@idpref as char(1))+isnull(Str2,'')
end
set @Str1=@Str1+@Str2
--得到的@Str1 就是你期望的字符串了
--祝好运
jsidiot 2003-05-14
  • 打赏
  • 举报
回复
谢谢。我回去试一下
加载更多回复(6)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧