我在Visual Studio 2015和SQL Server 2014中使用了SSRS/SSDT。存在大於8年的錯誤,其中you can't select multiple columns from different tables that have the same name。爲了解決這個問題,我需要使用子查詢。我發現每一個答案都會重寫給定的查詢以刪除子查詢,這通常會很好,但不適用於這種情況。如何將參數傳遞給SQL Server中的子查詢?將參數傳遞給子查詢
列別名不適用於此錯誤 - 使用AS
會在「重複」列上返回未知列錯誤,即使它與所有其他列一起使用。 SELECT
子句中的最後兩行工作是因爲正在查詢值以便報表可以使用它們,但實際查詢的其餘部分不使用它們。
這是我當前的代碼(因爲子查詢返回多行不起作用)。
SELECT t.[Description],
t.RequestedCompletionDate,
t.CommitDate,
t.StatusId,
t.PriorityId,
p.ProjectNumber,
s.Name AS StatusDescription,
pr.Name AS PriorityDescription
FROM ProjectTask t
inner join Project p
on p.Id = t.ProjectId
inner join Project_TaskStatus s
on s.Id = t.StatusId
inner join Project_Priority pr
on pr.Id = t.PriorityId
WHERE t.Type = 'ET'
AND t.StatusId NOT IN (4,7)
AND
(
SELECT StatusId FROM Project
-- WHERE ?
)
NOT IN (3, 4)
ORDER BY t.PriorityId,
t.CommitDate,
t.RequestedCompletionDate
這是帶有註釋中所要求的別名的代碼。它拋出一個錯誤:
SELECT t.[Description],
t.RequestedCompletionDate,
t.CommitDate,
t.StatusId AS TaskStatusId,
t.PriorityId,
p.ProjectNumber,
p.StatusId AS ProjectStatusId,
s.Name AS StatusDescription,
pr.Name AS PriorityDescription
FROM ProjectTask t
inner join Project p
on p.Id = t.ProjectId
inner join Project_TaskStatus s
on s.Id = TaskStatusId
inner join Project_Priority pr
on pr.Id = t.PriorityId
WHERE t.Type = 'ET'
AND TaskStatusId NOT IN (4,7)
AND ProjectStatusId NOT IN (3,4)
ORDER BY t.PriorityId,
t.CommitDate,
t.RequestedCompletionDate
-- Invalid column name 'TaskStatusId'.
-- Invalid column name 'TaskStatusId'.
-- Invalid column name 'TaskStatusId'.
-- Invalid column name 'ProjectStatusId'.
-- Invalid column name 'ProjectStatusId'.
理想的代碼如下,但它引發錯誤An item with the same key has already been added
,這是錯誤SSRS/SSDT試圖返回同名的多個列時拋出。
SELECT t.[Description],
t.RequestedCompletionDate,
t.CommitDate,
t.StatusId,
t.PriorityId,
p.ProjectNumber,
p.StatusId,
s.Name AS StatusDescription,
pr.Name AS PriorityDescription
FROM ProjectTask t
inner join Project p
on p.Id = t.ProjectId
inner join Project_TaskStatus s
on s.Id = t.StatusId
inner join Project_Priority pr
on pr.Id = t.PriorityId
WHERE t.Type = 'ET'
AND t.StatusId NOT IN (4,7)
AND p.StatusId NOT IN (3,4)
ORDER BY t.PriorityId,
t.CommitDate,
t.RequestedCompletionDate
只給'p.StatusId'像'p.StatusId作爲ProjectStatusId'什麼 – JamieD77
@ JamieD77,這不符合SSRS工作一個新的名字,我只是編輯這個問題要澄清一下。 – vaindil
爲什麼當您的示例查詢中沒有參數時,您的問題是「傳遞參數」? –