久久久久久久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)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想在 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,否則我認為總是存在重復風險

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

我不知道這是否是最好的方法,但我可以說它提供了預期的輸出.您可以創(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精品国产 | 欧美日韩高清在线观看 | 成人性视频免费网站 | 国产精品极品美女在线观看免费 | 欧美日韩亚洲国产综合 | 久热精品免费 | 国产精品99999999 | 亚洲欧洲视频 | 日韩中文字幕 | 黄色一级片aaa | 狠狠干天天干 | 亚洲精品国产电影 | 亚洲有码转帖 | 成人综合久久 | 免费久久久| 亚洲国产成人久久综合一区,久久久国产99 | 成人免费视频网站在线看 | 欧美视频偷拍 | 国产精品美女久久久 | 国产盗摄视频 | 日韩国产在线观看 | 婷婷精品 | 欧美 日韩 国产 成人 在线 | 视频一二三区 | 日韩另类 | 国产精品一区二区欧美 | 欧美亚洲在线 | 日本在线免费看最新的电影 | 在线看av的网址 | 国产精品色av | 国产精品区二区三区日本 | 精品一区二区三区电影 | 欧美亚洲在线视频 | 国产一二三区在线 | 欧美456| 久久精品性视频 | 国产剧情一区二区三区 | 黄网站在线播放 | 亚洲欧美日韩成人在线 | 国产高清视频在线观看播放 | 操久久久|