run string as query in oracle - oracle

i got a little problem in Oracle. I try to create a sequence for generating IDs in a table that already has data in it. I try to use the following anonymous block.
declare y varchar2(2000);
BEGIN
SELECT 'CREATE SEQUENCE ID_SEQ MINVALUE 1 MAXVALUE 9999999999 START WITH ' || (max(ID)+1) || ' INCREMENT BY 1 CACHE 20;' INTO y FROM TEST_TABLE;
--dbms_output.put_line(y);
execute immediate y;
end;
I get the following error:
Error report:
ORA-00911: invalid character
ORA-06512: at line 5
00911. 00000 - "invalid character"
If I execute the value of the y variable it works perfectly. I'm using SQL Developer as input interface and working on a 11g r2 Oracle server. I found similar codes where 'INCREMENT BY' parameter were script generated. Can someone explain my error?

When you execute immediate, the command you run should not have a semi-colon on the end; that is a command separator in SQL Developer (and SQL*Plus, and other clients), not part of the SQL statement itself.
SELECT 'CREATE SEQUENCE ID_SEQ MINVALUE 1 MAXVALUE 9999999999 START WITH '
|| (max(ID)+1) || ' INCREMENT BY 1 CACHE 20' INTO y FROM TEST_TABLE;
This is shown in the examples for plain SQL. Just to help confuse you though, if you are using PL/SQL within the dynamic SQL, you do still need semi-colons appropriate to PL/SQL itself - though not the executing / you'd use to run it directly from a client. This is shown in other examples.

Related

Extracting output from PLSQL procedure to local drive of my laptop

I have a database connection server "server_dev" in sqldeveloper .
Now i want to create a procedure whose output can be directly saved in a csv file for data comparison later in the local drive of my laptop.
So i tried using UTL_FILE oracle package but when i ran the procedure the UTL_FILE was trying to write in the file of the server "server_dev" whereas i dont have any access to that server hence that command isnt working.
for example: the code is:-
CREATE OR REPLACE PROCEDURE export_to_csv_test
IS
v_file UTL_FILE.file_type;
v_string VARCHAR2 (4000);
CURSOR c_contexts
IS
SELECT workspace_id,context_id from contexts where rownum<5;
BEGIN
v_file :=
UTL_FILE.fopen ('Z:\My_Project_knowledge\CSVDIR', 'empdata.csv','w',1000);
FOR cur IN c_contexts
`enter code here`LOOP
v_string :=
cur.workspace_id
|| ','
|| cur.context_id;
UTL_FILE.put_line (v_file, v_string);
END LOOP;
UTL_FILE.fclose (v_file);
END;
for calling it :-
BEGIN
export_to_csv_test;
END;
Error report:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "RAY_DEV07_OWNER.EXPORT_TO_CSV_TEST", line 20
ORA-06512: at line 3
29280. 00000 - "invalid directory path"
*Cause: A corresponding directory object does not exist.
*Action: Correct the directory object parameter, or create a corresponding
directory object with the CREATE DIRECTORY command.
So,I analysed it and found that my SQL developer is connected to a server to my local machin and since its my office laptop I cant alter it.
Can i have any other way in which I can save the output of my stored procedure to my local drive in a text or Csv file?
To write a file to your local machine you may use dbms_output; for example in SQLPlus:
SQL> set feedback off
SQL> set echo off
SQL> set serveroutput on
SQL> spool d:\spool.txt
SQL> begin
2 for i in (select level from dual connect by level <= 5) loop
3 dbms_output.put_line('Level ' || i.level);
4 end loop;
5 end;
6 /
WIll produce the file d:\spool.txt:
Level 1
Level 2
Level 3
Level 4
Level 5
If you can select directly from a table or table function, then SQL*Plus 12.2's new SET MARKUP CSV option will be useful. Instead of paginating the query output it will produce CSV. The full syntax is
SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]
Output generation will faster if you turn on this mode with the sqlplus -m option.
It's also useful for querying JSON types. See https://blogs.oracle.com/opal/entry/fast_generation_of_csv_and

Oracle triggers error on sqldeveloper export script

