Friday, January 24, 2014

Using a Subquery in the FROM clause : inline view

Inline view:

You can use  a subquery in the FROM clause of a SELECT statement , which is very similar to how views are used. A subquery in the FROM clause of a SELECT statement is also called an inline view.  A subquery in the FROM clause  of a SELECT statement defines a data source for the particular SELECT statement, and only that SELECT statement.  The example below displays employee last names, salaries,department number  and average salaries  for all the employees who earn more than the average salary in their department. The subquery in the FROM clause is named b , the outer query references the SALAVG column using this alias.

SELECT  a.last_name ,a.salary,a.department_id,b.salavg
FROM employees a , ( SELECT department_id,
                                 AVG(salary) salavg
                                 FROM employees 
                                 GROUP BY department_id) b
                                 
WHERE a.department_id =b.department_id
AND a.salary >b.salavg;


LAST_NAME                     SALARY DEPARTMENT_ID     SALAVG
------------------------- ---------- ------------- ----------
Hartstein                      13000            20       9500
Higgins                        12000           110      10150
King                           24000            90 19333.3333
.........
.........
38 rows selected.

Thursday, January 23, 2014

Advanced Subqueries: (Pairwise and Non-pairwise Subquery)

Sub Query:
A sub query is a SELECT statement that is embeded in a clause of another SQL statement,called the parent statement.

The subquery (inner query )returns a value that is used by the parent statement. Using a nested sub query is equivalent to performing two sequential queries and using the result of inner query as search value in the outer query (main query).

Purpose of use of sub query:

To  Provide values for conditions in WHERE ,HAVING  and START WITH  clause of SELECT statements.

  • To define the set of rows to be inserted into the target table of an INSERT or CREATE TABLE statement.
  • To define the set of rows to be included in a view or snapshot in a CREATE VIEW or CREATE SNAPSHOT statement.
  • To definf one or more values to be assigned to existing rows in an update statement.
  • To define a table to be operated on by a containing query .( You do this by placing the sub query in the FROM  clause. This can be done in INSERT ,UPDATE and DELETE statements as well.)


A  sub query is evaluated once for the entire parent statement.

Syntax:

SELECT select_list

FROM table

WHERE expr operator (SELECT select_list

                                        FROM table);


The subquery (inner query) executes once before the main query.

The result of the sub query is used by the main query (outer query).                                                                                            

Ex-1: Display the last name of employees who earn more than the the employee with id 200;


SELECT last_name
FROM employees
WHERE salary >
           
               (SELECT salary
       FROM employees
       WHERE employee_id=200);



Ex-1: Display the names of all who earn less than the average salary in the company.

SELECT last_name,job_id,salary
FROM employees
WHERE salary <
              (SELECT AVG(salary)
               FROM employees );



Single-row and multiple-row subqueries: 

Subqueries that can return only one or zero rows to the outer statement are called single-row subqueries. Single-row subqueries are subqueries used with a comparison operator in a WHERE, or HAVING clause.

Subqueries that can return more than one row (but only one column) to the outer statement are called multiple-row subqueries. Multiple-row subqueries are subqueries used with an IN, ANY, or ALL clause.


Example 1: Single-row subquery
You store information particular to products in one table, Products, and information that pertains to sales orders in another table, SalesOrdersItems. The Products table contains the information about the various products. The SalesOrdersItems table contains information about customers' orders. If a company reorders products when there are fewer than 50 of them in stock, then it is possible to answer the question "Which products are nearly out of stock?" with this query:

SELECT ID, Name, Description, Quantity
FROM Products
WHERE Quantity < 50;


However, a more helpful result would take into consideration how frequently a product is ordered, since having few of a product that is frequently purchased is more of a concern than having few product that is rarely ordered.

You can use a subquery to determine the average number of items that a customer orders, and then use that average in the main query to find products that are nearly out of stock. The following query finds the names and descriptions of the products which number less than twice the average number of items of each type that a customer orders.

SELECT Name, Description
FROM Products WHERE Quantity <  2 * (
   SELECT AVG( Quantity )
   FROM SalesOrderItems
   );

In the WHERE clause, subqueries help select the rows from the tables listed in the FROM clause that appear in the query results. In the HAVING clause, they help select the row groups, as specified by the main query's GROUP BY clause, that appear in the query results.

