iT邦幫忙

0

ORACLE 除某欄位外其他資料都取要如何寫?

小弟不才
關於ORACLE有個問題想問一下
假設 今天我有個表格叫A
裡面欄位有1,2,3,4,5,6......,1000
今天我只要選取2,3,4,5,6....,1000的欄位
這要怎麼寫
平時如果欄位數少
可以直接SELECT 2,3,....1000 FROM A
可是今天欄位數很多
懇請各位高手幫幫忙

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

5
一級屠豬士
iT邦大師 1 級 ‧ 2016-07-01 14:20:22
最佳解答
先建立一個 table 

create table ithelp160701 (
  id integer not null
, col1 char(1) not null
, col2 char(1) not null
);


利用系統資訊產生全部欄位列表的 SQL Command

select 'select ' 
    || listagg(column_name, ',') within group (order by column_id)
    || ' from ITHELP160701;' as cmd
  from user_tab_cols
 where table_name = 'ITHELP160701';
 
 
CMD
------------------------------------------------------------------------------------------------------------------------------------
select ID,COL1,COL2 from ITHELP160701;

---
自行將不要的欄位移除就可以了!
看更多先前的回應...收起先前的回應...
尼克 iT邦大師 1 級 ‧ 2016-07-01 14:35:14 檢舉

/images/emoticon/emoticon32.gif

延伸方法:
例如 id 不要

select 'select ' 
    || listagg(column_name, ',') within group (order by column_id)
    || ' from ITHELP160701;' as cmd
  from user_tab_cols
 where table_name = 'ITHELP160701'
   and column_name NOT IN ('ID');

CMD
------------------------------------------------------------------------------------------------------------------------------------
select COL1,COL2 from ITHELP160701;

這麼無聊的需求您都寫得出來
我只能給您一個 /images/emoticon/emoticon12.gif
/images/emoticon/emoticon68.gif

非常感謝你> <

我要發表回答

立即登入回答