读书人

随机位置批量插入内容 如何实现?

发布时间: 2012-08-10 12:19:33 作者: rapoo

随机位置批量插入内容 怎么实现??
随机位置批量插入内容 怎么实现??
例:表1有字段1(ID)、字段2(内容)

原有数据:

字段1(ID)——字段2(内容)
1——2、3、4、5、6、7、8
2——2、3、4、5、6、7、8
3——2、3、4、5、6、7、8
4——2、3、4、5、6、7、8
5——2、3、4、5、6、7、8

定义a,b, 批量更新后为:

字段1(ID)——字段2(内容)
1——2、a3、4、5、b6、7、8
2——2、b3、4、a5、6、7、8
3——2、a3、4、5、b6、7、8
4——2、b3、4、5、a6、7、8
5——2、3、b4、5、6、a7、8

就是批量在字段2随机位置插入2个定义好的内容

老师高手能给个具体代码 带注释那种吗,我搞了好几天了,不知道怎么弄??


[解决办法]

VBScript code
'str---原字符串'instr_str---需要随即插入的字符串,每个词请用都好隔开'instr_len---插入的个数function random_instr(str,instr_str,instr_len)if trim(str)="" then '为空情况下的处理    random_instr=""   exit functionend ifif trim(instr_str)="" then '为空情况下的处理    random_instr=""   exit functionend ifif trim(instr_len)="" or not isnumeric(instr_len) then '为空情况下的处理    instr_len=1end ifrandom_arr=split(instr_str,",")'分割需要随即插入的字符串if UBound(random_arr)+1<instr_len then'当需要随即插入的字符串的个数小于设定的个数的时候,取需要随即插入的字符串的个数    instr_len=UBound(random_arr)+1end ifeninser=""'保存已插入的字符串for i=1 to instr_lenRANDOMIZErandom=round(len(str)*rnd())'随即插入的位置inser_stred= eninser_str(instr_str,eninser)if trim(eninser)="" then'保存已插入的字符串    eninser=inser_stredelse'保存已插入的字符串    eninser=eninser& "," &inser_stredend ifstemp=left(str,random)if random=0 then stempd=inser_stred & str else stempd= stemp&inser_stred end if 'random等于0时插入在第一个位置str=replace(str,stemp,stempd)'顺序插入nextrandom_instr=strend functionfunction eninser_str(ByRef instrstrd,ByRef eninser)random_arrs=split(instrstrd,",")'分割需要随即插入的字符RANDOMIZErandomd=round(UBound(random_arrs)*rnd())'随即取得要插入的字符串strd=random_arrs(randomd)if eninser="" then '为空情况下的处理    eninser_str=strd   exit functionend ifif instr(","& strd &",",","& eninser &",")>0 then'判断是否已插入数据    eninser_str instrstrd,eninserelse    eninser_str=strd    exit functionend ifend functionresponse.Write random_instr("2、3、4、5、6、7、8","A,F,D",2) 

读书人网 >ASP

热点推荐