关闭

自动生成用户名或密码

标签: stringdate
1134人阅读 评论(0) 收藏 举报
分类:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		爱因光
-- Create date: 2012年3月21日14:37:10
-- Description:	自动生成用户名或密码
-- =============================================
CREATE PROCEDURE [dbo].[sp_GenerateString]
	@Length INT,
	@string NVARCHAR(200) OUTPUT
AS
	DECLARE @RandomID VARCHAR(32);
	DECLARE @counter SMALLINT;
	DECLARE @RandomNumber FLOAT;
	DECLARE @RandomNumberInt TINYINT;
	DECLARE @CurrentCharacter VARCHAR(1);
	DECLARE @ValidCharacters VARCHAR(255);
	SET @ValidCharacters = 
	    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	DECLARE @ValidCharactersLength INT;
	SET @ValidCharactersLength = LEN(@ValidCharacters);
	SET @CurrentCharacter = '';
	SET @RandomNumber = 0;
	SET @RandomNumberInt = 0;
	SET @RandomID = '';
	
	SET NOCOUNT ON;
	
	SET @counter = 1;
	
	WHILE @counter < (@Length + 1)
	BEGIN
	    SET @RandomNumber = RAND();
	    SET @RandomNumberInt = CONVERT(
	            TINYINT,
	            ((@ValidCharactersLength - 1) * @RandomNumber + 1)
	        );
	    
	    SELECT @CurrentCharacter = SUBSTRING(@ValidCharacters, @RandomNumberInt, 1);
	    --判断不能以数字开头
	    IF (@counter = 1)
	    BEGIN
	        WHILE (ISNUMERIC(@CurrentCharacter) = 1)
	        BEGIN
	            SET @RandomNumber = RAND();
	            SET @RandomNumberInt = CONVERT(
	                    TINYINT,
	                    ((@ValidCharactersLength - 1) * @RandomNumber + 1)
	                );
	            SELECT @CurrentCharacter = SUBSTRING(@ValidCharacters, @RandomNumberInt, 1);
	        END
	    END
	    
	    SET @counter = @counter + 1;
	    
	    SET @RandomID = @RandomID + @CurrentCharacter;
	END
	SET @string = @RandomID

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:175263次
    • 积分:2886
    • 等级:
    • 排名:第12288名
    • 原创:116篇
    • 转载:29篇
    • 译文:0篇
    • 评论:13条
    公告
    由于作者水平有限,如果在您阅读博客过程中发现问题,请留言!作者非常感谢您的帮助!
    文章分类