0

我正在尝试确定列是否是 oracle 数据库中的主键。

我有以下sql:

SELECT cols.column_name 
FROM all_constraints as cons, all_cons_columns as cols 
WHERE (cols.table_name = 'TABLE_NAME' 
       AND cols.columnName= 'SRC_CODE' 
       AND cons.constraint_type = 'P' 
       AND cons.constraint_name = cols.constraint_name 
       AND cons.owner = cols.owner 
       AND cons.status = 'ENABLED' 
       AND lower(cons.owner)='resmaid') 
ORDER BY cols.table_name

我收到以下错误:

Caused by: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:193)
    at oracle.jdbc.driver.T4CStatement.executeForDescribe(T4CStatement.java:852)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1153)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1275)
    at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:1477)
    at oracle.jdbc.driver.OracleStatementWrapper.executeQuery(OracleStatementWrapper.java:392)

曾经有一段时间这个 sql 可能有效,不记得是什么时候(大约 2 年前)。

这很紧急,因为我需要连接到该数据库并进行一些数据插入。

提前致谢

4

2 回答 2

5

我已经修好了。

两件事情。1. 删除表名别名中使用的“AS” 2. where 子句中的 column_name 拼写错误

请看下面

SELECT cols.column_name
  FROM all_constraints cons,
  all_cons_columns   cols
  WHERE (cols.table_name = 'TABLE_NAME'
    AND cols.column_Name = 'SRC_CODE'
    AND cons.constraint_type = 'P'
    AND cons.constraint_name = cols.constraint_name
    AND cons.owner = cols.owner
    AND cons.status = 'ENABLED'
    AND lower(cons.owner) = 'resmaid')
 ORDER BY cols.table_name;
于 2013-09-23T11:34:01.227 回答
0
 SELECT cols.column_name
 --modify FROM all_constraints as cons, all_cons_columns as cols  
FROM all_constraints cons, all_cons_columns cols 
WHERE (cols.table_name = 'TABLE_NAME' 
       --modify AND cols.columnName= 'SRC_CODE'  
       AND cols.column_name= 'SRC_CODE' 
       AND cons.constraint_type = 'P' 
       AND cons.constraint_name = cols.constraint_name 
       AND cons.owner = cols.owner 
       AND cons.status = 'ENABLED' 
       AND lower(cons.owner)='resmaid') 
ORDER BY cols.table_name
于 2013-09-23T11:49:47.007 回答