My tables and data as follow,declare @SysRunno table(Pref varchar(10), ryear char(4), rmoth char(2), runn int);/*ryear and rmonth is unique*/insert into @SysRunno values('TR','2010','05',1);insert into @SysRunno values('TR','2010','06',11);declare @tInfo table(DDate datetime, RID varchar(30), Posi tinyint, TripN varchar(10),BusN varchar(10), Stat varchar(1), Cflg varchar(1), Iflg varchar(1));/*@tInfo(DDate,RID,Posi) is unique*/insert into @tInfo values('2010-06-16 00:00:00.000','_R100400000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-06-15 00:00:00.000','_R100600000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-06-16 00:00:00.000','_R100600000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-06-15 00:00:00.000','_R100600000002','1','19','WNR4474','1','1','1');insert into @tInfo values('2010-06-16 00:00:00.000','_R100600000002','1','19','WNR4474','1','1','1');1. I want every row in @tInfo have TID column2. This TID value generated based on DDate and @SysRunno(runn)3. After TID has been generated, @SysRunno(runn) will runn+14. This TID must be unique on every rowSo, below is my T-SQL--Insert tRunNo for new recordsinsert into @SysRunno (Pref, ryear, rmoth, runn)select 'TR', year(DDate), right('0' + convert(varchar(2), month(DDate)), 2), 0from @tInfowhere not exists ( select * from @SysRunno x where x.Pref='TR' and x.ryear = year(DDate) and x.rmoth = month(DDate) )select t.DDate, tid = 'TR' + right(r.ryear, 2) + r.rmoth + right('00000000' + convert(varchar(10), r.runn + t.row_no), 8), rid,posi,TripN,BusN,Stat, Cflg, Iflgfrom ( select rid,posi,TripN,BusN,DDate,Stat, Cflg, Iflg, row_no = row_number() over (partition by dateadd(month, datediff(month, 0, DDate), 0) order by DDate) from @tInfo ) t inner join @SysRunno r on year(t.DDate) = r.ryear and month(t.DDate) = r.rmoth-- Update tRunNoupdate rset runn = r.runn + cntfrom @SysRunno r inner join ( select ryear = year(DDate), rmoth = month(DDate), cnt = count(*) from @tInfo group by year(DDate), month(DDate) ) t on r.ryear = t.ryear and r.rmoth = t.rmoth;/*my result as follow,DDate | tid | rid | posi | TripN | Busn | Stat | Cflg | iflg------------------------------------------------------------------2010-06-15 00:00:00.000 TR100600000012 _R100600000001 1 1 WKM1925 1 1 12010-06-15 00:00:00.000 TR100600000013 _R100600000002 1 19 WNR4474 1 1 12010-06-16 00:00:00.000 TR100600000014 _R100600000002 1 19 WNR4474 1 1 12010-06-16 00:00:00.000 TR100600000015 _R100600000001 1 1 WKM1925 1 1 12010-06-16 00:00:00.000 TR100600000016 _R100400000001 1 1 WKM1925 1 1 1my current @sysrunnoPref | ryear | rmoth | runn---------------------------------TR 2010 05 1TR 2010 06 16till here, my T-SQL running perfectly*/My problem is, if my @tInfo data as follow,declare @tInfo table(DDate datetime, RID varchar(30), Posi tinyint, TripN varchar(10),BusN varchar(10), Stat varchar(1), Cflg varchar(1), Iflg varchar(1));/*@tInfo(DDate,RID,Posi) is unique*/insert into @tInfo values('2010-06-30 00:00:00.000','_R100400000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-07-01 00:00:00.000','_R100400000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-07-02 00:00:00.000','_R100400000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-07-03 00:00:00.000','_R100400000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-06-30 00:00:00.000','_R100600000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-07-01 00:00:00.000','_R100600000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-07-02 00:00:00.000','_R100600000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-07-03 00:00:00.000','_R100600000001','1','1','WKM1925','1','1','1');insert into @tInfo values('2010-06-30 00:00:00.000','_R100600000002','1','19','WNR4474','1','1','1');insert into @tInfo values('2010-07-01 00:00:00.000','_R100600000002','1','19','WNR4474','1','1','1');insert into @tInfo values('2010-07-02 00:00:00.000','_R100600000002','1','19','WNR4474','1','1','1');insert into @tInfo values('2010-07-03 00:00:00.000','_R100600000002','1','19','WNR4474','1','1','1');If I'm running my T-SQL, my tid is not unique anymore and my @sysrunno also have a redundant valueHow to adjust my T-SQL?