2012-07-10 49 views
2

爲什麼在select語句中使用別名時會被視爲外星人?這個特定的別名SQL列有問題嗎?

我無法想象這是總是如此,但我不得不改變此代碼:

const string query = 
    @"SELECT C.PLATYPUSCLIENTID, E.DEWDROPNAME FOGGY 
    FROM DUCKBILLACCOUNT B, 
     PLATYPUSCLIENT C, 
     ENTITY E 
    WHERE B.DUCKBILLNUMBER = :PLATYPUSACCOUNTID 
     AND B.DUCKBILLACCOUNTABCID = C.DUCKBILLACCOUNTABCID 
     AND C.PLATYPUSCLIENTABCID = E.ABCID 
    ORDER BY FOGGY, PLATYPUSCLIENTID"; 
    try { 
     oc.Open(); 
     using (OracleCommand ocmd = new OracleCommand(query, oc)) { 
      ocmd.Parameters.Add("PLATYPUSACCOUNTID", PLATYPUSACCOUNTID); 
      using (OracleDataReader odr = ocmd.ExecuteReader()) { 
       while (odr.Read()) { . . . 

...這樣的:

const string query = 
    @"SELECT C.PLATYPUSCLIENTID, E.DEWDROPNAME 
    FROM DUCKBILLACCOUNT B, 
     PLATYPUSCLIENT C, 
     ENTITY E 
    WHERE B.DUCKBILLNUMBER = :PLATYPUSACCOUNTID 
     AND B.DUCKBILLACCOUNTABCID = C.DUCKBILLACCOUNTABCID 
     AND C.PLATYPUSCLIENTABCID = E.ABCID 
    ORDER BY DEWDROPNAME, PLATYPUSCLIENTID"; 
    try { 
     oc.Open(); 
     using (OracleCommand ocmd = new OracleCommand(query, oc)) { 
      ocmd.Parameters.Add("PLATYPUSACCOUNTID", PLATYPUSACCOUNTID); 
      using (OracleDataReader odr = ocmd.ExecuteReader()) { 
       while (odr.Read()) { . . . 

...得到它在運行時返回vals(我在第一個版本中遇到了「IndexOutOfRangeException」)

+0

請問您的查詢是否在SQL Developer中根據需要針對數據庫運行?我記得以前需要使用'AS'關鍵字,但我不記得Oracle SQL引擎是否期望它。 – 2012-07-10 22:09:13

+0

@Jesse:我不知道SQL Developer是什麼,但它在TOAD中運行良好 – 2012-07-10 22:26:28

+0

SQL Developer是Oracle用於數據庫管理的GUI工具。它類似於微軟的SSMS for SQL Server。 http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html – 2012-07-11 14:29:10

回答

4

您需要引用別名。

E.DEWDROPNAME 'FOGGY' ... 
+0

小東西...... – Wolfram 2012-07-10 22:28:46

+0

確實如此! :-) – Shawn 2012-07-10 22:30:40