Example 2: Single-row subquery

The following example of a single-row subquery calculates the average price of the products in the Products table. The average is then passed to the WHERE clause of the outer query. The outer query returns the ID, Name, and UnitPrice of all products that are less expensive than the average:

SELECT ID, Name, UnitPrice
FROM Products
WHERE UnitPrice <
  ( SELECT AVG( UnitPrice ) FROM Products )
ORDER BY UnitPrice DESC;


ID Name             UnitPrice
401 Baseball Cap 10.00
300 Tee Shirt 9.00
400 Baseball Cap 9.00
500 Visor            7.00
501 Visor            7.00

Example 3: Simple multiple-row subquery using IN

Suppose you want to identify items that are low in stock, while also identifying orders for those items. You could execute a SELECT statement containing a subquery in the WHERE clause, similar to the following:

SELECT *
FROM SalesOrderItems
WHERE ProductID IN
   (  SELECT ID
       FROM Products
       WHERE Quantity < 20 )
ORDER BY ShipDate DESC;


In this example, the subquery makes a list of all values in the ID column in the Products table, satisfying the WHERE clause search condition. The subquery then returns a set of rows, but only a single column. The IN keyword treats each value as a member of a set and tests whether each row in the main query is a member of the set.

Example 4: Multiple-row subqueries comparing use of IN, ANY, and ALL

Two tables in the SQL Anywhere sample database contain financial results data. The FinancialCodes table is a table holding the different codes for financial data and their meaning. To list the revenue items from the FinancialData table, execute the following query:

SELECT *
FROM FinancialData
WHERE Code IN
    ( SELECT Code
        FROM FinancialCodes
        WHERE type = 'revenue' );

Year Quarter Code Amount
1999 Q1 r1 1023
1999 Q2 r1 2033
1999 Q3 r1 2998
1999 Q4 r1 3014
2000 Q1 r1 3114
... ... ... ...

The ANY and ALL keywords can be used in a similar manner. For example, the following query returns the same results as the previous query, but uses the ANY keyword:

SELECT *
FROM FinancialData
WHERE FinancialData.Code = ANY
   (  SELECT FinancialCodes.Code
       FROM FinancialCodes
       WHERE type = 'revenue' );


While the =ANY condition is identical to the IN condition, ANY can also be used with inequalities such as < or > to give more flexible use of subqueries.

The ALL keyword is similar to the word ANY. For example, the following query lists financial data that is not revenue:

SELECT *
FROM FinancialData
WHERE FinancialData.Code <> ALL
   (  SELECT FinancialCodes.Code
       FROM FinancialCodes
      WHERE type = 'revenue' );

This is equivalent to the following command using NOT IN:

SELECT *
FROM FinancialData
WHERE FinancialData.Code NOT IN
   (  SELECT FinancialCodes.Code
      FROM FinancialCodes
      WHERE type = 'revenue' );


Multiple Column Subqueries:

In the single row sub queries and multiple row sub queries only one column is returned by the inner SELECT statement and this is used to evaluate the expression in the parent SELECT statement.

If you want to compare two or more columns , you must write a compund WHERE clause using logical operators.

Using Multiple-Column subqueries, you can combine duplicate WHERE conditions into a single WHERE clause.

Syntax:

SELECT  column, column, ...

FROM table

WHERE (column,column,....)  IN   --------------------------------- Main Query
                                     
                                     (SELECT column, column ,......
                                      FROM table
                                      WHERE condition );


************************************************************************
Main Query

....WHERE (manager_id, department_id) IN
     
                              Subquery

                      100                   90

                      102                   60

                      124                   40


Each row of the main query is compared to values from a multiple-row and multiple-column subquery.

In the above, the value of manager_id and department_id  from the main query are being compared with the manager_id and department_id  values retrieved by the subquery. Since the number of columns that are being compared are more than one , the example qualifies as a multiple coulmn subquery.


Column Comparisons:

Column comparisons in a multiple column subquery can be :

Pairwise Comparisons
Non-Pairwise Comparisions


Pairwise Comparisons: 

Ex:

/* Display the details of employees who are managed by the same manager and work in the same department as the employees with EMPLOYEE_ID 178 and 174*/

