我想让四个字符串随机更新到某个列中,
如字符串是aa,bb,cc dd
应该怎样操作
如字符串是aa,bb,cc dd
应该怎样操作
解决方案 »
- 有一个连接语句如何再扩充与其它表进行关联?
- join 让我抓狂哦? 怎么因事哦?
- 做套打时想实现可以预览但有些内容无需打印如何实现 fastreport或reportmachine
- 怎么在保存大字段的时候, 只保存其地址呢
- 数据库服务器启动后,报错: database cannot be autostarted during server shutdown or startup.
- 十万火急,数据库启动不了该怎么办??谢谢,在线等!!!
- Text字段内容发生截断和溢出的问题
- 我想听听大家对NULL值得理解,好处坏处
- 非常急,关于求余数
- 一個關于delphi小問題請幫忙解決?
- SQL2008建立数据库时除了C盘建立其它的盘(D/E.F)都不能建立
- 统计一下
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())
set @str='aa,bb,cc,dd'
update tb set 列=parsename(replace(@str,',','.'),cast(rand(datepart(ms,getdate())*1000)*4 as int)+1)
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
select parsename(REPLACE('aa,bb,cc,dd',',','.'),abs(CHECKSUM(NEWID()))%4+1) from t;
第二步产生随机数,随机数的范围就选择在0~4 代码简单写下:
Random rd=new Random();
int a=rd.next(0,4)
然后将读取的字符串从数组中提取出来 根据下标 a 来提取。
然后将提取出来的字符串
update 到数据库就 OK了。
我现在的思路就这样。你看行不,至于细节性的代码就不写了。
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
(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