这是我试图以这种格式插入到TID列中的表Task中的代码" ADDDMMYYYYxxxx"。输出值为NULL
create table Task
(
TID nvarchar(50) default 'QA201712120000'
);
go
create trigger insertTask
on Task
after insert as
begin
update Task set TID=
'QA' + REPLACE(CONVERT(CHAR(10), GETDATE(), 103), '/', '') +
RIGHT( '000' + CAST(SCOPE_IDENTITY() AS VARCHAR(4)) , 4 )
end
insert into Task default values;
答案 0 :(得分:0)
请尝试下面的示例代码。
create table Task
(
ID int not null identity (1,1) primary key,
TID nvarchar(50) default 'QA201712120000'
);
go
create trigger insertTask
on Task
after insert as
begin
update Task
set TID = 'QA' + REPLACE(CONVERT(CHAR(10), GETDATE(), 103), '/', '')
+ RIGHT( '000' + CAST(inserted.ID AS VARCHAR(4)), 4)
from Task
join inserted on inserted.ID = Task.ID;
end;
go
insert into Task default values;
insert into Task default values;
insert into Task default values;
select * from Task;
此解决方案的特点:
答案 1 :(得分:0)
创建触发器[dbo]。[Tr_GenerateRegistrationNo]开启[dbo]。[T_Registration] 插入后 AS
DECLARE @regisId BIGINT ,
@registrationNo VARCHAR(20)
SELECT @regisId = regisId FROM inserted
SELECT @registrationNo= 'AUTO'+ RIGHT('0000000'
+ CONVERT(VARCHAR(20), @regisId),
7)
UPDATE T_Registration SET registrationNo = @registrationNo WHERE regisId = @regisId