工作环境是SQL Server 2000.我有一个没有索引的表,没有PK ...总行数是600,000。
如何更新第0行的列 - > 100,000,其值为100,001 - > 200,000与另一个,等等?
谢谢。
答案 0 :(得分:3)
将所有值设置为null,然后设置为ROWCOUNT 100000.然后使用不同的值进行连续更新(每个将影响100000行),其中该列为空。 @@ rowcount将是每次更新后受影响的行数,因此当它小于100000时停止。
对于@ Shannon的评论,ROWCOUNT将不会在下一版本的SQL Server(后SQL Server 2008)中获得更新/删除/插入语句,但它适用于SQL Server 2000.建议的更改是使用TOP子句,但我不认为在SQL Server 2005之前支持更新。
我想如果你想要你可以起诉光标......
update mytable set myid = null
SET NOCOUNT ON -- prevent all those "1 row(s) updated" messages
declare @count int, @value int, @myid int
set @count = 1
set @value = 1
declare cursor_update cursor for select myid from mytable
open cursor_update
fetch cursor_update into @myid
while @@FETCH_STATUS = 0
begin
update mytable set myid = @value where current of cursor_update
set @count = @count + 1
if (@count > 100000)
begin
set @count = 1
set @value = @value + 1
end
fetch cursor_update into @myid
end
close cursor_update
deallocate cursor_update
答案 1 :(得分:2)
注意:我相信这适用于SQL Server 2000,但没有要测试的版本。
要更改不确定的行集,请使用top
查询,如:
drop table t
create table t (c varchar(20))
insert into t
select top 15 'unchanged' from information_schema.columns
update alias
set c = 'changed'
from (select top 5 * from t) alias
-- note later queries need to be able to look at data
-- to tell if the row has already been processed.
update alias
set c = 'another change'
from (select top 5 * from t where c = 'unchanged') alias
select * from t
答案 2 :(得分:1)
在SQL Server 2005+中,您可以使用排名根据任意或特定顺序分配相对行号:
WITH ranked AS (
SELECT
*,
rn = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
FROM table
)
UPDATE ranked
SET column = CASE
WHEN rn BETWEEN 1 AND 100000 THEN value1
WHEN rn BETWEEN 100001 AND 200000 THEN value2
…
END
将(SELECT 1)
替换为列列表以使用特定订单。
答案 3 :(得分:1)
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" />
<GridView Grid.Row="1"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Details}"
ItemContainerStyle="{StaticResource GridViewItemStyleIOhneHover}"
ItemTemplateSelector="{StaticResource MyDataTemplateSelector}">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"
HorizontalAlignment="Stretch"
MaximumRowsOrColumns="2"
HorizontalChildrenAlignment="Stretch"
VerticalChildrenAlignment="Stretch">
</WrapGrid>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</Grid>
答案 4 :(得分:0)
您可能正在考虑旧时代,在VSAM数据文件中的IBM SQL-DS中,您可以在物理文件中获取RelativeRecordNumber吗?
不幸的是(或者我应该说幸运的是)这是历史。