你在delphi,bcb,java中2,3都好弄!
记录号嘛oracle 有rowid
sqlserver:
select inentity(int,1,1) rowid,* into #temp from 表 where 条件
select * from #temp where rowid>10 and rowid<=20
记录号嘛oracle 有rowid
sqlserver:
select inentity(int,1,1) rowid,* into #temp from 表 where 条件
select * from #temp where rowid>10 and rowid<=20
你要移动到别的记录,你需要定义游标,然后用
FETCH
[ [ NEXT | PRIOR | FIRST | LAST
| ABSOLUTE { n | @nvar }
| RELATIVE { n | @nvar }
]
FROM
]
{ { [ GLOBAL ] cursor_name } | @cursor_variable_name }
[ INTO @variable_name [ ,...n ] ]
来移动游标,此时也就是移动到别的数据了不知道你的1、2题中的记录号是不是也是指的游标
sorry ,i want to know how to do that in SQL?
to wancyang:
can you give me an example?
比如在存储过程中:我想
1.选出记录号大于20并且小于40的记录,然后遍历某个字段的值.
2.选出表中的最后一条记录或第一条记录
i run the code in SQLSERVER
"select inentity(int,1,1) rowid,* into #temp from 表 "
but SQLSERVER show error:
"'inentity'is not a recognize function name"
你能把你的问题写出来吗
2.如何通过指定记录号来移动?
3.如何移到下一条,下一条,最头条,最后条记录?用存储过程中的游标cursor 用fetch方法,有很多参数
movefirst movenext movelast
2.好象第2个问题还没人来解答
3.关于第1人问题:我想知道SQL SERVER中难道没有一个隐含的列对象(OBJECT)直接可以调用出各行的记录号吗?难道只用通过一个中间表来查询记录号吗.
另外,设我改变记录号为10的记录的F!字段的值岂不没办法了.to magnetmoon:
你的例了已经调通,但再请问一下:如何清队临时表#TEMP?还有没有其他更直接的方法呢.比如说是否有一个类似 row()这样的函数呢?
alter table tablename
add id int identity(1,1)
选择第一个记录:
select * from tablename where id = (select min(id) from tablename)
或
select * from tablename where id = 1
或
select top 1 * from tablename order by id
选择最后一个记录:
select * from tablename where id = (select max(id) from tablename)
或
select top 1 * from tablename order by id desc
选择某一记录号:
select * from tablename where id = @id但要选择下一条或上一条,就不好办了,最好用游标:
declare cur_name scroll cursor
for select item1, item2, ... from tablename //定义游标
open cur_name //打开游标
fetch first from cur_name //选择第一个记录
fetch next from cur_name //选择下一个记录
fetch last from cur_name //选择最后一个记录
fetch prior from cur_name //选择上一个记录
close cur_name //关闭游标
deallocate cur_name //销毁游标
如果用pengdali(大力)的方法查询除带记录号的记录集rs,那:
2.如何通过指定记录号来移动?
rs.Filter="rowid=30"
or:
rs.Move 30, adBookFirst3.如何移到下一条,下一条,最头条,最后条记录?
MoveFirst、MoveLast、MoveNext 和 MovePrevious 有个vb的函数可以参考一下:Public Sub MoveAny(intChoice As Integer, _
rstTemp As Recordset) ' 使用指定方法捕获 BOF 和 EOF。
Select Case intChoice
Case 1
rstTemp.MoveFirst
Case 2
rstTemp.MoveLast
Case 3
rstTemp.MoveNext
If rstTemp.EOF Then
MsgBox "Already at end of recordset!"
rstTemp.MoveLast
End If
Case 4
rstTemp.MovePrevious
If rstTemp.BOF Then
MsgBox "Already at beginning of recordset!"
rstTemp.MoveFirst
End If
End SelectEnd Sub
最前一条:dw_1.ScrollToRow(0)前一条:integer row_current
row_current=dw_1.ScroolPriorRow()
if row_current=1 then
MessageBox("警告","it is the end!")
end if
最后一条。: inteter row_count
integer row_current
row_current=dw_1.ScroolNextRow()
row_count=dw_1.RowCount()
if row_current=row_count then
MessageBox("","")
end if
最后一条: integer row_count
row_count=dw_1.rowcount()
dw_1.ScroolToRow(row_count)
--1.如何知道当前的记录号? 不用回答了吧--2.如何通过指定记录号来移动? DECLARE Temp CURSOR local FOR
SELECT * FROM #temp
where Nid=@YourInputID
OPEN Temp FETCH NEXT FROM Temp
INTO ...'........WHILE @@FETCH_STATUS = 0
BEGIN
......
End
CLOSE Temp
DEALLOCATE Temp3.如何移到下一条,下一条,最头条,最后条记录?
下一条,只要在上面的代码中的@YourInputID+1就是,
上一条,-1
最头条,1
最后条,@yourinputID=(select max(id) from #temp)
but i have to test first.
请问:dw_1是SQLSERVER中的对象吗?如果是VB中的一个对象就没意义了。
to all:
我可以在客户端解决这三个问题,
但我想在后台数据库用存储过程解决,
在后台数据库中使用游标解决第三个问题我现在也明白了。
但据我所知使用游标会影响数据库的运行效率,所以想知道有无更直接的方法。若后来者还是使用游标的方案,就恕我不给分了。
另,我现在最直接的一个问题是,我只想取某个表中的最后一条记录进行处理,若先select all 出来,再在客户端移动到最后一条记录,似乎效率太差。----现在利用identity()的方法我也知道了。除此之外有无更有效的解答?
又或TABEL1中有几万条记录,
我能否用类似TOP n这样的方式取最后的n条记录?