tmpid password morestuff1 BSLPYRZPOAH8LDCW19MJ one2 T2OD2LLJC9IPWLNEOIJE two3 C3U4KN5NHSXEIGOZ6BGH three
Create View OneRandomString20AS witha1 as (select 1 as N union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1),a2 as (select 1 as N from a1 as a cross join a1 as b),a3 as (select 1 as N from a2 as a cross join a2 as b),a4 as (select 1 as N from a3 as a cross join a2 as b),Tally as (select row_number() over (order by N) as N from a4), cteRandomString ( RandomString) as (select top (20) substring(x,(abs(checksum(newid()))%36)+1,1)from Tally cross join (select x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') a) select replace((select ',' + RandomStringfrom cteRandomStringfor xml path ('')),',','') AS Results;GOCREATE FUNCTION fn_RandomString()returns varchar(20)ASBEGINDeclare @results varchar(20)SELECT top 1 @results = Results from OneRandomString20return @resultsENDGOselect dbo.fn_RandomString()CREATE TABLE tmp(tmpid int identity(1,1) not null primary key,password varchar(20) default(dbo.fn_RandomString()),morestuff varchar(30) )insert into tmp(morestuff)select 'one' union all select 'two' union all select 'three'select * from tmp
create view dbo.MyNewID asselect newid() as NewIDValue;gocreate function dbo.ufn_RandomString( @pStringLength int = 20) returns varchar(max)as begin declare @RandomString varchar(max); with a1 as (select 1 as N union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 1), a2 as (select 1 as N from a1 as a cross join a1 as b), a3 as (select 1 as N from a2 as a cross join a2 as b), a4 as (select 1 as N from a3 as a cross join a2 as b), Tally as (select row_number() over (order by N) as N from a4) , cteRandomString ( RandomString ) as ( select top (@pStringLength) substring(x,(abs(checksum((select NewIDValue from MyNewID)))%36)+1,1) from Tally cross join (select x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') a ) select @RandomString = replace((select ',' + RandomString from cteRandomString for xml path ('')),',',''); return (@RandomString);endGO