如何在Oracle SQL中的Select语句中重命名表列?

时间:2015-07-17 14:36:47

标签: java oracle jdbc

enter image description here 上面显示的是结果。我得到了正确的数据,只需要更改列名。 问题是我试图在我的表中重命名我的列。 我尝试了以下错误 - java.sql.SQLSyntaxErrorException:ORA-00923:找不到FROM关键字:

$('#example1').popover();

我在JAVA中使用NetBeans IDE和编码。我已经尝试了上面的其他几个变种而没有运气。谢谢您的帮助!

"SELECT to_char(logdate,'dd-MON-yy') AS DATE, adj_login_time AS LOGIN, adj_logout_time AS LOGOUT, adj_lunch_in AS LUNCHIN, adj_lunch_out AS LUNCHOUT, round(logduration,2) AS LOGDURATION " +
                        "FROM employee_time_log " +
                        "WHERE employee_id_number = " +userInputIdNumber +" AND ROWNUM <= 10" +
                        "ORDER BY logdate DESC");
AND
"SELECT to_char(logdate,'dd-MON-yy') AS 'Log Date', adj_login_time AS 'Log In Time', adj_logout_time AS 'Log Out Time', adj_lunch_in AS 'Lunch In', adj_lunch_out AS 'Lunch Out', round(logduration,2) AS 'Log Duration' " +
                        "FROM employee_time_log " +
                        "WHERE employee_id_number = " +userInputIdNumber +" AND ROWNUM <= 10" +
                        "ORDER BY logdate DESC");

1 个答案:

答案 0 :(得分:2)

除了@EvgeniyDorofeev正确指出的缺失空间之外,在您的第一个查询中,您尝试使用reserved word DATE作为列别名,这会让解析器感到困惑。使用其他名称:

"SELECT to_char(logdate,'dd-MON-yy') AS ACTIVITY_DATE, adj_login_time AS LOGIN, adj_logout_time AS LOGOUT, adj_lunch_in AS LUNCHIN, adj_lunch_out AS LUNCHOUT, round(logduration,2) AS LOGDURATION " +
                        "FROM employee_time_log " +
                        "WHERE employee_id_number = " +userInputIdNumber +" AND ROWNUM <= 10 " +
                        "ORDER BY logdate DESC");

或者,如果你真的希望它拥有该名称,那么使用带引号的标识符(在你的字符串中使用转义双引号),虽然我真的建议不要这样做,因为它也会导致混淆调用代码:

"SELECT to_char(logdate,'dd-MON-yy') AS \"DATE\", adj_login_time AS LOGIN, adj_logout_time AS LOGOUT, adj_lunch_in AS LUNCHIN, adj_lunch_out AS LUNCHOUT, round(logduration,2) AS LOGDURATION " +
                        "FROM employee_time_log " +
                        "WHERE employee_id_number = " +userInputIdNumber +" AND ROWNUM <= 10 " +
                        "ORDER BY logdate DESC");

在你的第二个版本中,你使用单引号来尝试引用标识符,但这些只是被视为字符串文字而不是标识符,并且它们在语法中的那一点上无效。您必须对引用的标识符使用双引号:

"SELECT to_char(logdate,'dd-MON-yy') AS \"Log Date\", adj_login_time AS \"Log In Time\", adj_logout_time AS \"Log Out Time\", adj_lunch_in AS \"Lunch In\", adj_lunch_out AS \"Lunch Out\", round(logduration,2) AS \"Log Duration\" " +
                        "FROM employee_time_log " +
                        "WHERE employee_id_number = " +userInputIdNumber +" AND ROWNUM <= 10 " +
                        "ORDER BY logdate DESC");

你也在错误的地方进行ROWNUM检查;它将在ORDER BY之前应用,因此您将从任何一天获得十个不确定的行,然后将对其进行排序。如果您确实想要查看最近的十行,那么您需要在子查询中进行排序,然后将ROWNUM过滤器应用于:

"SELECT * FROM (" +
    "SELECT to_char(logdate,'dd-MON-yy') AS \"Log Date\", " +
        "adj_login_time AS \"Log In Time\", " +
        "adj_logout_time AS \"Log Out Time\", " +
        "adj_lunch_in AS \"Lunch In\", " +
        "adj_lunch_out AS \"Lunch Out\", " +
        "round(logduration,2) AS \"Log Duration\" " +
    "FROM employee_time_log " +
    "WHERE employee_id_number = " +userInputIdNumber + " " +
    "ORDER BY logdate DESC" +
    ") WHERE ROWNUM <= 10";

您还应该考虑使用参数化查询而不是在字符串中嵌入userInputIdNumber,部分原因是每个ID都需要对唯一查询进行硬解析,部分原因是为了避免SQL注入 - 特别是如果该值正在由用户提供,似乎是。