以下代码在sql2008和sql2000上的运行方式存在差异。在sql 2000中,结果是正确的(从第一行到最后一行的提取是正常的),而在sql 2008中,提取显示出奇怪的行为(从最后插入的行开始直到第一行。下面是问题的代码其中“区域”是任何表格:
create trigger tr on area for insert as
declare @id int
select @id = id from inserted
print 'trigger: ' + convert(varchar(50), @id)
declare c cursor scroll for select id from inserted order by id
open c
fetch next from c into @id
while @@FETCH_STATUS = 0
begin
print 'cursor id: ' + convert(varchar(50), @id)
fetch next from c into @id
end
close c
deallocate c
下面是 sql 2008 中的结果:
trigger: 1828
cursor id: 1837
cursor id: 1836
cursor id: 1835
cursor id: 1834
cursor id: 1833
cursor id: 1832
cursor id: 1831
cursor id: 1830
cursor id: 1829
cursor id: 1828
sql 2000 中显示的结果是:
trigger: 1837
cursor id: 1828
cursor id: 1829
cursor id: 1830
cursor id: 1831
cursor id: 1832
cursor id: 1833
cursor id: 1834
cursor id: 1835
cursor id: 1836
cursor id: 1837
最佳答案
如果你在SQL2008中使用游标,它会从最后到第一个。你是对的,SQL改变了它的选择规则。一般来说,像这样使用光标会更好。使用 OrderBy。
关于sql-server - sql 2008游标与sql2000的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2161019/