I had built a few tables with sequences and triggers, since I need to share the script with my team at uni I did an export with sqldeveloper, now when I try to import/execute the resulted .sql I'm getting errors with triggers.
This is the error message:
"Error starting at line 250 in command:
CREATE OR REPLACE EDITIONABLE TRIGGER "TRG_ACCOUNTS"
BEFORE INSERT ON ACCOUNTS
FOR EACH ROW
BEGIN
SELECT SEQ_ACCOUNTS.NEXTVAL INTO :NEW.ACCOUNT_ID FROM DUAL
Error report:
SQL Command: editionable TRIGGER
Failed: Warning: execution completed with warning
Error starting at line 258 in command:
END
Error report:
Unknown Command
trigger "TRG_ACCOUNTS" altered."
This is the part of the script it's complaining about. I have a few other triggers and it's giving me the same error on all of them. I've checked how the triggers are created after executing the script, and all of them seem to be missing a semi colon and the "END;" at the end.
Example:
create or replace
TRIGGER "TRG_FL_AR"
BEFORE INSERT ON FLOOR_AREAS
FOR EACH ROW
BEGIN
SELECT SEQ_FL_AR.NEXTVAL INTO :NEW.FLOOR_AREA_ID FROM DUAL <-- missing ";" here
<missing "END;" here>
Could you please help me?
Thank you.
Try to put a back slash / after line 258 and check again

Dynamic query error "invalid character" [duplicate]

i got a little problem in Oracle. I try to create a sequence for generating IDs in a table that already has data in it. I try to use the following anonymous block.
declare y varchar2(2000);
BEGIN
SELECT 'CREATE SEQUENCE ID_SEQ MINVALUE 1 MAXVALUE 9999999999 START WITH ' || (max(ID)+1) || ' INCREMENT BY 1 CACHE 20;' INTO y FROM TEST_TABLE;
--dbms_output.put_line(y);
execute immediate y;
end;
I get the following error:
Error report:
ORA-00911: invalid character
ORA-06512: at line 5
00911. 00000 - "invalid character"
If I execute the value of the y variable it works perfectly. I'm using SQL Developer as input interface and working on a 11g r2 Oracle server. I found similar codes where 'INCREMENT BY' parameter were script generated. Can someone explain my error?
When you execute immediate, the command you run should not have a semi-colon on the end; that is a command separator in SQL Developer (and SQL*Plus, and other clients), not part of the SQL statement itself.
SELECT 'CREATE SEQUENCE ID_SEQ MINVALUE 1 MAXVALUE 9999999999 START WITH '
|| (max(ID)+1) || ' INCREMENT BY 1 CACHE 20' INTO y FROM TEST_TABLE;
This is shown in the examples for plain SQL. Just to help confuse you though, if you are using PL/SQL within the dynamic SQL, you do still need semi-colons appropriate to PL/SQL itself - though not the executing / you'd use to run it directly from a client. This is shown in other examples.

PL SQL output is not getting displayed

I have fairly simply code ..running in Oracle Virtualbox. However for some reason it is not displaying pl/sql output.
Here is code snippet
SQL> set serveroutput on
SQL> list
1 Create or Replace procedure mytz
2 IS
3 v_mytz TIMESTAMP WITH TIME ZONE DEFAULT '2013-05-05 12:00:00 AM';
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE ('Default timestamp is ' );
6* end mytz ;
SQL> /
Procedure created.
SQL>
Is there anything I need to do special to see the output on SQL prompt ?
You have to actually run the procedure, not just create it, e.g.:
set serverputput on
exec mytz;
The set serveroutput SQL*Plus command has to be in the session the procedure is executed, not the one where it is created (if they are different).
You are not showing the value of your variable at the moment; maybe you wanted this?
dbms_output.put_line('Default timestamp is: ' || v_mytz);

Long PL/SQL output in SQLPLUS

Got problem with executing my script under SQLPLUS. Under SQL Developer it works well.
set serveroutput on size 1000000
declare
yyy varchar2(32000):='';
begin
yyy := 'XxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzz';
yyy := yyy || 'XxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzz';
yyy := yyy || 'XxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzzXxxYyyZzz';
dbms_output.put_line(yyy);
end;
/
Under SQLPLUS I get error:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at line 1
Any solution how to force running scripts with long output under SQLPLUS? I need output on because I spool results to file.
You are using a Oracle client older than Oracle 10g against an Oracle 10g Database
Please find the quote from ASKTOM below
The problem is NOT during runtime, you are apparently using an old client against a 10.2 or above database. The database code runs fine
It is when the client goes to PICK UP THE STRING from the database that you are hitting the exception.
sqlplus is doing something like this:
a) call procedure/code - it runs and fills a dbms_output buffer, an array in a package.
b) then sqlplus calls dbms_output.GET_LINES to get the buffered output to print. THIS is the call that fails.
For more information Kindly refer
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1011431134399

Resources