2010-01-07 6 views
4

SQL 서버 2000 작동하지 않습니다 내가 테스트 데이터 (약 100,000 행)와 테이블이업데이트 테이블

을, 나는 어떤 임의의 데이터를 다른 테이블에서 열 값을 업데이트 할 다른 테이블에서. this question에 따르면,이 내가 노력하고 무엇 :

그러나
UPDATE testdata 
SET type = (SELECT TOP 1 id FROM testtypes ORDER BY CHECKSUM(NEWID())) 

-- or even 
UPDATE testdata 
SET type = (SELECT TOP 1 id FROM testtypes ORDER BY NEWID()) 

에서, "유형"필드는 모든 행에 대해 동일한 값으로 여전히; 어떤 생각을 잘못 했나요?

[편집] 나는이 쿼리가 각 행에 대해 하나 개의 다른 값을 반환 기대하지만, 그렇지 않습니다 :

SELECT testdata.id, (SELECT TOP 1 id FROM testtypes ORDER BY CHECKSUM(NEWID())) type 
FROM testdata 

-- however seeding a rand value works 
SELECT testdata.id, (SELECT TOP 1 id FROM testtypes ORDER BY CHECKSUM(NEWID()) + RAND(testdata.id)) type 
FROM testdata 

답변

4

귀하의 문제 : 한 단일 값을 선택하는 그런 다음 모든 열을 하나의 단일 값으로 업데이트하십시오.

무작위 화를 진행하려면 단계별/반복 방식을 수행해야합니다. SQL Server 2008에서이 방법을 시도했지만 SQL Server 2000에서도 작동해야한다고 생각합니다.

-- declare a temporary TABLE variable in memory 
DECLARE @Temporary TABLE (ID INT) 

-- insert all your ID values (the PK) into that temporary table 
INSERT INTO @Temporary SELECT ID FROM dbo.TestData 

-- check to see we have the values 
SELECT COUNT(*) AS 'Before the loop' FROM @Temporary 

-- pick an ID from the temporary table at random  
DECLARE @WorkID INT 
SELECT TOP 1 @WorkID = ID FROM @Temporary ORDER BY NEWID() 

WHILE @WorkID IS NOT NULL 
BEGIN 
    -- now update exactly one row in your base table with a new random value 
    UPDATE dbo.TestData 
    SET [type] = (SELECT TOP 1 id FROM dbo.TestTypes ORDER BY NEWID()) 
    WHERE ID = @WorkID 

    -- remove that ID from the temporary table - has been updated 
    DELETE FROM @Temporary WHERE ID = @WorkID 

    -- first set @WorkID back to NULL and then pick a new ID from 
    -- the temporary table at random   
    SET @WorkID = NULL 
    SELECT TOP 1 @WorkID = ID FROM @Temporary ORDER BY NEWID() 
END 

-- check to see we have no more IDs left 
SELECT COUNT(*) AS 'After the update loop' FROM @Temporary 
1

당신은

이 트릭을 할 것입니다 .. 새로운 식별자의 선택에 행 계산 당을 시행 할 필요가

UPDATE testdata 
SET type = (SELECT TOP 1 id FROM testtypes ORDER BY outerTT*CHECKSUM(NEWID())) 
FROM testtypes outerTT