要查詢 oracle 所有表,可執行:select table_name from user_tables;。要查詢所有數據,則需:1. 獲取表名稱;2. 遍歷表并查詢:select * from table_name;。
如何查詢 Oracle 數據庫中的所有表和數據
查詢所有表
要查詢 Oracle 數據庫中的所有表,可以使用以下 SQL 語句:
<code class="sql">SELECT table_name FROM user_tables;</code>
登錄后復制
這個查詢將返回當前用戶模式下所有表的名稱。
查詢所有數據
要查詢 Oracle 數據庫中的所有數據,可以執行以下步驟:
獲取所有表的名稱:使用上面的查詢獲取所有表的名稱。
遍歷表并查詢數據:使用 FOR 循環遍歷表的名稱列表,并對每個表執行以下語句以查詢數據:
<code class="sql">SELECT * FROM table_name;</code>
登錄后復制
其中 table_name 是當前循環中表的名稱。
示例代碼
以下示例代碼演示了如何使用上述方法查詢所有表和數據:
<code class="<a style='color:#f60; text-decoration:underline;' href=" https: target="_blank">python">import cx_Oracle
# 連接到數據庫
connection = cx_Oracle.connect('username', 'password', 'dsn')
# 獲取游標
cursor = connection.cursor()
# 查詢所有表
cursor.execute('SELECT table_name FROM user_tables')
# 獲取表名
table_names = [row[0] for row in cursor.fetchall()]
# 遍歷表并查詢數據
for table_name in table_names:
cursor.execute('SELECT * FROM ' + table_name)
print(cursor.fetchall())
# 關閉游標和連接
cursor.close()
connection.close()</code>
登錄后復制






