我想要下面的结果(USR2019-12-000001)是我的代码。 当我添加声明并设置年/月时,出现错误 “从字符串转换日期和/或时间时转换失败。”
//Original code that got error
ALTER PROCEDURE [dbo].[spAutoCustom_number]
AS
BEGIN
declare @max int
set @max =0;
declare @trn_name varchar(5)
set @trn_name='USR';
declare @trn_year datetime
set @trn_year=DATEDIFF(YYYY, 0, GETDATE());
declare @trn_month datetime
set @trn_month=DATEDIFF(MM, 0, GETDATE());
declare @trn_No varchar(20)
declare @start int
set @start=1;
declare @no varchar(10)
create table #user(trn_No varchar(20))
select @max=MAX(Cast(right(UserNumber,6) as int)) + 1 from UsersList
if (@start<=@max)
begin
set @start=@max;
end
set @no=Right('000000'+ CAST(@start AS varchar(10)),6)
select @trn_No=@trn_name+@trn_year+('-')+@trn_month+('-')+@no
insert into #user values(@trn_No)
select * from #user
END
答案 0 :(得分:0)
尝试一下
declare @max int
set @max =0;
declare @trn_name varchar(5)
set @trn_name='USR';
declare @trn_year INT
set @trn_year=YEAR(GETDATE());
declare @trn_month INT
set @trn_month=MONTH(GETDATE());
declare @trn_No varchar(20)
declare @start int
set @start=1;
declare @no varchar(10)
create table #user(trn_No varchar(20))
select @max=MAX(Cast(right(UserNumber,6) as int)) + 1 from UsersList
if (@start<=@max)
begin
set @start=@max;
end
set @no=Right('000000'+ CAST(@start AS varchar(10)),6)
select @trn_No=concat(@trn_name,@trn_year,'-',@trn_month,'-',+@no)
insert into #user values(@trn_No)
select * from #user
已针对您的错误进行了更新,这只是数据类型问题