use School --指定数据库 declare @min_id int --声明整数变量@x set @min_id=(select MIN(Id) from Students) --给变量@x赋初值为当前最小的Id值 while @min_id>0 begin update Students set Age=ROUND(RAND()*100,0) where Id=@min_id --round()四舍五入把原值转化为指定小数位数 --rand()取得是随机数 默认范围为(0·1) ra…
我们知道在SqlServer中可以用Select语句给变量赋值,比如如下语句就为int类型的变量@id赋值 ; select @id=id from ( as id union all as id union all as id ) as t select @id 执行上面的代码会显示下面的查询结果,结果显示最后@id的值为3,那么意味着上面第3行的select语句每返回一行数据记录,sqlserver就用id列为@id进行了一次赋值,而最后一行数据记录id列为3,所以在第12行的查询中最后查得…