Showing posts sorted by relevance for query procedure. Sort by date Show all posts
Showing posts sorted by relevance for query procedure. Sort by date Show all posts

Saturday, July 23, 2022

PL/SQL Tutorial: Anonymous Blocks, Procedures, Functions, and Packages with Simple Examples

PL/SQL Tutorial: Anonymous Blocks, Procedures, Functions, and Packages

๐Ÿ“ PL/SQL Tutorial: Anonymous Blocks, Procedures, Functions, and Packages with Simple Examples

PL/SQL is a powerful block-structured language used in Oracle databases for procedural programming. It allows developers to write modular, reusable code.


๐Ÿ“š 1. PL/SQL Block Structure Overview

PL/SQL code can be categorized as:

  • Anonymous Blocks: Unnamed, not stored in the database, used for quick scripts
  • Named Blocks: Stored in the database, reusable
    • Functions
    • Procedures
    • Packages

Example to get current year:

SELECT TO_CHAR(SYSDATE, 'YYYY') FROM DUAL;
Anonymous blocks are quick, one-time use code blocks. Named blocks like functions, procedures, and packages are stored in the database and can be reused multiple times, making your code cleaner and more maintainable.

๐Ÿ”น 2. Functions

What is a Function?

A function returns a value and can be used in SQL statements or PL/SQL code.

  • Oracle Provided Functions: TO_CHAR(), TO_DATE(), SUM(), AVG(), etc.
  • User Defined Functions: Custom functions for specific logic.
Functions take input parameters, perform calculations or operations, and return a single value. You can use built-in Oracle functions or create your own to encapsulate logic you want to reuse.

Syntax to Create a Function

CREATE [OR REPLACE] FUNCTION function_name [(parameters)] RETURN return_datatype IS | AS
  [declaration_section]
BEGIN
  executable_section
  [EXCEPTION exception_section]
END [function_name];

Example: Calculate Area of Circle

CREATE OR REPLACE FUNCTION AREA_CIRC_FUNC (V_RADIUS INTEGER) RETURN INTEGER IS
  V_AREA NUMBER;
BEGIN
  V_AREA := 3.14 * (V_RADIUS * V_RADIUS);
  RETURN V_AREA;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('SOME ERROR: ' || SQLERRM);
    RETURN NULL;
END;

How to Run the Function?

1. From PL/SQL Block:

DECLARE
  V_AREA NUMBER;
  V_RADIUS INTEGER := &a;
BEGIN
  V_AREA := AREA_CIRC_FUNC(V_RADIUS);
  DBMS_OUTPUT.PUT_LINE('AREA OF CIRCLE IS: ' || V_AREA);
END;

2. From SQL Query:

SELECT AREA_CIRC_FUNC(100) FROM DUAL;
You can call functions directly within PL/SQL blocks or SQL queries. Functions simplify code by encapsulating reusable logic like calculating the area of a circle.

๐Ÿ”น 3. Procedures

What is a Procedure?

A procedure is a named PL/SQL block that performs an action but does not return a value directly.

Procedures are used when you want to perform tasks such as inserting data, printing messages, or any operation that does not need to return a value.

Basic Procedure Example

CREATE OR REPLACE PROCEDURE PRINT_MSG_PRC IS
BEGIN
  DBMS_OUTPUT.PUT_LINE('HELLO WORLD');
END PRINT_MSG_PRC;

Execute Procedure

EXEC PRINT_MSG_PRC;

or

BEGIN
  PRINT_MSG_PRC;
END;

Procedure with Parameters

IN Parameter (default mode)

CREATE OR REPLACE PROCEDURE ADD_VAL_PRC(X IN NUMBER) IS
  V NUMBER(5);
BEGIN
  V := X + 5000;
  DBMS_OUTPUT.PUT_LINE(V);
END;

Run:

EXEC ADD_VAL_PRC(1000);

Procedure with Multiple IN Parameters

CREATE OR REPLACE PROCEDURE PRINT_EMP_PRC(
  v_empno NUMBER,
  v_sal   NUMBER
) IS
  vname VARCHAR2(10);
BEGIN
  SELECT ename INTO vname FROM empl WHERE empno = v_empno AND sal = v_sal;
  DBMS_OUTPUT.PUT_LINE('Employee name: ' || vname);
