How to make unique random alphanumeric sequence in SQL Server

前端 未结 4 345
情话喂你
情话喂你 2021-01-16 20:21

I want to make unique random alphanumeric sequence to be the primary key for a database table.

Each char in the sequence is either a letter (a-z) or number (0-9)

4条回答
  •  借酒劲吻你
    2021-01-16 20:56

    NewID() Function will generate unique numbers.So i have incremented them with loop and picked up the combination of alpha numeric characters using Charindex and Left functions

    ;with list as 
        (
            select 1 as id,newid() as val
                 union all
            select id + 1,NEWID()
            from    list   
            where   id + 1 < 100
        ) 
        select ID,left(val, charindex('-', val) - 2) from list
        option (maxrecursion 0)
    

提交回复
热议问题