SQL server 游标,随机数使用

SELECT *
  FROM [goods]
  
 DECLARE test_cursor CURSOR scroll FOR
    SELECT goods_no,
           [unitprice]
    FROM   [goods]
  
  OPEN test_cursor
  
  DECLARE @goodsno INT
  DECLARE @num numeric(10, 2)
  FETCH next FROM test_cursor INTO @goodsno, @num
  WHILE @@FETCH_STATUS = 0
   begin
      PRINT @goodsno
      PRINT @num
        
   IF @num % 2 = 0
    update [goods] set sex='' where goods_no=@goodsno
  ELSE
    update [goods] set sex='' where goods_no=@goodsno
      FETCH next FROM test_cursor INTO @goodsno, @num
   end
  
  CLOSE test_cursor
  DEALLOCATE test_cursor 
  

 

以下是随机增加进店人数和试衣人数

DECLARE @BeginDate DATE;
SELECT @BeginDate = '2013-1-1';
WHILE @BeginDate <= '2014-2-8'
  BEGIN
  
  DECLARE test_cursor CURSOR scroll FOR
  select 渠道ID from Dim_渠道 where 经营方式='自营'
  
  OPEN test_cursor
  DECLARE @customerNo varchar(60)

  FETCH next FROM test_cursor INTO @customerNo
  WHILE @@FETCH_STATUS = 0
   begin
      PRINT @customerNo
      
      insert into Fact_终端销售动作
      values(@customerNo,@customerNo,CONVERT(VARCHAR(8), @BeginDate, 112),
      cast(ceiling(rand() * 100) as int),
      cast(ceiling(rand() * 20) as int))
       
      FETCH next FROM test_cursor INTO @customerNo
   end
     SET @BeginDate = Dateadd(DAY, 1, @BeginDate);
  
  CLOSE test_cursor
  DEALLOCATE test_cursor 
  end

 

posted @ 2014-02-09 15:32  Rain520  阅读(298)  评论(0编辑  收藏  举报