当我执行“select union select”时,我得到正确的数字或行(156) 独立执行,选择#1返回65行,选择#2返回138行。
当我使用“select union select”和Insert into时,我得到203行(65 + 138),带有重复项。
我想知道是否是导致此问题的代码结构?
INSERT INTO dpapm_MediaObjectValidation (mediaobject_id, username, checked_date, expiration_date, notified)
(SELECT FKMediaObjectId, CreatedBy,@checkdate,dateadd(ww,2,@checkdate),0
FROM dbo.gs_MediaObjectMetadata
LEFT JOIN gs_MediaObject mo
ON gs_MediaObjectMetadata.FKMediaObjectId = mo.MediaObjectId
WHERE UPPER([Description]) IN ('CAPTION','TITLE','AUTHOR','DATE PHOTO TAKEN','KEYWORDS')
AND FKMediaObjectId >=
(SELECT TOP 1 MediaObjectId
FROM dbo.gs_MediaObject
WHERE DateAdded > @lastcheck
ORDER BY MediaObjectId)
GROUP BY FKMediaObjectId, CreatedBy
HAVING count(*) < 5
UNION
SELECT FKMediaObjectId, CreatedBy,getdate(),dateadd(ww,2,getdate()),0
FROM gs_MediaObjectMetadata yt
LEFT JOIN gs_MediaObject mo
ON yt.FKMediaObjectId = mo.MediaObjectId
WHERE UPPER([Description]) = 'KEYWORDS'
AND FKMediaObjectId >=
(SELECT TOP 1 MediaObjectId
FROM dbo.gs_MediaObject
WHERE DateAdded > @lastcheck
ORDER BY MediaObjectId)
AND NOT EXISTS
(
SELECT *
FROM dbo.fnSplit(Replace(yt.Value, '''', ''''''), ',') split
WHERE split.item in (SELECT KeywordEn FROM gs_Keywords) or split.item in (SELECT KeywordFr FROM gs_Keywords)
)
)
我很感激解决这个问题的任何线索......
谢谢!
答案 0 :(得分:0)
UNION关键字应仅返回两个查询之间的不同记录。但是,如果我没记错的话,只有数据类型相同时才会出现这种情况。日期变量可能会抛弃它。根据排序规则类型,空格也可能会有不同的处理方式。您可能希望在执行插入后在dpapm_MediaObjectValidation表上执行SELECT DISTINCT,但请务必在比较中修剪两侧的空白。另一种方法是进行第一次插入,然后在第二次插入时,完全放弃UNION并进行手动EXISTS检查以查看要插入的项目是否已存在。