要求插入方式采用如下形式,一条语句实现,不能采用循环方 以下写法不正确,但希望能够达到抛砖引玉的效果 INSERT INTO HSL_TestPassStatistics(classId, passSubjectCount) SELECT classId, ( SELECT COUNT(Id) + 1 FROM TestPassStatistics A WHERE A.classId = B.ClassId ) FROM classTest B
------解决方案--------------------
SQL code
INSERT INTO HSL_TestPassStatistics(classId, passSubjectCount)
SELECT classId,(select max(passSubjectCount) from HSL_TestPassStatistics where classid=b.classid)+row_number() over(order by getdate() )
FROM classTest b where b.classid='1'
------解决方案--------------------
SQL code
INSERT INTO HSL_TestPassStatistics(classId, passSubjectCount)
SELECT classId,(select max(passSubjectCount) from TestPassStatistics a
where a.classid=b.classid)+row_number() over(partition by classId order by (select 0)) nexsque
FROM #HSL_TestPassStatistics b
------解决方案--------------------
------解决方案--------------------
------解决方案-------------------- insert HSL_TestPassStatistics select @classId ,convert(int, (select max(passSubjectCount) from HSL_TestPassStatistics where classId=@classId) )+1as rr