使用UQ列

时间:2015-06-10 10:54:36

标签: sql-server tsql

我正在从少数源数据库迁移用户信息,并在目标中生成用户名到以两种方式编译的用户:1。如果用户不属于'usergroup',则username是lastname + runningnumber(for duplicates)2。它们属于usergroup,用户名为usergroup + runningnumber。 RunningNumber不应该是所有用户的“通用”,而是仅限于重复f.ex

User1,User2,User3,UserGroup1,UserGroup2,UserGroup3

有人告诉我,在SSIS之外实现这一点会更简单。

运行查询时,dest.db中可能已存在重复值,或者可能通过查询生成它们。

这是我当前的查询(工作不正常),我在处理此问题时需要一些帮助:

SET XACT_ABORT OFF ;
BEGIN
DECLARE @Any_error int;
WITH Kep as (SELECT Id,  CASE WHEN UserGroup IS NULL THEN CAST(LEFT(LOWER(REPLACE(LastName, ' ','')), 10) +'-'+ 
RIGHT(CAST(ROW_NUMBER() OVER(PARTITION BY LEFT(LOWER(REPLACE(LastName, ' ','')), 10) ORDER BY LastName) as nvarchar(50)),4) AS nvarchar(50)) ELSE CAST(LEFT(LOWER(REPLACE(UserGroup, ' ','')), 10) +'-'+ 
RIGHT(+CAST(ROW_NUMBER() OVER(PARTITION BY LEFT(LOWER(REPLACE(UserGroup, ' ','')), 10) ORDER BY UserGroup) as nvarchar(50)),4) AS nvarchar(50)) END AS rn
FROM Users)
UPDATE TOP(1000) Users
SET UserName = rn
SELECT @Any_error = @@ERROR
IF @Any_error = 2627 GOTO ErrorHandler

ErrorHandler: 
 DECLARE @RunningNumber INT
 SET @RunningNumber = 1
 Loop:
   BEGIN 
     WHILE (@Any_error = 2627)
SET @RunningNumber = @RunningNumber+1;
       WITH Kep as (SELECT Id,  CASE WHEN UserGroup IS NULL THEN CAST(LEFT(LOWER(REPLACE(LastName, ' ','')), 10) +'-'+ 
        RIGHT(+CAST(ROW_NUMBER() OVER(PARTITION BY LEFT(LOWER(REPLACE(LastName, ' ','')), 10) ORDER BY LastName)+@RunningNumber as nvarchar(50)),4) AS nvarchar(50)) ELSE CAST(LEFT(LOWER(REPLACE(UserGroup, ' ','')), 10) +'-'+ 
        RIGHT(+CAST(ROW_NUMBER() OVER(PARTITION BY LEFT(LOWER(REPLACE(UserGroup, ' ','')), 10) ORDER BY UserGroup)+@RunningNumber as nvarchar(50)),4) AS nvarchar(50)) END AS rn
        FROM Users)
        UPDATE Users
        SET UserName = rn
        SELECT @Any_error = @@ERROR
        IF (@Any_error = 2627) GOTO Loop;
   END
END

非常感谢任何建议/帮助!

编辑:数据..

来源

LastName    UserGroup
Smith       Sales
Smith       Sales
Smith       NULL
Smith       NULL
Johnson     Development
Johnson     NULL

目标

LastName    UserGroup       Username
Smith       Sales           sales-1
Smith       Sales           sales-2
Smith       NULL            smith-1
Smith       NULL            smith-2
Johnson     Development     development-1
Johnson     NULL            johnson-1

2 个答案:

答案 0 :(得分:2)

哎呀,你的查询看起来很吓人。尝试这样的事情:

我的桌子版本

DECLARE @yourTable TABLE (LastName VARCHAR(15),UserGroup VARCHAR(15));
INSERT INTO @yourTable
VALUES  ('Smith','Sales'),
        ('Smith','Sales'),
        ('Brown','Sales'), --added this row
        ('Smith',NULL),
        ('Smith',NULL),
        ('Johnson','Development'),
        ('Johnson',NULL);

DECLARE @destinationTable TABLE (LastName VARCHAR(15),UserGroup VARCHAR(15),UserName VARCHAR(15))
INSERT INTO @destinationTable
VALUES  ('Smith',NULL,'Smith-1'),
        ('Stevens','Sales','Sales-1'),
        ('Stevens','Sales','Sales-2'),
        ('Lopez','Development','Development-1');

实际查询

--INSERT INTO @destinationTable
SELECT  LastName,
        UserGroup,
        COALESCE(UserGroup,LastName) 
        + '-'
        + CAST(ROW_NUMBER() OVER (PARTITION BY COALESCE(UserGroup,LastName)  ORDER BY (SELECT NULL)) + COALESCE(max_num,0) AS VARCHAR(10))
        AS UserName
FROM @yourTable AS A
CROSS APPLY (
                SELECT MAX(CAST(SUBSTRING(UserName,CHARINDEX('-',UserName) + 1,1000) AS INT)) --finds maximum number already used in destination table
                FROM @destinationTable AS B
                WHERE COALESCE(A.UserGroup,A.LastName) = COALESCE(B.UserGroup,B.LastName)
            ) CA(max_num)

结果:

LastName        UserGroup       UserName
--------------- --------------- --------------------------
Johnson         Development     Development-2
Johnson         NULL            Johnson-1
Smith           Sales           Sales-3
Smith           Sales           Sales-4
Brown           Sales           Sales-5
Smith           NULL            Smith-2
Smith           NULL            Smith-3

答案 1 :(得分:0)

如果您的LastName与UserGroup相同,则可能会产生重复

SELECT  LastName,
        UserGroup,
        LEFT(LOWER(REPLACE(UserGroup, ' ','')), 10) 
        + '-'
        + CAST(ROW_NUMBER() OVER (PARTITION BY LEFT(LOWER(REPLACE(UserGroup, ' ','')), 10) ORDER BY (SELECT NULL)) AS VARCHAR(10))
        AS UserName
FROM Users 
WHERE UserGroup is not null  

SELECT  LastName,
        UserGroup,
        LEFT(LOWER(REPLACE(LastName, ' ','')), 10)
        + '-'
        + CAST(ROW_NUMBER() OVER (PARTITION BY LEFT(LOWER(REPLACE(LastName, ' ','')), 10) ORDER BY (SELECT NULL)) AS VARCHAR(10))
        AS UserName
FROM Users 
WHERE UserGroup is null