批量查詢語句如何通過儲存過程來自動生成

2021-08-19 13:15:17 字數 790 閱讀 3959

平時我們工作中常常會遇到分庫分表後,單單根據乙個條件無法定位該資訊具體是在哪個表中的時候,我們就需要多表搜尋,可是如果是在100張表裡面,我們還需要寫100條select語句嗎?這裡我們可以批量生成select語句。

批量列印查詢條件

select * from user_channel_info_0-user_channel_info_99 where channel_id=2 or channel_id=4 ;

create or replace procedure test_002 is

vsql varchar2(2000);

vtable varchar2(50);

vgrant varchar2(100);

begin

vgrant:='grant create table to testuser';

execute immediate vgrant;

for m in 0..99 loop

vtable := 'user_channel_info_'||m;

vsql := 'select * from '|| vtable ||' where channel_id=2 or channel_id=4 union all';

dbms_output.put_line(vsql);

--execute immediate vsql;

end loop;

end test_002;

執行上面的儲存過程以後,會發現控制台上面已經列印了批量查詢的語句,直接執行即可。

通過儲存過程 查詢表資訊

region 查詢 獲取裝置列表 翻頁引數 裝置編號 裝置名稱 裝置sn號 裝置狀態 溫度 濕度 電壓 訊號 通訊方式 空字串為忽略 分配情況 0為未分配,1為分配 分配物件 最後更新時間 建立時間 裝置資訊列表 datatable public datatable getequipmentslis...

mysql批量查詢 修改表, 批量刪除儲存過程

場景 有的時候需要批量更新部分有規律的表或者修改其屬性。處理方案 使用 函式concat 來生成批量執行sql語句,再執行批量sql語句。如 批量刪除所有表 select concat drop table table name,from information schema.tables wher...

mysql查詢語句通過limit來限制查詢的行數

mysql查詢語句,通過程式設計客棧limit來限制查詢的行www.cppcns.com數。例如 select name from usertb where age 20 limit 0,1 限制從第一條開始,顯示1條 select name from usertb where age 20 lim...