Oracle Apex Conditional Link - oracle

I have a conditional link as follows:
case
when My_Flag = 'Y' then
'<a href="' || apex_util.prepare_url('f?p=&APP_ID.:65:&APP_SESSION.::&DEBUG.:65:
:P65_MY_ID_ITEM,:P65_MY_ID_ITEM_2:' || TABLE_ID || ',' || TABLE_ID_2) || '">
<p>Details</p>'
else
'<a href="' || apex_util.prepare_url('f?p=&APP_ID. :65:&APP_SESSION.::&DEBUG.:65::
P65_MY_ITEM_3,:P65_MY_ITEM_4:' || TABLE_ID3 || ',' || TABLE_ID4) || '"><p>Details</p>'
end my_link
This link takes me to page 65 as intended but...items are not being populated.
Any idea what might be happening?
Thanks!

You may consider using the apex_page.get_url procedure to ensure your parameters are in the correct location. Currently you have an extra colon after the clear cache parameter.
I would also recommend using the :APP_SESSION bind variable in SQL instead of the substitution string, otherwise you'll blow out your SQL pool.

Related

PL/SQL - Oracle based function to get content of all varibles during runtime

I want to improve our exception handling. In case of an exception it would be good to have more than the error message, the error backtrace or error callstack.
We work with dynamic SQL, and for me it would be an improvement if I could log all Input and Output variables, and the content of the dynamic SQLStatement in each exception of the code.
Sure I can seperately address each variable and log them with the data content like in this example:
exception
when others then
cg$errors.log_new (null,null,'Test-Unit',null,'SQLCODE: ' || SQLCODE || chr(10)
|| ' --- SQLERRM: ' || sqlerrm || chr(10)
|| ' --- Stacktrace: ' || dbms_utility.format_error_backtrace || chr(10)
|| ' --- Parameter:'|| chr(10)
|| 'vWorkstation in Varchar2: ' || vWorkstation || chr(10)
|| 'vUser in Varchar2: ' || vUser || chr(10)
|| 'vtest clob : ' || vtest|| chr(10)
|| 'vtest1 clob : ' || vtest1|| chr(10)
|| 'vtest1m clob : ' || vtest1m|| chr(10)
|| 'temp_table clob : ' || temp_table|| chr(10));
raise;
But this is high-maintenance, because I need to explicitly address different variables (vWorkstation, vUser and so on) in each exception.
When I run a PL/SQL unit in Debug Mode I can see a list of all variables (vWorkstation, vUser, and so on) with each step I debug the code. So I think when the SQL developer shows me this data during debugging there must be a way to get a list of the variables during runtime.
So I thought if there is a function or procedure embedded by Oracle, that I can call and which returns all variables used in the code with their names an their content this would be great. I would like to call this function in each of my tougher exceptions.
Do you know such a Oracle function?
Thank you very much.
IDEs such as PL/SQL Developer have a built-in debug capability (which is what I think you mean by "run a PL/SQL unit in Debug Mode"). This is a framework built over an Oracle built-in package DBMS_DEBUG. This is very much a toolbox, rather than a working tool. The most important thing to bear in mind is that the debugger runs in a separate session which probes the running code. This means it's not suitable for your purpose.
Logging long lists of variables and parameters is a bit of a nause, and maintaining them a matter of discipline. But alas there's no better option.

Query related issue

I am unable to upload an image, but on my query the crime id repeats as there are two evidence types, is it possible to make it so it says Crime 1....... contains ..... Physical of which there are 1 and DNA of which there are 3?
The current result is:
Crime 1 occured on 11/12/0013 and contains evidence type: Physical of
which there are 1.
Crime 1 occured on 11/12/0013 and contains evidence type: DNA of which
there are 3.
The code is as follows:
select 'Crime' || ' ' || crime.crime_id || ' ' || 'occured on' || ' ' ||
crime.crime_date|| ' ' || ' and contains evidence type:' || ' ' ||
evidence.evidence_type || ' ' || 'of which there are' || ' '||
evidence.quantity || '.' "Crime Data"
from crime, evidence
where crime.crime_id=evidence.crime_id
order by crime.crime_id;
We can use LISTAGG() to get quite close to this.
select crime_str
|| evidence.evidence_agg_str
|| '.' "Crime Data"
from ( select crime.crime_id
, 'Crime' || ' ' || crime.crime_id || ' occured on ' ||
crime.crime_date|| ' and contains evidence type: ' as crime_str
from crime ) crime,
( select distinct crime_id
, listagg (evidence_str , ',') within group (order by evidence_type)
over (partition by crime_id) evidence_agg_str
from (
select crime_id
, evidence_type
, evidence_type || ' of which there are '||
quantity as evidence_str
from evidence )
) evidence
where crime.crime_id=evidence.crime_id
order by crime.crime_id;
As you can see from the SQL Fiddle, the sentence structure is not perfect:
Crime 1 occured on 12-NOV-13 and contains evidence type: DNA of which there are 3,Physical of which there are 1.
If this really matters to you, you can use REGEXP_COUNT to identify the last comma in the evidence string and REGEXP_REPLACE to substitute and. It depends how complicated you want to get. Find out more.

Building some dynamic query select and display its output immediately in Oracle PL/SQL

in my actual job I need, very often, to read some tables and acting consequently, sometimes updating these data manually.
So I built a PL/SQL block that creates my SELECT statements (yes, with the "FOR UPDATE" clause, just commented).
As an example, this is just one of the queries I build:
phtr_QUERY := 'SELECT *
FROM ' || tabriabi_impianto || '.pdfhtr t
WHERE t.k_abi=''' || tabriabi_abi || ''' ';
if length(myNag) > 0 then
phtr_QUERY := phtr_QUERY || 'and t.ndg like ''%' || myNag || '%'' ';
end if;
if length(myPrat) > 0 then
phtr_QUERY := phtr_QUERY || ' and t.pratica like ''%' || myPrat || '%'' ';
end if;
phtr_QUERY := phtr_QUERY || crlf || ' order by 2 ';
phtr_QUERY := phtr_QUERY || crlf || '--for update';
phtr_QUERY := phtr_QUERY || crlf || ';';
Then I copy these statements from the Output window (obtained through the dbms_output.put_line) and paste it into a new SQL Window and executing it, obtaining the results in multiple tabs.
I was wondering if there is a better way, some commands that I can use just to have the (editable) results directly without the need of cut&paste...
TIA.
F.
A very horrifying/hackish way to do what you want would be to store the resulting query in a temporary table, afterwards you could do something like the process described here:
How can I use an SQL statement stored in a table as part of another statement?
Please Note: This is probably a bad idea.
select a.rowid, a.* from table_name a;
will open in edit mode in many tools.
I was wondering if there is a better way, some commands that I can use just to have the (editable) results directly without the need of cut&paste
You should understand that editing features are features of database tool you are using. When you insert, update or delete some record in the results grid this tool translates your actions into respective SQL statements and executes it on the fly.
As a kind of workaround I suggest you to create a stored procedure which takes some parameters as 'table name', 'where conditions' and then creates updateable database view. After execution of procedure and preparation of the view you can run "select ... for update" and work with returned data as you do it now.

Regenerate all sequences in db (oracle)

Good day. I have a very specific task: regenerate all sequences in database. There is a 400+ tables in it, so I can't do it by hands.
Can somebody help me to do it?
Thanks a lot..
Please note this is highly dangerous. You may very well make mistakes. Run the select first to check what you're about to do and create a table of the select so you can re-create the synonyms manually later if you need to.
Using all_synonyms or dba_synonyms instead of user_synonyms may result in dropping system synonyms do not do this if you want your database to work afterwards
I'd also recommend testing the code on 1 test synonym you create to ensure that it does exactly what you want.
Plus I don't really see the point of doing this at all? If the synonyms are there why do you need to re-generate them? Or is this being done on another server? If so add #server_name after user_synonyms and remove the drop.
begin
for xx in ( select * from user_sequences ) loop
execute immediate 'drop sequence ' || xx.sequence_name;
execute immediate 'create sequence ' || xx.sequence_name
|| ' start with ' || xx.min_value
|| ' ends with ' || xx.max_value
|| case when xx.cycle_flag = 'N' then ' nocycle '
else ' cycle ' end
|| case when xx.cache_size = 0 then ' nocache '
else ' cache ' end || xx.cache_size
|| case when xx.order_flag = 'N' then ' noorder '
else ' order ' end
;
end loop;
end;

Writing in ExcelSheet using UTL_FILE package in Oracle

I have no problem in writing data in excel sheet which is stored in some predefined directory.
Now I have 10 sets of data and for each set I have to create 10 seperate excel sheet.
But what I want is to create Workbook conating sheet1,sheet2,. Sheet10. Which will have all 10 sets of record. If my question is not clear let me know.
I'm using PL/SQL Oracle 9i
My Code which will write to excel for one set of data. What if I have more set of data and I don't want to have multiple excel sheet instead I want one workbook with diff sheet.
CREATE OR REPLACE PROCEDURE SP_ORACLE_EXCEL(I_FILE_NAME IN VARCHAR2) AS
FILENAME UTL_FILE.FILE_TYPE;
FILENAME1 VARCHAR2(1000);
CURSOR C1 IS
SELECT * FROM MY_TABLE;
VARC1 C1%ROWTYPE;
BEGIN
FILENAME1 := 'TEST_' || I_FILE_NAME || '_' || SYSDATE || '.CSV';
FILENAME := UTL_FILE.FOPEN('TEMP_DIR', FILENAME1, 'W');
/* THIS WILL CREATE THE HEADING IN EXCEL SHEET */
UTL_FILE.PUT_LINE(FILENAME,
'HEADER1' || ',' || 'HEADER2' || ',' || 'HEADER3' || ',' ||
'HEADER4' || ',' || 'HEADER5');
OPEN C1;
LOOP
FETCH C1
INTO VARC1;
EXIT WHEN C1%NOTFOUND;
/* THIS WILL PRINT THE RECORDS IN EXCEL SHEET AS PER THE QUERY IN CURSOR */
UTL_FILE.PUT_LINE(FILENAME,
'"' || VARC1.COL1 || '"' || ' ,' || '"' ||
VARC1.COL2 || '"' || ' ,' || '"' ||
VARC1.COL3 || '"' || ' ,' || '"' ||
VARC1.COL4 || '"' || ' ,' || '"' ||
VARC1.COL5|| '"');
END LOOP;
UTL_FILE.FCLOSE(FILENAME);
END SP_ORACLE_EXCEL;
There's no ready-to-use implementation that I'm aware of.
Excel files (.xslx) since '07 are actually zip archives containing separate xml files for each worksheet.
The XML schema they're using is pretty straight forward. You'd have to use Java to create the folders and do the zip compression in order to write such files.
You can do this with the open source PL/SQL ExcelDocumentType. (Although I haven't tried it on 9i.)
I was able to find a solution to this problem recently posted here on SO (Create an Excel File (.xlsx) using PL/SQL).
i used a package called as_xlsx created by Anton Scheffer, Create an Excel-file with PL/SQL and did a few modifications to the package
so it creates multiple sheets inside a single Excel Workbook by putting it through a Loop.
Hope this helps!

Resources