Skip to main content

Posts

Showing posts from September, 2008

DELETE DUPLICATE ROWS FROM A TABLE

DECLARE @COUNT INT DECLARE @TOTAL INT DECLARE @POST VARCHAR(100) DECLARE @TBL TABLE ( ID INT IDENTITY, Post VARCHAR(100), TOTAL INT ) INSERT INTO @TBL SELECT Post, count(*) as [count] FROM tblemp GROUP BY Post HAVING count(*) > 1 SET @COUNT=(SELECT COUNT(*) FROM @TBL) WHILE @COUNT>0 BEGIN SET @POST=(SELECT POST FROM @TBL WHERE ID=@COUNT) SET @TOTAL=(SELECT TOTAL FROM @TBL WHERE ID=@COUNT) DELETE TOP(@TOTAL-1) FROM tblemp WHERE POST=@POST SET @COUNT=@COUNT-1 END Other Way////////////////////////// How to Delete duplicate rows from a table? http://www.cryer.co.uk/brian/sql/sql_delete_duplicates.htm Create table tbltest(Id int identity,Name varchar(50),Age int) //Insert Duplicate Record in the table insert into tbltest values('Amandeep',25) //Select * from tbltest 1 Manpreet 24 7 Amandeep 25 8 Amandeep 25 4 Amandeep 25 9 Amandeep 25 10 Manpreet 24 11 Manpreet 24 12 Manpreet 24 delete T1 from tbltest T1, tbltest T2 where T1.Name = T2.Name and...

How to ReIndex Your DataBase?

When You have bulk of Data in Your Tables .Thousands of Records You have then Some time Your DataExtract Rate is Very Slow.Then ,You have To need Reindexing Your DataBase. Run the Gollowing Query USE CM20_Nestle--Your DataBase Name GO EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)" GO EXEC sp_updatestats GO I think after Executing it your Data Extraction Rate is So High

How to take backup of database and place them on your Disk with a query !

USE FMP EXEC sp_addumpdevice 'disk', 'MyNwind_1', 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\man\MyNwind_1.BAK' Explanation---sp_addumpdevice is a stored procedure used to add a dummy device on your disk that accept 3 parameter 1.Place where you want to create adummy device.Here disk 2.Name of dummydevice-----MyNwind_1 3.Absolute Path of the Device--C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\man\MyNwind_1.BAK' Here bold 1 is the name of database file. After Executing Query,Your Device is Created.Then Execute the following Query BACKUP DATABASE FMP TO MyNwind_1 This Query takes the backup of DataBase name 'FMP' to device MyNwind_1 with a name of MyNwind_1.BAK

How to Delete duplicate rows from a table?

Supppose we have a table without primary key and duplicate records get entered.The problem arises when you have two identical rows in the table and there is no way to distinguish between the two rows.then how do you delete the duplicate record? Solution You have a table named tblEmp with Duplicate EmpIds EmpID Manpreet Manpreet Manpreet Sandeep Sandeep Sandeep Sandeep Vikas Vikas You can Delete duplicate records with Top Command DELETE TOP (SELECT COUNT(*) -1 FROM dbo.tblEmp WHERE EmpID = 'Manpreet') FROM dbo.tblEmp WHERE EmpID = 'Manpreet'