久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為 SQL Server 表中的用戶生成隨機令牌(唯一 ID)

Generate random tokens (unique id#39;s) for users in a table in SQL server(為 SQL Server 表中的用戶生成隨機令牌(唯一 ID))
本文介紹了為 SQL Server 表中的用戶生成隨機令牌(唯一 ID)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!

問題描述

我想在 sql server 中創(chuàng)建一個表并用數(shù)據(jù)(人們的信息)填充它.目標是讓每個人在第一行都有一個唯一的 ID,該 ID 必須以固定字母 GM 開頭,后跟 AZ2-9數(shù)字,名字的首字母,姓氏的首字母和 AZ2-9.

I want to create a table in sql server and fill it up with data (people's info). The goal is to get every person a unique ID in the first row which has to start with fixed alphabets GM followed by A-Z or 2-9 numeric , initial of First name, Initial of Last name and A-Z or 2-9.

推薦答案

注意:根據(jù)您所要求的邏輯,我認為您無法在不檢查表中不包含生成的值的情況下生成唯一值,唯一的方法是使用 NEWID() 函數(shù)生成 GUID,否則我認為總是存在重復(fù)風險

Note: With the logic you are requesting i don't think you can produce Unique value without check that the table doesn't contains the generated value, the only way is to use NEWID() function that generate a GUID, else i think that there is always a duplication risk

我不知道這是否是最好的方法,但我可以說它提供了預(yù)期的輸出.您可以創(chuàng)建此函數(shù)并使用它來生成標識符:

I don't know if this is the best way to do that, but i can say that it gives the expected output. You can create this function and use it to generate the identifier:

ALTER FUNCTION dbo.CreateIdentifier
(
    -- Add the parameters for the function here
    @Firstname varchar(255),
    @Lastname varchar(255),
    @random1 decimal(18,10) ,
    @random2 decimal(18,10)
)
RETURNS varchar(10)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @S VARCHAR(10)
    DECLARE @S1 VARCHAR(1)
    DECLARE @S2 VARCHAR(1)
    DECLARE @len INT
    DECLARE @Random1Fixed INT
    DECLARE @Random2Fixed INT

    declare @alphabet varchar(36) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789'



    SET @alphabet = REPLACE(@alphabet,LEFT(@Lastname,1),'')
    SET @alphabet = REPLACE(@alphabet,LEFT(@Firstname,1),'')

    SELECT @len = len(@alphabet)
    SET @Random1Fixed = ROUND(((@len - 1 -1) * @random1 + 1), 0)
    SET @Random2Fixed = ROUND(((@len - 1 -1) * @random2 + 1), 0)


    SET @S1 = substring(@alphabet, convert(int, @Random1Fixed ), 1)
    SET @S2 = substring(@alphabet, convert(int, @Random2Fixed), 1)

    SET @S = 'GM' + @S1 + LEFT(@Firstname,1) + LEFT(@Lastname,1) + @S2
    RETURN @S

END

您可以將其用作以下內(nèi)容

And you can use it as the following

SELECT dbo.CreateIdentifier('John','Wills',RAND(),RAND())

我將 RAND() 作為參數(shù)傳遞,因為它不能在函數(shù)中使用

I am passing RAND() as parameter because it cannot be used within a function

這篇關(guān)于為 SQL Server 表中的用戶生成隨機令牌(唯一 ID)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 在线视频99 | 美女一级片 | av网站免费在线观看 | 欧美综合在线观看 | 午夜影院免费观看 | 精品国产欧美一区二区三区成人 | 国产aaaaaa | 伊人操 | 婷婷激情综合网 | 成人国产精品久久久网站 | 亚洲成人免费在线观看 | 欧美日韩亚洲天堂 | 91成人免费 | 天天爽夜夜爽夜夜爽 | 精品久久久久久久久久久久久久久久 | 免费毛片在线 | 日本精品国产 | 天天操天天做 | 亚洲精选一区 | 青青青草视频 | 日韩精品福利 | 国产精品成人一区二区网站软件 | 国产激情在线视频 | 在线一级片 | 亚洲二区在线 | 久久久国产视频 | 日韩免费小视频 | 日韩av一级| 日韩在线免费播放 | 免费一级片 | 福利网站在线观看 | 日本视频一区二区三区 | 成人免费看片视频 | 国产理论在线 | 久久只有精品 | 九九热精品在线 | 欧美91 | 日韩国产欧美 | 国产免费一区二区 | 日韩福利视频 | 五月婷婷在线观看 |