I am using Oracle Forms and facing issue in writing a cursor. My cursor is
CURSOR ss
IS
SELECT pjno, bdate, PROJECT
FROM billcrown
LEFT JOIN bank_details ON billcrown.pjno = bank_details.pjno
WHERE billcrown.bdate > '01-Jul-2017'
GROUP BY billcrown.pjno
HAVING SUM (billcrown.PAMT) <> NVL (SUM (bank_details.AMOUNT), 0);
s ss%ROWTYPE;
It's showing error on pining on left join.
Error is (Encountered the symbol 'JOIN' when expecting one of the following. , ; )
It is working fine at SQL prompt. Please suggest.
Which Forms version is it? If it complains on LEFT JOIN, that's probably 6i or something. Previous (very old) Forms versions' PL/SQL engine didn't quite follow database's PL/SQL engine so not everything, that worked in the database, worked in Forms as well.
Therefore, I'd suggest you to try with the old Oracle outer join operator, (+).
Furthermore, if BDATE column's datatype is DATE, you should use DATE values against it, not strings. '01-Jul-2017' is a string. DATE '2017-07-01' is date (literal).
Finally, saying that your code works fine in SQL*Plus - no, it is not. GROUP BY contains only c.pjno column, so bdate, PROJECT should be included in there as well (or you should rewrite that query).
Something like this:
CURSOR ss
IS
SELECT c.pjno
-- , bdate, PROJECT --> removed because of the GROUP BY clause
FROM billcrown c, bank_details d
WHERE c.pjno = d.pjno (+) --> this
and c.bdate > date '2017-07-01' --> use dates, not strings!
GROUP BY c.pjno
HAVING SUM (c.PAMT) <> NVL (SUM (d.AMOUNT), 0);
Related
I have created a user defined report within Oracle SQL Developer, with a bind variable.
The report consists of a master_report (style is table), child_report_a containing with a style of 'table' and child_report_b which is the same query, but with the style of 'script'.
I am able to select a cell/row of my master report, and child_report_a data changes accordingly (ie, it returns the cells of the same date I selected.
However, when I try to view this in child_report_b (With the 'script' style) it errors with "Missing IN or OUT parameter at index:: 1".
So the setup is:
tablea
id_pk (number)
name_pk (varchar2)
1
Jack
2
John
3
Amy
tableb
id_fk (number)
start_time(timestamp(6))
1
01-JAN-23 12.00.00.123000000
2
01-JAN-23 13.00.00.123000000
3
02-JAN-23 14.05.00.123000000
User Defined Report:
master_report (Style=Table):
SELECT * FROM tablea
child_report_a (Style=Table) & child_report_b (Style=Script):
SELECT * FROM tablea a INNER JOIN tableb b ON b.id_fk = a.id_pk WHERE trunc(start_time) = trunc(to_timestamp(:STARTTIME)
Any help is appreciated.
EDIT: Included better example of setup.
My goal is to be able to select a row from the master_report, and the child_report_b would return all results which match the date (as currently happens in child_report_a) in the script format.
Welcome to StackOverflow!
The reporting feature is one of my favorites in SQL Developer, so I will try to get you started.
We can't technically answer your question without a fair bit of guessing. We know what's happening on one side of the JOIN, you have a timestamp that you're using TRUNC on.
But we can't tell what is on the other side of the equality predicate in your JOIN.
Here's a working example, see if this helps you.
Parent Query
select trunc(systimestamp) A
from dual
Child Query
select object_name, object_type, last_ddl_time
from user_objects where :A > (trunc(systimestamp) - 30)
And the report -
To debug your report, substitute the bind :STARTTIME with a literal value that is equivalent to what your parent query would return. If that works, so should your report.
Disclaimer: I work for Oracle and am a product manager for SQL Developer.
I do voluntary work at an animal shelter. We have an application which uses a SQL Server 2019 database. I have created a view that includes a varbinary(max) column. The value in this column is a picture, stored in hexadecimal-format. I would like to convert this Hex-value to a base64-binary file and add these to the view as an extra column.
I found the perfect solution for my situation in SQL Server : hex to base64. The example provided converts 1 single hex-value into 1 base64-value. I now need to add this solution to my view, but I'm not having any success.
The offered solution:
DECLARE #TestBinHex varchar(max), #TestBinary varbinary(max), #Statement nvarchar(max);
SELECT #TestBinHex = '0x012345';
SELECT #Statement = N'SELECT #binaryResult = ' + #TestBinHex;
EXECUTE sp_executesql #Statement, N'#binaryResult varbinary(max) OUTPUT', #binaryResult=#TestBinary OUTPUT;
SELECT
CAST(N'' AS XML).value(
'xs:base64Binary(xs:hexBinary(sql:column("bin")))'
, 'VARCHAR(MAX)'
) Base64Encoding
FROM
(SELECT #TestBinary AS bin) AS bin_sql_server_temp;
A simplified version of my view:
SELECT
a.cat_id, a.catname, s.cat_id,
s.stay_id, s.shelter_handler, s.shelter_kennel, s.picture
FROM
dbo.animal AS a
OUTER APPLY
(SELECT TOP 1 *
FROM dbo.shelterdata
WHERE a.cat_id = s.cat_id
ORDER BY s.stay_id DESC) AS S
WHERE
(a.cat_id IS NOT NULL) AND (s.leave_date IS NULL)
The view shows an overview of all cats currently present in the shelter (leave_date is NULL). The reason for the TOP 1 is that sometimes shelter animals get returned, and the application then assigns a new stay_id. To prevent duplicate values from the join, I only return the value of the most recent stay_id.
What I am trying to achieve: the second table (dbo.shelterdata) includes the picture, stored in hex value. I'd like to add a column Base64Encoding to the view which includes the converted value.
My attempts
I was successful in replacing the static value '0x012345' by a SELECT statement. But the way the solution is formatted, it only allows for one input value. So I had to restrict it with a WHERE clause. It is obvious to me that I need to make a subquery which inputs the hex value based on the unique cat_id. However, it has been many years since I worked with variable, so I'm struggling with the formatting of the statement.
My request
Does anyone have a suggestion how to build the conversion into the view?
Any assistance would be greatly appreciated.
After searching for a few more hours, I stumbled onto the solution. Maybe it will help someone else in the future. The solution is remarkably simple, as is often the case.
My view, mentioned above, is called dbo.shelter_view
select sv.picture,sv.cat_id,
cast('' as xml).value(
'xs:base64Binary(sql:column("sv.picture"))', 'varchar(max)'
) as Base64Encoding
from dbo.shelter_view as SV
I have an app build in Oracle APEX 18.2. Every number field in app have missing leading zero. For example when the number is 0.5, APEX displays it as .5. The problem occurs also in SQL Workshop. In SQL Developer numbers with leading zeros are formatted well, so I think this is problem with Oracle APEX, not with Oracle DB. Is there any global setting for number formatting in APEX?
As far as I can tell, there's no such a global setting, which means that you'd have to apply some format mask either
directly (in SELECT statement, within the TO_CHAR function call), or
in column's (item's) property
Format mask you might consider is FM999G990D00 as
FM will remove leading spaces and superfluous trailing zeros
instead of using explicit , and . grouping & decimal characters, use G and D instead
I had an issue adding zeros to numbers, but it was fixed using Function with Oracle Developer
select LPAD((max(ID))+1, 6, '0') from Yourtable
and call it as a function.
Probably you could use PL/SQL expressions to fix it
I had the same issue recently. i fixed it using to_char(xxx,'FM999G990D00')
SELECT mont.period_name AS PERIOD
, to_char(ivo.total_overtime,'FM999G990D00')
, to_char(emho.ACTUAL_TRANSFERRED_HOURS,'FM999G990D00')
, to_char(emho.actual_recup_days,'FM999G990D00')
FROM .....
worked like a charm
I'll assume you have a Classic or Interactive Report, in such case:
Go to page designer and select the column
Go to the format mask option
Select the numeric format you wish to have.
You'll probably get something like 999G999G999G999G990D00
if it has a 9D00 at the end, change the 9 to a 0
I am trying to place a date in a where clause. I want to update all rows in which the date column is before or after a certain date. How do I specify that I only want to update these columns. Here is the coding that I have so far (not including specific column names):
update table1
set column1 = value
where (select date from table2) < date;
Am I on the right track?
Also, could someone please explain the difference between SQL and PL/SQL. I am taking a class in PL/SQL at the moment. Whenever I post a question on this forum I say that I have a question in PL/SQL, but the people who answer my question say that a certain function - update/if/case/etc. - is a SQL statement and not a PL/SQL statement. What is the difference?
-Neil
Your update statement
update table1
set column1 = value
where (select date from table2) < date;
is correct and it will work but only if the inner query (select date from table2) returns a single row. If you are trying to compare to specific date you don't need the inner query, for example:
update table1
set column1 = value
where to_date('01/02/2012', 'DD/MM/YY') < date;
You can adjust date format mask to whatever format of data you prefer. to_date will convert from char to date type, and to_char will do the opposite.
SQL is a standardized query language that is supported by all compliant relational databases (with some proprietary extensions sometimes). SQL is not a programming language. PL/SQL is a procedural programming language that is supported on Oracle only (Postgres has similar syntax). PL/SQL is SQL + regular programming language features like conditional statements (if/else), loops (for), functions and procedures and such. PL/SQL is used whenever it's too difficult or impossible to get some data using SQL solely.
As Aleksey mentioned, your query is correct but you need to either [1] set conditions around the sub-SQL to only return ONE record or [2] make sure the data in tabl2 only has ONE record when it runs.
ie
If you have to refer to data from another table in your WHERE clause consider explicit joins (example in SQL Server) ...
update t1
set t1.column1 = value -- <-- some arbitary value here I assume?
from table1 t1
inner join table2 t2
on (t2.key = t1.key) -- you need to specify the primary keys of the tables here
where t2.date < t1.date
That way you are not assuming table2 has only one record. It can have many records as long as they relate to table1 via their keys/indexes and the WHERE clause simply makes sure you only UPDATE based on data from table2 that has a date LESS THAN the date in table1.
In PLSQL I run:
truncate table MyOracleTableName;
commit work;
insert into MyOracleTablename
select a,b,c,trunc(sysdate) as datadate
from AnotherOracleTableName
where there is a ton of nasty criteria
union
select a,b,c,trunc(sysdate) as datadate from AnotherOracleTableName
where there is a ton of different nasty criteria;
commit work;
In PLSQL Developer this inserts one row.
When I run the SQL (without the semi colons and the commit work statements) in SSIS, I get a primary key violation from MyOracleTableName.
I have validated that the truncate from SSIS is committed in Oracle.
When I run the SQL above in PLSQL Developer and replace the union with union all, I see a second row and the insert fails for a PK violation. As it should with a union all allowing the duplicate.
This is currently part of an SSIS 2005 package using MSDAORA where it works just fine. I am now re-writing in SSIS 2008 using Native OLE DB providor for Oracle.
I cannot use MSDAORA in my new environment. Is this a driver issue and is there a work around other than breaking these into multiple statements where the second inserts only what is not already in MyOracleTableName?
Regards.
I figured out the problem after dinner.
The Primary key constraint is a composite key on columns A and B. The Union de-dups on columns a,b,c and the date. In Oracle the trunc(sysdate) returns mm/dd/yyyy. In SSIS the trunc(sysdate) is being parsed out to the second or milisecond. This results in two unique rows (to SQL Server and Microsoft) due to the timestamp, and then attempts to insert duplicate rows where columns a,b, and c are duplicated.
The solution is this:
truncate table MyOracleTableName;
commit work;
insert into MyOracleTablename
select a.*,
trunc(sysdate) as datadate
from(
select a,b,c
from AnotherOracleTableName
where there is a ton of nasty criteria
union
select a,b,c from AnotherOracleTableName
where there is a ton of different nasty criteria) a
commit work;
This allows the union to kill the duplicate and runs the trunc(sysdate) once thereby presenting the single row to my primary key constraint.
Thank you.