sql数据库记录循环处理

ljq_2002 2003-12-10 11:24:52
把一个数据表的记录逐条的进行处理,如下:

use xxx
first
do while not xxx.eof
begin
xxxx...
xxxxx...
next
end

实现的如上的一个过程,SQL语句如何写,谢谢!
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-12-10
  • 打赏
  • 举报
回复
--用游标,例子:
declre tb cursor for select * from xxx
declare @变量 int
open tb
fetch next from tb
while @@fetch_status=0
begin
xxxx...
xxxxx...
next
fetch next from tb
end
close tb
deallocate table tb
victorycyz 2003-12-10
  • 打赏
  • 举报
回复
看具体要求,也许能用一条SQL语句代替,但是,如果是要通用的,恐怕不行。
ghosthjt 2003-12-10
  • 打赏
  • 举报
回复
只能使用游标,因为只有游标是基于行集的。
ljq_2002 2003-12-10
  • 打赏
  • 举报
回复
如果不使用游标,如何写!谢谢
txlicenhe 2003-12-10
  • 打赏
  • 举报
回复
用游标。
以下是SQL2000使用游标的模板

-- =============================================
-- Declare and using a READ_ONLY cursor
-- =============================================
DECLARE <cursor_name, sysname, test_cursor> CURSOR
READ_ONLY
FOR <select_statement, , SELECT au_fname FROM pubs.dbo.authors>

DECLARE @name varchar(40)
OPEN <cursor_name, sysname, test_cursor>

FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO @name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
-- PRINT 'add user defined code here'
-- eg.
DECLARE @message varchar(100)
SELECT @message = 'my name is: ' + @name
PRINT @message
END
FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO @name
END

CLOSE <cursor_name, sysname, test_cursor>
DEALLOCATE <cursor_name, sysname, test_cursor>
GO

34,872

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