Skip to Main Content
  • Questions
  • How to use ' From clasue query ' for a data block as a datasource.

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, dileep.

Asked: January 23, 2001 - 7:17 am UTC

Last updated: March 27, 2006 - 3:22 pm UTC

Version: forms (6.0)

Viewed 10K+ times! This question is

You Asked

I want to use 'From Clause Query' as a datasource for a data block.
Please illustrate with code.


and Tom said...

Here is a support note on this subject:

PURPOSE
-------

The purpose of this note is to demonstrate the use of dynamic From Clause Query with Forms variables.


SCOPE & APPLICATION
-------------------

This article is intended for all Developers using Oracle Forms 6.0.

Using From Clause Query dynamically with Forms variables.
---------------------------------------------------------

In Forms Builder build a block manually. In this example we use a block that contains 4 items. Name the items EMPNO, ENAME, JOB and DEPTNO. We can call the items whatever we want, but remember to use alias in the select statement with the same name as the items in the block. We will come back to this later in the article.

Let us call BLOCK1 the block we have just created. In the Property Palette of BLOCK1 the following properties must be set:

Database Data Block = YES
Query Allowed = YES
Query Data Source Type = FROM clause query
Query Data Source Name = SELECT '1','2','3','4' FROM DUAL

The select statement in Query Data Source Name is necessary, otherwise we get a FRM-41380 error message. It looks like Forms is expecting to having something different from null in the Query Data Source Name-property. The number of items
in BLOCK1 should match the number of columns in the select statement, i.e. if we had a block with 10 items then the select statement should look like this:

SELECT '1',.....'10' FROM DUAL.

Next we check that the items in BLOCK1 match the properties of the columns in the select statement we are interested in executing. This means that the items in the block should have the same type and length of the data from the database.

Now we build another block. This time a control block to provide a dynamical variable. We call this block BLOCK3. In BLOCK3 we create an item: x_deptno.

We create a When-New-Block-Instance trigger in BLOCK1. The code is as follows:

DECLARE
q varchar2(1000);
BEGIN
q := '(SELECT empno, ename, job, deptno FROM emp WHERE deptno = '
||:BLOCK3.x_deptno||')';
CLEAR_BLOCK;
SET_BLOCK_PROPERTY('BLOCK1', QUERY_DATA_SOURCE_NAME, q);
EXECUTE_QUERY;
END;

Note the select statement is in parenthesis.
The value of BLOCK3.x_deptno is assigned dynamically and compared to deptno in the select statement. Note that the select statement in the code above could have been:
'(SELECT empno AS e_num, ename AS e_name, job AS e_pos, deptno AS e_dno FROM emp
...........
Then the items in BLOCK1 should be named like this:
E_NUM, E_NAME, E_POS and E_DNO.



Rating

  (6 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

I have a little different case, you may have the ideal to fix it.

A reader, March 23, 2002 - 10:47 am UTC

Form6.0.8, oracle8.1.6

I am relatively new to the forms.
The automatic query refresh is not working for my application, tested on client/server and the
Web.

In order to refreshing, I have to disconnect the form application from the database and
reconnect it to.
I used the SQLPLUS to verify the user does not need reconnect inorder to view the data
updated.

Settings in LOVs

automatic refresh =yes
filter before display = no
automatic display=no
automatic select =no
automatic skip =no
automatic position =no

settings in the record groups

record group fetch size =20
record group type =query
record group query =SELECT distinct(GL_CODE_COMBINATIONS.SEGMENT18) FROM
GL.GL_CODE_COMBINATIONS order by 1

I have created a small form for just test the auto-refresh, and it works by the same settings. The problem for me is I could not find out what else other than the settings of LOVs and the Record Groups .

from clause query

omer, February 21, 2004 - 3:09 am UTC

Hi!
I am using from claue query as a datablock source property.
my query is
select empno ,to_char(name) name from emp
When i try to update any record error come
frm-40654 record has been updated by another user. requery to see change
there is no user except me.
i think the problem is of using To_CHAR function but
I have to use to_char function
is there any other option like procedure or else
plz help
regards

Tom Kyte
February 21, 2004 - 11:05 am UTC

why would you do that?

from clause

omer, February 24, 2004 - 5:05 am UTC

Hi!
I am using to_char function because
name is nvarchar2 column
desc emp
----------
empno number
name nvarchar2(4000)
there is no nvarchar2 datatype property in form. (forms 6)
i am using char datatype for name column in form.
if i do not use to_char function then name column remain empty and only empno is retreived.
regards

Tom Kyte
February 24, 2004 - 6:48 am UTC

you'll get this because the data in your form does not match the data in the database (you turned the nvarchar2 into a varchar2, they are different)

You'll have to program your own "on-lock" trigger -- but I would suggest you first goto otn.oracle.com -> discussion forums and get into the developer discussion forum and ask them for advice -- I haven't touched forms since 1995

Couple of things...

Connor, February 24, 2004 - 10:36 am UTC

Whilst playing with this on an app (6i/9i) at work, we found that

a) its wise to wrap the SQL in parentheses - otherwise updateable blocks have errors in some places

