Hot answers tagged

18

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


18

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-...


14

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 ...


13

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 ...


12

The most likely cause of a mutating table error is the misuse of triggers. Here is a typical example: you insert a row in table A a trigger on table A (for each row) executes a query on table A, for example to compute a summary column Oracle throws an ORA-04091: table A is mutating, trigger/function may not see it This is an expected and normal behaviour,...


12

Its an interesting question, to be sure. Most people who are familiar with Oracle development wouldn't give it a thought but when you come down to it, its sometimes confusing to define the demarcation between SQL and PL/SQL. By looking at the definition of the acronyms, you start to get an idea of what areas of functionality each covers: SQL - Structured ...


12

If it is wrapped in BEGIN ... END DECLARE ... END CREATE OR REPLACE ... END Is a one-liner prefixed EXECUTE Then it is PL/SQL. What does this mean under the hood? SQL gets "compiled" to a query plan and executed, immediately returning a result set in the event of SELECT or the number of rows affected in other cases, or an error. PL/SQL however is more ...


12

Benefits of Packages Logical Grouping – Methods that work together can be put into a cohesive unit rather than just logically coupled but physically separate. Secure Private Methods - Functions and Procedures can be made private to the package and only be used within it. This makes the public surface simpler and more secure. Privilege Management – ...


11

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 ...


11

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 ...


10

If T2 is derived data based on the contents of T1, then you may want to replace T2 with a materialized view. If T2 is rarely accessed, and quick to derive, then you may just want to use a view. On the application side, if you stop maintaining table T2, then the values it gets will become increasing incorrect. This can be partially corrected by ...


10

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 ...


10

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 ( '...


10

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'); ...


9

National Language Support (because Americans have this weird idea of using MM-DD-YYYY as a date format, and different languages have different words for their months).


8

A mutating table occurs when a statement causes a trigger to fire and that trigger references the table that caused the trigger. The best way to avoid such problems is to not use triggers, but I suspect the DBA didn’t take the time to do that. He could have done one of the following: Changed the trigger to an after trigger. Changed it from a row level ...


8

Here is an example of the API structure to replace a trigger. By calling the AddEntry procedure in the package, the data is inserted into both tables. CREATE OR REPLACE PACKAGE MyPackage IS Procedure AddEntry (pValue1 IN Number, pValue2 IN Number); END; / CREATE OR REPLACE PACKAGE BODY MyPackage IS Procedure AddEntry (pValue1 IN Number, pValue2 IN ...


8

The Oracle Concepts Guide for 11.2 says the following (The note is of particular interest): Globalization Support Environment The globalization support environment includes the client application and the database. You can control language-dependent operations by setting parameters and environment variables on the client and server, which ...


8

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 ...


8

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 ...


8

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 ...


7

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...


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

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

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 ...


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 ...


6

So the way I do this is I don't use default values. Or rather, I set them to NULL as default. Then within my procedure body I check to see if it's equal to NULL and if so set it to a sane default. At that point you could mangle a flag or two in the same space to see what's being set or not set. However, there's the edge case of if they intended to pass in ...


6

The error in you script is that the script now expects a table named tablename, having a column named columnname. In this case you don't know the table and column names so you should use dynamic sql to run this. Next to that, if possible, forget about LONG and implement lobs instead. For docu see http://download.oracle.com/docs/cd/B28359_01/appdev.111/...


6

Assuming that you are using PL/SQL to dequeue the message (there may be additional JMS limitations-- I don't know enough about the interplay between JMS and AQ), you should be able to use a priority queue to implement LIFO (though it would be a bit hokey). When you enqueue a message, you can specify a priority The priority attribute specifies the ...


6

It didn't seem like people were really addressing the second part of your question so I thought I'd chime in. First, I like both answers to part 1 of your question. I think either a materialized view or creating a stored procedure to manage inserting into the second table are satisfactory (+1'd both of them). I like the flexibility of using a stored ...



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