CREATE VIEW v_RAND
AS
SELECT re=STUFF(RAND(),1,2,'')GO--生成随机编号的函数CREATE FUNCTION f_RANDBH(@BHLen int)RETURNS varchar(50)ASBEGINDECLARE @r varchar(50)IF NOT(ISNULL(@BHLen,0) BETWEEN 1 AND 50)SET @BHLen=10SELECT @r=CHAR(CASE WHEN SUBSTRING(re,1,1)>5 THEN 97 ELSE 65 end+(SUBSTRING(re,1,1)+SUBSTRING(re,2,1)+SUBSTRING(re,3,1))%26)+CHAR(CASE WHEN SUBSTRING(re,4,1)>5 THEN 97 ELSE 65 end+(SUBSTRING(re,4,1)+SUBSTRING(re,5,1)+SUBSTRING(re,6,1))%26)FROM v_RANDWHILE LEN(@r)<@BHLenSELECT @r=@r+CHAR(CASE WHEN SUBSTRING(re,1,1)>5 THEN 97 ELSE 65 end+(SUBSTRING(re,1,1)+SUBSTRING(re,2,1)+SUBSTRING(re,3,1))%26)+CHAR(CASE WHEN SUBSTRING(re,4,1)>5 THEN 97 ELSE 65 end+(SUBSTRING(re,4,1)+SUBSTRING(re,5,1)+SUBSTRING(re,6,1))%26)FROM v_RANDRETURN(LEFT(@r,@BHLen))ENDGO--调用SELECT dbo.f_RANDBH(6),dbo.f_RANDBH(8)
--结果: YZVBOj LASCrhSO