我想在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
感謝你的幫助。這也是工作,但我必須使用循環。我如何讓循環工作? –
@WannabeJavaGeek - 當你有更簡單的方法時,你爲什麼選擇更難的選項。 –
@WannabeJavaGeek - 要解決你目前的做法,請檢查Gordan的答案 –