SELECT employee_id, manager_id, department_id
FROM employees
WHERE (manager_id,department_id) IN
                                                     
                                  (SELECT manager_id,department_id
                                   FROM employees
                                   WHERE employee_id in (178,174))

AND employee_id NOT IN (178,174);

The example above is a multiple column subquery because the subquery returns more than one column. It compares the values in the manager_id column and department_id column of each row in the EMPLOYEES  table with the values in the manager_id column and the department_id column for the employees with the employee_id 178 or 174.

First, the subquery to retrieve the manager_id and department_id values for the employees with the employee_id 178 or 174 is executed. These values are compared with the with the manager_id column and department_id column of each row in the EMPLOYEES table. If the value match, the row is displayed.  In the output, the records of the employees with the EMPLOYEE_ID 178 or 174 will not be displayed .

EMPLOYEE_ID MANAGER_ID DEPARTMENT_ID
----------- ---------- -------------
        175        149                              80
        176        149                             80
        177        149                             80
        179        149                            80



Non-Pairwise Comparisons:


/* Display the details of employees who are managed by the same manager and work in the same department as the employees with EMPLOYEE_ID 178 and 174*/

SELECT employee_id, manager_id, department_id
FROM employees
WHERE manager_id IN
                (SELECT manager_id
                 FROM employees
                 WHERE employee_id in (178,174))

AND department_id IN
                 (SELECT department_id
                 FROM employees
                 WHERE employee_id in (178,174))

AND employee_id NOT IN (178,174);


The above example shows a non pairwise comparison of the columns. It displays the employee_id, manager_id and department_id of any employee whose manager_id matches any othe manager IDs of employees whose employee IDs are either 178 or 174 and department_id match any of the department IDs of employees whose employee IDs are either 178 or 174.

First, the subquery to retrieve the manager_id values of the employees with the employee_id 178 or 174 is executed. Similarly, the second subquery to retrieve the department_id  values for the employees with the employee_id 178 or 174 is executed. The retrieved values values of manager_id and department_id columns are compared with the manager_id and department_id column for each row in the EMPLOYEES table . If the manager_id column of the row in the EMPLOYEES  table matches with any of the values of  the manager_id retrieved by the inner subquery  and if the department_id column of the row in the EMPLOYEES table matches with any of the values of the department_id retrieved by the second subquery , the record is displayed.

EMPLOYEE_ID MANAGER_ID DEPARTMENT_ID
----------- ---------- -------------
        179        149                            80
        177        149                             80
        176        149                            80
        175        149                            80


Both the output are same but in different order.

Pairwise :
/*Display the order_id, product_id and quantity of any item in which the product_id and quantity match both the product_id and quantity of an item in order_id 2366*/

Table available on OE Schema in oracle starter database like scott or hr:


SELECT order_id , product_id ,quantity
FROM           order_items
WHERE        (product_id , quantity)    IN
                                        (SELECT product_id,   quantity
                                         FROM       order_items
                                         WHERE     order_id =   2366)
AND  order_id =   2366;





  ORDER_ID PRODUCT_ID   QUANTITY
---------- ---------- ----------
      2366       2359          8
      2366       2373          7
      2366       2382         10
      2366       2394         11
      2366       2395         12
      2366       2400         16
      2366       2406         20
      2366       2409         22
      2366       2415         24
      2366       2419         24

10 rows selected.






Non-Pairwise:

SELECT order_id , product_id ,quantity
FROM      order_items
WHERE   product_id    IN   (SELECT product_id
                                         FROM       order_items
                                         WHERE     order_id =   2366)
                                       
AND  quantity IN    (SELECT quantity
                             FROM       order_items
                            WHERE     order_id =   2366)
                                     
AND  order_id =   2366;



 ORDER_ID PRODUCT_ID   QUANTITY
---------- ---------- ----------
      2366       2359          8
      2366       2373          7
      2366       2382         10
      2366       2394         11
      2366       2395         12
      2366       2400         16
      2366       2406         20
      2366       2409         22
      2366       2419         24
      2366       2415         24

10 rows selected.



/*Write a query to display the name, department number, and salary of any employee whose department number and salary match the department number and salary of any employee who earns a commission*/

