Tuesday, March 27, 2012

Duplicate records

I'm trying to use this script to delete duplicate records but am getting
errors
Thanks for you help in advance
http://www.sql-server-performance.c..._duplicates.asp
Stephen K. Miyasato
/*
** This file will delete dupe rows from a table based on a column list. ie.,
the compound value of the chosen column list is used to determine dupe
status and subsequent delete status. We construct some dynamic SQL and use
the ROWCOUNT function in determining how many rows to delete.
**
** Revision History:
** ----
** Date Name Description
** ----
** 07/24/03 DVDS Create
*/
-- declare all variables!
DECLARE @.iErrorVar int,
@.PID_1 varchar(50),
@.DateReport varchar(50),
@.OrderNo varchar(50),
@.TestL varchar(50),
@.Result varchar(50),
@.iReturnCode int,
@.iCount int,
@.chCount char(3),
@.nvchCommand nvarchar(4000)
-- set initial environment
SET ROWCOUNT 0
SET NOCOUNT ON
-- Build cursor to find duplicated information
DECLARE DelDupe CURSOR FOR
SELECT COUNT(*) AS Amount,
PID_1,
DateReport,
OrderNo,
TestL,
Result
FROM LData2Batch
GROUP BY PID_1,DateReport,OrderNo,TestL,Result
HAVING COUNT(*) > 1
ORder by dateReport DESC
OPEN DelDupe
FETCH NEXT FROM DelDupe INTO @.iCount,
@.PID_1,
@.DateReport ,
@.OrderNo ,
@.TestL ,
@.Result
WHILE (@.@.fetch_status = 0)
BEGIN
-- Calculate number of rows to delete for each grouping by subtracting
-- 1 from the total count for a given group.
SELECT @.iCount = @.iCount - 1
SELECT @.chCount = CONVERT(char(3),@.iCount)
-- now build the rowcount and delete statements.
SELECT @.nvchCommand = N'SET ROWCOUNT ' + @.chCount +
'DELETE LData2Batch ' + <= original was 'DELETE IndTest '+
' WHERE PID_1 = ' + CHAR(34) + @.PID_1 + CHAR(34) +
' AND DateReport = ' + CHAR (34) + @.DateReport + CHAR(34) +
' AND OrderNo = ' + CHAR(34) + @.OrderNo + CHAR(34) +
' AND TestL = ' + CHAR(34) + @.TestL + CHAR(34) +
' AND Result = ' + CHAR(34) + @.Result + CHAR(34)
-- print the statement. For your viewing pleasure only.
PRINT @.nvchCommand
-- execute the statement.
EXEC sp_executesql @.nvchCommand
SELECT @.iErrorVar = @.@.Error
IF @.iErrorVar <> 0
BEGIN
RETURN
END
FETCH NEXT FROM DelDupe INTO @.iCount,
@.PID_1 ,
@.DateReport ,
@.OrderNo ,
@.TestL ,
@.Result
END
CLOSE DelDupe
DEALLOCATE DelDupe
RETURN
This is the error:
SET ROWCOUNT 1 DELETE LData2Batch WHERE PID_1 = "77237813" AND DateReport
= "Oct 3 2005 8:35PM" AND OrderNo = "M73283" AND TestL = "Comments" AND
Result = "DNR"
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name '77237813'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'Oct 3 2005 8:35PM'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'M73283'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'Comments'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'DNR'.
/****** Object: Table [dbo].[LData2Batch] Script Date: 10/5/2005 7:21:25
PM ******/
if not exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[LData2Batch]') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
BEGIN
CREATE TABLE [LData2Batch] (
[LabIndex] [int] NOT NULL ,
[PID_1] [varchar] (10) NULL ,
[OrderNo] [varchar] (10) NULL ,
[dateDrawn] [smalldatetime] NULL ,
[Rindex] [int] IDENTITY (1, 1) NOT NULL ,
[testO] [varchar] (100) NULL ,
[Tests] [varchar] (50) NULL ,
[TestL] [varchar] (50) NULL ,
[Result] [varchar] (50) NULL ,
[Units] [varchar] (50) NULL ,
[Range] [varchar] (50) NULL ,
[AbnFlag] [varchar] (1) NULL ,
[memo] [varchar] (1000) NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NOT NULL CONSTRAINT
[DF_LData2Batch_rowguid] DEFAULT (newid()),
[LastUpdate] AS (getdate()) ,
[Who] [smallint] NULL ,
[DateReport] [datetime] NULL ,
CONSTRAINT [PK_LData2Batch] PRIMARY KEY CLUSTERED
(
[Rindex]
) ON [PRIMARY]
) ON [PRIMARY]
END
GOYOu have to set Quoted_Identifier OFF (Look in the BOL for
quoted_identifier --> SET QUOTED_IDENTIFIER OFF, because the CHAR(34) =
" is treated as a identifier that a Identitifier is follwing, such as a
column name.
Or you use the CHAR(39) which is the ' Sign and converts the
epxressions you use in a string.
HTH, Jens Suessmeyer.

No comments:

Post a Comment