将文本添加到字段以使其唯一

发布于 2024-08-22 03:08:34 字数 448 浏览 6 评论 0原文

我正在使用 SQL Server 2000,有一种情况是将数据从一个表复制到另一个表,目标数据表要求每个名称行都是唯一的。这是我的问题的一个简单示例

Source table
RowID | Name
1       A
2       B
3       B
4       B
5       C
6       D
7       C

我想做的是将其转入此

Destination table
RowID | Name
1       A
2       B
3       B(2)
4       B(3)
5       C
6       D
7       C(2)

名称列是 varchar(40),任何关于如何处理它的想法,我有 2561 行有重复项,因此手动执行是不是一个选择。

关于从哪里开始有什么想法吗?

I am using SQL Server 2000, I have a situation where I am copying data over from one table to another, the destination data table requires each Name row to be unique. Here is a quick example of my issue

Source table
RowID | Name
1       A
2       B
3       B
4       B
5       C
6       D
7       C

What I want to do is turn it in to this

Destination table
RowID | Name
1       A
2       B
3       B(2)
4       B(3)
5       C
6       D
7       C(2)

The Name column is a varchar(40), any idea on how do to it, I have 2561 rows that have duplicates so doing it by hand is not a option.

Any ideas on where to begin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛滥成性 2024-08-29 03:08:34

如果这是一次性交易,并且完成后您将创建一个唯一约束:

创建一个临时表
选择姓名
从表
按名称分组
具有 count(*) > 1

设置 rowCount=1
更新基表
在 temp.name=base.name 上加入临时表
set name = name + '(1)'

重复直到完成
删除所有 (1) 个条目

抱歉,您需要自己编写真正的 SQL。如果您有 SQL2K5,您可以使用 Row_Number() 来执行此操作。

If it's a 1-time deal and you're going to create a unique constraint when you get done:

create a temp table
select name
from table
group by name
having count(*) > 1

set rowCount=1
update base table
join temp table on temp.name=base.name
set name = name + '(1)'

repeat until done
remove all (1) entries

Sorry you'll need to write the real SQL yourself. If you had SQL2K5 you could use Row_Number() to do this.

掩于岁月 2024-08-29 03:08:34

你将需要一个光标。

像下面这样:

CREATE TABLE TempTable ( RowID INT IDENTITY PRIMARY KEY, SomeValue varchar(10))
INSERT INTO TempTable (SomeValue) VALUES( 'A')
INSERT INTO TempTable (SomeValue) VALUES( 'B')
INSERT INTO TempTable (SomeValue) VALUES( 'B')
INSERT INTO TempTable (SomeValue) VALUES( 'B')
INSERT INTO TempTable (SomeValue) VALUES( 'C')
INSERT INTO TempTable (SomeValue) VALUES( 'C')
INSERT INTO TempTable (SomeValue) VALUES( 'D')
INSERT INTO TempTable (SomeValue) VALUES( 'D')
INSERT INTO TempTable (SomeValue) VALUES( 'D')
INSERT INTO TempTable (SomeValue) VALUES( 'D')


CREATE TABLE #Counts (SomeValue varchar(10), ValCount int CONSTRAINT COunts_Unique UNIQUE(SomeValue))
INSERT INTO #Counts(SomeValue, ValCount)
SELECT DISTINCT SomeValue, 0 FROM TempTable

DECLARE @RowID int
DECLARE @SomeValue VARCHAR(10)
DECLARE @ValCount int
DECLARE curs CURSOR for SELECT RowID, SomeValue FROM TempTable ORDER BY RowID ASC
OPEN curs
FETCH NEXT FROM curs into @RowID, @SomeValue
WHILE(@@FETCH_STATUS = 0)
BEGIN
    SELECT @ValCount = ValCount FROM #Counts WHERE SomeValue = @SomeValue
    IF(@ValCount > 0)
    BEGIN
        UPDATE TempTable 
        SET SomeValue = SomeValue + '(' + Convert(varchar, @valCount) + ')'
        WHERE RowID = @RowID
    END

    UPDATE #Counts SET ValCount = ValCount + 1 where SomeValue = @SomeValue

    FETCH NEXT FROM curs into @RowID, @SomeValue
END
CLOSE curs
DEALLOCATE curs

DROP TABLE #Counts

You're going to need a cursor.

Something like below:

CREATE TABLE TempTable ( RowID INT IDENTITY PRIMARY KEY, SomeValue varchar(10))
INSERT INTO TempTable (SomeValue) VALUES( 'A')
INSERT INTO TempTable (SomeValue) VALUES( 'B')
INSERT INTO TempTable (SomeValue) VALUES( 'B')
INSERT INTO TempTable (SomeValue) VALUES( 'B')
INSERT INTO TempTable (SomeValue) VALUES( 'C')
INSERT INTO TempTable (SomeValue) VALUES( 'C')
INSERT INTO TempTable (SomeValue) VALUES( 'D')
INSERT INTO TempTable (SomeValue) VALUES( 'D')
INSERT INTO TempTable (SomeValue) VALUES( 'D')
INSERT INTO TempTable (SomeValue) VALUES( 'D')


CREATE TABLE #Counts (SomeValue varchar(10), ValCount int CONSTRAINT COunts_Unique UNIQUE(SomeValue))
INSERT INTO #Counts(SomeValue, ValCount)
SELECT DISTINCT SomeValue, 0 FROM TempTable

DECLARE @RowID int
DECLARE @SomeValue VARCHAR(10)
DECLARE @ValCount int
DECLARE curs CURSOR for SELECT RowID, SomeValue FROM TempTable ORDER BY RowID ASC
OPEN curs
FETCH NEXT FROM curs into @RowID, @SomeValue
WHILE(@@FETCH_STATUS = 0)
BEGIN
    SELECT @ValCount = ValCount FROM #Counts WHERE SomeValue = @SomeValue
    IF(@ValCount > 0)
    BEGIN
        UPDATE TempTable 
        SET SomeValue = SomeValue + '(' + Convert(varchar, @valCount) + ')'
        WHERE RowID = @RowID
    END

    UPDATE #Counts SET ValCount = ValCount + 1 where SomeValue = @SomeValue

    FETCH NEXT FROM curs into @RowID, @SomeValue
END
CLOSE curs
DEALLOCATE curs

DROP TABLE #Counts
拥抱我好吗 2024-08-29 03:08:34

我决定不需要每次都从 1 开始,所以我决定复制产品末尾的行 ID 列的内容。

update #Inv
set name = left(rtrim(name), 40-len(RowId)-1) + ' ' + RowId
where name in (SELECT distinct name
               FROM [#Inv] a
               WHERE exists (select [name] from [#Inv] where not [RowId] = a.[RowId] and [name] = a.[name]))

I have decided that I do not need it starting at 1 every time so I just decided to copy the contence of the row ID column at the end of the product.

update #Inv
set name = left(rtrim(name), 40-len(RowId)-1) + ' ' + RowId
where name in (SELECT distinct name
               FROM [#Inv] a
               WHERE exists (select [name] from [#Inv] where not [RowId] = a.[RowId] and [name] = a.[name]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文