SELECT first_name , department_id, salary
FROM employees
WHERE (department_id, salary)  IN  (SELECT department_id, salary
                           FROM employees
                           WHERE commission_pct  IS NOT NULL ) ;


/*Display the name, department name, and salary of any employee whose salary and commission match the salary and commission of any employee located in location id 1700.*/

SELECT e.first_name, d.department_name, e.salary
    FROM employees e, departments d
    WHERE e.department_id = d.department_id
    AND
    d.location_id  = 1700 ;



FIRST_NAME           DEPARTMENT_NAME                    SALARY
-------------------- ------------------------------ ----------
Jennifer             Administration                       4400
Shelley              Accounting                          12000
William              Accounting                           8300
Steven               Executive                           24000
Neena                Executive                           17000
Lex                  Executive                           17000
Nancy                Finance                             12000
Daniel               Finance                              9000
John                 Finance                              8200
Ismael               Finance                              7700
Jose Manuel          Finance                              7800
Luis                 Finance                              6900
Den                  Purchasing                          11000
Alexander            Purchasing                           3100
Shelli               Purchasing                           2900
Sigal                Purchasing                           2800
Guy                  Purchasing                           2600
Karen                Purchasing                           2500

18 rows selected.





/*Create a query to display the name, hiredate, and salary of any employee who have both the same salary and commission as Scott.*/

SELECT ename, hiredate, sal
    FROM emp
    WHERE
    ename <> 'SCOTT'
AND
(sal, NVL(comm,0) )  IN
                    ( SELECT sal, NVL(comm,0)
                      FROM emp
                      WHERE ename = 'SCOTT');


/*Create a query to display the employees that earn a salary that is higher than the salary of all of the clerks. Sort the results on salary from highest to lowest */

SELECT ename, job, sal
    FROM emp
    WHERE sal  >  ALL
                                       ( SELECT sal
                                          FROM emp
                                          WHERE job = 'CLERK')
ORDER BY SAL DESC;


In pair wise comparison we write like this:

"where (MGR_ID, DEPT_ID) IN (SUBQUERY)"

IN non pair wise comparison we write like this:

"where (MGR_ID) IN (SUBQUERY)
and (DEPT_ID) IN (SUBQUERY)"


In first one we are checking two columns simultaneously within a single query, whereas in the second one we are checking each column individually with two queries.

So the output will also differes, check it out by practical implementation.

Wednesday, January 22, 2014

Advantages of Procedure / Sub program

Block Structure :A subprogram is based on standard pl/sql structure that contains a declarative section, an executable section   and an optional exception handling section.
Modularity:   Mopdularisation is the process of breaking up large blocks of code into smaller groups of code called modules.          
               It is easier to maintain and debug code of smaller modules than a single large program.

Extensibility : The modulues can be easily extended for customization by incorporating more functionality,

                if required without affecting the remaining modules of the program.

Reusability :     After the code is modularized, the modules can be reused by the same program or shared by other programs.

Maintainibility : Subprograms provide easy maintainance because the code is located in one place and hence any modifications  required to the subprogram can be performed in this single location.

Improved data security and integrity and code clarity: subprograms provide improved data integrity and security . The data objects are accessed through the subprogram and a user can invoke the subprogram only if appropriate access priviledge is given to user.

Thursday, January 16, 2014

Stored Procedure in 10g

A procedure is a group of PL/SQL statements that you can call by name. Stored procedure is a named PL/SQL block which performs one or more specific task.

Stored procedures offer advantages in the areas of development, integrity, security, performance, and memory allocation.

A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block.

A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage.

We can pass parameters to procedures in three ways.
1) IN-parameters
2) OUT-parameters
3) IN OUT-parameters

A procedure may or may not return any value.


General Syntax to create a procedure is:

CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters]
IS  
   Declaration section
BEGIN  
   Execution section
EXCEPTION  
  Exception section
END;


IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL Blocks. The code between IS and BEGIN forms the Declaration section.

The syntax within the brackets [ ] indicate they are optional. By using CREATE OR REPLACE together the procedure is created if no other procedure with the same name exists or the existing procedure is replaced with the current code.

argument
Specify the name of an argument to the procedure. If the procedure does not accept arguments, you can omit the parentheses following the procedure name.

IN
Specify IN to indicate that you must supply a value for the argument when calling the procedure.


