ORA 00907 缺失右括號

2022-07-26 07:15:17 字數 2784 閱讀 6683

最近在開發過程中使用oracle資料庫,在程式中進行查詢資料時遇到了「ora-00907: 缺失右括號」的問題,但是如果直接把sql語句直接在資料庫或pl/sql中執行時,卻又能夠正常查詢,為了解決這個問題,折騰了半天,查詢了一些資料,所以就對各種導致出現「缺失右括號」的情況進行了整理總結。行文如下。

在有union all的子查詢中使用了order by,會導致缺失右括號的錯誤,事實上在有union all的子查詢中根本沒必要進行排序,因為聯合之後又組成了乙個新的集合,之前的排序對新集合而言沒什麼用,直接查詢聯合之後的新集合然後再進行排序即可。

示例如下:

select * from

select column_a,column_b

from table_example_a

order by column_a

union all

select column_a,column_b

from table_example_b

order by column_a

) a;

解決方案如下:

select * from

select column_a,column_b

from table_example_a

union all

select column_a,column_b

from table_example_b

) a;

order by column_a

此種情況跟1中描繪的有些類似,首先在in(子查詢)用法使用order by 會報錯,其次,子查詢裡用order by,純屬多此一舉,子查詢的目的,只是找出合適的資料。如果需要排序,在外邊排即可。

示例如下:

select * from tabel_example where id in(select id from table_example where id>500 oder by id desc)

解決方案如下:

select * from tabel_example where id in(select id from table_example where id>500)oder by id desc

示例如下:

create table t_example ( 

id serial primary key, 

t_id int not null default 0

解決方案如下:

create table t_example ( 

id serial primary key, 

t_id int default 0 not null

示例如下:

create tbale t_example

(id       number(18,0) not null, 

desc     varchar(45) not null

解決方案如下:

給對應的關鍵字加上雙引號

create tbale t_example

(id       number(18,0) not null, 

「desc」     varchar(45) not null

示例如下:

create tabel t_example

id bigint not null primary key,

name varchar not null

解決方案如下:

create tabel t_example

id bigint not null primary key,

name varchar(200) not null

主外來鍵型別不完全一致時也會報缺失右括號的錯誤

在查詢時引發缺失右括號錯誤的原因大多數在於查詢語句中有關於日期的轉化、過濾。很多時候都是因為關於日期型別的轉化少了單引號。

示例如下:

select * from t_example where t_date in(2015-01-20 22:37)

解決方案如下:

select * from t_example where t_date in(『2015-01-20 22:37『)

還有一種情況就是在where過濾中進行時間的轉化時,有時候在sql/plus中直接執行沒問題,但是在程式中卻會出現錯誤。

示例如下:

select * from t_example where 

id in(select id from t_example_b where d_date>=to_date(『2015-01-20』,』yyyy-mm-dd』))

該語句在資料庫直接執行是沒問題的,但是在程式中執行傳入日期引數時有時會報缺失右括號的錯誤,為了解決這個問題,我們可以改變傳入的日期引數的格式,如下:

select * from t_example where 

id in(select id from t_example_b where d_date>=to_date(20150120,』yyyy-mm-dd』))

還有一種關於oracle日期格式的錯誤是:ora-01840:輸入值對於日期格式不夠長

示例如下:select to_date(2015-01-01,'yyyy-mm-dd') from dual

為了解決這個問題,我們也可以用改變傳入的日期引數的格式,來解決,如下:

select to_date(20150101,'yyyy-mm-dd') from dual

如果在建立表時,表欄位名全部是大寫,則不存在此問題

ORA 00907 缺失右括號

好久沒寫sql指令碼建立oracle表,之前也是沒注意,最近寫了乙個建立表語句如下 create table jc task task id number 11 primary key,task code varchar2 255 default null,task type number 1 no...

ora 00907缺失右括號

最近在開發過程中使用oracle資料庫,在程式中進行查詢資料時遇到了 ora 00907 缺失右括號 的問題,但是如果直接把sql語句直接在資料庫或pl sql中執行時,卻又能夠正常查詢,為了解決這個問題,折騰了半天,查詢了一些資料,所以就對各種導致出現 缺失右括號 的情況進行了整理總結。行文如下。...

ora 00907 缺失右括號3

ora 00907 缺失右括號 剛剛接觸oracle資料庫,在使用pl sql dev建表的時候,碰到了ora 00907這個錯誤,在網上找了很多資料,發現別人碰到的那個問題,跟我碰到的這個問題不一樣,所以一直沒有解決方法,後來,在網上搜尋使用oracle建立外來鍵約束的示例,終於發現自己的錯誤在什...