当我执行以下查询时,它将同一行添加4次到我的表中。
INSERT INTO [Project].[dbo].[Document]
(
project_id,
classification_id,
document_nid,
number,
title_1,
title_2,
title_3,
revision,
revision_description_id,
date_revision,
number_external,
revision_external,
created_by_id,
modified_by_id,
date_modified,
filename,
file_extension,
file_size,
source_id,
pdf,
status_id,
hidden
)
SELECT p.id,
v.classification,
v.nid,
v.docNo,
v.title1,
v.title2,
v.title3,
v.rev,
rd.id,
v.dateRevision,
v.docNoExt,
v.revExt,
cr.id,
mo.id,
v.dateModified,
v.filename,
v.fileExtension,
v.fileSize,
co.id,
v.pdf,
v.status,
v.hidden
FROM (VALUES ('00000001', 80, 4, 'DOC004', 'Title 1', 'Title 2', 'Title 3', '0', 'Final', CURRENT_TIMESTAMP, NULL, NULL, 'User Name', 'User Name', CURRENT_TIMESTAMP, 'DOC004' + '_' + '0', '.xlsx', '999 MB', 'MyCompany', 0, 1, 0))
v(project, classification, nid, docNo, title1, title2, title3, rev, revision_description, dateRevision, docNoExt, revExt, created_by, modified_by, dateModified, filename, fileExtension, fileSize, source, pdf, status, hidden)
LEFT JOIN [MyCompany].[dbo].[Project] p
ON p.number = v.project
LEFT JOIN [MyApp].[dbo].[Revision_Description] rd
ON rd.name = v.revision_description
LEFT JOIN [MyCompany].[dbo].[User] cr
ON cr.first_name + ' ' + cr.last_name = v.created_by
LEFT JOIN [MyCompany].[dbo].[User] mo
ON mo.first_name + ' ' + mo.last_name = v.modified_by
LEFT JOIN [MyCompany].[dbo].[Company] co
ON co.name = v.source;
我只想要1个条目,但不确定为什么要输入4个。
查询关联4个表...
任何帮助将不胜感激。
我想在此表上重新启用唯一键,这使我无法做到这一点。
编辑:
现在正在检索添加了machine_name的2行:
LEFT JOIN [MyCompany].[dbo].[User] cr
ON cr.first_name + ' ' + cr.last_name = v.created_by AND cr.machine_name = 'DESKTOP-01'
LEFT JOIN [MyCompany].[dbo].[User] mo
ON mo.first_name + ' ' + mo.last_name = v.modified_by AND cr.machine_name = 'DESKTOP-01'
如果您查看2个id
条目之后的2个NULL
列,则第一行的ID仍应为id = 2(如前一列id一样),它仍选择id = 1 2是具有正确的machine_name。