table1有两个字段 
(现需要对该表进行补录数据)
id 整型 
jfwd 字符型 
jfsd 字符型 
sj   日期型 现在需要从2006年1月1日到2008年5月19日在该表随机生成如下要求的数据(每天生成2次数据,分上午9点到10点,下午2点到3点): 
jfwd 在21.0到22.9范围内的随机数 
jfsd 在55到65范围内的随机数 
sj   从2006年1月1日到2008年5月19日内(上午9点到10点,下午2点到3点) 
预期数据效果如下: 
id    jfwd   jfsd    sj 
1    22       55     2001-01-01 9:20:33 
2    21       45     2001-01-01 14:21:13 
3    23       50     2001-01-02 9:23:30 
4    24       51     2001-01-02 14:41:13 
....略 

解决方案 »

  1.   

    忘了说补充一点的就是table还有一个字段name,在以上要求补充数据时,如何让name在"aa","bb","cc"三个固定字符串内随机填写?即:
    预期数据效果如下:  
    id    jfwd   jfsd    sj                      name (name在"aa","bb","cc"三个固定字符串内随机填写)
    1    22       55     2001-01-01 9:20:33       aa
    2    21       45     2001-01-01 14:21:13      bb
    3    23       50     2001-01-02 9:23:30       aa
    4    24       51     2001-01-02 14:41:13      cc  
    ....略  
      

  2.   

    基本一样:
    http://topic.csdn.net/u/20080516/21/5568900a-81c4-4ac5-a0df-ac11c3285b71.html
      

  3.   

    if object_id('tempdb.dbo.#table1') is not null drop table #table1
    create table #table1 (id int identity,jfwd varchar(4),jfsd varchar(4),sj datetime, name varchar(10))if object_id('tempdb.dbo.#') is not null drop table #
    --> select datediff(day,'20060101','20080519')+1 --> 870
    select top 870 id=identity(int,1,1), date=cast(null as datetime) into # from sysobjects,syscolumns
    update # set date=id-1+datediff(day,0,'20060101')insert
    #table1
    select
    ltrim(convert(numeric(10,1),convert(int,rand(binary_checksum(newid()))*10+210)/10.0)),
    ltrim(convert(int,rand(binary_checksum(newid()))*11+55)),
    dateadd(ms,convert(int,rand(binary_checksum(newid()))*3599998)+case b.id when 1 then 32400000 else 50400000 end,a.date),
    case cast(rand(binary_checksum(newid()))*3 as int) when 0 then 'aa' when 1 then 'bb' else 'cc' end
    from # a, # b where b.id<=2
    order by a.id,b.id-->效果自己看,太多
    select * from #table1
      

  4.   

    jfwd 在21.0到22.9范围内的随机数  
    但实际上生成的数据却全在21.0-21.9里。
      

  5.   


    那将精度改为秒:if object_id('tempdb.dbo.#table1') is not null drop table #table1
    create table #table1 (id int identity,jfwd varchar(4),jfsd varchar(4),sj datetime, name varchar(10))if object_id('tempdb.dbo.#') is not null drop table #
    --> select datediff(day,'20060101','20080519')+1 --> 870
    select top 870 id=identity(int,1,1), date=cast(null as datetime) into # from sysobjects,syscolumns
    update # set date=id-1+datediff(day,0,'20060101')insert
        #table1
    select
        ltrim(convert(numeric(10,1),convert(int,rand(binary_checksum(newid()))*10+210)/10.0)),
        ltrim(convert(int,rand(binary_checksum(newid()))*11+55)),
        dateadd(second,convert(int,rand(binary_checksum(newid()))*3600)+case b.id when 1 then 32400 else 50400 end,a.date), --> 这里改精度
        case cast(rand(binary_checksum(newid()))*3 as int) when 0 then 'aa' when 1 then 'bb' else 'cc' end
    from # a, # b where b.id<=2
    order by a.id,b.id-->效果自己看,太多
    select * from #table1
      

  6.   


    ltrim(convert(numeric(10,1),convert(int,rand(binary_checksum(newid()))*10+210)/10.0)),
    -->
    ltrim(convert(numeric(10,1),convert(int,rand(binary_checksum(newid()))*20+210)/10.0)),
      

  7.   


    这是格式显示的问题,显示的时候自己转换:
    select convert(varchar,getdate(),120)SQL没有精确到秒的时间类型:要么精确到3.33333毫秒——datetime,要么精确到分钟——smalldatetime
      

  8.   

    但不明白的是,为什么平常的datetime型字段插入日期数据为什么没有.后的毫秒出现?
      

  9.   

    那不是datetime,是smalldatetime或用字符型存储declare @i smalldatetime
    set @i = getdate()
    select @i, getdate()