Hot answers tagged

30

This is actually a really bad thing to do IMHO, and it's not supported in most other database platforms. The reasons people do it: they're lazy - I don't know why people think their productivity is improved by writing terse code rather than typing for an extra 40 milliseconds to get much more literal code. The reasons it's bad: it's not self-documenting -...


26

I think this is what you're after: V_COUNT := MY_ARRAY.COUNT;


17

Oracle's official documentation library has some great resources, including: The Database Concepts guide, which is essential reading for beginner and Oracle ninja alike. The 2 Day DBA The 2 Day Developer's Guide Oracle 12c official PL/SQL documentation: PL/SQL Language Reference PL/SQL Packages and Types Reference Chapter 5 Developing Stored Subprograms ...


16

The code you posted is using a cursor. It is using an implicit cursor loop. There are cases where using an explicit cursor loop (i.e. declaring a CURSOR variable in the declaration section) produces either cleaner code or better performance If you have more complex queries that you can't refactor out into views, it can make the code easier to read if your ...


15

If you're really only looking to learn about the development side and have no interest in the administration or installation side at the moment, a quicker route might be to download a prebuilt developer VM image for Virtualbox. That can get you up and running very quickly, and you can connect to the DB running inside the VM from outside, so you can continue ...


14

Anonymous PL/SQL blocks don't start with IF. The above code should be in between a begin and end; at least. EXISTS is an SQL function, it can't be used in PL/SQL like that. Try something like this: set serveroutput on declare c number; begin select count(*) into c from my_table where rownum = 1; if c != 0 then dbms_output.put_line('has rows'); ...


12

You can do this with DBMS_LOCK and an exclusive lock. See the following procedure: CREATE OR REPLACE PROCEDURE myproc IS lockhandle VARCHAR2(128); retcode NUMBER; BEGIN DBMS_LOCK.ALLOCATE_UNIQUE('myproclock',lockhandle); retcode:=DBMS_LOCK.REQUEST(lockhandle,timeout=>0, lockmode=>DBMS_LOCK.x_mode); IF retcode<>0 THEN ...


12

