直接切入正题:如何让下面的key后面的5位序列数成为随机数?并且最大数不超过相同类列下的行总数。
现在的t1表
----------------------------------------
idcard address subject key
13233 02 22 022200001
45455 02 22 022200002
33555 02 22 022200003
13356 02 22 022200004
94432 02 22 022200005
22444 01 21 012100001
12342 01 21 012100002
10321 01 21 012100003(现在的状态,后面的序列是连续的)
………………………………
需要的t1表
----------------------------------------
idcard address subject key
13233 02 22 022200005
45455 02 22 022200002
33555 02 22 022200001
13356 02 22 022200004
94432 02 22 022200003(后面的序列数根据类别0222下面的数据总数生成0---这个总数的随机数)
22444 01 21 012100002
12342 01 21 012100003
10321 01 21 012100001(后面的序列数根据类别0121下面的数据总数生成0---这个总数的随机数)
……………………………
使用asp.net+C#的后台代码。。怎么实现这个功能。
往谁能给段代码实现这个功能。
------解决方案--------------------------------------------------------
马上插入一个临时表。
=======>
cursor就是相当于临时表的功能.如果你要用临时表也可以或许更容易操作.
------解决方案--------------------------------------------------------
Sql2000
- SQL code
--建立测试环境set nocount oncreate table test(idcard varchar(20),address varchar(2),subject varchar(2))insert into test select '13233','02','22' insert into test select '45455','02','22' insert into test select '33555','02','22' insert into test select '13356','02','22' insert into test select '94432','02','22' insert into test select '22444','01','21' insert into test select '12342','01','21' insert into test select '10321','01','21' go--测试declare @table table(idcard varchar(20),randRow uniqueidentifier)insert into @table select idcard,newid() from testselect idcard,address,subject,address+subject+right('00000'+cast(rownum as varchar(10)),5) as [Key] from(select a.*, (select count(*)+1 from @table t inner join test on t.idcard=test.idcard where randRow>b.randRow and address=a.address and subject=a.subject) as rownumfrom test a inner join @table b on a.idcard=b.idcard )corder by address,subject,idcard --删除测试环境drop table test set nocount off/*idcard address subject Key-------------------- ------- ------- --------------10321 01 21 01210000312342 01 21 01210000222444 01 21 01210000113233 02 22 02220000413356 02 22 02220000533555 02 22 02220000145455 02 22 02220000294432 02 22 022200003*/