|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 6,997,
Visits: 13,942
|
|
declare @n varchar(64)
set @n='';
;with MyCTE1 as ( select N, newid() as rid from tally), MyCte2 as ( select top(64) char(n%84+32) randchar from MyCTE1 order by rid) select @n=@n+randchar from MyCTE2
select @n
---------------------------------------------------------------------------------- Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 1:29 PM
Points: 2,941,
Visits: 10,490
|
|
Another variation from Lynn's version:
;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 ), cteRandomString as ( select substring('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',(abs(checksum(newid()))%36)+1,1) as RandomString from ( select 1 as N from a1 as a cross join a1 as b ) a ) select convert(varchar(64),replace(( select ','+RandomString from cteRandomString for xml path ('')) ,',','')) as RandomString;
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:43 PM
Points: 11,613,
Visits: 27,666
|
|
great code examples; what if i wanted to generate X number strings, for arguments sake lets say 10...i can't seem to visualize how to generate multiple records though.
rub my nose in it and get the newspaper....
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 6,997,
Visits: 13,942
|
|
here's one way. Not necessarily the most efficient, but it's not bad.
drop table #matt
declare @rows_needed int declare @length_needed int
select @rows_needed=1000, @length_needed=64
select top(@rows_needed*@length_needed) identity(int,1,1) RN ,cast(N as int) N ,0 as batchcol ,'' as randchar into #matt from tally order by newid()
update #matt set batchcol = cast(rn/@length_needed as int), randchar = char(n%52+65)
create index m on #matt(batchcol) include(randchar)
select distinct batchcol, (select randchar as 'text()' from #matt m_in where m_in.batchcol=m_out.batchcol for xml path ('')) from #matt m_out
---------------------------------------------------------------------------------- Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
|
|
|
|