现在我知道没有UPDATE INTO
这样的命令,但是有可能做类似的事情,而不是基于非主键列的INSERT INTO
吗?
例如,我想检查给定人员是否已存在具有今天日期的记录。如果该值已经存在,则使用新数字更新记录,如果它不存在(比如说明天运行该脚本),则将其插入表中。
以下是我正在使用的代码;
if OBJECT_ID('dbo.Temp') IS NULL
create table [dbo].[Temp] (
UserID int,
FullName varchar(300),
DocsInBatch int,
NumReviewed int,
Outstanding int,
Relevant int,
HighlyRelevant int,
Irrelevant int,
OutOfScope int,
Question int,
TechnicalQuestion int,
LastUpdate date
PRIMARY KEY (UserID)
)
insert INTO [dbo].[Temp]
SELECT a.UserID,
a.FullName,
a.DocsInBatch,
a.NumReviewed,
(a.DocsInBatch - a.NumReviewed) AS "Outstanding",
a.Relevant,
a.[Highly Relevant],
a.[Irrelevant],
a.[Out-Of-Scope],
a.[Question],
a.[Technical Question],
GetDate()
FROM
(
SELECT au.UserID as [UserID],
au.FullName,
count(1) AS "DocsInBatch",
count([Document1LR_Relevancy].[CoArtID]) AS "NumReviewed",
sum(case when [Document1LR_Relevancy].[1LR_Relevancy] = 'Relevant' then 1 else 0 end) as 'Relevant',
sum(case when [Document1LR_Relevancy].[1LR_Relevancy] = 'Highly Relevant' then 1 else 0 end) as 'Highly Relevant',
sum(case when [Document1LR_Relevancy].[1LR_Relevancy] = 'Irrelevant' then 1 else 0 end) as 'Irrelevant',
sum(case when [Document1LR_Relevancy].[1LR_Relevancy] = 'Out-Of-Scope' then 1 else 0 end) as 'Out-Of-Scope',
sum(case when [Document1LR_Relevancy].[1LR_Relevancy] = 'Question' then 1 else 0 end) as 'Question',
sum(case when [Document1LR_Relevancy].[1LR_Relevancy] = 'Technical Question' then 1 else 0 end) as 'Technical Question'
from [EDDS1024194].[EDDSDBO].[Document] doc
LEFT JOIN (
SELECT XXX.ArtifactID, co.Name AS [1LR_Relevancy], Co.ArtifactID [CoArtID]
FROM [EDDSDBO].[Document] AS XXX WITH (NOLOCK)
INNER JOIN [EDDSDBO].[ZCodeArtifact_1001288] AS ca WITH (NOLOCK)
ON XXX.[ArtifactID] = ca.[AssociatedArtifactID]
INNER JOIN [EDDSDBO].[Code] AS co WITH (NOLOCK)
ON ca.[CodeArtifactID] = co.[ArtifactID]
) AS [Document1LR_Relevancy]
ON doc.[ArtifactID] = [Document1LR_Relevancy].[ArtifactID]
INNER JOIN [EDDS1024194].[EDDSDBO].[DocumentBatch] db on
db.DocumentArtifactID = doc.ArtifactID
INNER JOIN [EDDS1024194].[EDDSDBO].[Batch] bat on
bat.ArtifactID = db.BatchArtifactID
INNER JOIN [EDDS1024194].[EDDSDBO].[AuditUser] au on
au.UserID = bat.AssignedTo
GROUP BY au.FullName, au.UserID
)a
ORDER BY a.FullName
此代码有效。它每次运行时都会在表中插入新记录,但我不确定如何进行上述操作。