Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server sql 2012中循环达到9时生成唯一登录id失败_Sql Server - Fatal编程技术网

Sql server sql 2012中循环达到9时生成唯一登录id失败

Sql server sql 2012中循环达到9时生成唯一登录id失败,sql-server,Sql Server,我试图在SQL 2012中为每个用户生成唯一的logoid,但当循环达到9时失败。logonid取名字、姓氏的第一个字母,预告片加上一个。当end为9时失败,因为在此之后的所有logoid都不是唯一的。请查看下面的代码,并告诉我代码中有什么错误 create database passwords go use passwords go create table Users ( id int identity(1,1), forename v

我试图在SQL 2012中为每个用户生成唯一的logoid,但当循环达到9时失败。logonid取名字、姓氏的第一个字母,预告片加上一个。当end为9时失败,因为在此之后的所有logoid都不是唯一的。请查看下面的代码,并告诉我代码中有什么错误

  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 'p@$$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 = 'p@$$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)+@surname

  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 = 'p@$$w0rd' 
    set @logonid = ''
    set @min = @min+1
       end 

    --use passwords
    --select * from Users

这是一种不可思议的神秘方式。那么:

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等等。这使得增加它们的逻辑更加简单。

这只是一种不可思议的神秘方法。那么:

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等等。这使得增加它们的逻辑更加容易。

为每个用户生成
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 'p@$$w0rd'
使用
持久化
选项创建
计算列
,可以节省在此表上执行
选择
时的时间

update Users set Usr_pwd = 'p@$$w0rd'
当我
选择表时

SELECT *
FROM   Users 
结果:

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

为每个用户生成
登录名
,不需要做这么多。使用自动生成的
ID
列生成
Login

你只需要这个

ALTER TABLE Users
  ADD logonid  as  Substring(forename, 1, 1) + surname + cast(id as varchar(50)) PERSISTED
  , Usr_pwd VARCHAR(40) DEFAULT 'p@$$w0rd'
使用
持久化
选项创建
计算列
,可以节省在此表上执行
选择
时的时间

update Users set Usr_pwd = 'p@$$w0rd'
当我
选择表时

SELECT *
FROM   Users 
结果:

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

谢谢你的帮助。这也是可行的,但我必须使用循环。我怎样才能使循环工作?@WannabeJavaGeek-当有一种更简单的方法时,你为什么选择更难的方法。@WannabeJavaGeek-要修复你当前的方法,请检查Gordan的回答谢谢你的帮助。这也是可行的,但我必须使用循环。我如何才能使循环工作?@WannabeJavaGeek-当有一种更简单的方法时,为什么选择更难的方法。@WannabeJavaGeek-要修复您当前的方法,请检查Gordan的答案