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

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

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

問題描述

我想在 sql server 中創(chuàng)建一個(gè)表并用數(shù)據(jù)(人們的信息)填充它.目標(biāo)是讓每個(gè)人在第一行都有一個(gè)唯一的 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ù)您所要求的邏輯,我認(rèn)為您無法在不檢查表中不包含生成的值的情況下生成唯一值,唯一的方法是使用 NEWID() 函數(shù)生成 GUID,否則我認(rèn)為總是存在重復(fù)風(fēng)險(xiǎn)

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ù)并使用它來生成標(biāo)識(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ù)傳遞,因?yàn)樗荒茉诤瘮?shù)中使用

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

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
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?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問題和答案的組合))
主站蜘蛛池模板: av中文字幕网 | 精品国产一区二区三区成人影院 | 亚洲欧美国产精品久久 | av在线二区 | 一区二区三区视频在线免费观看 | 国产精品视频免费观看 | 久久中文字幕在线 | 欧美综合网 | 国产成人精品久久 | 91 久久 | 在线视频亚洲 | 国产精品视频免费播放 | 91精品久久久久久久久久入口 | 亚洲日韩欧美一区二区在线 | 久久小视频 | 国产精品永久久久久久久www | 日韩精品在线看 | 日韩高清一区二区 | 国产一区三区在线 | 国产精品国产精品 | 国产探花在线精品一区二区 | 国产91亚洲精品一区二区三区 | 亚洲成人精品 | 天天干天天爱天天操 | 亚洲h色| 另类二区 | 一区二区精品 | 特黄色毛片 | 久久99精品久久久久久 | 一区二区在线不卡 | 久久国产成人午夜av影院武则天 | 久久99精品久久久久久国产越南 | 亚洲国产成人精品久久久国产成人一区 | 欧美激情一区二区三级高清视频 | 黄色精品| 欧美一区二区三区免费电影 | 97人人草| 日本一区二区三区四区 | 国产精品福利视频 | 国产精品女人久久久 | 日韩第一页|