OUT
Specify OUT to indicate that the procedure passes a value for this argument back to its calling environment after execution.


IN OUT
Specify IN OUT to indicate that you must supply a value for the argument when calling the procedure and that the procedure passes a value back to its calling environment after execution.

If you omit IN, OUT, and IN OUT, then the argument defaults to IN.

NOCOPY
Specify NOCOPY to instruct the database to pass this argument as fast as possible. This clause can significantly enhance performance when passing a large value like a record, an index-by table, or a varray to an OUT or IN OUT parameter. IN parameter values are always passed NOCOPY.

When you specify NOCOPY, assignments made to a package variable may show immediately in this parameter, or assignments made to this parameter may show immediately in a package variable, if the package variable is passed as the actual assignment corresponding to this parameter.

Similarly, changes made either to this parameter or to another parameter may be visible immediately through both names if the same variable is passed to both.

If the procedure is exited with an unhandled exception, then any assignment made to this parameter may be visible in the caller's variable.

These effects may or may not occur on any particular call. You should use NOCOPY only when these effects would not matter.

datatype
Specify the datatype of the argument. An argument can have any datatype supported by PL/SQL.

Datatypes cannot specify length, precision, or scale. For example, VARCHAR2(10) is not valid, but VARCHAR2 is valid. Oracle Database derives the length, precision, and scale of an argument from the environment from which the procedure is called.




How to execute a Stored Procedure?
There are two ways to execute a procedure.

1) From the SQL prompt.

 EXECUTE [or EXEC] procedure_name;
2) Within another procedure – simply use the procedure name.

  procedure_name;
NOTE: In the examples given above, we are using backward slash ‘/’ at the end of the program. This indicates the oracle engine that the PL/SQL program has ended and it can begin processing the statements.


Stored Procedure without any Parameter

CREATE TABLE LOG_TABLE

( USER_ID VARCHAR2(10 ) , LOG_DATE DATE  )

Table Created.

CREATE OR REPLACE PROCEDURE SCOTT.log_execution
IS
BEGIN
   INSERT INTO log_table (user_id, log_date)
        VALUES (USER, SYSDATE);
 
  END log_execution;
 /

IN  PARAMETER EXAMPLE
1st example:

CREATE OR REPLACE PROCEDURE HR.remove_emp (employee_id NUMBER)
AS
   tot_emps   NUMBER;
BEGIN
   DELETE FROM employees1
         WHERE employees1.employee_id = remove_emp.employee_id;

   tot_emps := tot_emps - 1;
END;
/

SHOW ERRORS;


To execute :

SET SERVEROUTPUT ON

DECLARE
  EMPLOYEE_ID NUMBER;

BEGIN
  EMPLOYEE_ID := 205;
REMOVE_EMP ( EMPLOYEE_ID );
  --COMMIT;
Dbms_output.put_line (EMPLOYEE_ID || ' has been deleted');
END;



2nd Example:

The below example creates a procedure ‘employee_details’ which gives the details of the employee.

CREATE OR REPLACE PROCEDURE employee_details
 IS
 CURSOR emp_cur IS
SELECT first_name, last_name, salary FROM employees1;
--emp_rec emp_cur%rowtype;
BEGIN
FOR emp_rec in emp_cur
LOOP
dbms_output.put_line(emp_rec.first_name || ' ' ||emp_rec.last_name || ' ' ||emp_rec.salary);
END LOOP;
END;

OUT PARAMETER EXAMPLE:

Definition:

In Oracle PL/SQL, the term OUT refers to one of the parameter passing modes. Unlike the IN parameter mode, it must be specified in the parameter declaration. An OUT parameter is a 'pass by value' method. The parameter must be assigned with a value within the subprogram body. This value is then passed to the host environment, from where the subprogram has been called.

Notes
A default value cannot be assigned to the OUT parameter.
The NOCOPY hint can be used in the parameter declaration to convert the parameter calling method from 'Call by Value' method to the 'Call by reference' method.

Example Syntax:

PARAMETER [IN | OUT | IN OUT] [DATA TYPE] [DEFAULT],

Example Usage:

The procedure P_GET_SAL fetches the salary of an employee from EMPLOYEE table for the given employee id and returns it to the calling environment.

