我正在研究一個項目,該項目將允許用戶通過選擇列來構建查詢。SqlCommand在嘗試使用c執行查詢時返回錯誤#
我的應用程序似乎工作正常,並生成正確的查詢。但是,當我執行查詢我得到下面的異常
列「Tasks.Duration」在選擇列表中無效,因爲它是 不包含在聚合函數或GROUP BY子句。
什麼,因爲這裏不作是我的查詢實際上包括正確GROUP BY
聲明
這裏是我的應用程序生成的
SELECT
[Campaign].[Title] AS [CampaignTitle]
,CASE WHEN [Task].[Duration] <= @p0 THEN @p1 WHEN [Task].[Duration] >= @p2 AND [Task].[Duration] <= @p3 THEN @p4 ELSE @p5 END AS [9pl1WbY1ZkSvSpel3g4xmw]
,COUNT(1) AS [Ib7ZS4arHkSUWhf4saEtmg]
,SUM(CASE WHEN [Task].[Duration] <= @p6 THEN @p7 ELSE @p8 END) AS [xW1GmttC6kWHsc9QIEPN9w]
FROM [Tasks] AS [Task]
INNER JOIN [Campaigns] AS [Campaign] ON [Campaign].[Id] = [Task].[CampaignId]
INNER JOIN [Users] AS [User] ON [User].[Id] = [Task].[CompletedBy]
INNER JOIN [Results] AS [Result] ON [Result].[Id] = [Task].[ResultId]
WHERE
[Result].[IsCompleted] = @p9 AND ([User].[LastName] = @p10 OR [User].[LastName] = @p11)
GROUP BY
[Campaign].[Title]
,CASE WHEN [Task].[Duration] <= @p13 THEN @p14 WHEN [Task].[Duration] >= @p15 AND [Task].[Duration] <= @p16 THEN @p17 ELSE @p18 END
HAVING
COUNT(1) >= @p12
ORDER BY
[Campaign].[Title]
,CASE WHEN [Task].[Duration] <= @p19 THEN @p20 WHEN [Task].[Duration] >= @p21 AND [Task].[Duration] <= @p22 THEN @p23 ELSE @p24 END
查詢更重要的是,我試圖手動更換帶參數值的參數名稱。然後我把他們輸出的查詢,我在SSMS中執行它,它沒有任何問題。
這是我如何執行查詢
using (SqlConnection connection = new SqlConnection(this.connectionString))
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(selectCommand))
{
try
{
selectCommand.Connection = connection;
DataTable table = new DataTable();
sqlAdapter.Fill(table);
return table;
}
catch (Exception e)
{
throw new ReportException(string.Format("{0} {1}", e.Message, e.GetType().FullName));
}
}
下面是我用與他們的價值觀
private string GetPlainQuery(SqlCommand selectCommand)
{
string output = selectCommand.CommandText;
for (int i = selectCommand.Parameters.Count - 1; i >= 0; i--)
{
var p = selectCommand.Parameters[i];
string value = p.Value.ToString();
if (p.SqlDbType == SqlDbType.NChar || p.SqlDbType == SqlDbType.NText || p.SqlDbType == SqlDbType.NVarChar || p.SqlDbType == SqlDbType.Text || p.SqlDbType == SqlDbType.Char || p.SqlDbType == SqlDbType.Date || p.SqlDbType == SqlDbType.DateTime || p.SqlDbType == SqlDbType.SmallDateTime)
{
value = string.Format("'{0}'", value.Replace("'", "''"));
}
output = output.Replace(p.ParameterName, value);
}
return output;
}
是什麼原因造成這個錯誤,以取代參數的方法?我該如何解決它?
修訂
下面是該查詢後,我替換爲值的參數
SELECT
[Campaign].[Title] AS [CampaignTitle]
,CASE WHEN [Task].[Duration] <= 100 THEN '<100' WHEN [Task].[Duration] >= 100 AND [Task].[Duration] <= 200 THEN 'BETWEEN 100 AND 200' ELSE '>200' END AS [9pl1WbY1ZkSvSpel3g4xmw]
,COUNT(1) AS [Ib7ZS4arHkSUWhf4saEtmg]
,SUM(CASE WHEN [Task].[Duration] <= 100 THEN 1 ELSE 0 END) AS [xW1GmttC6kWHsc9QIEPN9w]
FROM [Tasks] AS [Task]
INNER JOIN [Campaigns] AS [Campaign] ON [Campaign].[Id] = [Task].[CampaignId]
INNER JOIN [Users] AS [User] ON [User].[Id] = [Task].[CompletedBy]
INNER JOIN [Results] AS [Result] ON [Result].[Id] = [Task].[ResultId]
WHERE
[Result].[IsCompleted] = 1 AND ([User].[LastName] = 'Yo' OR [User].[LastName] = 'Smith')
GROUP BY
[Campaign].[Title]
,CASE WHEN [Task].[Duration] <= 100 THEN '<100' WHEN [Task].[Duration] >= 100 AND [Task].[Duration] <= 200 THEN 'BETWEEN 100 AND 200' ELSE '>200' END
HAVING
COUNT(1) >= 0
ORDER BY
[Campaign].[Title]
,CASE WHEN [Task].[Duration] <= 100 THEN '<100' WHEN [Task].[Duration] >= 100 AND [Task].[Duration] <= 200 THEN 'BETWEEN 100 AND 200' ELSE '>200' END
錯誤說'Tasks.Duration',但我在你的SQL查詢中只看到'Task.Duration'。有沒有錯別字? –
你可以給我們正常的查詢,也沒有參數。 – Surendra
@JonathonOgden我認爲這是因爲表「任務」被別名爲「任務」。 – juharr