Validate Syntax of Oracle Dynamic SQL Before Execution

Kiran P
Updated on 04-Dec-2020 10:31:30

2K+ Views

Problem Statement:You want to validate the syntax of a SQL before execution.Solution: The DBMS_SQL default package allows SQL to be dynamically executed. Owned by SYS it has been defined with the AUTHID CURRENT_USER keyword so it runs with the privileges of the invoker. We can leverage DBMS_SQL.PARSE function to validate the syntax.We will first define a function to accept the SQL statement as the parameter and parse the SQL statement./*  * ---------------------------------------------------------------------------  *  Function : check_syntax  *    Input  : sql statement  *   Output  : Number  * ---------------------------------------------------------------------------  */    FUNCTION check_syntax     ( p_query IN CLOB ) ... Read More

Produce Group Subtotals for All Combinations of Dimensions in Oracle

Kiran P
Updated on 04-Dec-2020 10:27:27

251 Views

Problem Statement:You want to find out subtotals for all combinations of the dimensions specified in Oracle.Solution:The CUBE function will generate subtotals for all combinations of the dimensions specified. If “n” is the number of columns listed in the CUBE, there will be 2n subtotal combinations.We will begin by create the necessary data for this requirement.Example-- Drop table DROP TABLE atp_titles; -- Create table CREATE TABLE atp_titles (   player             VARCHAR2(100) NOT NULL,   title_type         VARCHAR2(100) NOT NULL,   titles             NUMBER NOT NULL);Example-- insert ... Read More

Produce Group Subtotals and Grand Total in Oracle

Kiran P
Updated on 04-Dec-2020 10:24:31

4K+ Views

Problem Statement:You want to find out totals, subtotals and a grand total in Oracle.Solution:Oracle ROLLUP function performs grouping at multiple levels, using a right to left method of rolling up through intermediate levels to any grand total. To demonstrate the ROLLUP function we will create a table to hold tennis player along with the ATP tour titles and Grandslam titles acheived by the player.We will begin by create the necessary data for this requirement.Example-- Drop table DROP TABLE atp_titles; -- Create table CREATE TABLE atp_titles (   player             VARCHAR2(100) NOT NULL,   title_type ... Read More

Find and Replace Text in Oracle

Kiran P
Updated on 04-Dec-2020 10:20:55

7K+ Views

Problem Statement: You want to find and replace a string in Oracle.Solution:Function: TRANSLATESyntax: TRANSLATE(expr, from_string, to_string)TRANSLATE funtion in oracle allows you to make many single character, one on one substitutions in a single operation.However, the to_string and from_string values must not be empty, if an empty string is passed to TRANSLATE Function, Oracle Database interprets the empty string as null and returns null./* replace all occurances of b with j in below string */ SELECT 'back and bill ' AS original_string        , TRANSLATE('back and bill', 'b', 'j') AS replaced_string   FROM dual;Outputback and bill   jack and ... Read More

Identify Rows Not Parents in Oracle Hierarchy Table

Kiran P
Updated on 04-Dec-2020 10:17:54

274 Views

Problem Statement: How to identify leaf rows in a Hierarchy table i.e. rows that are not parents of any other rows.Solution: Oracle provides CONNECT_BY_ISLEAF clause to identify rows that are not parents of any other rows. To begin with let us see how does a connect_by_isleaf works.SQL:/*   Function - Example to show if the row is parent of any other rows or not   Tables Used - students Data - Documented below */ SELECT student_id,        level,        CASE WHEN connect_by_isleaf = 0             THEN 'Yes'         ... Read More

Remove a Branch Within a Hierarchy of Data in Oracle

Kiran P
Updated on 04-Dec-2020 10:15:53

585 Views

Problem Statement: You need to traverse the hierarchy data from top to bottom but doesn’t want a particular branch in the output.Solution: We will look at couple of examples for this problem statement.Oracle provides CONNECT BY clause to specify a hierarchical query i.e. how to connect the parent nodes and child nodes and the PRIOR operator to define the join condition/s between the parent nodes, and the LEVEL pseudo-column to indicate how far from the root/parent row the current row is.Additionally, we can use the START WITH clause to indicate where to start the tree navigation. We must use the PRIOR ... Read More

Traverse Hierarchical Data in Oracle and Sort Rows at Same Level

Kiran P
Updated on 04-Dec-2020 10:12:54

737 Views

Problem Statement: You need to traverse the hierarchy data from top to bottom and sort the rows at the same level in a hierarchy.Solution: The usual ORDER BY clause will not sort the rows at the same hierarchy level. We need to use SIBLINGS with in the ORDER BY Clause.Additionally, Oracle provides CONNECT BY clause to specify a hierarchical query i.e. how to connect the parent nodes and child nodes and the PRIOR operator to define the join condition/s between the parent nodes, and the LEVEL pseudo-column to indicate how far from the root/parent row the current row is.Additionally, we can ... Read More

Traverse Hierarchical Data in Oracle

Kiran P
Updated on 04-Dec-2020 04:16:13

845 Views

Problem Statement: You need to traverse the hierarchy data from top to bottom marking the level of each row in the hierarchy.Solution:Oracle provides CONNECT BY clause to specify a hierarchical query i.e. how to connect the parent nodes and child nodes and the PRIOR operator to define the join condition/s between the parent nodes, and the LEVEL pseudo-column to indicate how far from the root/parent row the current row is.Additionally, we can use the START WITH clause to indicate where to start the tree navigation. We must use the PRIOR operator to specify the column/s in the parent row that have ... Read More

Count of Distinct Rectangles Inscribed in an Equilateral Triangle in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:50:15

163 Views

We are an equilateral triangle with side length. The goal is to count the number of distinct rectangles that can be present inside the triangle such that horizontal sides of the rectangle are parallel to the base. Also all end points of the rectangle touch the dots as shown.Let us understand with examplesInput − sides=3Output − Count of distinct rectangles inscribed in an equilateral triangle are − 1Explanation − The figure above shows the rectangle.Input − sides=10Output − Count of distinct rectangles inscribed in an equilateral triangle are − 200Approach used in the below program is as followsAs from the ... Read More

Count of Strings Where Adjacent Characters Are of Difference One in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:42:00

964 Views

We are given a num number as input. The goal is to count the number of possible strings of length num such that all adjacent characters have difference between ascii values as 1.If num is 2 then strings will be “ab”, “ba”, “bc”, “cb”, ……..”yz”, “zy”.Let us understand with examplesInput − num=3Output − Count of strings where adjacent characters are of difference one are − 98Explanation − Some sample strings are: “abc”, “aba”, “cde” …..”xyx”, “zyz”, “xyz”.Input − num=2Output − Count of strings where adjacent characters are of difference one are − 50Explanation − Some sample strings are: “ab”, “ba”, ... Read More

Advertisements