/**********************************************************************存储过程: P_AutoGenerUserInfo功能: 生成用户表(包括规则的用户代码和随机密码) 如果表不存在,随机生成表名参数: 表名(@TableName), 业务规则编码(@RuleCode), 业务规则长度(@RuleCodeCount), 随机密码长度(@PassWordLength), 用户数(@Count)返回: 用户表信息调用方法: 如:生成"ZLP010"为前导码、8位后导码、12位随机密码、50条记录。表名:Test 调用: P_AutoGenerUserInfo 'TEST','ZLP010',8,12,50联系方式: zlp321001@hotmail.com最后更改日期: 2005-1-10作用: 用户自动生成规则用户名和随机密码**********************************************************************/Create Proc P_AutoGenerUserInfo@TableName varchar(20),@RuleCode varchar(20),@RuleCodeCount int,@PassWordLength int,@Count intasBegin--如果用户表不存在,创建用户表declare @s varchar(2000),@RuleCodeString varchar(20),@i intdeclare @RandValue varchar(50),@ZeroString varchar(50)select @i=1,@ZeroString='',@RuleCodeString=''while @i<=@RuleCodeCountbegin set @ZeroString=@ZeroString+'0' set @i=@i+1end set @s=' if exists (select * from sysobjects where xtype=''u'' and status>=0 and name = '''+@TableName+''')'+char(13) set @s=@s+' drop table '+@TableName+' '+char(13) set @s=@s+' CREATE TABLE '+@TableName+'(用户名 varchar(20),密码 varchar(20))' exec(@s)
set @i=1if @@Error=0beginwhile @i<=@Count begin select @RuleCodeString=@RuleCode+left(@ZeroString,@RuleCodeCount-len(@i))+cast(@i as varchar(10))--生成@PassWordLength位随机密码 select @RandValue=right(convert(numeric(18,12),rand()),@PassWordLength) --生成6位随机密码中,第一位不为0--select @RandValue=right(convert(numeric(18,12),rand()),@PassWordLength)-- where substring(right(convert(numeric(18,12),rand()),@PassWordLength),1,1)<>0--如果需要所有用户密码不能相同,则需要锁表,判断密码是否存在效率不高.如下:--IF EXISTS(SELECT * FROM @TableName WITH(XLOCK,PAGLOCK) WHERE 密码=@RandValue) exec('insert '+@TableName+' values ('''+@RuleCodeString+''','''+@RandValue+''')') set @i=@i+1 end
end
End
GO
