防止重复数值

huangapple go评论64阅读模式
英文:

Prevent duplicated value

问题

我有一个名为Process的表,其列大致如下:

Hold, GId, Source, Type, Operation

还有一个看起来类似于这样的Temp_Table

Hold, GId, Source, Type

Hold_Table看起来类似于这样:

Hold, GId, Source, Type

我比较Temp_TableHold_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, &#39;ADD&#39; 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 &lt;&gt; &#39;NOT FOUND&#39;
            AND LEN(TRIM(ISNULL(Thi.Source, &#39;&#39;))) &gt; 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], &#39;ADD&#39; 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 &lt;&gt; &#39;NOT FOUND&#39;
            AND LEN(TRIM(ISNULL(Thi.Source, &#39;&#39;))) &gt; 0
        ) T1
    )  

To :

INSERT INTO Process (Hold, GId, Source, Type, Operation)
SELECT Hold, GID, [Source], [Type], &#39;ADD&#39; Operation 
FROM Temp_Table Thi
    WHERE NOT EXISTS (
        SELECT 1
        FROM Hold_Table Hi 
        WHERE Thi.Source = Hi.Source
    )
    AND Thi.Source &lt;&gt; &#39;NOT FOUND&#39;
    AND LEN(TRIM(ISNULL(Thi.Source, &#39;&#39;))) &gt; 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], &#39;ADD&#39; Operation 
FROM Temp_Table Thi
WHERE 
NOT EXISTS (
    SELECT 1
    FROM Hold_Table Hi 
    WHERE Thi.Source = Hi.Source
)
AND Thi.Source &lt;&gt; &#39;NOT FOUND&#39;
AND LEN(TRIM(ISNULL(Thi.Source, &#39;&#39;))) &gt; 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] 
)

huangapple
  • 本文由 发表于 2023年6月9日 03:36:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定