如何在 SQL Server 2008 R2 中不使用游标填充表
我需要识别编号连续的缺失产品。换句话说,我们每家商店出售的一组产品都有唯一的编号。最终用户想要一份能够识别日期范围内丢失的小部件的报告。
Store Product Number
Store 1 Widget 100
Store 1 Widget 101
Store 1 Widget 102
Store 1 Widget 104
Store 2 Widget 201
Store 2 Widget 202
Store 2 Widget 203
我认为最好的方法是获取 MAX(Number) 和 MIN(Number),然后用序列中的所有数字填充表变量。如果表变量中的小部件编号在产品表中不存在,我将返回该编号并将其标记为缺失。
然而;我们的 DBA 非常反对游标,而且我知道 WHILE 循环需要大量开销。另外,我不确定如何按商店执行此操作。
有谁知道如何以基于集合的方式执行此操作?
小部件本身有编号,因此当您出售小部件 #1 时,您出售的下一个小部件应该是小部件 #2。然而;有时,这种情况不会发生,小部件 #3 已售出 - 小部件 #2 丢失且不在数据库中。我需要在报告中确定小部件 #1 和 #3 已售出,而 #2 丢失。
I need to identify missing products who's numbers are sequential. In other words, there's a set of products that are sold each with a unique number at each of our stores. The end user wants a report that will identify the missing widgets in a date range.
Store Product Number
Store 1 Widget 100
Store 1 Widget 101
Store 1 Widget 102
Store 1 Widget 104
Store 2 Widget 201
Store 2 Widget 202
Store 2 Widget 203
I thought the best way to do this is to obtain the MAX(Number) and the MIN(Number) and then populate a table variable with all of the numbers in the sequence. If the Widget Number from the table variable doesn't exist in the Product table, I would return the number and mark it as missing.
However; our DBA is very much against Cursors and I know WHILE loops take a lot of overhead. Plus, I'm not sure how to do this on a by store basis.
Does anyone know of the way to do this in a set based manner?
The widgets themselves have numbers, so when you sell widget #1, the next widget you sell should be widget #2. However; sometimes, that doesn't happen and widget #3 is sold - widget #2 is missing and not in the database. I need to identify on a report that widgets #1 and #3 were sold and #2 is missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新答案
创建表和工作解决方案的示例代码如下。基本上,您会执行 2 个
EXISTS
检查 - 查看是否有比当前数字大的数字,并且没有比当前数字大 1 的数字。Updated Answer
Sample code to create a table and working solution are below. Basically you do 2
EXISTS
checks - to see if there are numbers higher than the current number, and there is not a number one higher than the current number.创建一个数字表并在其中填充比您需要的更多的数字。
然后您可以使用基于集合的查询来获取结果
Create a numbers table and populate it with more numbers than you need.
Then you can use a set-based query to get the results