求一条MSSQL的循环语句

andylist 2011-12-14 04:55:22
有表Client_Temp,其中有字段 UpdateOrInsert 类型为 int
要求:


SELECT * FROM Client_Temp
WHERE UserID =2009 AND UpdateOrInsert <>2

结果可能为N条,然后开始循环,从第一条开始,

BEGIN
IF UpdateOrInsert = 0
BEGIN
--余下的操作俺自个来,呵呵。
END

IF UpdateOrInsert = 1
BEGIN
--余下的操作俺自个来,呵呵。
END
END
...全文
89 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-12-14
  • 打赏
  • 举报
回复
如果你那个"余下的操作",会更改 userid 或 updateorinsert,那可以直接用它来判断而不要用游标:

begin
while exists(select 1 from client_temp where userid=2009 and updateorinsert=0)
begin
这儿你写
end

while exists(select 1 from client_temp where userid=2009 and updateorinsert=1)
begin
这儿你写
end
end
快溜 2011-12-14
  • 打赏
  • 举报
回复
declare @UpdateOrInsert int
DECLARE cursor1 CURSOR --声明游标
FOR SELECT UpdateOrInsert FROM Client_Temp
WHERE UserID =2009 AND UpdateOrInsert <>2

OPEN cursor1 --打开游标

FETCH NEXT FROM cursor1 --获取游标的下一行数据

into @UpdateOrInsert --使变量获得当前游标指定行的UpdateOrInsert

WHILE @@FETCH_STATUS = 0 --FETCH语句执行成功
BEGIN
if @UpdateOrInsert=0
begin
---你的操作
end

FETCH NEXT FROM cursor1 --获取游标的下一行
into @ids,@names --使变量获得当前游标指定行的UpdateOrInsert

End
CLOSE cursor1 --关闭游标
DEALLOCATE cursor1 --释放游标
andylist 2011-12-14
  • 打赏
  • 举报
回复
不懂游标啊。希望各位高手帮帮忙。
快溜 2011-12-14
  • 打赏
  • 举报
回复
用游标吧

34,587

社区成员

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

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