2016-10-23 14 views
0

我想在SQL 2012中爲每個用戶生成獨特的邏輯實例,但它循環達到9時失敗。該logonid採取的第一個字母的名字,姓氏和預告片這是添加一個。當結束符爲9時失敗,因爲在此之後所有的邏輯符號都不是唯一的。請看看下面的代碼,告訴我什麼是錯的代碼生成唯一的登錄ID失敗,當循環達到9在SQL 2012

create database passwords 
    go 
    use passwords 
    go 

    create table Users 
    (
    id int identity(1,1), 
    forename varchar(80), 
    surname varchar(40) 
    ) 
    go 
    INSERT INTO USERS VALUES('Peter','Kimani'),('Paul','Kimani'), 
('Pius','Kimani'),('Pyuwa','Kimani'), 
('Poetry','Kimani'),('Pig','Kimani'),('Paul','Kimani'), 
('Pk','Kimani'),('Paul','Kimani'),('Petra','Kimani'), 
('Paul','Kimani'),('Popeye','Kimani'),('George','Onyango') 

    go 
    --select * from Users 
    go 
    alter table Users add 
    logonid varchar(40), 
    Usr_pwd varchar(40) default '[email protected]$$w0rd' 
    go 
    --trim any spaces in the data. (OPTIONAL) 

    update Users set surname = LTRIM(rtrim(surname)), 
    forename = LTRIM(rtrim(forename)) 
    go 

    declare @fname varchar(40), @surname varchar(40), @logonid varchar(40), 
    @pass varchar(40), @min int, @max int, @trailer varchar(10), @end int 

    -- there are 2 ways to do this. as below Or 
    select @min = MIN(id), @max = MAX(id) from Users 
    /* 
set @min = (select min(id) from Users) 
set @max = (select max(id) from Users) 
*/ 
set @fname = '' set @surname = '' set @pass = '[email protected]$$w0rd' 
set @logonid = '' 

while @min <= @max--loops through from smallest to biggest 
begin 
    select @fname = forename, @surname = surname from Users where ID = @min 

    set @logonid = SUBSTRING(@fname,1,1)[email protected] 

    if not exists(select '1' from Users where logonid = @logonid) 
     update Users set logonid = @logonid, Usr_pwd = @pass 
     where ID = @min 
    else 

    begin 
     set @logonid = (select top 1 logonid from Users 
     where logonid like @logonid+'%' order by logonid desc) 
     set @trailer = RIGHT(@logonid,1) 
     if ISNUMERIC(@trailer) = 1 
     begin 
       set @end = @trailer 
       set @end = @end+1 
       set @logonid = SUBSTRING(@logonid, 1, len(@logonid)-1) 
       print @end print @trailer print @min 
       set @logonid = @logonid+CAST(@end as varchar) 
       print @logonid 
       update Users set logonid = @logonid, Usr_pwd = @pass 
      where ID = @min 
     end 
     else 
     begin 
       set @logonid = @logonid+'1' 
       update Users set logonid = @logonid, Usr_pwd = @pass 
      where ID = @min 
     end 
     end 
    set @fname = '' set @surname = '' set @pass = '[email protected]$$w0rd' 
    set @logonid = '' 
    set @min = @min+1 
     end 

    --use passwords 
    --select * from Users 

輸出

id forenamesurname logoid  Usr_pwd 
1 Peter Kimani PKimani  [email protected]$$w0rd 
2 Paul Kimani PKimani1 [email protected]$$w0rd 
3 Pius Kimani PKimani2 [email protected]$$w0rd 
4 Pyuwa Kimani PKimani3 [email protected]$$w0rd 
5 Poetry Kimani PKimani4 [email protected]$$w0rd 
6 Pig  Kimani PKimani5 [email protected]$$w0rd 
7 Paul Kimani PKimani6 [email protected]$$w0rd 
8 Pk  Kimani PKimani7 [email protected]$$w0rd 
9 Paul Kimani PKimani8 [email protected]$$w0rd 
10 Petra Kimani PKimani9 [email protected]$$w0rd 
11 Paul Kimani PKimani10 [email protected]$$w0rd 
12 Popeye Kimani PKimani10 [email protected]$$w0rd 
13 George Onyango GOnyango [email protected]$$w0rd 

回答

2

這僅僅是做這一個令人難以置信的神祕方式。如何:

select @logon_suffix = coalesce(max(cast(substr(logon, patindex('%[0-9]%', logon + '0'), 100) as int) + 1, 1) 
from users u 
where logon like @base_logon + '[0-9]%; 

這應該爲您計算沒有循環的後綴。

注:我不會用這種方式表示重複值。我會使用零填充後綴,例如0001,0002等。這使得增加它們的邏輯更容易。

1

您不必爲此爲每個用戶生成Login。使用自動生成的ID列生成Login

你只需要這個

ALTER TABLE Users 
    ADD logonid as Substring(forename, 1, 1) + surname + cast(id as varchar(50)) PERSISTED 
    , Usr_pwd VARCHAR(40) DEFAULT '[email protected]$$w0rd' 

創建你computed columnPERSISTED選項能節省一些時間,當你在這個表上

update Users set Usr_pwd = '[email protected]$$w0rd' 

執行select當我Select

SELECT * 
FROM Users 

結果:

+----+----------+---------+------------+----------+ 
| id | forename | surname | logonid | Usr_pwd | 
+----+----------+---------+------------+----------+ 
| 1 | Peter | Kimani | PKimani1 | [email protected]$$w0rd | 
| 2 | Paul  | Kimani | PKimani2 | [email protected]$$w0rd | 
| 3 | Pius  | Kimani | PKimani3 | [email protected]$$w0rd | 
| 4 | Pyuwa | Kimani | PKimani4 | [email protected]$$w0rd | 
| 5 | Poetry | Kimani | PKimani5 | [email protected]$$w0rd | 
| 6 | Pig  | Kimani | PKimani6 | [email protected]$$w0rd | 
| 7 | Paul  | Kimani | PKimani7 | [email protected]$$w0rd | 
| 8 | Pk  | Kimani | PKimani8 | [email protected]$$w0rd | 
| 9 | Paul  | Kimani | PKimani9 | [email protected]$$w0rd | 
| 10 | Petra | Kimani | PKimani10 | [email protected]$$w0rd | 
| 11 | Paul  | Kimani | PKimani11 | [email protected]$$w0rd | 
| 12 | Popeye | Kimani | PKimani12 | [email protected]$$w0rd | 
| 13 | George | Onyango | GOnyango13 | [email protected]$$w0rd | 
+----+----------+---------+------------+----------+ 
+0

感謝你的幫助。這也是工作,但我必須使用循環。我如何讓循環工作? –

+0

@WannabeJavaGeek - 當你有更簡單的方法時,你爲什麼選擇更難的選項。 –

+0

@WannabeJavaGeek - 要解決你目前的做法,請檢查Gordan的答案 –