Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 4th, 2008, 02:29 AM
Authorized User
 
Join Date: Dec 2006
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Table updation - SQL 2000

Hi,

I've millions of records something like this in the SQL Server 2000 database.

ID Version
--- -------
1 1
1 NULL
1 NULL
2 1
2 NULL
2 NULL

Please provide me an simple update query which update the NULL with the incremental value of version by grouping the ID and my updation should not affect my performance and keep in mind that i have millions of records.

My final output after updation:

ID Version
--- -------
1 1
1 2
1 3
2 1
2 2
2 3




Thanks,
Chandra
__________________
Thanks,
Chandra
 
Old July 21st, 2008, 03:43 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is a solution for you with no tampering with original table.


DECLARE @Sample TABLE (ID INT, Version INT)

INSERT @Sample
SELECT 1, 1 UNION ALL
SELECT 1, NULL UNION ALL
SELECT 1, NULL UNION ALL
SELECT 4, 1 UNION ALL
SELECT 4, 3 UNION ALL
SELECT 4, NULL

SELECT *
FROM @Sample

DECLARE @MinVersion INT,
    @MaxVersion INT,
    @MinID INT,
    @MaxID INT

SELECT @MinVersion = 1,
    @MaxVersion = MAX(Version),
    @MinID = MIN(ID),
    @MaxID = MAX(ID)
FROM @Sample

SET ROWCOUNT 1

WHILE @MinVersion <= @MaxVersion
    BEGIN
        WHILE @MinID <= @MaxID
            BEGIN
                IF NOT EXISTS (SELECT * FROM @Sample WHERE ID = @MinID AND Version = @MinVersion)
                    UPDATE @Sample
                    SET Version = @MinVersion
                    WHERE ID = @MinID
                        AND Version IS NULL

                SELECT @MinID = MIN(ID)
                FROM @Sample
                WHERE ID > @MinID
            END

        SELECT @MinVersion = @MinVersion + 1,
            @MinID = MIN(ID)
        FROM @Sample
    END

SET ROWCOUNT 0

SELECT *
FROM @Sample







Similar Threads
Thread Thread Starter Forum Replies Last Post
Table records updation from Excel Sheets ayazhoda Access VBA 0 April 25th, 2007 07:56 AM
Connect SQL Server 2000 table with VB6 sanjna000 Access 1 May 2nd, 2006 06:28 AM
generate XML from SQL server 2000 table sasidhar79 XML 2 August 3rd, 2005 11:05 AM
Access 2000 "Open ODBC SQL Table for Additions" cjdphlx Access VBA 3 July 18th, 2005 02:05 PM
Access and update linked SQL Server 2000 table Lizu Access 9 May 10th, 2004 12:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.