CREATE OR REPLACE PROCEDURE P_GET_SAL (P_EMPID NUMBER, P_SAL OUT NUMBER)
IS
BEGIN
SELECT SALARY INTO P_SAL
FROM EMPLOYEES1
WHERE EMPLOYEE_ID=P_EMPID;
END;
/

SQL> VAR G_SAL NUMBER;
SQL> EXEC P_GET_SAL(100,:G_SAL);

PL/SQL procedure successfully completed.

SQL> PRINT G_SAL;

     G_SAL
----------
     26400

OR:

DECLARE
  P_EMPID NUMBER;
  P_SAL NUMBER;

BEGIN
  P_EMPID := 100;
  P_GET_SAL (P_EMPID,P_SAL );
 DBMS_OUTPUT.PUT_LINE(P_EMPID ||' draws salary' ||  P_SAL);
 END;

100 draws salary 26400
PL/SQL procedure successfully completed.


IN OUT PARAMETER EXAMPLE

CREATE OR REPLACE procedure format_phone
(p_phone_no IN OUT varchar2)
is
begin
p_phone_no :='(' || SUBSTR(p_phone_no,1,3) ||
                       '(' || SUBSTR(p_phone_no,4,3) ||      
                       '-' || SUBSTR(p_phone_no,7 );
END format_phone;
/
Procedure created.

SQL> var g_phone_no varchar2(15)
SQL> begin
  2  :g_phone_no :='9880002441';
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> print g_phone_no

G_PHONE_NO
--------------------------------
9880002441

SQL> exec format_phone(:g_phone_no);

PL/SQL procedure successfully completed.

SQL> print g_phone_no

G_PHONE_NO
--------------------------------
(988)000-2441


OR 

SET SERVEROUTPUT ON

DECLARE
P_PHONE_NO VARCHAR2(32767);

BEGIN
P_PHONE_NO := '9880002441';

FORMAT_PHONE ( P_PHONE_NO );

DBMS_OUTPUT.PUT_LINE(P_PHONE_NO);

END;

Output:
(988)000-2441
PL/SQL procedure successfully completed.
************************************************************


A subprogram is a program unit/module that performs a particular task.
These subprograms are combined to form larger programs. This is basically called the 'Modular design'.

A subprogram can be invoked by another subprogram or program which is called the calling program.

A subprogram can be created:

At schema level

Inside a package

Inside a PL/SQL block

A schema level subprogram is a standalone subprogram. It is created with the CREATE PROCEDURE or CREATE FUNCTION statement.

It is stored in the database and can be deleted with the DROP PROCEDURE or DROP FUNCTION statement.

A subprogram created inside a package is a packaged subprogram.

It is stored in the database and can be deleted only when the package is deleted with the DROP PACKAGE statement.


PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters. PL/SQL provides two kinds of subprograms:

Functions: these subprograms return a single value, mainly used to compute and return a value.

Procedures: these subprograms do not return a value directly, mainly used to perform an action.

Cheers:
Rajani

Friday, November 22, 2013

How to generate a spool CSV file with current date in windows envrionment discarding the select statement.

Hi Friends,

Sometimes we face problem in generating the CSV spool file which contains only records based on the filter condition on the select statement.

Basically we face this problem in Windows environment because whatever we mention after the set trimspool on , the same is copied to the CSV file also including the select statement and no of rows selected etc.

So to avoid such issue please proceed as mentioned below.


PROMPT
PROMPT *** my_script.sql *** START ***
PROMPT
set termout off
set echo off
set verify off
set heading off
set lines 420
set trimspool on
set pagesize 0
Set feedback off
col dcol new_value mydate noprint
select to_char(sysdate,'YYYY-MM-DD') dcol from dual;
spool C:\&mydate._MY_SPOOL_FILENAME.csv
select 'INIT,'|| e.employee_id ||','|| e.department_id||','||d.manager_id||','|| d.department_id
from employees e, departments d 
where e.department_id = d.department_id 
order by 1
/
spool off
set echo on
set termout on
PROMPT
PROMPT *** my_script.sql *** END ***



Cheers
Rajani

Thursday, October 10, 2013

Re-generating .fmx in UNIX Platform

Move the form from desktop to server ($AU_TOP/forms) in binary mode and also generate the .fmx file.















