适合于一次往TABLE1插入一条记录:
create trigger tri on table1
for insert
as
begin
declare @a int,@i int
select @a=col1/100 from inserted
set @i=1
while @i<=@a
  begin 
  insert table2(col2) select rand(checksum(newid()))
  set @i=@i+1
  end
end

解决方案 »

  1.   

    create trigger 名 on table1 for insert,update
    as
    udpate COL1
    declare @num int
    select @num=(sum(isnull(COL1,0))/100) from inserted
    while @num<=0
    begin
      insert table2 (COL2) select cast(rand(checksum(newid()))*5555 as int)
      set @num=@num-1
    end
      

  2.   

    谢谢!不好意思,还有个条件 ,生成的随机数在TABLE2中不能重复!感谢、!!!!
      

  3.   

    我写错了:
    create trigger 名 on table1 for insert,update
    as
    declare @num int
    select @num=(sum(isnull(COL1,0))/100) from inserted
    while @num<=0
    begin
      insert table2 (COL2) select cast(rand(checksum(newid()))*5555 as int)
      set @num=@num-1
    end
      

  4.   

    重复的概率很小吧!
    create trigger 名 on table1 for insert,update
    as
    declare @num int,@i int
    select @num=(sum(isnull(COL1,0))/100) from inserted
    while @num<=0
    begin
      set @i=cast(rand(checksum(newid()))*666 as int)
      while exists(select 1 from table2 where COL2=@i)
        set @i=cast(rand(checksum(newid()))*666 as int)    
      insert table2 (COL2) values (@i)
      set @num=@num-1
    end
      

  5.   

    负担会增加!
    你试试嘛!
    0-9:create trigger 名 on table1 for insert,update
    as
    declare @num int,@i int
    select @num=(sum(isnull(COL1,0))/100) from inserted
    while @num<=0
    begin
      set @i=cast(rand(checksum(newid()))*10 as int)
      while exists(select 1 from table2 where COL2=@i)
        set @i=cast(rand(checksum(newid()))*666 as int)    
      insert table2 (COL2) values (@i)
      set @num=@num-1
    end
      

  6.   

    更正:
    create trigger 名 on table1 for insert,update
    as
    declare @num int,@i int
    select @num=(sum(isnull(COL1,0))/100) from inserted
    while @num<=0
    begin
      set @i=cast(rand(checksum(newid()))*10 as int)
      while exists(select 1 from table2 where COL2=@i)
        set @i=cast(rand(checksum(newid()))*10 as int) ---^_^
      insert table2 (COL2) values (@i)
      set @num=@num-1
    end
      

  7.   

    出现错误!'checksum' is not a recognized function name.‘
      

  8.   

    checksum()函数不行啊 !!!
      

  9.   

    create trigger 名 on table1 for insert,update
    as
    declare @num int,@i int
    select @num=(sum(isnull(COL1,0))/100) from inserted
    while @num<=0
    begin
      set @i=cast(rand()*10 as int)
      while exists(select 1 from table2 where COL2=@i)
        set @i=cast(rand()*10 as int) ---^_^
      insert table2 (COL2) values (@i)
      set @num=@num-1
    end
      

  10.   

    怎么用啊?我在查询分析器里执行了,怎么没有在TABLE2中生成随机数啊?
      

  11.   

    drop trigger 名
    go
    create trigger 名 on table1 for insert,update
    as
    declare @num int,@i int
    select @num=(sum(isnull(COL1,0))/100) from inserted
    while @num<=0
    begin
      set @i=cast(rand()*10 as int)
      while exists(select 1 from table2 where COL2=@i)
        set @i=cast(rand()*10 as int)
      insert table2 (COL2) values (@i)
      set @num=@num-1
    end
    go
    insert .......