---
title: "Sqlserver2005 游标 随机数 查询"
author: "studentGuy1"
date: "2013-11-18T16:51:00Z"
tags: []
slug: "sqlserver2005"
draft: false
lastmod: "2014-04-02T16:37:44Z"
id: 2040020
url: "https://studentguy1.iteye.com/blog/2040020"
---

使用SqlServer2005 随机数产生模拟数据 更新表

```
declare @gs int,@ge int
set @gs = -15
set @ge = -1


declare @id varchar(20)

declare cr_cursor cursor --1.定义游标

for select id from tabData where dt between '2013-11-18 12:00:00' and '2013-11-18 15:00:00'

open cr_cursor --2.打开游标

fetch From cr_cursor into @id --3.提取游标

while @@fetch_status=0

begin
update tabData set gs = cast(ceiling(rand() * (@ge - @gs) + @gs) as int) where id = @id

fetch next From cr_cursor into @id

end;

close cr_cursor --4.关闭游标

deallocate cr_cursor --5.释放游标
```