T-SQL how to identfiy tables that have dulicate roaws

25 pts.
Tags:
Microsoft SQL Server 2000
SQL
T/SQL
I have a MS 2000 SQL server. How can I search the db and indentify the tables that have duplicate raws. Thanks

Answer Wiki

Thanks. We'll let you know when a new response is added.

There is no generic script which can be used for this. You need to query each table to see if it has duplicate rows in it.

If your table looked like this:
<pre>CREATE TABLE Employee
(EmployeeId INT,
FirstName VARCHAR(50),
LastName VARCHAR(50))</pre>

And you wanted to check for duplicate records you would use this query to identify the duplicate rows.

<pre>SELECT EmployeeId, FirstName, LastName, count(*)
FROM Employee
GROUP BY EmployeeId, FirstName, LastName
HAVING count(*) <> 1</pre>

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Denny Cherry
    Check out my SQL Server blog "SQL Server with Mr Denny" for more SQL Server information.
    66,130 pointsBadges:
    report
  • Techzon12
    */ I have created the following query. It does the job in idenifying tables that have duplicate "Date_Stamp" id's. I am working on improving it, so it will only display the tables with duplicate "date_stamp". Right now it will output all table names. The ones that don't have duplicates will show a null value. The ones with duplicates will display all the duplicate records. */ DECLARE @name_var varchar(50) DECLARE @get_name CURSOR -- Define cursor - get all tables names SET @get_name = CURSOR FOR SELECT name FROM dbo.sysobjects WHERE xtype = 'U' -- Open the cursor OPEN @get_name -- Fetch the cursor into the declared variable FETCH NEXT FROM @get_name INTO @name_var WHILE (@@FETCH_STATUS = 0) BEGIN if exists ( select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME= @name_var and COLUMN_NAME = 'Date_Stamp' ) Begin select @name_var execute ('select Date_Stamp from ' + @name_var + ' group by Date_Stamp having count (Date_Stamp) <>1') End FETCH NEXT FROM @get_name INTO @name_var END -- Cleanup CLOSE @get_name DEALLOCATE @get_name
    25 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following