English 中文(简体)
• 如何重新更新服务器中的小组
原标题:Howto recursively update groups in SQL Server

I m 使用了2008年ql服务器。

缩略语 集合:

PK  Col1        Col2
1   SomeValue1  DataToTake1
2   SomeValue1
3   SomeValue1
4   SomeValue1
5   SomeValue2  DataToTake2
6   SomeValue2
...

i want to insert DataToTake1 into Col2 of records with PK 2, 3 and 4 and DataToTake2 into Col2 of record with PK 6.
to make it more obvious: records with PK 1-4 are groups, indicated by Col1
another info: PK might not be incremental nor sequenced on productive system (but as we can group by Col1, i hope that might not be a problem).

是否有办法用 s服务器来做到这一点? (一个伙伴用一位协调人......arggggh执行)

edit
thank you for your answers, but i have to revise my request, as my initial situation changed: i need to take PK in account. eg.

PK  Col1        Col2
1   SomeValue1  DataToTake1
2   SomeValue1
4   SomeValue1
5   SomeValue2  DataToTake2
6   SomeValue2
9   SomeValue1

如何更新顺序排列的浏览量? PK 1 = 2 = 4

最佳回答

我认为,你必须使用临时表格:

create table #DataToTake ( seq int not null identity(1, 1), PK_S int not null, -- set this to same type as PK in dataPool PK_E int null, -- set this to same type as PK in dataPool Col1 varchar(20) null, -- set this to same type as Col1 in dataPool Col2 varchar(20) null -- set this to same type as Col2 in dataPool )

insert into #DataToTake (PK_S, Col1, Col2) select PK, Col1, Col2 from dataPool where Col2 is not null order by PK

update #DataToTake set PK_E = dtt2.PK_S from #DataToTake inner join #DataToTake dtt2 on #DataToTake.seq = dtt2.seq - 1

update #DataToTake set PK_E = (select Max(PK) + 1 from dataPool) where PK_S = (select Max(PK_S) from #DataToTake )

update dataPool set Col2 = dtt.Col2 from dataPool inner join #DataToTake dtt on dataPool.PK > dtt.PK_S and dataPool.PK < dtt.PK_E and dataPool.Col1 = dtt.Col1

表2

问题回答
UPDATE
   D1
SET
   Col2 = D2.Col1
FROM
   dataPool D1
   JOIN
   dataPool D2 ON D1.col1 = D2.col1
WHERE
   D1.col2 <> D2.col2 OR D1.col2 IS NULL
-- Select the top 1 value which has data, and matches whats in Col1
UPDATE dataPool
SET Col2 = (SELECT TOP 1 t3.Col2 
            FROM dataPool t3 
            WHERE t3.col1 = t2.col1 
            AND t3.Col2 IS NOT NULL) -- 
FROM dataPool t2
WHERE t2.Col1 = Col1
AND col2 IS NULL -- Only update Rows with NULLs in




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签