END;

Run:

EXEC PRINT_EMP_PRC(7839, 5000);
Procedures can accept parameters to make them flexible. IN parameters allow passing values to the procedure. You can create procedures to perform various tasks using the input parameters.

OUT Parameter Example

CREATE OR REPLACE PROCEDURE PRINT_ENAME_PRC(
  vin_empno IN NUMBER,
  vout_ename OUT VARCHAR2
) IS
BEGIN
  SELECT ename INTO vout_ename FROM empl WHERE empno = vin_empno;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('SOME ERROR: ' || SQLERRM);
END;

Call with anonymous block:

DECLARE
  V_EMPNO NUMBER := &A;
  V_ENAME VARCHAR2(100);
BEGIN
  PRINT_ENAME_PRC(V_EMPNO, V_ENAME);
  DBMS_OUTPUT.PUT_LINE('Value from procedure: ' || V_ENAME);
END;
OUT parameters allow a procedure to send data back to the caller. This is useful when you want to retrieve values like employee names or calculations.

IN OUT Parameter Example

CREATE OR REPLACE PROCEDURE DER_SAL_PRC(x IN OUT NUMBER) IS
BEGIN
  SELECT sal INTO x FROM empl WHERE empno = x;
END DER_SAL_PRC;

Call:

DECLARE
  V_EMPNO NUMBER := &A;
BEGIN
  DER_SAL_PRC(V_EMPNO);
  DBMS_OUTPUT.PUT_LINE('Salary of employee is: ' || V_EMPNO);
END;
IN OUT parameters act as both input and output. You pass a value in, and the procedure modifies or returns a related value.

๐Ÿ”น 4. Packages

What is a Package?

A package groups related procedures, functions, variables, and other elements together.

Packages help organize your code logically and improve maintainability by grouping related subprograms together. They have two parts: specification (interface) and body (implementation).

Package Specification Syntax

CREATE [OR REPLACE] PACKAGE package_name IS
  -- Public declarations
  [PROCEDURE procedure_name (parameters);]
  [FUNCTION function_name (parameters) RETURN datatype;]
END package_name;
/

Package Body Syntax

CREATE [OR REPLACE] PACKAGE BODY package_name IS
  -- Implementation of procedures and functions
END package_name;
/

Example: Math Package

Package Spec

CREATE OR REPLACE PACKAGE MATH_PKG IS
  FUNCTION AREA_CIRC_FUN (VIN NUMBER) RETURN NUMBER;
  PROCEDURE AREA_SQR_PRC (VIN NUMBER);
END MATH_PKG;
/

Package Body

CREATE OR REPLACE PACKAGE BODY MATH_PKG IS

  FUNCTION AREA_CIRC_FUN (VIN NUMBER) RETURN NUMBER IS
    V_AREA NUMBER;
  BEGIN
    V_AREA := 3.14 * (VIN * VIN);
    RETURN V_AREA;
  EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('ERROR in AREA_CIRC_FUN: ' || SQLERRM);
      RETURN 0;
  END;

  PROCEDURE AREA_SQR_PRC (VIN NUMBER) IS
    V_AREA NUMBER;
  BEGIN
    V_AREA := VIN * VIN;
    DBMS_OUTPUT.PUT_LINE('AREA OF SQUARE IS: ' || V_AREA);
  EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('ERROR in AREA_SQR_PRC: ' || SQLERRM);
  END;

END MATH_PKG;
/

Using the Package

DECLARE
  V_IN_RAD NUMBER := &A;
  V_IN_SIDE NUMBER := &B;
  V_AREA_CIRCLE NUMBER;
BEGIN
  -- Call package function
  V_AREA_CIRCLE := MATH_PKG.AREA_CIRC_FUN(V_IN_RAD);
  DBMS_OUTPUT.PUT_LINE('AREA OF CIRCLE IS: ' || V_AREA_CIRCLE);

  -- Call package procedure
  MATH_PKG.AREA_SQR_PRC(V_IN_SIDE);
END;
/

Summary

Concept Description Example Use Case
Anonymous Block Temporary unnamed PL/SQL block Quick scripts or tests
Function Named block returning a value Calculating areas, business logic
Procedure Named block performing actions, no return Print messages, complex logic
Package Group of related functions and procedures Organizing code by functionality

