PL SQL實現獲得所有表名及列名到表

2021-09-30 15:02:39 字數 1354 閱讀 5148

首先建立儲存表

create table table_tmp(user_name varchar2(20),table_name varchar2(100),col_name varchar2(200));

commit;

然後執行匿名塊如下(注意匿名塊中用不了ddl操作,所以先建立表):

declare

cursor cur_table is --定義游標取出表名和列名

select table_name, column_name from user_tab_columns;

cur01 cur_table%rowtype;

u_name varchar2(100) := '&a';--自定義輸入使用者名稱

u_count integer; --儲存行數用

t_name user_tab_columns.table_name%type; --儲存表名

c_name user_tab_columns.column_name%type;--儲存列名

begin

open cur_table; --開啟游標

loop

fetch cur_table

into cur01;

exit when cur_table%notfound; --沒有資料就退出

t_name := cur01.table_name; --取出游標的值賦給變數

c_name := cur01.column_name;

insert into table_tmp values (u_name, t_name, c_name); --插入到table_tmp表

end loop;

u_count := cur_table%rowcount; --獲得行數

close cur_table;

commit;

dbms_output.put_line('success,effect lines:'||u_count); --成功輸出影響行數

exception

when no_data_found then

dbms_output.put_line('no_data_found');

when others then

rollback; --沒成功就回滾

dbms_output.put_line(u_count || 'error');

end;

執行結果如下:

sql查詢所有表名及注釋

oracle查詢使用者下的所有表 select from all tab comments 查詢所有使用者的表,檢視等 select from user tab comments 查詢本使用者的表,檢視等 select from all col comments 查詢所有使用者的表的列名和注釋.se...

C 獲得Sqlserver資料庫中所有表名

歡迎加入bim行業開發交流1群 群號 711844216 小夥伴們在使用資料庫時,可能需要程式自動去獲取指定資料庫中所有表的名稱,或許根據表名特徵獲取相關表。1.c 連線sqlserver的就不再贅述了 2.sqlconnection類的getschema 方法 3.對錶進行篩選,如果有需要的話 u...

獲得乙個表中的所有列名和列數

在一次的開發過程中,因需要要知道某張表下到底有多少個列名。故得知在mssql裡有乙個系統表可以查出指定表的所有列。如下 已知表名的情況下,查出表的字段及列數 select from syscolumns where id object id 表名 select count from syscolum...