想这样更新一列怎么操作

wanggenhu0 2011-03-23 04:25:14
我想让四个字符串随机更新到某个列中,
如字符串是aa,bb,cc dd
应该怎样操作
...全文
106 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tosp2012 2011-03-24
  • 打赏
  • 举报
回复
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
bluesmiler 2011-03-24
  • 打赏
  • 举报
回复
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.id

select * from tb

a dd
b cc
c bb
d aa
e dd
f cc
g bb
h aa
gw6328 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jinfengyiye 的回复:]
学色儿狼的。
select parsename(REPLACE('aa,bb,cc,dd',',','.'),abs(CHECKSUM(NEWID()))%4+1) from t;
[/Quote]
X,多打了个字。
祁连雪狼之家 2011-03-23
  • 打赏
  • 举报
回复
我给你提示下,就是你把这些字符串放到一个数组中。
第二步产生随机数,随机数的范围就选择在0~4 代码简单写下:
Random rd=new Random();
int a=rd.next(0,4)
然后将读取的字符串从数组中提取出来 根据下标 a 来提取。
然后将提取出来的字符串
update 到数据库就 OK了。
我现在的思路就这样。你看行不,至于细节性的代码就不写了。
gw6328 2011-03-23
  • 打赏
  • 举报
回复
学色儿狼的。
select parsename(REPLACE('aa,bb,cc,dd',',','.'),abs(CHECKSUM(NEWID()))%4+1) from t;
--小F-- 2011-03-23
  • 打赏
  • 举报
回复
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
快溜 2011-03-23
  • 打赏
  • 举报
回复
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)
bluesmiler 2011-03-23
  • 打赏
  • 举报
回复
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())

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