Happy Coding! ๐Ÿš€

Sunday, September 1, 2019

Sample format to include all in parameter list

๐Ÿ“Œ Sample Format to Include All IN Parameters in PL/SQL Procedures

When designing PL/SQL procedures in Oracle, you often encounter scenarios where multiple inputs are needed. Organizing all IN parameters in a consistent and scalable format not only enhances readability but also improves maintainability. This guide provides a clean and reusable sample format for writing PL/SQL procedures with multiple IN parameters.

PLSQL IN Parameters Format

Example format for handling IN parameters in PL/SQL

๐Ÿงพ Sample Procedure Format with All IN Parameters

CREATE OR REPLACE PROCEDURE insert_employee_info (
    p_emp_id       IN NUMBER,
    p_first_name   IN VARCHAR2,
    p_last_name    IN VARCHAR2,
    p_email        IN VARCHAR2,
    p_salary       IN NUMBER,
    p_hire_date    IN DATE,
    p_department   IN VARCHAR2
)
IS
BEGIN
    INSERT INTO employees (
        emp_id, first_name, last_name, email,
        salary, hire_date, department
    )
    VALUES (
        p_emp_id, p_first_name, p_last_name, p_email,
        p_salary, p_hire_date, p_department
    );

    DBMS_OUTPUT.PUT_LINE('Employee record inserted successfully.');
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END insert_employee_info;

๐Ÿง  Why Use This Format?

  • Clarity: Clean separation between parameter names and logic.
  • Scalability: Easy to extend when more parameters are added.
  • Standardization: Follows Oracle best practices for readable procedures.
  • Debug-Friendly: Includes DBMS_OUTPUT for status and exception handling.

๐Ÿ’ป How to Execute the Procedure

EXEC insert_employee_info (
    101, 'John', 'Doe', 'john.doe@example.com',
    55000, SYSDATE, 'HR'
);
๐Ÿ’ก Tip: Use meaningful prefixes like p_ for parameters to differentiate them from column names or variables.

❗ Common Mistakes to Avoid

  • Omitting `IN` keyword (required in explicit mode).
  • Using undeclared variables or mismatched data types.
  • Missing exception block for error handling.
  • Using same name for parameter and table column (leads to confusion).

๐Ÿ“š Related Resources

๐Ÿ“Œ Conclusion

Following a structured and readable format for your IN parameters can make your PL/SQL code easier to maintain and debug. As your applications grow in complexity, these best practices become even more important.

Happy coding with PL/SQL!

Saturday, November 12, 2022

Top 25 Oracle Apps Interview Question and Answers.

๐Ÿ” Top 25 Oracle Apps Technical Interview Questions with Answers

Looking to crack your next Oracle Apps technical interview? This guide covers 25 of the most frequently asked questions along with detailed explanations to help you understand core Oracle concepts, tools, and best practices. Whether you're preparing for a developer, consultant, or support role, these Q&As will give you the edge you need.

1. How to use WHO columns in RDF reports?

WHO columns are standard audit columns: CREATED_BY, CREATION_DATE, LAST_UPDATED_BY, LAST_UPDATE_DATE, and LAST_UPDATE_LOGIN. In RDF reports, you should add these columns to the SELECT statement and populate them using user exits like FND_SRWINIT and FND_GLOBAL.USER_ID.

2. What are user exits in Oracle Apps?

User exits are used in Oracle Reports to access AOL (Application Object Library) features. Common ones:

  • FND_SRWINIT – Initializes AOL context

  • FND_USEREXIT – Calls other exits

  • FND_FORMAT_CURRENCY, FND_STANDARD_DATE – Format utilities

3. What is PRAGMA AUTONOMOUS_TRANSACTION?

This allows a PL/SQL block to perform a transaction (commit/rollback) independently of the main transaction. Useful for logging or auditing.

PRAGMA AUTONOMOUS_TRANSACTION;

4. What is BULK COLLECT?

BULK COLLECT allows fetching multiple rows into PL/SQL collections in a single context switch, improving performance.

SELECT empno, ename BULK COLLECT INTO l_empnos, l_names FROM emp;

5. Error handling in BULK COLLECT

Use SAVE EXCEPTIONS in combination with FORALL to continue processing even if some DML operations fail. Capture errors using SQL%BULK_EXCEPTIONS.

