英文:
Prevent duplicated value
问题
我有一个名为Process
的表,其列大致如下:
Hold, GId, Source, Type, Operation
还有一个看起来类似于这样的Temp_Table
:
Hold, GId, Source, Type
Hold_Table
看起来类似于这样:
Hold, GId, Source, Type
我比较Temp_Table
和Hold_Table
,如果Hold_Inv
表中未找到Source,就将记录插入到Process
表中,并使用"Add"操作。
插入到Process表 (Hold, GId, Source, Type, Operation)
选择 Hold, GID, Source, Type, 'ADD' as Operation
从Temp_Table中
其中Source在 (
选择Source
从 (
选择Source
从Temp_Table Thi
其中NOT EXISTS (
选择1
从Hold_Table Hi
其中Thi.Source = Hi.Source
)
并且Thi.Source <> 'NOT FOUND'
并且LEN(TRIM(ISNULL(Thi.Source, ''))) > 0
) T1
)
我运行这个查询两次,然后来自Temp_Table的记录就会在Process表中插入两次。
如果一个列的值不同,它应该插入两次,但如果它们全部相同,那就不应该再插入两次。
任何帮助或建议都会受到极大的赞赏。
英文:
I have a a table called Process
that has columns something like this
Hold, GId, Source, Type, Operation
and Temp_Table
that looks something like this
Hold, GId, Source, Type
Hold_Table
looks something like this
Hold, GId, Source, Type
I compare Temp_Table
and Hold_Table
and insert the record into Process
table with "Add" Operation if the Source is not found in Hold_Inv
table but I'm just wondering how can I prevent it from inserting duplicated value into Process
table.
INSERT INTO Process (Hold, GId, Source, Type, Operation)
SELECT Hold, GID, Source, Type, 'ADD' Operation
FROM Temp_Table
WHERE Source IN (
SELECT Source
FROM (
SELECT Source
FROM Temp_Table Thi
WHERE NOT EXISTS (
SELECT 1
FROM Hold_Table Hi
WHERE Thi.Source = Hi.Source
)
AND Thi.Source <> 'NOT FOUND'
AND LEN(TRIM(ISNULL(Thi.Source, ''))) > 0
) T1
)
I run the query twice then the record from Temp_Table
are inserted twice into the Process
table.
It should be inserting twice if one column value is different but if they all the same then it shouldn't be inserting twice anymore.
Any help or suggestion would be really appreciated.
答案1
得分: 1
以下是翻译好的部分:
首先,您的查询可以简化为:
INSERT INTO Process (Hold, GId, Source, Type, Operation)
SELECT Hold, GID, [Source], [Type], 'ADD' Operation
FROM Temp_Table Thi
WHERE
NOT EXISTS (
SELECT 1
FROM Hold_Table Hi
WHERE Thi.Source = Hi.Source
)
AND Thi.Source <> 'NOT FOUND'
AND LEN(TRIM(ISNULL(Thi.Source, ''))) > 0
AND NOT EXISTS (
SELECT 1
FROM Process Pro
WHERE
Thi.Hold = Pro.Hold and
Thi.GID = Pro.GID and
Thi.[Source] = Pro.[Source] and
Thi.[Type] = Pro.[Type]
)
要排除重复插入,您可以在SQL中添加目标表格到NOT EXISTS中。
英文:
First of, your query can be simplified from :
INSERT INTO Process (Hold, GId, Source, Type, Operation)
SELECT Hold, GID, [Source], [Type], 'ADD' Operation
FROM Temp_Table
WHERE Source IN (
SELECT Source
FROM (
SELECT Source
FROM Temp_Table Thi
WHERE NOT EXISTS (
SELECT 1
FROM Hold_Table Hi
WHERE Thi.Source = Hi.Source
)
AND Thi.Source <> 'NOT FOUND'
AND LEN(TRIM(ISNULL(Thi.Source, ''))) > 0
) T1
)
To :
INSERT INTO Process (Hold, GId, Source, Type, Operation)
SELECT Hold, GID, [Source], [Type], 'ADD' Operation
FROM Temp_Table Thi
WHERE NOT EXISTS (
SELECT 1
FROM Hold_Table Hi
WHERE Thi.Source = Hi.Source
)
AND Thi.Source <> 'NOT FOUND'
AND LEN(TRIM(ISNULL(Thi.Source, ''))) > 0
To exclude insert of duplicates, you can add the target table in the SQL in the NOT EXISTS :
INSERT INTO Process (Hold, GId, Source, Type, Operation)
SELECT Hold, GID, [Source], [Type], 'ADD' Operation
FROM Temp_Table Thi
WHERE
NOT EXISTS (
SELECT 1
FROM Hold_Table Hi
WHERE Thi.Source = Hi.Source
)
AND Thi.Source <> 'NOT FOUND'
AND LEN(TRIM(ISNULL(Thi.Source, ''))) > 0
AND NOT EXISTS (
SELECT 1
FROM Process Pro
WHERE
Thi.Hold = Pro.Hold and
Thi.GID = Pro.GID and
Thi.[Source] = Pro.[Source] and
Thi.[Type] = Pro.[Type]
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论