1

我正在尝试使用带有 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();
        }
    }

问题是,没有抛出异常,只有零结果。有任何想法吗?

4

1 回答 1

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 
于 2012-09-19T14:01:55.383 回答