6. Dynamic logo in XML Publisher

Use a FORM-FIELD placeholder in the RTF template and pass the image path as a concurrent program parameter. Use url: prefix for dynamic URL.

<?xdofx: url:{LOGO_URL}?>

7. Multilanguage XML Reports

Use XLIFF or translation templates in BI Publisher. Upload different RTFs for each language or use conditional statements in RTF for dynamic labels.

8. Difference between Alerts and Triggers

  • Alerts are Oracle Workflow-based notifications that run periodically.

  • Triggers are DB-level code that fires automatically on DML actions.

9. How to use WHO columns in Oracle Forms

Assign WHO columns using fnd_standard.set_who in WHEN-NEW-FORM-INSTANCE trigger. This populates audit columns automatically.

10. Difference between Procedure and Function

  • Procedure: Does not return a value directly; used for DML.

  • Function: Must return a value; often used in SQL queries.

11. What is Bursting in XML Publisher?

Bursting lets you split a report output and deliver it to multiple recipients or destinations (email, printer, etc.) based on control XML.

12. Triggers in XML Publisher

Not DB triggers. Refers to logic in Data Template like beforeReport, afterReport, beforeData, afterData, etc.

13. Sequence of Trigger Firing in RDF Reports

  1. Before Parameter Form

  2. After Parameter Form

  3. Before Report

  4. Between Pages

  5. After Report

14. Tables in O2C and P2P Cycle

The Order to Cash (O2C) cycle refers to the end-to-end process of receiving and fulfilling customer orders, from order entry to cash receipt. The Procure to Pay (P2P) cycle involves acquiring goods and services from suppliers and making payments.

  • O2C: OE_ORDER_HEADERS_ALL, OE_ORDER_LINES_ALL, AR_INVOICE_HEADERS_ALL

  • P2P: PO_HEADERS_ALL, PO_LINES_ALL, AP_INVOICES_ALL, AP_SUPPLIERS

  • O2C: OE_ORDER_HEADERS_ALL, OE_ORDER_LINES_ALL, AR_INVOICE_HEADERS_ALL

  • P2P: PO_HEADERS_ALL, PO_LINES_ALL, AP_INVOICES_ALL, AP_SUPPLIERS

15. How to find Requisition related to PO

Join PO_HEADERS_ALL → PO_REQ_DISTRIBUTIONS_ALL → REQUISITION_HEADERS_ALL using REQUISITION_LINE_ID.

16. Form Personalization vs Customization

  • Personalization: Using Oracle Forms personalization for UI changes without coding.

  • Customization: Changing the form itself using Forms Developer.

17. Conversion vs Interface

  • Conversion: One-time data load.

  • Interface: Repeated data movement via interface tables.

18. UNION vs UNION ALL

  • UNION: Removes duplicates.

  • UNION ALL: Keeps all rows including duplicates.

19. 2-Way, 3-Way, 4-Way Matching

  • 2-Way: PO vs Invoice

  • 3-Way: PO vs Invoice vs Receipt

  • 4-Way: Adds inspection

20. Types of Purchase Orders (POs)

  • Standard PO

  • Planned PO

  • Blanket PO

  • Contract PO

21. What is Drop Shipment?

A sales order where the item is shipped directly from supplier to customer without going through inventory.

22. What is IR/ISO?

  • IR: Internal Requisition

  • ISO: Internal Sales Order Used for transferring items between inventory organizations.

23. TCA Architecture

Trading Community Architecture standardizes customer, supplier, and partner data. Key tables:

  • HZ_PARTIES

  • HZ_CUST_ACCOUNTS

  • HZ_LOCATIONS

24. Mandatory Parameters for Standard API Calls

  • p_api_version

  • p_init_msg_list

  • p_commit

  • x_return_status

  • x_msg_count

  • x_msg_data

Use FND_MSG_PUB to retrieve messages.

Example Usage:

BEGIN
  hr_employee_api.create_employee (
    p_api_version        => 1.0,
    p_init_msg_list      => FND_API.G_TRUE,
    p_commit             => FND_API.G_FALSE,
    p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
    x_return_status      => l_return_status,
    x_msg_count          => l_msg_count,
    x_msg_data           => l_msg_data
  );

  IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
    FND_MSG_PUB.GET (l_msg_count, l_msg_data);
    DBMS_OUTPUT.put_line('Error: ' || l_msg_data);
  END IF;
