更新时间:2022-05-20 02:02:05
需要定制,就是这样。下面有一个样本很少你想要什么。
使用SQL Server自定义自动生成的序列 [ ^ ]
Customization needed, that's it. Below one has few samples what do you want.
Custom Auto-Generated Sequences with SQL Server[^]
你可以这样做,假设表名是mytable
You can do it this way suppose the table name is mytable
create table mytable(
id varchar(6) primary key,
name varchar(30),
someint int identity(0,1)
);
为此创建一个触发器它看起来像这样
for this create a trigger which looks something like this
create trigger [dbo].[dummy]
on [dbo].[mytable]
for insert
as
declare @id varchar(6);
declare @someint int;
select @id=id from inserted;
select @someint=someint from inserted;
if(@someint<10)
begin
update mytable
set id='M'+'00'+convert(varchar,@someint) where id=@id
end
else
if(@someint>=10 and @someint<100)
begin
update mytable
set id='M'+'0'+convert(varchar,@someint) where id=@id
end
else
if(@someint>=100 and @someint<1000)
begin
update mytable
set id='M'+convert(varchar,@someint) where id=@id
end
else-- Since the transaction exceeds the limit so rollback the transation
rollback
我认为这必须解决你的问题uestion
I think this must solve your question
我上面讲过的解决方案很好,但我找到了一个更好的方法来处理你的问题,因为我觉得在前一种情况下执行的时间会更多。 />
如上所述,假设我有一个名为mytable的表,就像
The solution i have told above is good but i have found a better way to deal with your problem as i felt that execution time taken in the previous case will be bit more.
As mentioned above suppose i have a table named mytable which is like
create table mytable(
id varchar(6) primary key,
name varchar(30),
someint int identity(0,1)
);
现在为此我创建一个这样的触发器
now for this i create a trigger like this
create trigger [dbo].[dummy1]
on [dbo].[mytable]
for insert
as
declare @id varchar(6);
declare @someint int;
select @id=id from inserted;
select @someint=someint from inserted;
update mytable
set id='M'+right('000'+convert(varchar,@someint),3) where id=@id
此工作正常
This works fine