Sql 使用For循环插入+;1身份证
我有两张桌子:Sql 使用For循环插入+;1身份证,sql,loops,for-loop,insert,Sql,Loops,For Loop,Insert,我有两张桌子: InspectionID ID Comment 1 1 Text1 1 2 Text2 2 1 Text1 3 1 Text1 3 2 Text2 InspectionID ID Comment 1 1 TextA 2 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
表1:
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
表2:
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
我需要一个FOR循环公式,它将表2插入到表1中,但根据InspectionID将表2的ID更新为下一个按时间顺序排列的数字。我希望我的结果如下所示:
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
表1(完整):
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
谁能帮我解决这个问题,我从来没有做得很好
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
谢谢 如果您的DBMS支持窗口函数,您可以使用这些函数-无需循环。它们是一个非常强大的工具,在各种情况下都很有用
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
SQL Server 2008及以上版本:
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
INSERT INTO dbo.Table1
(InspectionID, ID, Comment)
SELECT t1.InspectionID
,MAX(t1.ID) + ROW_NUMBER() OVER (PARTITION BY t1.InspectionID ORDER BY t2.Comment)
,t2.Comment
FROM dbo.Table1 t1
INNER JOIN dbo.Table2 t2 ON t1.InspectionID = t2.InspectionID
GROUP BY t1.InspectionID
,t2.Comment
概念证明(在表2中增加一行):
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
结果:
InspectionID ID Comment
1 1 Text1
1 2 Text2
2 1 Text1
3 1 Text1
3 2 Text2
InspectionID ID Comment
1 1 TextA
2 1 TextA
3 1 TextA
4 1 TextA
5 1 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
InspectionID ID Comment
1 1 Text1
1 2 Text2
1 3 TextA
2 1 Text1
2 2 TextA
3 1 Text1
3 2 Text2
3 3 TextA
3 4 TextB
您正在使用哪个
DBMS
?为什么插入顺序很重要?为什么需要for循环?这个数据真的正确吗?为什么表2有一个似乎没有用作标识的ID字段?这里肯定有些东西……这应该可以在SQL中完成,而不需要for循环。您能否修改您的问题/标签,以指示您正在使用的DBMS?