本人使用Select 赋值时出错了,请高手赐教?

taixinltd 2006-03-28 08:48:55
情况是这样的,我建了一个A表,里边有十条数据;

......

declare @i int
set @i = 0
select @i=@i+1
from A
print @i
.....

在执行以上语句时,有时候最后@i的值是10,有时候是1;为什么赋值有时候循环10次,有时候只循环一次呢?本人百思不解!!!!!!!!!!!!!!!
...全文
247 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
撸大湿 2006-04-03
  • 打赏
  • 举报
回复
晕过去


真希奇



关注
taixinltd 2006-04-03
  • 打赏
  • 举报
回复
关键是,没有条件的,有时候执行一次,有时候执行多次;这一点令人破解!
kylike 2006-03-29
  • 打赏
  • 举报
回复
单纯从楼主提供的语句来看。

declare @i int
set @i = 0
select @i=@i+1
from A
print @i

是不会出现 10 的情况的。

但楼主说到有“循环”,那么就可能是循环的条件出了问题,楼主不妨把循环的条件公布出来。
李必胜 2006-03-29
  • 打赏
  • 举报
回复
用 Sql Profiler 跟一下sql看看
selectplayer 2006-03-28
  • 打赏
  • 举报
回复
10行纪录,结果为10
lovcal 2006-03-28
  • 打赏
  • 举报
回复
呵呵……搂主欢歌标再试试,应该不会有这样的事出现

除非某些地方进行了set rowcount操作
wangdehao 2006-03-28
  • 打赏
  • 举报
回复
create table t(id int)

insert t select 1
union select 2
union select 3
union select 4
union select 5
union select 6
union select 7
union select 8
union select 9
union select 10

declare @i int
set @i = 0
select @i=@i+1
from t
print @i
----结果
10
wangdehao 2006-03-28
  • 打赏
  • 举报
回复
测试结果都是10
lsqkeke 2006-03-28
  • 打赏
  • 举报
回复
测试:

declare @t table(a int)
insert @t
select 2 union all
select 2 union all
select 2 union all
select 2 union all
select 2 union all
select 2 union all
select 2 union all
select 2 union all
select 2 union all
select 2

declare @i int
set @i = 0
select @i=@i+1
from @t
print @i

/*
(所影响的行数为 10 行)

10

*/
ghtyan 2006-03-28
  • 打赏
  • 举报
回复
//這個就是等於1
set rowcount 1
declare @i int
set @i = 0
select @i=@i+1
from A
print @i
set rowcount 0

//這個等於10
set rowcount 0
declare @i int
set @i = 0
select @i=@i+1
from A
print @i
ghtyan 2006-03-28
  • 打赏
  • 举报
回复
不可能吧,我測試都是對的啊

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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