SQL Server对select语句返回多条记录给变量赋值时的处理情况

发表于:2007-07-13来源:作者:点击数: 标签:
declare @user nvarchar(20) select @user='user1' use countme /*判断用户是否存在*/ declare @userweek int select @userweek=[week] from base where userid=@user select @@rowcount if @@rowcount=0 return select @userweek 最后的@userweek打印出来的
declare @user nvarchar(20)
select @user='user1'
use countme
/*判断用户是否存在*/
declare @userweek int
select @userweek=[week] from base where userid=@user
select @@rowcount
if @@rowcount=0
return
select @userweek

最后的@userweek打印出来的结果就是记录用户名为user1的week字段的值

而当我们用下面的语句选择所有记录时(没有where限制)
declare @user nvarchar(20)
select @user='user1'
use countme
/*判断用户是否存在*/
declare @userweek int
select @userweek=[week] from base
select @@rowcount
if @@rowcount=0
return
select @userweek

最后@userweek打印出来的值是返回的所有记录中排名最后一条记录的week字段值
  

原文转自:http://www.ltesting.net