PL/SQL Script - first timer - plsql

I have data in an interface table that I need to copy over to the application table based on a set of criteria (project #, task_Number). I am able to access everything I need in a select statement. I am unsure of how to start the script. Any pointers would be wonderful. I havent written a PL/SQL script before.

Welcome to PLSQL!
How you write your script depends on the context from which it will be run.
If I am running a script through sqlplus I have some bolier plate which I have below which you might find starts you off.
set define off;
set serveroutput on;
declare
l_error varchar2(4094);
begin
dbms_output.put_line('Start of script named XXX');
--Select/Insert statements here
--commit
dbms_output.put_line('End of script named XXX');
EXCEPTION WHEN OTHERS THEN
l_error := NVL(SUBSTR(SQLERRM,0,990),'NULL');
dbms_output.put_line(l_error || ':-' || NVL(SUBSTR(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE,0,3000),'NULL'));
rollback;
end;

Related

PL/SQL anonymous block completed" with no result displayed

When I executed the code below i got the message "anonymous block completed" without any results back. Can anyone help me out?
BEGIN
FOR r IN
(SELECT DBMS_METADATA.GET_DDL
(object_type => 'VIEW', name => view_name, schema => USER)
AS view_text
FROM USER_VIEWS)
LOOP
IF INSTR (r.view_text, 'Project') > 0 THEN
DBMS_OUTPUT.PUT_LINE (r.view_text);
END IF;
END LOOP;
END;
Before running this piece of code, you have to enable output. In SQL*Plus and SQL Developer, it is done by running
set serveroutput on
PL/SQL Developer must have something similar, either by explicitly running that statement (if it is supported), or by clicking somewhere in the output window so that DBMS_OUTPUT has something to display the result to.
I have also found this (in case someone needed):
SQL Developer seems to only output the DBMS_OUTPUT text when you have explicitly turned on the DBMS_OUTPUT window pane.
Go to (Menu) VIEW -> Dbms_output to invoke the pane.
Click on the Green Plus sign to enable output for your connection and then run the code.

Dynamically create folder in plsql procedure in server

I hve written a pl/sql procedure :
PROCEDURE xx_WriteBLOBToFILE (myfilename IN VARCHAR2,L_PERSON_ID IN NUMBER) IS
v_blob BLOB;
blob_length INTEGER;
out_file utl_file.file_type;
v_buffer RAW(32767);
chunk_size BINARY_INTEGER := 32767;
Blob_Position Integer := 1;
G_Zipped_Blob Blob;
B_Dl_File1 Blob;
BEGIN
-- Retrieve the BLOB for reading
Select Image Into V_Blob From Per_Images
Where Parent_Id =L_PERSON_ID;
-- Retrieve the SIZE of the BLOB
blob_length:=DBMS_LOB.GETLENGTH(v_blob);
-- Open a handle to the location where you are going to write the BLOB to file
-- NOTE: The 'wb' parameter means "write in byte mode" and is only availabe
-- in the UTL_FILE package with Oracle 10g or later
Out_File := Utl_File.Fopen ('**INT_DIR_IMG_BLOB**', Myfilename, 'wb', Chunk_Size);
-- Write the BLOB to file in chunks
WHILE blob_position <= blob_length LOOP
IF blob_position + chunk_size - 1 > blob_length THEN
chunk_size := blob_length - blob_position + 1;
End If;
DBMS_LOB.READ(v_blob, chunk_size, blob_position, v_buffer);
UTL_FILE.PUT_RAW(out_file, v_buffer, TRUE);
blob_position := blob_position + chunk_size;
END LOOP;
-- Close the file handle
Utl_File.Fclose (Out_File);
End;
I want to dynamically create the folder in /Blobfile in a location and then create the directory like INT_DIR_IMG_BLOB dynamically.
How can i create the folder in the server in plsql
If the operating system directory does not already exist, you probably can't create it directly in PL/SQL. You could create a Java stored procedure (or a .Net stored procedure if your database runs on Windows) that creates the operating system directory and call that from PL/SQL. You could also create a dbms_scheduler job that calls out to the operating system to create the directory and call that from PL/SQL.
Once the operating system directory exists, you can create the Oracle directory object using dynamic SQL. You'd also, presumably, need to grant privileges on that directory object to users dynamically.
execute immediate 'create directory ' || directory_name_you_want ||
' as ''' || directory_path || '''';
execute immediate 'grant read on ' || directory_name_you_want ||
' to some_role';
You could then use the newly created directory in your code.
Although you could do this, I would generally advise you not to go down this path. Dynamically creating objects at runtime is generally a poor idea, it's going to make your code a lot more complicated and much harder to support. And it creates lots of opportunities for things to go wrong in new and interesting ways.
You can use DBMS_SCHEDULER for create the folder in os.
1. create credentials.
2. write scheduler for external job execution.
BEGIN
DBMS_CREDENTIAL.CREATE_CREDENTIAL(
credential_name => 'credintial', ----- credintial name give by u
username => 'dons', ----- os username
password => 'password'); ----- os password
END;
/
CREATE OR REPLACE PROCEDURE PROC_MAKEFOLDER AS
BEGIN
DBMS_SCHEDULER.CREATE_JOB
(JOB_NAME=>'folder_maker', --- job name
JOB_ACTION=>'mkdir /home/anil/new_folder', --- executable file with path
JOB_TYPE=>'executable', ----- job type
NUMBER_OF_ARGUMENTS=>0, -- parameters in numbers
ENABLED=>false,
AUTO_DROP =>true,
CREDENTIAL_NAME=>'credintial', -- give credentials name which you have created before "credintial"
COMMENTS=> 'folder or os directory creation');
DBMS_SCHEDULER.RUN_JOB('folder_maker');
END ;
EXEC PROC_MAKEFOLDER;

Cursor and SUBSTR usage in procedure

I need to trim instance name from complete dblink name .For example The select query returns result like HHVISDEV.TRINITI.COM. I need to get HHVISDEV. And ofcourse There are such multiple results. So I need to use cursor and print the final result. I am getting Warning: Procedure created with compilation errors., when I compile. And when I call the procedure I am getting ERROR at line 1:
ORA-06575: Package or function DELETEDBLINKS1 is in an invalid state. Can any one please guide me.
create or replace procedure DeleteDBLinks1 is
cursor mycursor is
SELECT SUBSTR(DB_LINK, 1, INSTR(DB_LINK, '.', 1, 1) - 1)
FROM dba_db_links;
myvar dba_db_links.dblinks%TYPE;
BEGIN
OPEN mycursor;
LOOP
FETCH mycursor
INTO myvar;
EXIT WHEN mycursor%NOTFOUND;
DBMS_OUTPUT.put_line(myvar);
END LOOP;
CLOSE mycursor;
end;
/
If you see Warning: Procedure created with compilation errors, then, I can guess, you compile your procedure in SQL*Plus. In SQL*Plus you can run command
show errors
and you will see errors list. Your procedure looks OK, so I think problem is - you have no access to dba_db_links view. Try to use all_db_links or user_db_links instead.
Along with what Dmitry said about you probably not having access to dba_db_links table, you also had a typo in the myvar variable definition. It should have been:
myvar dba_db_links.db_link%TYPE;
SQL*Plus> SHOW ERRORS will help you in your PL/SQL endeavors, until you start using a PL/SQL IDE like SQL Developer or (my favorite) PL/SQL Developer, which will show you the errors automatically.

variable substitution in plpgsql(postgres) REINDEX query

--DROP FUNCTION tempscript();
CREATE OR REPLACE FUNCTION tempscript()
RETURNS integer AS
$BODY$
DECLARE
my_index TEXT;
b integer;
BEGIN
my_index := 'example_index';
RAISE NOTICE '% ', my_index;
reindex index my_index; /* problem with this line , we are not able to pass local variable as paramenter*/
b :=2;
return b;
END;
$BODY$
LANGUAGE
plpgsql VOLATILE
COST 100;
SELECT * from tempscript();
please find my comment in the above code where exactly i am facing the problem.
please let me know the solution or workaround.
Thank you.
Per the user manual, you can only use variables where query parameters are permitted. That doesn't include identifiers and utility statements.
You need to use the PL/pgSQL EXECUTE statement for dynamic SQL.
EXECUTE format('REINDEX %I', my_index);
Separately, if you need to automate REINDEXing then you're almost certainly doing something else wrong. It should not be necessary in most situations. Maybe you've turned autovacuum down too far so it's failing to keep up?
If you're writing a function for a one-off, use a DO block so you don't have to create, execute, and drop the function.

Is this code written in Pl/Sql, and if not, then what language is it?

I have encountered this code... Is this Pl/Sql? What do you think it is?
[Script 1.0]
script package up is
import native def_1;
procedure p(
i_g text
)
is
l_txt text;
begin
with mem_m(idx) as msg do
with book_aud(evt_id) as book do
book.upd_pkt(
evt_nr => i__nr
,ref_nr => msg.h.id
,account_nr => msg.h.id
,status => '1'
);
end with;
end with;
end p;
I am surprised by import and end with;
It is not the full code. It is reduced version of it.
It also contained familiar elements such as:
c_max constant number := 95;
c_VE_BA constant text := 'A07000';
-- comment
if i_mt is null then
return rpad('/',16);
else
if i_id = zconst_.c_JPY then
l_fmt := '9999999999999999';
else
l_fmt := '9999999999999D99';
end if;
end if;
case i_typ_id
when def_typ.contr then
l_zuonr := zfx2.c_avqt;
when def_typ.fx then
l_zuonr := zfx2.c_avqd;
when def_typ.fxswap then
l_zuonr := zfx2.c_avqd;
when def_typ.forex then
l_zuonr := zfx2.c_avqd;
when def_typ.xfer then
l_zuonr := zfx2.c_avqd;
when def_typ.intr then
l_zuonr := zfx2.c_avqt;
else
assert(false,'Meta Typ');
end case;
It looks like an extension of Pl/Sql.
Based on the responses and my own research, I guess it is Avaloq+PL/Sql.
I contacted Avaloq,I am still waiting for official answer.
It looks like Avaloq Script, used by (ahem) Swiss banks, and while there is very little about it online, I found a grammar which perfectly matches the terms in your samples.
Avaloq Script, the Avaloq Banking
System's script language, facilitates
entering specific business logic. The
structure of the data that can be
accessed through Avaloq Script is
defined in a DDIC (Data Dictionary),
making it unnecessary to know the data
storage structure.
yes it is avaloq script. its some kind of pl/sql pre compiler, you should be able to find a package called s#up where the real pl/sql code resides.
It definitely is Avaloq Script. The code snippet is a script package that Avaloq compiler compiles into PL/SQL. The point of Avaloq Script is to disallow direct database access and make the customizer of the Avaloq product to use the Avaloq API instead. The API is the Avaloq script language and a whole array of other ways like setting up rule tables to be loaded or special syntax to define forms, reports, workflows etc. often allowing snippets of Avaloq script within that other kind of sources.
Avaloq script has many PL/SQL elements but also some VB language concepts can be found. Here are some comments in the code to give some idea what the code means.
[Script 1.0] -- Have not seen other than 1.0 version
script package up is -- The PL/SQL package name is going to be s#up
import native def_1; -- import native means a PL/SQL package named
-- def_1 can be used, without native it is
-- another Avaloq script package
procedure p( -- declares a procedure with the name "p"
i_g text -- input variable i_g defined text.
-- in PL/SQL this becomes a VARCHAR2
)
is
l_txt text; -- local variable VARCHAR2(4000) in PL/SQL
begin
with mem_m(idx) as msg do -- mem_m is a DDIC (Data Dictionary)
-- It actually is a kind of "class" with
-- fields and methods
-- "with" is like in VB to avoid writing
-- mem_m(idx) all the time e.g. mem_m(idx).h.id
with book_aud(evt_id) as book do -- book_aud is another DDIC that it is not
-- prefixed with mem implies this is not a
-- in memory structure but direct access
-- to a Oracle table book_aud with index
-- evt_id which looks undefined to me and
-- should bring a compiler error
book.upd_pkt( -- method call in the book_aud DDIC
evt_nr => i__nr -- like in PL/SQL named parameters
,ref_nr => msg.h.id
,account_nr => msg.h.id
,status => '1'
);
end with;
end with;
end p;
I could also comment on the other code snippet above but I think you already get out the general concept. Neither mem_m nor book_aud is a known DDIC in the Avaloq version I am working with, wonder where you got it from. Since your post is many years old I suppose this was a very old Avaloq release.
I'm sure that isn't PL/SQL.
I know this doesn't directly answer your question but I might suggest that you go though the list here. It might be listed in here. There are several examples of programs in different programming languages. It may be hard to 100% identify the language unless someone happens to recognize it and finds a "finger print" to prove the language... Do you have more examples you could post?
http://www.ntecs.de/old-hp/uu9r/lang/html/lang.en.html
I don't think that is a functional language. Knowing this might help narrow your search.
The only language I can think of offhand that has "with...end with" syntax is visual basic. Could this be some scripting form of VB?

Resources