我想让四个字符串随机更新到某个列中,
如字符串是aa,bb,cc dd
应该怎样操作

解决方案 »

  1.   

    with cte
    as
    (select 'aa' col union select 'bb'union select 'cc'union select 'dd')update tb
    set tb.col=(select top 1 * from cte order by newid())
      

  2.   

    declare @str varchar(20)
    set @str='aa,bb,cc,dd'
    update tb set 列=parsename(replace(@str,',','.'),cast(rand(datepart(ms,getdate())*1000)*4 as int)+1)
      

  3.   

    create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
    returns @temp table(a varchar(100))
    --实现split功能 的函数
    --date    :2003-10-14
    as 
    begin
        declare @i int
        set @SourceSql=rtrim(ltrim(@SourceSql))
        set @i=charindex(@StrSeprate,@SourceSql)
        while @i>=1
        begin
            insert @temp values(left(@SourceSql,@i-1))
            set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
            set @i=charindex(@StrSeprate,@SourceSql)
        end
        if @SourceSql<>'' 
           insert @temp values(@SourceSql)
        return 
    end
    select * from dbo.f_split('aa,bb,cc,dd',',')a                                                                                                    
    -------------------- 
    aa
    bb
    cc
    dd
      

  4.   

    学色儿狼的。
    select parsename(REPLACE('aa,bb,cc,dd',',','.'),abs(CHECKSUM(NEWID()))%4+1) from t;
      

  5.   

    我给你提示下,就是你把这些字符串放到一个数组中。
    第二步产生随机数,随机数的范围就选择在0~4  代码简单写下:
    Random rd=new Random();
    int a=rd.next(0,4)
    然后将读取的字符串从数组中提取出来 根据下标 a 来提取。
    然后将提取出来的字符串
    update 到数据库就 OK了。
    我现在的思路就这样。你看行不,至于细节性的代码就不写了。
      

  6.   

    create table tb (col1 varchar(10),col2 varchar(10))
    insert into tb values ('a','a')
    insert into tb values ('b','b')
    insert into tb values ('c','c')
    insert into tb values ('d','d')
    insert into tb values ('e','e')
    insert into tb values ('f','f')
    insert into tb values ('g','g')
    insert into tb values ('h','h')
    with cte
    as
    (select 'aa' col union all select 'bb'union all select 'cc'union all select 'dd')update a
    set a.col2=b.col
    from (select *,ntile(4) over (order by newid()) id from tb) a,
    (select ROW_NUMBER() over (order by NEWID()) id,col from cte)  b
    where a.id=b.idselect * from tba dd
    b cc
    c bb
    d aa
    e dd
    f cc
    g bb
    h aa
      

  7.   

    declare @Ta table 
    (id int,
    col char(10)
    )
    insert @Ta
    select 1001,'' union all 
    select 1002,'' union all
    select 1003,''
    update @Ta set col=(select  top 1 * from ( select 'aa'  col  union select 'bb'union select 'cc'union select 'dd' ) as a order by newid()) where id=(select top 1  id from @Ta order by newid())
    select * from @Ta