Connect to Putty and run the below code.
F60gen module='/apps/aptest/visappl/au/11.5.0/forms/US/NA_QUERY_ONLY_FORM.fmb' module_type=form userid=apps/apps compile_all=special output_file='/apps/aptest/visappl/xxcus/11.5.0/forms/US/NA_QUERY_ONLY_FORM.fmx'





Wednesday, October 9, 2013

Moving Forms Applications From One Platform To Another

For this example the assumption is that the "development" machine is a MS Windows platform and the "deployment" machine is a Unix platform.
IMPORTANT: FMX, MMX, and PLX files are NOT portable between platforms. They are also not portable between major versions. For example, you cannot run a 9.0.4 FMX in a 10.1.2 environment. Source code (FMB, MMB, PLL) must be recompiled when moving from one platform and/or major version to another. This includes moving from one Unix platform to another (i.e. Sun Solaris to Linux) or one Windows platform version to another.
( FRM-40031: File xxxxx.fmx is not a Forms Runtime File.)
Failure to correctly compile source files on the machine where they are being deployed will cause instability or prevent the application from running or may cause trigger code to not fire.
Requirements for "deployment" machine:
Log-in access as the "oracle" user
X-session (GUI)
It is NOT supported to perform the following via a remote session. 


1. Verify that the deployment environment has been correctly installed and configured as documented in the product Installation Guide.

2. Create a staging directory where the application source files (FMB, MMB, PLL, OLB) can be stored permanently or temporarily. This directory will also be where the compiled executables (FMX, MMX, PLX) will be created. For example:
mkdir /u02/oracle/ias904_mid/forms90/myApplication


3. Copy/transfer all of the Forms components which make up the application to the directory created in step 2. These will include FMB, MMB, PLL and OLB files.
IMPORTANT: Unix is a case sensitive operating system. Be sure that any references to files within your application have been corrected to match files on the new file system. This will usually impact references to image (icon) files, other forms, menus, and libraries.


4. Copy/transfer all other application files as needed. These files may include custom resource (.res), image or custom JARs files. These files will need to be placed in specific locations based on how you developed the application. Refer to the product documentation for more details.
IMPORTANT: If transferring using FTP, all files must be transferred in binary mode.
The following steps must be performed from the local "deployment" machine. Performing these steps remotely is not supported. 


5. Open a shell session.

6. Set the ORACLE_HOME variable and point it to the Application Server installation. Be sure to use the appropriate syntax for the particular shell you are using. For example in csh the command would look something like this:
export ORACLE_HOME=/u02/oracle/ias904_mid


7. Set the FORMS90_PATH variable to the directory which was created in step 2. For example:
export FORMS90_PATH=/u02/oracle/ias904_mid/forms90/myApplication
Additional variables may be necessary or desired based on your needs and system configuration. Here are a few examples:
TNS_ADMIN
NLS_LANG
CLASSPATH
TERM
DISPLAY
In most cases, setting these will not be necessary if you use the provided script (.sh file) noted in the next step.
When compiling a Forms application it is important to understand the application. Most important is to understand the dependencies which may exist between components. In other words, for example you will not be able to compile an FMB if it has a dependant PLL which has not yet been compiled. In most cases the order in which compiling should occur is as follows:
1. PLL
2. MMB
3. FMB
There are exceptions, but this will work in most cases.


8. Using the compiler, generate "X" files for all of the application's binaries (PLL, MMB, FMB). The command will be something like the following:
f90genm.sh module=myForm module_type=form compile_all=yes userid=scott/tiger@orcl
Other possible module_type values:
library
menu
form
Addition compiler options can be found in the Forms Builder online help. For Forms 9.0.4, the Builder help is also available online at:
http://www.oracle.com/forms/10g/help/
Click on Reference then Form Compiler Options
NOTE: In Forms 10.1.2.0.2 and newer, the script names have changed. f90genm.sh is now frmcmp.sh and f90gen.sh is now frmcmp_batch.sh. Also, FORMS90_PATH is now FORMS_PATH. Please refer to the product documentation for more details.


9. Once all of the executables have been generated, the path to these files will need to be included in the Forms deployment environment. In most cases this will be the FORMS90_PATH value in the Forms env file, "default.env"