[PL/SQL] Using an IN clause?
So I got a request to pull over 300,000 records today. I would use an employee_id # (aid) to pull their demographic data, but the IN Clause is limited to 1,000 conditions.
I would be doing a bunch of copying/pasting, which I don't want to do.
So, I wrote an anoymous block to do it for me, but then I though about it, and wondered what would be the best way to do this with PL/SQL?
I wrote this up
declare
cursor demo is
select p.associate_id, p.list_id, d.first, d.last, c.street1, c.street2, c.city, c.state, c.country, c.postal_code, c.email
from sf.profiles p, demographics d, contact_info c
where p.entity_id = d.entity_id
and p.entity_id = c.entity_id
and p.list_id = 502
and aid in (insert employee_id here);
v_count number := 0;
begin
for x in demo
loop
v_count := v_count + 1;
dbms_output.put_line(v_count||'AID '||x.associate_id||'List_ID '||x.list_id||'First '||x.first||'Last '||x.last
||'Street1 '||x.street1||'Street2 '||x.street2||'City '||x.city||'State '||x.state||'Country '||x.country||
'Postal_Code '||x.postal_code);
end loop;
dbms_output.put_line('Records Processed: '||v_count);
end;
In PL/SQL, is my IN clause still limited to 1,000 values? Because I have to pull for over 300,000 employees. Can I just paste the 300,000 values into the IN clause?
Or is there a better way to do this?
thanks
Can I just paste the 300,000 values into the IN clause?
Or is there a better way to do this?Yes.
How do you know which employess you need? Is there a list of 300,000 employee IDs somewhere? Does the list exist in a table? Or is it all employees in the table?
Similar Messages
-
How to write a SQL Query without using group by clause
Hi,
Can anyone help me to find out if there is a approach to build a SQL Query without using group by clause.
Please site an example if is it so,
RegardsI hope this example could illuminate danepc on is problem.
CREATE or replace TYPE MY_ARRAY AS TABLE OF INTEGER
CREATE OR REPLACE FUNCTION GET_ARR return my_array
as
arr my_array;
begin
arr := my_array();
for i in 1..10 loop
arr.extend;
arr(i) := i mod 7;
end loop;
return arr;
end;
select column_value
from table(get_arr)
order by column_value;
select column_value,count(*) occurences
from table(get_arr)
group by column_value
order by column_value;And the output should be something like this:
SQL> CREATE or replace TYPE MY_ARRAY AS TABLE OF INTEGER
2 /
Tipo creato.
SQL>
SQL> CREATE OR REPLACE FUNCTION GET_ARR return my_array
2 as
3 arr my_array;
4 begin
5 arr := my_array();
6 for i in 1..10 loop
7 arr.extend;
8 arr(i) := i mod 7;
9 end loop;
10 return arr;
11 end;
12 /
Funzione creata.
SQL>
SQL>
SQL> select column_value
2 from table(get_arr)
3 order by column_value;
COLUMN_VALUE
0
1
1
2
2
3
3
4
5
6
Selezionate 10 righe.
SQL>
SQL> select column_value,count(*) occurences
2 from table(get_arr)
3 group by column_value
4 order by column_value;
COLUMN_VALUE OCCURENCES
0 1
1 2
2 2
3 2
4 1
5 1
6 1
Selezionate 7 righe.
SQL> Bye Alessandro -
How to Use SQL Query having IN Clause With DB Adapter
Hi,
I am using 11.1.1.5 want to find out how to Use SQL Query having IN Clause With DB Adapter. I want to pass the IN values dynamically. Any ideas.
Thanksinvoke a stored procedure, it's safer than trying to put together an arbitrary SQL statement in the JCA adapter
-
Need sql query to remove duplicates using UNION ALL clause
Hi,
I have a sql query which has UNION clause.But the UNION clause is causing some performance issues.
To overcome that I have used UNION ALL to improve performance but its returning duplicates.
Kindly anyone send a sample SQL query where my primary objective is used to use UNION ALL clause and to consider unique rows (elimating duplicate
ones)
Any help will be needful for me
Thanks and Regardswhy not UNION? :(
another way also use MINUS
SQL>
SQL> with t as
2 (
3 select 1 if from dual union all
4 select 2 if from dual union all
5 select 1 if from dual union all
6 select 3 if from dual union all
7 select 3 if from dual
8 )
9 ,t2 as
10 (
11 select 1 if from dual union all
12 select 2 if from dual union all
13 select 3 if from dual union all
14 select 4 if from dual union all
15 select 5 if from dual
16 )
17 (select if from t
18 union all
19 select if from t2)
20 /
IF
1
2
1
3
3
1
2
3
4
5
10 rows selected
SQL> so
SQL>
SQL> with t as
2 (
3 select 1 if from dual union all
4 select 2 if from dual union all
5 select 1 if from dual union all
6 select 3 if from dual union all
7 select 3 if from dual
8 )
9 ,t2 as
10 (
11 select 1 if from dual union all
12 select 2 if from dual union all
13 select 3 if from dual union all
14 select 4 if from dual union all
15 select 5 if from dual
16 )
17 (select if from t
18 union all
19 select if from t2)
20 minus
21 select -99 from dual
22 /
IF
1
2
3
4
5
SQL> -
In a SQL statement, the SELECT clause is used to
In a SQL statement, the SELECT clause is used to select
(a) columns
(b) rows
(c) tables
(d) none of the above
can any one help ImmediatelyIs used to select rows of specified column...
SELECT column_name(s) FROM table_name -
SQL Loader to append data in same table but using differnet WHEN clauses
In my data file i have a header record and a detail record identified by Record_type = 1 and 2 respectively.
The Database table has all the columns to capture detail records but i want to capture jus one column of header record now also in my existing table. So i have added that column (DATA_DATE)in my table but how to capture that value ?
im writing my control file using two WHEN clauses, something like -
load data
into table t_bdn
append
when RECORD_TYPE = '2'
FIELDS TERMINATED BY "|" TRAILING NULLCOLS
SEQUENCE_NO
, RECORD_TYPE
, DISTRIBUTOR_CODE
, SUPPLIER_CODE
, SUPPLIER_DISTRIBUTOR_CODE
, DISTRIBUTOR_SKU
, SUPPLIER_SKU
when RECORD_TYPE = '1'
FIELDS TERMINATED BY "|" TRAILING NULLCOLS
SEQUENCE_NO FILLER
, RECORD_TYPE FILLER
, CREATE_DATE FILLER
, DATA_DATE "NVL(to_date(:DATA_DATE, 'YYYY/MM/DD'),to_date('9999/12/31', 'YYYY/MM/DD'))"
im getting error " expecting INTO and foung WHEN RECORD_TYPE = '1' "
if i give iNTO second time it will append a new row altogether in my table but i want the same row to be updated with this DATA_DATE value coming from RECORD_TYPE =1 and header record has 4 delimited data text fields only and i am interested in fetching just the 4th column..
KIndly suggest what to do ?Ravneek, I could be wrong but sqlldr is a 'load' program, that is, it inserts data. I am unaware of any ability to update existing rows as you seem to want. What you appear to want to do is more the job of a merge statement.
I would look at writing a pro* language, a .net, or a java program to perform inserts where some or all of the newly inserted rows are also to be updated.
From the manual: (Oracle® Database Utilities 10g Release 2 (10.2) Part Number B14215-01)
Updating Existing Rows
The REPLACE method is a table replacement, not a replacement of individual rows. SQL*Loader does not update existing records, even if they have null columns. To update existing rows, use the following procedure:
1. Load your data into a work table.
2. Use the SQL language UPDATE statement with correlated subqueries.
3. Drop the work table.
HTH -- Mark D Powell -- -
OPEN CURSOR using a WITH clause in the select query
Hi,
I am using Oracle 9i. I have a requirement where I have a REFCURSOR as an OUT parameter for my procedure. I have declared the TYPE and created the procedure.
In the procedure, I am using OPEN <cursor_name> FOR <query>;
Ideally this works in most of the cases that I have tried earlier. However, in the current case I am using a WITH clause in my query to get the results.
I need help in understanding if the above mentioned syntax would not allow me to use the WITH clause in the query.What error do you get , seems to work ok for me on 10g
SQL> begin
2 open :cv for 'with x as (select * from emp) select * from x';
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> print :cv
EMPNO
ENAME
JOB MGR HIREDATE SAL COMM DEPTNO
7521
WARD
SALESMAN 7698 22-FEB-81 1250 500 30
7566
JONES
MANAGER 7839 02-APR-81 2975 20
EMPNO -
P2 report SQL using P1 report columns
Hi,
I have a page1 (P1) report table with 3 columns COL1, COL2 and COL3.
COL1 has 3 rows with values 'A', 'B' and 'C'
I'd like to produce a report table on P2 from a SQL query using the values (not the column name) from P1 COL1 like:
select * from TABLE where (COL1='A', COL1='B, COL1='C');
but get the values for A, B and C from the P1 report.
Any ideas? Is it even possible?
Cheers,
DaveHi,
You can edit your previous post - there's an icon top-right above the post - just make sure you leave the blurb that says you've edited it.
The only way to get the values from an entire column would be to use an IN clause in your P2's query but you would have to the WHERE clause of P1's sql to get this.
For example, assuming P1's statement is:
SELECT COL1, COL2, COL3
FROM MYTABLE1
WHERE X = :P1_FILTER
Your P2 statement would be:
SELECT COLX, COLY, COLZ
FROM MYTABLE2
WHERE COLX IN (SELECT COL1 FROM MYTABLE1 WHERE X = :P1_FILTER)
AND COLY IN (SELECT COL2 FROM MYTABLE1 WHERE X = :P1_FILTER)
AND COLZ IN (SELECT COL3 FROM MYTABLE1 WHERE X = :P1_FILTER)
There are other possible WHERE clause but the principle would be the same
Andy -
How do you use 3 Where Clauses in a query
Hi, i am trying to figure out how to use 3 Where Clauses in a Query where 2 of the Where Clauses uses a Sub query.
Display the OrderID of all orders that where placed after all orders placed by “Bottom-Dollar Markets”.
Order the result by OrderID in ascending order.
First WHERE clause checks for OrderDate and uses a sub query with ALL keyword.
Second WHERE clause use equals and sub query.
Third WHERE clause uses equal and company name.
This is what i have so far but i am pretty confused on how to do this.
My Code for NorthWind:
Select OrderID
From Orders o
Where o.OrderID IN (Select OrderDate From Orders Where Orders.OrderID > ALL
(Select CompanyName From Customers Where CompanyName = 'Bottom-Dollar Markets'));
The book shows how to use the ALL Keyword but not in a Sub query with Multiple Where Clauses.
Select VenderName, InvoiceNumber, InvoiceTotal
FROM Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE InvoiceTotal > ALL (Select InvoiceTotal From Invoices Where VendorID = 34)
ORDER BY VendorName;>Where Orders.OrderDate
> ALL (Select
CompanyName
The comparison operator (>) requires compatible data types.
DATETIME is not compatible with VARCHAR string for comparison.
Here is your homework:
SELECT orderid
FROM orders o
WHERE o.orderdate > ALL (SELECT orderdate
FROM orders
WHERE shipvia = (SELECT Max(shipvia)
FROM orders o
INNER JOIN customers c
ON c.customerid =
o.customerid
WHERE
c.companyname = 'Bottom-Dollar Markets'));
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014 -
Use of EXISTS clause in Interface
I want to build an interface from one table but using an exist clause to check the data change in the same table.
In SQL statement it can be expressed as follows
SELECT PB.PROJECT_KEY, COUNT (DISTINCT DW_PROJECT_BUILDING_KEY)
FROM DW_PROJECT_BUILDING PB
WHERE EXISTS
(SELECT 1
FROM DW_PROJECT_BUILDING PB1
WHERE PB1.PROJECT_KEY = PB.PROJECT_KEY
AND PB1.RECORD_STATUS_ID = 1)
AND PB.RECORD_STATUS_ID = 1
AND TRUNC (PB.LOAD_DT) >= '03-OCT-2013'
GROUP BY PB.PROJECT_KEY;
How can I build this SQL with exists clause in the interface ?
ThanksCreate the interface with DW_PROJECT_BUILDING table and give this table a name PB.
Now put one filter on the table with the conditions that you have just mentioned in the query above, with the exist statement.
I assume data from this table is targeted into one more aggregated table: here is the definition for the table:
CREATE TABLE PROJECT_BUILDING_AGGR
(PROJ_BUILD_KEY VARCHAR2(20),
PROJECT_KEY varchar2(20));
perform mapping like :
PROJECT_KEY=PB.PROJECT_KEY
PROJ_BUILD_KEY=count(distinct PB.DW_PROJECT_BUILDING_KEY)
Select IKM as IKM SQL control append.
FLOW control as false.
run the interface, it is giving something like this for me, I hope this is according to your requirement:
insert into
ORACLE_SOURCE.PROJECT_BUILDING_AGGR
PROJ_BUILD_KEY,
PROJECT_KEY
select
PROJ_BUILD_KEY,
PROJECT_KEY
FROM (
select
count(distinct PB.DW_PROJECT_BUILDING_KEY) PROJ_BUILD_KEY,
PB.PROJECT_KEY PROJECT_KEY
from
ORACLE_SOURCE.DW_PROJECT_BUILDING PB
where
(1=1)
And (EXISTS
(SELECT 1
FROM DW_PROJECT_BUILDING PB1
WHERE PB1.PROJECT_KEY = PB.PROJECT_KEY
AND PB1.RECORD_STATUS_ID = 1)
AND PB.RECORD_STATUS_ID = 1
AND TRUNC (PB.LOAD_DT) >= '03-OCT-2013')
Group By PB.PROJECT_KEY
) ODI_GET_FROM -
Why Scalar Subquery expression cannot be used in HAVING clauses?
Hi All,
I'm new to SQL. I'm confused with Scalar Subquery.
Is there anyone who can answer me why Scalar Subquery expression cannot be used in HAVING clauses.
Can you show me a example?
Thanks very much,
Xianyi.Ye
Edited by: 908428 on 2012-1-16 下午7:24Hi,
908428 wrote:
Hi Frank,
Thank you for your quick reply. I also agree with your point.
But when I read the book, "OCA Oracle Database SQL Certified Expert Exam Guide",( link to illegal copies of book removed by moderator )
on Page 359, it said that
Scalar subquery expressions cannot be used in the following locations:
1. In CHECK constraints
2. In GROUP BY clauses
3. In HAVING clauses
4. In a function-based index (which is coming up in Chapter 11)
5. As a DEFAULT value for a column
6. In the RETURNING clause of any DML statement
7. In the WHEN conditions of CASE
8. In the START WITH and CONNECT BY clauses, which we discuss in
Chapter 16.7 and 8 are wrong. Scalar sub-queries can be used in WHEN conditions and START WITH and CONNECT BY clauses. (At least in Oracle 10.2. Is the book based on some earlier version?)
Edited by: BluShadow on 17-Jan-2012 09:08 -
hi,
can we use output of a function in where clause directly ?
i.e
select node(condition1) clause from dual;
clause
occupation='SALARY'
then i want to use this output i.e clause string directly in select stmt.
select * from abc_table where clause
more strictly
select * from abc_table where (select node(condition1) clause from dual)
which is interpreted as
select * from abc_table where occupation='SALARY'
Is there any way to use functions in clause area ???
I tried CURSOR but not workd for this
Thanks in advance,
RupI cant really understand your problem
--fn1 is a function
sql>
select fn1
from dual;
FN1
CLERK
sql>
select * from emp
where job = fn1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
Message was edited by:
jeneesh -
Can a recursive sql have a where clause
can the following sql have a where clause and I dont want to hard code the id is is that possible...
select DISPLAY_NAME, ID, PARENT_ID, display_in_parent_folder, sub_folder_sequence
from wwsbr_all_folders start with id = 8888
connect by prior id= parent_actually g is another alias I created sorry I missed it... if I do this as a regular sub query I get three records but when I implement it as a recursive query it does not retutn any thing. Below is my regular query followed by the recursive query which I am using to organize the result but get no results:
REGULAR Query that retrieves records with parent_id = from the list of parent ids
select distinct f.DISPLAY_NAME, f.id, f.type_id, f.type_siteid, f.base_type_id, f.parent_id, f.sub_folder_sequence
from WWSBR_ALL_FOLDERS f, WWSBR_ALL_FOLDERS g
where g.parent_id = ( select g.parent_id from WWSBR_ALL_FOLDERS e
where
e.caid = 103 AND
e.parent_id <> 0 AND
e.type_id = 1 AND
e.display_in_parent_folder <> 0 AND
e.id = f.parent_id)
order by f.id, f.sub_folder_sequence;
Recursive query that results nothing:
select distinct f.DISPLAY_NAME, f.id, f.type_id, f.type_siteid, f.base_type_id, f.parent_id, f.sub_folder_sequence
from WWSBR_ALL_FOLDERS g, WWSBR_ALL_FOLDERS f start with f.id = ( select g.parent_id from WWSBR_ALL_FOLDERS e
where
e.caid = 103 AND
e.parent_id <> 0 AND
e.type_id = 1 AND
e.display_in_parent_folder <> 0 AND
e.id = f.parent_id)
connect by prior f.id= g.parent_id
order by f.id, f.sub_folder_sequence; -
Why do I have to use SELECT INTO clause in a Stored Proc ?
Hi,
I'm new to oracle. I've bought Oracle Database 10g SQL and Oracle Database 10 A beginner's Guide, two books from Oracle press but I can't find any answer for that.
Why when I'm writing a Stored Procedure with a SELECT statement do I have to use an INTO clause ?? And why when the select returns more than one row do I have to use a cursor ? By the way is this the only way to return more than one row from a stored procedure ?
Any help will be appreciated.
Regards.
S. NunesSounds like you may have some experience with other databases (SQL Server, maybe?) which allow you to create stored procedures which implicitly return result sets simply by embedding select statements.
In Oracle, the correct way to do this is using ref cursors.
See the following link for a good explanation:
http://osi.oracle.com/~tkyte/ResultSets/index.html -
Output not sorted when using group by clause
Recently we migrated our application from Forms5 to Forms10g and database from 7.3.2 to 10gR2.
More than 400 applications using sql stmt with "group by" clause and without "order by".
In Earlier version of oracle 7.3.2 "group by" clause in SQL stmt sorts the output by order of the column in the "group by"
stmt, where new version of oracle 10g (10.2.0.1.0 - 64bit Production) doesnt behave like older version.
eg select div_cd,dept_cd,count(*) from class group by div_cd,dept_cd
Output of the above query in Oracle7
DIV_CD DEPT_CD COUNT(*)
0 1 120
0 9 131
1 4 938
1 6 1
4 1 1490
5 2 59
6 6 848
6 9 295
8 1 45
9 5 19
Output of same query in Oracle10g
DIV_CD DEPT_CD COUNT(*)
0 6 120
0 9 131
4 1 1490
6 9 295
8 1 45
9 5 19
1 4 938
1 6 1
5 2 59
6 6 848
My question is
a) Does the oracle behave in that manner?.
b) if so, Any patch set available for this?
c) if no patch set how to resolve this issue.
if i use "order by" clause
along with group by then i can get the output how i want.
BUt problem is more than 400 applications (forms) using above method.
Identify Forms/Reports and rectifing is a tedious job.
Anyone can give me a good solution to solve the above issueGroup By does not, and never has guaranteed order. Just because it happened that way on the past does not mean it will continue to do so.
In the past, the algorithm used for Group By invoked a sort. It was convenient for the system to simply dump the output in that resulting order. These days, Group By could also be accomplished using a hash algorithm. Walking through the hash can easily result in a completely different ordering, as you have seen.
Your problem is that the assumptions made previously are now invalid. Oracle is still working correctly. No patch, but you might be able to work around it by crippling your database and using the COMPATIBLE parameter (although the official lower limit is 9.2.0)
Maybe you are looking for
-
I was receiving this error while trying to restore my new iphone 5 from my iphone 4s backup file over the icloud. I finally figured out the solution after HOURS of working with both ATT and Apple. Both were as helpful as possible and after speaking
-
HT1338 How do you know what is installed on your MacBook Air? Which system?
How do you know what operating system you have?
-
What is an Abstract in message interface ?
hai pals, can anybody clarify ,what is an Abtract in message interface of integration builder of xi. Is this,neccessary to specify and create the interface with type abstract. if yes , can u pls tell me why? right answers will be ap
-
Binary Ldap field doesn't return an byte[]
Dear experts I use JNDI to get information from a Siemens DirX Ldap Directory. Two of the fields I get are jpeg images. One of them is the standard jpegPhoto binary fields and the second is a user-defined binary field based on the same Syntax definit
-
Touch "Crashes" since dowloading 3.1.1
A little over a week ago, I paid for and downloaded version 3.1.1 of the Touch software. Since doing that, when using the Genius on certain but not all songs, my touch "crashes" and reverts to the home screen. I've tried restoring from back-up and as