我正在尝试使用带有C#代码隐藏的ASP.net中的OleDbCommand读取SQL查询的结果。我正在使用以下Oracle SQL语句,该语句在Oracle SQL Developer中有效且正常工作:
WITH "bis" AS
(SELECT v.name AS "vendor",
SUM(CASE WHEN i.bi_rating=5 THEN 1 ELSE 0 END) AS "bi5",
SUM(CASE WHEN i.bi_rating=6 THEN 1 ELSE 0 END) AS "bi6",
SUM(CASE WHEN i.bi_rating=7 THEN 1 ELSE 0 END) AS "bi7"
FROM incident_tbl i, vendor_tbl v
WHERE i.vendor_code=v.vendor_code
GROUP BY v.name
ORDER BY v.name)
SELECT DISTINCT
"bis"."vendor",
"bis"."bi5",
"bis"."bi6",
"bis"."bi7",
("bis"."bi5"+"bis"."bi6"+"bis"."bi7") AS "total"
FROM "bis"
WHERE "vendor"!='[No Vendor]'
ORDER BY "total" DESC
这会产生92行的正确结果。但是,当我在我的ASP.net页面上运行它时,它返回零行。我甚至将查询隔离到一个测试方法中来尝试对其进行排序:
(con
是已存在的工作连接字符串)
protected void test()
{
OleDbCommand sql = new OleDbCommand("WITH \"bis\" AS (SELECT v.name AS \"vendor\", SUM(CASE WHEN i.bi_rating=5 THEN 1 ELSE 0 END) AS \"bi5\", SUM(CASE WHEN i.bi_rating=6 THEN 1 ELSE 0 END) AS \"bi6\", SUM(CASE WHEN i.bi_rating=7 THEN 1 ELSE 0 END) AS \"bi7\" FROM incident_tbl i, vendor_tbl v WHERE i.vendor_code=v.vendor_code GROUP BY v.name ORDER BY v.name) SELECT DISTINCT \"bis\".\"vendor\", \"bis\".\"bi5\", \"bis\".\"bi6\", \"bis\".\"bi7\", (\"bis\".\"bi5\"+\"bis\".\"bi6\"+\"bis\".\"bi7\") AS \"total\" FROM incident_tbl i, \"bis\" WHERE \"vendor\"!='[No Vendor]' ORDER BY \"total\" DESC", con);
try
{
if (con.State == ConnectionState.Closed)
con.Open();
OleDbDataReader reader = sql.ExecuteReader();
while (reader.Read())
{
// Show on a temporary label for testing
lblDebug.Text += "<br />New row... vendor: " + reader["vendor"].ToString();
}
}
catch (Exception ex)
{
lblDebug.Text += "<br />ERROR WITH TEST METHOD: " + ex;
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
}
问题是,没有抛出异常,只有零结果。 有什么想法吗?
答案 0 :(得分:2)
尝试
SELECT DISTINCT
vendor,
bi5,
bi6,
bi7,
(bi5+bi6+bi7) total
FROM
(SELECT v.name vendor,
SUM(CASE WHEN i.bi_rating=5 THEN 1 ELSE 0 END) bi5,
SUM(CASE WHEN i.bi_rating=6 THEN 1 ELSE 0 END) bi6,
SUM(CASE WHEN i.bi_rating=7 THEN 1 ELSE 0 END) bi7
FROM incident_tbl i, vendor_tbl v
WHERE i.vendor_code=v.vendor_code
GROUP BY v.name
ORDER BY v.name) bis
WHERE vendor!='[No Vendor]'
ORDER BY total DESC