b) enforce primary key had some dramas - can't remember the specifics

Other than that, ran very nicely indeed.

Excellent as usual

A reader, March 02, 2005 - 4:02 pm UTC

Excellent as usual. I was trying to solve one big problem in my forms.Using this feature i solved it with in an hour.

Thanks again for your answers.

Subqueries in From clause queries

Andy Noble, March 27, 2006 - 1:07 pm UTC

I'm having a problem using a FROM CLAUSE query Query Data Source Type on an Oracle Applications template form.

I've successfully created a form with a single data block having a single item INVENTORY_ITEM_ID, with the Query Data Source Name as:

(SELECT INVENTORY_ITEM_ID FROM mtl_org_assign_v x WHERE 1=1 AND master_organization_id = 86 AND NVL(eam_enabled_flag, 'N') = NVL('N', 'N') AND inventory_item_id = 2)

however, if I add a subquery to the statement:

(SELECT INVENTORY_ITEM_ID FROM mtl_org_assign_v x WHERE 1=1 AND master_organization_id = 86 AND NVL(eam_enabled_flag, 'N') = NVL('N', 'N') AND inventory_item_id = 2 AND EXISTS ( SELECT 'x' FROM org_organization_definitions ood WHERE ood.organization_id = x.organization_id AND ood.inventory_enabled_flag = 'Y' ))

the whole thing blows up!! (i.e. throws a java exception and closes the application) I've tried and run the query directly from SQL*Plus no problem. I've also built a view with it and queried that directly - no problem.

The reason I am using this query is that I am trying to build up to a much more complex query with a UNION and bind variables which doesn't work - I'm trying to isolate the root cause, so I'm doing it one step at a time.

The query I'm finally trying to have in there is:
SELECT DECODE (organization_id, :master_org_id, 1, 2), assigned_flag,organization_id, inventory_item_id, organization_code,
organization_name, primary_unit_of_measure_tl,
cost_of_sales_account, sales_account, expense_account,
encumbrance_account
FROM mtl_org_assign_v x
WHERE 1=1
AND master_organization_id = :master_org_id
AND NVL (eam_enabled_flag, 'N') = NVL (:EAM_ITEM_FLAG, 'N')
AND inventory_item_id = :inventory_item_id
AND EXISTS (
SELECT 'x'
FROM org_organization_definitions ood
WHERE ood.organization_id = x.organization_id
AND ood.inventory_enabled_flag = 'Y'
)
UNION
SELECT DECODE (organization_id, :master_org_id, 1, 2), assigned_flag,organization_id, inventory_item_id, organization_code,
organization_name, primary_unit_of_measure_tl,
cost_of_sales_account, sales_account, expense_account,
encumbrance_account
FROM mtl_org_assign_v x
WHERE 1=1
AND master_organization_id = :master_org_id
AND NVL (eam_enabled_flag, 'N') = NVL (:EAM_ITEM_FLAG, 'N')
AND EXISTS (
SELECT 'x'
FROM org_organization_definitions ood
WHERE ood.organization_id = x.organization_id
AND ood.inventory_enabled_flag = 'Y'
)
AND (inventory_item_id IS NULL
AND organization_id IN (
SELECT
organization_id
FROM mtl_org_assign_v
WHERE inventory_item_id IS NULL
MINUS
SELECT organization_id
FROM mtl_org_assign_v
WHERE inventory_item_id = :inventory_item_id
)
)
ORDER BY
1 ASC
,2 DESC
,3

Any suggestions?

Tom Kyte
March 27, 2006 - 3:22 pm UTC

sorry - I know nothing about the application you are talking about (I don't do "apps")

no oracle error message?

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library