END;
  • p_api_version

  • p_init_msg_list

  • p_commit

  • x_return_status

  • x_msg_count

  • x_msg_data

Use FND_MSG_PUB to retrieve messages.


Would you like these turned into a PDF download or posted individually for SEO benefit?

Monday, November 2, 2020

Query to find out executable of a Concurrent Program

๐Ÿ” Query to Find Executable of a Concurrent Program in Oracle EBS

In Oracle E-Business Suite (EBS), each Concurrent Program is linked to an executable file that runs in the backend. To find out the executable associated with a given concurrent program, use the SQL query below.


๐Ÿ“Œ SQL Query to Fetch Executable Details

SELECT 
    prog.user_concurrent_program_name AS "Program Name",
    prog.concurrent_program_name AS "Program Short Name",
    appl.application_name AS "Program Application Name",
    prog.description AS "Program Description",
    exe.executable_name AS "Executable Name",
    exe.execution_file_name AS "Executable File Name",
    DECODE(
        exe.execution_method_code,
        'I', 'PL/SQL Stored Procedure',
        'P', 'Oracle Reports',
        'L', 'SQL*Loader',
        'Q', 'SQL*Plus',
        exe.execution_method_code
    ) AS "Execution Method"
FROM 
    apps.fnd_executables exe,
    apps.fnd_application_tl appl,
    apps.fnd_concurrent_programs_vl prog
WHERE 
    exe.application_id = appl.application_id
    AND exe.executable_id = prog.executable_id
    AND appl.language = 'US'
    AND prog.user_concurrent_program_name LIKE '%AP%'; -- Optional filter

Tip: Replace '%AP%' with any part of the program name you want to search. For example, to find a specific report like "Invoice Register", use '%Invoice%'.


๐Ÿง  Columns Explained

  • Program Name: User-friendly name of the concurrent program.
  • Executable Name: Name given to the executable in EBS.
  • Executable File Name: Actual file or procedure name that runs.
  • Execution Method: How the program is run (e.g., Report, PL/SQL, SQL Loader).

๐Ÿ“Ž Related Topics

Friday, November 11, 2022

Mandatory Parameters In calling Oracle APPS Standard API.

Mandatory Parameters In calling Oracle APPS  Standard API. 
API Stands for Application Programming Interface is PL/SQL packaged procedure which can be used as an alternative entry point into the system to the traditional online forms(forms, OAF pages,..).

The advantage being that the same logic used by the seeded online forms can

Standard IN Parameters:--

1) p_api_version IN NUMBER
This must match the version number of the API. An unexpected error is returned if the calling program version number is incompatible with the current API version number

Example: p_api_version => 1.0

2) p_init_msg_list IN VARCHAR2
The valid values for this parameter are:
  -->  FND_API.G_TRUE
  -->  FND_API.G_FALSE

By default API will take FND_API.G_FALSE

Example:  p_init_msg_list => fnd_api.g_true

3) p_commit IN VARCHAR2
The valid values for this parameter are:
  *  FND_API.G_TRUE   --> True
  *  FND_API.G_FALSE  --> False

If set to true, then the API commits before returning to the calling program. 
If set to false, then it is the calling program’s responsibility to commit the transaction.

Example:- p_commit => fnd_api.g_true

4) p_validation_level IN VARCHAR2

The valid values for this parameter are:
  *  fnd_api.g_valid_level_full 

Default = FND_API.G_VALID_LEVEL_FULL
If set to full, then the API validates all the IN parameter values.

Standard OUT Parameters

1) x_return_status OUT NOCOPY VARCHAR2
Indicates the return status of the API. Every API must return one of the following states as parameter x_return_status after the API is called:

  • S (Success)
  • E (Error)
  • U (Unexpected error)

2) x_msg_count OUT NOCOPY NUMBER
Holds the number of messages in the message list.

3) x_msg_data OUT NOCOPY VARCHAR2
Holds the encoded message if x_msg_count i

query to get Shipment number based on Order number

 SELECT DISTINCT     wnd.delivery_name,     wnd.actual_ship_date,     wdd.sales_order_number FROM     wsh_new_deliveries       wnd,     wsh_...

Popular Posts