1

我在 sql server 中有一个视图,每个项目应该返回一行。一些项目有多个行。该视图有很多表连接,所以我不想在每个表上手动运行脚本来找出哪个导致重复。有没有一种快速的自动化方法来找出哪个表是问题表(也就是具有重复行的表)?

4

3 回答 3

4

我发现最快的方法是:

  1. 找一个例子欺骗
  2. 复制查询
  3. 注释掉所有连接
  4. 一次添加一个连接,直到你得到另一行

无论连接是您开始被欺骗的地方,都是您拥有多条记录的地方。

于 2013-01-09T15:18:09.360 回答
1

My technique is to make a copy of the view and modify it to return every column from every table in the order of the FROM clause, with extra columns between with the table names as the column name (see example below). Then select a few rows and slowly scan to the right until you can find the table that does NOT have duplicate row data, and this is the one causing dupes.

SELECT
   TableA = '----------', TableA.*,
   TableB = '----------', TableB.*
FROM ...

This is usually a very fast way to find out. The problem with commenting out joins is that then you have to comment out the matching columns in the select clause each time, too.

于 2013-01-09T15:53:36.697 回答
0

I used a variation of SpectralGhost's technique to get this working even though neither method really solves the problem of avoiding the manual checking of each table for duplicate rows.
My variation was to use a divide and conquer method of commenting out the joins instead of commenting out each one individually. Due to the sheer number of joins this was much faster.

于 2013-01-09T15:46:27.607 回答