PL/SQL anonymous block completed" with no result displayed - plsql

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.

Related

PL/SQL Script - first timer

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;

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.

Teradata LEFT() function issue

I run the following query against Teradata using SAS, and it works fine
SELECT LEFT(first_name,7)
FROM testTab
However, when I run the same query in Sql Assistant it throws an error : Expected something between keyword SELECT and keyword LEFT.
On my other computer, the above code runs on Sql Assistant.
So, my question is, why does the LEFT function work sometimes, and sometimes it doesn't ?
There are a lot of workarounds, but I want to know what the hell is going on with this LEFT function ?
I'm not sure how the SAS version was running as LEFT is not a function in Teradata. LEFT is a keyword in Teradata because of LEFT OUTER JOIN. Perhaps SAS has some sort of parser/rewrite thing that changes it over to proper Teradata function.
At any rate, to do this in Teradata you can do:
SELECT SUBSTRING(first_name FROM 1 FOR 7) FROM testtab
Workaround:
SELECT { fn LEFT(first_name,7) } FROM testTab;
Note: This needs to be run from SQL Assistant with ODBC connection with following special settings:
In SQL Assistant go to Tools --> "Define ODBC Data Source" --> << Click on desired Teradata DNS >> --> Configure --> Options --> "Enable Legacy Parser"
To resolve, go to configure OBDC -> Options -> check "Enable Legacy Parsing"
Or you can write in SEL { fn FunctionName() };
e.g. SEL {fn MONTH(DATE) };

PL/SQL Perfom TRIM inside of a trigger

I am trying to make a trigger that when data is inserted, it trims the data that is being inserted.
Here is what I am trying ..
CREATE OR REPLACE TRIGGER trig_trim
BEFORE INSERT ON triggertest
FOR EACH ROW
BEGIN
TRIM(:new.testchar);
END;
/
I do an insert like this
INSERT INTO triggertest (testnum, testchar) VALUES (9, ' r9 ');
and I am getting this error...
04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation"
*Cause: A trigger was attempted to be retrieved for execution and was
found to be invalid. This also means that compilation/authorization
failed for the trigger.
*Action: Options are to resolve the compilation/authorization errors,
disable the trigger, or drop the trigger.
When I just run the code to create the trigger I get this
TRIGGER TRIG_TRIM compiled
Errors: check compiler log
and in the compiler log it says "'TRIM' is not a procedure or is undefined"
Is my syntax wrong or my logic? I don't know why this is failing.
Your assignment syntax is wrong. Try this:
:new.testchar := TRIM(:new.testchar);
TRIM has to return the result to something. I think you want:
:new.testchar := TRIM(:new.testchar);

How do we export a ms-word (or rtf) document (from a web browser) to generated by pl/sql?

I need to generate a ms-word document from a pl/sql query to export a set of reports through a web browser.
I have been searching for a specific way to modify the content headers using owa_util.mime_header etc etc but could not really get a working proof of concept to get the web page to present the user to download/open/save a ms-word document.
I know it's not much but..
begin
OWA_UTIL.MIME_HEADER ( 'application/vnd.ms-word', false);
htp.print('Content-Disposition:attachment;filename="test.doc"');
OWA_UTIL.HTTP_HEADER_CLOSE;
for x in (select first_column, second_column from my_table)
loop
htp.p(x.first_column||'<br/>'||x.second_column||'<br/><br/>');
end loop;
end;
Could someone please shed some light on how can I create a procedure that generates output to html which intercept the browser headers and prints out the query content to a word document?
By the way, the current environment (restriction) I am running this from:
Oracle APEX 3.0.1 (not 3.1, which I believe has an export to word doc feature).
Thanks in advance.
In Apex you can make a page that is completely generated from a procedure of your making:
Create a blank page.
Under Processes, click Create, and choose PL/SQL.
Give it a name, and leave the default option "On Load - Before Header".
For PL/SQL Page Process, enter the following:
BEGIN
myprocedure;
htmldb_application.g_unrecoverable_error := true;
END;
Create your procedure (the code you have should work ok) and it should work fine.
EDIT: the code in "myprocedure" should emit HTML code, including the HTML and BODY tags.

Resources