Wednesday, February 23, 2011

Unique records displaying


CREATE PROC UNIQ_NO
AS
BEGIN
DECLARE @TEMP TABLE (RID INT IDENTITY, TVAL INT)
INSERT INTO @TEMP
SELECT VAL FROM x ORDER BY val
DECLARE @RES TABLE (RNO INT IDENTITY, RVAL INT)
DECLARE @CNT INT, @I INT = 1, @J INT, @K INT, @CNT1 INT
SELECT @CNT = COUNT(1) FROM @TEMP
INSERT INTO @RES
SELECT TVAL FROM @TEMP WHERE RID = @I
SELECT @CNT1 = COUNT(1) FROM @RES
SELECT @J = RVAL FROM @RES WHERE RNO = @CNT1
WHILE(@I<=@CNT)
BEGIN
SELECT @K = TVAL FROM @TEMP WHERE RID = @I
IF(@J = @K)
BEGIN
DELETE FROM @TEMP WHERE TVAL = @J
END
ELSE
BEGIN
INSERT INTO @RES SELECT @K

END
SET @I = @I + 1
END
SELECT * FROM X
END

EXEC UNIQ_NO

No comments:

Post a Comment