I am not proud of publicly writing something like this. Sample + data: CREATE TABLE donaldduck ( SURNAME VARCHAR(100), TOWN VARCHAR(100) ); CREATE TABLE daisyduck ( SURNAME VARCHAR(100), TOWN VARCHAR(100) ); CREATE TABLE goofy ( something number(1), SURNAME VARCHAR(100), TOWN VARCHAR(100) ); INSERT INTO DONALDDUCK ( SURNAME, TOWN ) VALUES ( '...


11

As you can see from the output below, a test anonymous PL/SQL block works in SQL Plus as expected: [oracle@node1 ~]$ sqlplus phil/phil SQL*Plus: Release 11.2.0.2.0 Production on Mon Oct 1 21:55:35 2012 Copyright (c) 1982, 2010, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production With ...


11

Of course you ought to study the license, but as I recall it is free to download and use for non-production purposes. It includes all of the Option packs, and is not in any sense cripled, but patches will not be available as they would be if you had a support account. Oracle XE is free to use in production environments, but has limited functionality and ...


11

This is not an Oracle or PL/SQL issue, but a matter of implementing the proper algorithm. Here is an example: https://www.orafaq.com/wiki/Binary CREATE OR REPLACE FUNCTION dec2bin (N in number) RETURN varchar2 IS binval varchar2(64); N2 number := N; BEGIN while ( N2 > 0 ) loop binval := mod(N2, 2) || binval; N2 := trunc( N2 / 2 ); ...


10

First, if you are creating a procedure in a package, the package name will need to be included when you call the procedure. begin hotel.fill_city(10000); end; / should correctly invoke your procedure. Second, you have issues with the naming of your local variables. Normally, you would not create local variables like city and postal_number that are the ...


9

MySQL allows you to do GROUP BY with aliases (Problems with Column Aliases). This would be far better that doing GROUP BY with numbers. Some people still teach it Some have column number in SQL diagrams. One line says: Sorts the result by the given column number, or by an expression. If the expression is a single parameter, then the value is interpreted as ...


9

I think people were having trouble understanding your question due to the table structure which is so bad it seems designed to give you a headache. As Balazs Papp indicates very little can be implemented that will scale or not look like something hacked together. However there are solutions that can be done in PL/SQL. A pipelined table function will end ...


8

Interesting question. As far as I can tell, this is not possible, though it seems like it should be. Unless someone else can show how it can be done, a workaround would be to nest the code in a block: create or replace trigger trg_before_insert_temp1 before insert on temp1 for each row declare begin <<bob>> declare int_val int; ...


8

Fortunately, I found in the existing PL/SQL code I have to maintain, a working "native" behavior: V_COUNT := MY_ARRAY.COUNT; should do the trick. This one is very hard to find with Google, since "count" is more frequently referring to the SELECT COUNT(...) which can be found in SQL queries...


8

It sounds like you want the GREATEST function INSERT INTO my_table( date_column ) VALUES( GREATEST( date_default, date_insert_value ));


8

Consider below case: +------------+--------------+-----------+ | date | services | downloads | +------------+--------------+-----------+ | 2016-05-31 | Apps | 1 | | 2016-05-31 | Applications | 1 | | 2016-05-31 | Applications | 1 | | 2016-05-31 | Apps | 1 | | 2016-05-31 | Videos | 1 | | ...


7

Gathering statistics and rebuilding indexes are two completely separate things. It is exceedingly rare that an index in Oracle needs to be rebuilt so any process that is regularly rebuilding an index is highly suspect. I strongly suspect that rebuilding the indexes is unnecessary. And I would strongly suggest spending some quality time reading through ...


7

Is there some operator or function that can give me the indices of the array No, you have to loop through the associative array: declare type a_arr_t is table of PLS_INTEGER index by PLS_INTEGER; type keys_t is table of PLS_INTEGER; a_arr a_arr_t; keys_ keys_t := keys_t(); l_index integer; begin a_arr(1) := 2; ...


7

A cursor can be explicit or implicit, and either type can be used in a FOR loop. There are really two aspects to your question. Why use an explicit cursor FOR loop over an implicit cursor FOR loop? Use an explicit cursor FOR loop when the query will be reused, otherwise an implicit cursor is preferred. Why use a loop with a FETCH rather than a FOR ...


7

This is the expected behaviour of nextval as documented: Within a single SQL statement containing a reference to NEXTVAL, Oracle increments the sequence once: For each row returned by the outer query block of a SELECT statement [...] If any of these locations contains more than one reference to NEXTVAL, then Oracle increments the sequence ...


7

As it says in the documentation: Only dynamic SQL can execute the following types of statements within PL/SQL program units: Data definition language (DDL) statements such as CREATE, DROP, GRANT, and REVOKE A TRUNCATE operation is DDL. When using EXECUTE IMMEDIATE, remember that any DDL operations you execute will implicitly COMMIT the ...


7

I would just delete. Even if I had to only delete if there were say exactly 2 records, then I would add this condition to the where clause, like DELETE FROM table1 WHERE col1='somevalue' AND (SELECT COUNT(*) FROM table1 WHERE col1 = 'somevalue') = 2; In all cases when writing a delete statement consider that in general the where clause is usually either ...


7

Have you seen this? https://community.oracle.com/thread/889338?start=0&tstart=0 It states you can only exchange partitions from a partitioned table to a non-partitioned table, or vice-versa. You'd need to temporarily exchange the partition into an interim, non-partitioned, table, then into the target partitioned table. There are some interesting ...


7

WHERE PARENT_ID IN my_array; This will not work. First, as the error message states, you are not allowed to use local collection types in SQL statements, you need to define them in the database. Second, that syntax does not exist. So first you define the type: CREATE TYPE arr_type is TABLE of VARCHAR2(11 BYTE); And after that, you can use the collection ...


7

In SQL Developer, use the /*insert*/ "hint". select /*insert*/ * from t1; REM INSERTING into T1 SET DEFINE OFF; Insert into T1 (C1,C2) values (1,2); Insert into T1 (C1,C2) values (2,3); Insert into T1 (C1,C2) values (3,4); Another useful "hint": select /*csv*/ * from t1; "C1","C2" 1,2 2,3 3,4 Starting with version 4.1, the "hint" the below also works: ...


6

If I understand the question correctly you want to find numbers that have, for example, more than 4 digits of precision after the decimal? E.g., you want 3.0671, 3.06713, etc. but not 3.067 or 3.06 or 3. If that is the case then you could do something like: select number from table where number - trunc(number,4) <> 0; trunc here will simply remove ...


6

ORU-10027 will appear if you are using a lot of dbms_output.put_line in your PL/SQL with a small buffer amount. If you are just debugging then you should set this to a large amount (dbms_output.enable(10000000) is traditional though from 10g onwards it can be unlimted: dbms_output.enable(null)) but try to remove them when going to production.


6

I think the question is a little misleading & causes people to not think properly. Looks like the 3 conditions won't overlap, so all you need to do is OR the 3 statements together: create or replace package body If_Else_Pack is Procedure Moving(obj_A IN varchar2, obj_B IN varchar2, obj_C IN varchar2, ...


Only top voted, non community-wiki answers of a minimum length are eligible