簡體   English   中英

在oracle中查詢以從一張表中獲取多條記錄並輸入到另一張表中

[英]Query in oracle to fetch multiple records from one table & enter into other

我的要求是在oracle中寫一個查詢
“從用戶表中獲取用戶 ID 並插入到 USERQueries 表中”

用戶

USERID GPID GROUP
1682   1026   IBMSDL2S
1882   1028   IBMSDL2S
1573   1029   IBMSDL2S
1342   1124   IBMSDL2S
1976   2576   IBMSDL2S
1883   2575   IBMSDL2S
1854   2574   IBMSDL2S
2222   2573   IBMSDL2S
2207   2572   IBMSDL2S  

用戶查詢

APP  CLAUSENAME           USERID DEFAULTQUERYID OWNER
SR   Assgined_SRs_To_Me   1249   545            MAXADMIN
SR   Assgined_SRs_To_Me   1682   543            MAXADMIN

我可以插入一個用戶 ID,如下所示

insert into  USERSQUERIES  
(APP,CLAUSENAME,USERID,DEFAULTQUERYID,OWNER)   
values   
('SR','Assgined_SRs_To_Me',(select userid from USERS where  groupname='IBMSDL2S' and userid='1249
),DEFAULTQUERYSEQ.NEXTVAL,'MAXADMIN')

但不明白它如何適用於所有用戶 ID

您將編寫一個SELECT語句來獲取您感興趣的數據,然后在INSERT語句中使用它。 看看下面的例子:

先測試用例:

SQL> create table users as
  2  select 1682 userid, 1026 gpid, 'IBMSDL25' groupname from dual union all
  3  select 1882 userid, 1028 gpid, 'IBMSDL25' groupname from dual union all
  4  select 2222 userid, 2222 gpid, 'XXXXXX25' groupname from dual;            --> will NOT be inserted

Table created.

SQL> create table usersqueries (app varchar2(2),
  2  clausename varchar2(20), userid number, defaultqueryid number, owner
  3  varchar2(20));

Table created.

SQL> create sequence defaultqueryseq;

Sequence created.

SQL>

測試:從第 3 行開始是我之前提到的SELECT語句。

SQL> insert into usersqueries
  2    (app, clausename, userid, defaultqueryid, owner)
  3    select 'SR',
  4           'Assigned_SRs_to_me',
  5           u.userid,
  6           defaultqueryseq.nextval,
  7           'MAXADMIN'
  8    from users u
  9    where u.groupname = 'IBMSDL25';

2 rows created.

SQL> select * From usersqueries;

AP CLAUSENAME               USERID DEFAULTQUERYID OWNER
-- -------------------- ---------- -------------- --------------------
SR Assigned_SRs_to_me         1682              1 MAXADMIN
SR Assigned_SRs_to_me         1882              2 MAXADMIN

SQL>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM