1.
Declare @ SQL nvarchar (400)
Select @ SQL = 'select char ('+ convert (nvarchar, convert (INT, 26 * rand () + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) + ')'
Print @ SQL
Exec (@ SQL)
========================================================== ==================
2.
Declare @ STR varchar (10)
Set @ STR =''
Select @ STR = @ STR + CHR from
(Select top 6 CHR from (
Select 'A' as CHR Union all
Select 'B' Union all
Select 'C' Union all
Select 'D' Union all
Select 'E' Union all
Select 'F' Union all
Select 'G' Union all
Select 'H' Union all
Select 'I' Union all
Select 'J' Union all
Select 'K' Union all
Select 'l' Union all
Select 'M' Union all
Select 'n' Union all
Select 'O' Union all
Select 'P' Union all
Select 'q' Union all
Select 'R' Union all
Select's 'Union all
Select 't'union all
Select 'U' Union all
Select 'V' Union all
Select 'W' Union all
Select 'x' Union all
Select 'y' Union all
Select 'Z'
) As a order by newid () as t
---- View
Select @ Str
========================================================== =
3.
Declare @ myid varchar (1000)
Set @ myid = newid ()
Select convert (varchar (6), @ myid)
========================================================== =
4.
Declare @ below int
Declare @ Up int
Select @ below = 65, @ up = 90
Select char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
========================================================== =
5.
Create view v_rand as select rand1 = convert (INT, Rand () * 26), rand2 = rand () * 2
Go
Create Function DBO. f_getrandnum (@ Len int, @ flag INT)
Returns nvarchar (100)
As
-- @ Len: length of output characters
-- @ Flag return value contains character 1: uppercase letters 2: lowercase letters 3: uppercase and lowercase letters mixed
Begin
Declare @ SQL nvarchar (100), @ Rand int
Select @ SQL =''
If @ Len> 100
Set @ Len = 100
While @ Len> 0
Begin
Select @ Rand = rand1 + (Case @ flag when 1 then 65 when 2 then 97
Else (case when rand2> 1 then 97 else 65 end)
From v_rand
Select @ SQL = @ SQL + char (@ rand), @ Len = @ len-1
End
Return @ SQL
End
Go
Select DBO. f_getrandnum (7,3)