Subscribe:

Ads 468x60px

Pages

Showing posts with label FAQ. Show all posts
Showing posts with label FAQ. Show all posts

Tuesday, July 10, 2012

PL/SQL FAQ


                 PL/SQL FAQ - Oracle's Procedural Language extension 

       to SQL:      

 Contents

·      1 What is PL/SQL and what is it used for? 
 ·      2 What is the difference between SQL and PL/SQL? 
 ·      3 Should one use PL/SQL or Java to code procedures and triggers?
·      4 How can one see if somebody modified any code?
·      5 How can one search PL/SQL code for a string/ key value?
·      6 How does one keep a history of PL/SQL code changes?
·      7 How can I protect my PL/SQL source code?
·      8 Can one print to the screen from PL/SQL?
·      9 Can one read/write files from PL/SQL?
·      10 Can one call DDL statements from PL/SQL? 


                   What is PL/SQL and what is it used for?

SQL is a declarative language that allows database programmers to write a SQL declaration and hand it to the database for execution. As such, SQL cannot be used to execute procedural code with conditional, iterative and sequential statements. To overcome this limitation, PL/SQL was created.
PL/SQL is Oracle's Procedural Language extension to SQL. PL/SQL's language syntax, structure and data types are similar to that of Ada. Some of the statements provided by PL/SQL:
Conditional Control Statements:
·      IF ... THEN ... ELSIF ... ELSE ... END IF;
·      CASE ... WHEN ... THEN ... ELSE ... END CASE;
Iterative Statements:
·      LOOP ... END LOOP;
·      WHILE ... LOOP ... END LOOP;
·      FOR ... IN [REVERSE] ... LOOP ... END LOOP;
Sequential Control Statements:
·      GOTO ...;
·      NULL;
The PL/SQL language includes object oriented programming techniques such as encapsulation, function overloading, information hiding (all but inheritance).
PL/SQL is commonly used to write data-centric programs to manipulate data in an Oracle database



Example PL/SQL blocks:
/* Remember to SET SERVEROUTPUT ON to see the output */
BEGIN
  DBMS_OUTPUT.PUT_LINE('Hello World');
END;
/
BEGIN
  -- A PL/SQL cursor
  FOR cursor1 IN (SELECT * FROM table1) -- This is an embedded SQL statement
  LOOP
    DBMS_OUTPUT.PUT_LINE('Column 1 = ' || cursor1.column1 ||
                       ', Column 2 = ' || cursor1.column2);
  END LOOP;
END;
/

                   What is the difference between SQL and PL/SQL?

Both SQL and PL/SQL are languages used to access data within Oracle databases.
SQL is a limited language that allows you to directly interact with the database. You can write queries (SELECT), manipulate objects (DDL) and data (DML) with SQL. However, SQL doesn't include all the things that normal programming languages have, such as loops and IF...THEN...ELSE statements.
PL/SQL is a normal programming language that includes all the features of most other programming languages. But, it has one thing that other programming languages don't have: the ability to easily integrate with SQL.
Some of the differences:
·      SQL is executed one statement at a time. PL/SQL is executed as a block of code.
·      SQL tells the database what to do (declarative), not how to do it. In contrast, PL/SQL tell the database how to do things (procedural).
·      SQL is used to code queries, DML and DDL statements. PL/SQL is used to code program blocks, triggers, functions, procedures and packages.
·      You can embed SQL in a PL/SQL program, but you cannot embed PL/SQL within a SQL statement.

                   Should one use PL/SQL or Java to code procedures and triggers?

Both PL/SQL and Java can be used to create Oracle stored procedures and triggers. This often leads to questions like "Which of the two is the best?" and "Will Oracle ever desupport PL/SQL in favour of Java?".
Many Oracle applications are based on PL/SQL and it would be difficult of Oracle to ever desupport PL/SQL. In fact, all indications are that PL/SQL still has a bright future ahead of it. Many enhancements are still being made to PL/SQL. For example, Oracle 9i supports native compilation of Pl/SQL code to binaries. Not to mention the numerous PL/SQL enhancements made in Oracle 10g and 11g.
PL/SQL and Java appeal to different people in different job roles. The following table briefly describes the similarities and difference between these two language environments:
PL/SQL:
·      Can be used to create Oracle packages, procedures and triggers
·      Data centric and tightly integrated into the database
·      Proprietary to Oracle and difficult to port to other database systems
·      Data manipulation is slightly faster in PL/SQL than in Java
·      PL/SQL is a traditional procedural programming language
Java:
·      Can be used to create Oracle packages, procedures and triggers
·      Open standard, not proprietary to Oracle
·      Incurs some data conversion overhead between the Database and Java type
·      Java is an Object Orientated language, and modules are structured into classes
·      Java can be used to produce complete applications
PS: Starting with Oracle 10g, .NET procedures can also be stored within the database (Windows only). Nevertheless, unlike PL/SQL and JAVA, .NET code is not usable on non-Windows systems.
PS: In earlier releases of Oracle it was better to put as much code as possible in procedures rather than triggers. At that stage procedures executed faster than triggers as triggers had to be re-compiled every time before executed (unless cached). In more recent releases both triggers and procedures are compiled when created (stored p-code) and one can add as much code as one likes in either procedures or triggers. However, it is still considered a best practice to put as much of your program logic as possible into packages, rather than triggers.

                   How can one see if somebody modified any code?

The source code for stored procedures, functions and packages are stored in the Oracle Data Dictionary. One can detect code changes by looking at the TIMESTAMP and LAST_DDL_TIME column in the USER_OBJECTS dictionary view. Example:
SELECT OBJECT_NAME,
       TO_CHAR(CREATED,       'DD-Mon-RR HH24:MI') CREATE_TIME,
       TO_CHAR(LAST_DDL_TIME, 'DD-Mon-RR HH24:MI') MOD_TIME,
       STATUS
FROM   USER_OBJECTS
WHERE  LAST_DDL_TIME > '&CHECK_FROM_DATE';
Note: If you recompile an object, the LAST_DDL_TIME column is updated, but the TIMESTAMP column is not updated. If you modified the code, both the TIMESTAMP and LAST_DDL_TIME columns are updated.

                   How can one search PL/SQL code for a string/ key value?

The following query is handy if you want to know where certain tables, columns and expressions are referenced in your PL/SQL source code.
SELECT type, name, line
  FROM   user_source
 WHERE  UPPER(text) LIKE UPPER('%&KEYWORD%');
If you run the above query from SQL*Plus, enter the string you are searching for when prompted for KEYWORD. If not, replace &KEYWORD with the string you are searching for.

                   How does one keep a history of PL/SQL code changes?

One can build a history of PL/SQL code changes by setting up an AFTER CREATE schema (or database) level trigger (available from Oracle 8.1.7). This will allow you to easily revert to previous code should someone make any catastrophic changes. Look at this example:
CREATE TABLE SOURCE_HIST                    -- Create history table
  AS SELECT SYSDATE CHANGE_DATE, ALL_SOURCE.*
  FROM   ALL_SOURCE WHERE 1=2;

CREATE OR REPLACE TRIGGER change_hist        -- Store code in hist table
  AFTER CREATE ON SCOTT.SCHEMA          -- Change SCOTT to your schema name
DECLARE
BEGIN
  IF ORA_DICT_OBJ_TYPE in ('PROCEDURE', 'FUNCTION',
                           'PACKAGE',   'PACKAGE BODY',
                           'TYPE',      'TYPE BODY')
  THEN
     -- Store old code in SOURCE_HIST table
     INSERT INTO SOURCE_HIST
            SELECT sysdate, all_source.* FROM ALL_SOURCE
             WHERE  TYPE = ORA_DICT_OBJ_TYPE  -- DICTIONARY_OBJ_TYPE IN 8i
               AND  NAME = ORA_DICT_OBJ_NAME; -- DICTIONARY_OBJ_NAME IN 8i
  END IF;
EXCEPTION
  WHEN OTHERS THEN
       raise_application_error(-20000, SQLERRM);
END;
/
show errors
A better approach is to create an external CVS or SVN repository for the scripts that install the PL/SQL code. The canonical version of what's in the database must match the latest CVS/SVN version or else someone would be cheating.

                   How can I protect my PL/SQL source code?

Oracle provides a binary wrapper utility that can be used to scramble PL/SQL source code. This utility was introduced in Oracle7.2 (PL/SQL V2.2) and is located in the ORACLE_HOME/bin directory.
The utility use human-readable PL/SQL source code as input, and writes out portable binary object code (somewhat larger than the original). The binary code can be distributed without fear of exposing your proprietary algorithms and methods. Oracle will still understand and know how to execute the code. Just be careful, there is no "decode" command available. So, don't lose your source!
The syntax is:
wrap iname=myscript.pls oname=xxxx.plb
Please note: there is no way to unwrap a *.plb binary file. You are supposed to backup and keep your *.pls source files after wrapping them.

                   Can one print to the screen from PL/SQL?

One can use the DBMS_OUTPUT package to write information to an output buffer. This buffer can be displayed on the screen from SQL*Plus if you issue the SET SERVEROUTPUT ON; command. For example:
set serveroutput on
begin
   dbms_output.put_line('Look Ma, I can print from PL/SQL!!!');
end;
/
DBMS_OUTPUT is useful for debugging PL/SQL programs. However, if you print too much, the output buffer will overflow. In that case, set the buffer size to a larger value, eg.: set serveroutput on size 200000
If you forget to set serveroutput on type SET SERVEROUTPUT ON once you remember, and then EXEC NULL;. If you haven't cleared the DBMS_OUTPUT buffer with the disable or enable procedure, SQL*Plus will display the entire contents of the buffer when it executes this dummy PL/SQL block.
To display an empty line, it is better to use new_line procedure than put_line with an empty string.

                   Can one read/write files from PL/SQL?

The UTL_FILE database package can be used to read and write operating system files.
A DBA user needs to grant you access to read from/ write to a specific directory before using this package. Here is an example:
CONNECT / AS SYSDBA
CREATE OR REPLACE DIRECTORY mydir AS '/tmp';
GRANT read, write ON DIRECTORY mydir TO scott;
Provide user access to the UTL_FILE package (created by catproc.sql):
GRANT EXECUTE ON UTL_FILE TO scott;
Copy and paste these examples to get you started:
Write File
DECLARE
  fHandler UTL_FILE.FILE_TYPE;
BEGIN
  fHandler := UTL_FILE.FOPEN('MYDIR', 'myfile', 'w');
  UTL_FILE.PUTF(fHandler, 'Look ma, Im writing to a file!!!\n');
  UTL_FILE.FCLOSE(fHandler);
EXCEPTION
  WHEN utl_file.invalid_path THEN
     raise_application_error(-20000, 'Invalid path. Create directory or set UTL_FILE_DIR.');
END;
/
Read File
DECLARE
  fHandler UTL_FILE.FILE_TYPE;
  buf      varchar2(4000);
BEGIN
  fHandler := UTL_FILE.FOPEN('MYDIR', 'myfile', 'r');
  UTL_FILE.GET_LINE(fHandler, buf);
  dbms_output.put_line('DATA FROM FILE: '||buf);
  UTL_FILE.FCLOSE(fHandler);
EXCEPTION
  WHEN utl_file.invalid_path THEN
     raise_application_error(-20000, 'Invalid path. Create directory or set UTL_FILE_DIR.');
END;
/
NOTE: UTL_FILE was introduced with Oracle 7.3. Before Oracle 7.3 the only means of writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command.

                   Can one call DDL statements from PL/SQL?

One can call DDL statements like CREATE, DROP, TRUNCATE, etc. from PL/SQL by using the "EXECUTE IMMEDIATE" statement (native SQL). Examples:
begin
  EXECUTE IMMEDIATE 'CREATE TABLE X(A DATE)';
end;
begin execute Immediate 'TRUNCATE TABLE emp'; end;
DECLARE
  var VARCHAR2(100);
BEGIN
  var := 'CREATE TABLE temp1(col1 NUMBER(2))';
  EXECUTE IMMEDIATE var;
END;
NOTE: The DDL statement in quotes should not be terminated with a semicolon.
Users running Oracle versions below Oracle 8i can look at the DBMS_SQL package (see FAQ about Dynamic SQL).

Tuesday, December 27, 2011

Technology Articles/FAQs

Technology Articles/FAQs
The technological advances of the last decade have brought about hitherto unimagined changes in the way we go about our lives.

Technology Articles/FAQs

What is Technology?
The Oxford Dictionary defines technology as the application of scientific knowledge for practical purposes, especially in industry. In other words, it can be said that it is an effort to put in practice the knowledge of tools, techniques, crafts, and methods to find a solutions of a particular problem.
The word technology is derived from Greek meaning, "art, skill, craft", and logia meaning the discipline of or "study of-". The term is used generally or in context with particular fields like information technology, Industrial technology and others.
Technology enables people to adjust with their natural surroundings, in other words, it make their adjustment simples and hassle free. If a particular technology makes things complex rather than making it simple it can't survive for long. Right from invention of wheels to super computer and methods of natural cure to genetically engineered medicine, the whole efforts are streamlined to make human adaptation with nature simples smooth and hassle free.
Technology Tutorials:
This advancement in technology has opened new vistas for human being and information technology is spearheading this change. The age of messenger is passé, now we have invented a swifter messenger in the form of emails. We are living in the age where any information is just a click away. The Internet is acting as an information super highway that connects every destination, big and small. This advancement in technology has made life of peoples simple. We appreciate the role of technology, but we can?t get content as a lot is still left to be done.
  • GPS Technology
    Best information on GPS.
      
  • What is GPS?
    The technological advances of the last decade have brought about hitherto unimagined changes in the way we go about our lives.
      
  • How GPS Works
    GPS or Global Positioning System is a technology for locating a person or an object in three dimensional space anywhere on the Earth or in the surrounding orbit. GPS is a very important invention of our time on account of the many different possibilities it brings.
      
  • Benefits of GPS
    GPS or Global Positioning System was originally developed as a military navigation tool. However the technology has grown along with a sub set of supporting technologies to serve other requirements within consumer budgets.
      
  • GPS Tracking and its Applications
    GPS technology was originally developed for defense purposes and later brought to the consumer market as a navigation technology.
Networking
1.        What is OSI Layer?
The OSI Model is used to describe networks and network application. Different layers of OSI Model has described.  

1.        What is WiMaX?
What is WiMAX? Simply put WiMAX is, Worldwide Interoperability for Microwave Access, a technology standard that enables high speed wireless internet.
 
2.        Why WiMAX?
WiMAX is an acronym for Worldwide Interoperability for Microwave Access. It is an assortment of technical specifications called 802.16. The technology used in WiMAX enables wireless transmission of broadband internet over distances in the range of up to 50 km.
 
3.        Will WiMAX Replace DSL?
There has been a lot of hype about WiMAX as the next generation technology that will wipe out wired internet connectivity. The contention is that, it is far easier and more cost effective to set up transmission towers than continuously extend the cables for last mile connectivity.
 
4.        Will WiMAX Replace WiFi?
Enthusiasts of WiMAX are of the view that this powerful technology will soon overtake WiFi, which looks feeble. This need not be the case, as both can have their separate applications.
 
5.        Is WiMAX Safe?
WiMAX enabled devices emit radio frequency electromagnetic waves when in use. These waves have been part of our everyday life for decades now, in the use of devices such as radio, television and mobile phones.
  
6.        WiMAX Billing ChallengesImagine being able to access your emails and attend an urgent office conference while you are holidaying in a remote hill station of India! It is this kind of possibility that makes WiMAX an irresistible contestant to the title ?the technology of tomorrow.? WiMAX has finally made high speed wireless internet access a reality no matter where you are or whether you are on the move.
1.        Internate Telephone Services
VoIP or voice over internet Protocol is that technology which makes it possible to make phone calls through the internet. This technology is given many common names as internet phone service or digital phone service or broadband phone service.
    
2.        VoIP History
VoIP stands for Voice over Internet Protocol. This phenomenon has made a profound change in the world of telephone communications. The traditional method of making calls the landlines are being fats replaced by this technology that has taken the world by storm.
    
3.        VoIP- What is VoIP?This section introduces you with the VoIP technology and its benefits. VOIP is so beneficial that is saves hundreds or even thousand of dollars if used extensively. That is the reason behind VOIP Popularity. Many Companies are using VoIP for communication and saving thousands of dollars.
    
4.        VoIP Phones
List of VoIP Phones that you can use to make VoIP Phone calls.
  
5.        Complete Guide to Setup VoIP on Linux Machine
Voice Over IP is a new communication means that let you telephone with Internet at almost null cost. How this is possible, what systems are used, what is the standard, all that is covered by this tutorial.
  
6.        VoIP Software Phones
VoIP Software Phones are basically a software for making VoIP calls using computer. Software phones are usually less expensive and it offers a better options for Computer Telephony Integration (CTI). Find free VoIP Software Phones for you home and business.
    
7.        Free VoIP Gateways
Here you will find the list of Free VoIP Gatways that you can use to connect the VoIP network to your public telephone network (PSTN). In this way, you can make calls and receive them, from your PTSN, even if you are using an IP-based system, just like your traditional phone.
  
8.        Free VoIP Gatekeepers
Here you will find the list of widely used free Gatekeepers for your VoIP system. These Gatekeepers provide centralized call management functions such as user location, authentication, bandwidth management, address translation, etc. along with bandwidth management.
 
9.        Free VoIP Software Development Library
These VoIP Software Development Libraries are being used for the development of VoIP software.
  
10.     Free VoIP Proxy
Here are the list of Free VoIP Proxy that you can use with your VoIP System
1.        What is WiFi?
WiFi is a globally used wireless networking technology that uses the 802.11 standard. The term WiFi is an abbreviation of ?wireless fidelity?. The technology used in WiFi was developed in 1997 by the Institute of Electrical and Electronics Engineers (IEEE).
 
2.        Why WiFi?
WiFi, or Wireless Fidelity, is a technology standard developed in 1997 by the Institute of Electrical and Electronics Engineers (IEEE). WiFi is all about high speed wireless internet access.
  
3.        WiFi Internet
WiFi, the short form of Wireless Fidelity, is a technology that allows you to connect to the internet without a cabled network. WiFi technology uses the 802.11 standard developed by the Institute of Electrical and Electronics Engineers (IEEE) and commercialized by the WiFi Alliance.
  
4.        What is WiFi Phone?
A WiFi phone is a wireless device that gives you the dual benefits of wireless connectivity and the cost savings of VoIP. It can be used in any areas- hotspots- where WiFi connectivity is provided.
  
5.        How to Become WiFi Hotspot?
WiFi technology is one of the hottest and fastest spreading worldwide. More and more businesses and individuals are setting up WiFi hotspots. Here are a few useful tips for those who are considering a hotspot as a business tool.
  
6.        What is WiFi Finder?
A WiFi finder is a device used for locating wireless hotspots without actually turning on the laptop or PDA. WiFi finders are small, battery operated and portable. A WiFi finder typically detects a hotspot within a radius of 200 feet and also determines the strength of the signal.
 
7.        Security for Home WiFi Network
Security is a huge concern for anyone setting up a WiFi network, as anyone who is close enough to the hotspot can break into your system and access the information.
 
8.        Security for Public WiFi Network
Wi-Fi hotspots present a unique set of security problems, quite different from the security issues involved in home and office networks. These hotspots have unknown computers accessing them. And in this case, the very nature of a public hotspot demands that it broadcasts its SSID.
1.        What is HSDPA?
HSDPA is an acronym for High Speed Downlink Packet Access which is an advanced protocol for mobile telephone data transmission. HSDPA is an evolved form of W-CDMA (Wideband Code Division Multiple Access) technology. HSDPA promises to provide download speeds.
 
1.        Location Based Service (LBS)This is one of the most popular services based on a different navigation technologies provided by the mobile communication network.
  
2.        Basic Components & Functionality of LBSConsists of basic electronic instruments like mobile phone, smart phone, laptop and other personal digital devices (PDA) for accessing information.
 
3.        How does LBS work?Location based service (LBS) is the application that users use to locate its own position with the help of some basic components like mobile devices, mobile communication network, service provider like the Global Positioning Service (GPS), data and yellow pages for the service station.
  
4.        How is LBS useful?LBS is designed to provide valuable information to the users based on location or position.
  
5.        Application of LBS in Different FieldsThe popularity of location- based service makes it one of most essential and useful asset in almost all industries. However, its market is divided into various categories including navigation, emergency assistance, tracking, advertising, billing, management, games and leisure.
 
6.        Types of LBSLocation based service enables us to find the geographical location of a mobile device with its user and provide various services related to the location information.
  
7.        Relation between GIS and LBSTechnological advances like Geographical Information System (GIS) and all kinds of location-based services like GPS have brought tremendous change in the lifestyle of people especially the way we communicate.
 
8.        Location Based Service (LBS) in TourismIn a simple way if we have to define LBS then we can say that it is a service that determines where a mobile device and its user are geographically located and also acts as an information gateway for the user by providing various kinds of services.
  
9.        Pros and Cons of LBSLocation based service is one of the most important technological innovation in today's world as it contributes a lot in bringing changes in society.
  
10.      Security and Privacy Issues in Location Based Service (LBS)Location Based Service (LBS) is primarily based on user?s location to provide other value added services by means of a wireless device functioning through common cellular network or radio stations.
 
11.     Wi-Fi as a part of LBS
Wi-Fi Positioning System (WPS) uses wireless fidelity network instead of GPS or cell tower systems or location beacons to determine position.  
1.        What is Business Intelligence
Business Intelligence is a form of business management that takes business forward by the proper use of available information and data.
   
2.        History of Business IntelligenceThe 20 th and 21 st century are known as the information and technology (IT) age where everything depends on the availability of information and innovation of new technology.
  
3.        Business Intelligence ToolsWe have already discussed that Business Intelligence is a process to collect data and extract information and then analyzing them in developing new and innovative business ideas but the whole method is based on different computing tools.
 
4.        Business Intelligence Value ChainIn competitive marketplace it is vital for every business enterprise whether small or big to cope with the pace of the market growth. Intelligence is the ability to learn and understand new situations by updating yourself with current information.
  
5.        Business Intelligence in Decision MakingToday Business Intelligence is the primary tool to collect and analyze information for developing a successful business strategy.
 
6.        Privacy and Security Issues in BIWITH THE GROWTH of Business Intelligence market, the privacy and security issues are also growing concern over business community.
 
7.        The Future of Business IntelligenceInformation and communication technology has offered a single platform to world market irrespective of geographical position. Growing number of consumers with varied demand and expectation make it very difficult to conduct any kind of business.
    
8.        Business Intelligence in HealthcareTo make a profit driven business decision, every company depends on their strategic planning and decision-making ability and that depends on the kinds of information available and the ability to sort out the relevant ones for analysis.
 
9.        Business Intelligence in Insurance
Success in today's business world is defined by the ability to access sophisticated data and derive information that is now primary need to survive in the competitive market place. This is also true in insurance business that deals with rich and complex data structure and most of them are real-time.
Vehicle Tracking
1.        Vehicle Tracking TOC
VEHICLE TRACKING SYSTEM or Automatic Vehicle Location System (AVL) is now one of the most hot topic. Learn more about Vehicle Tracking here..
 
2.        Vehicle Tracking In India
Vehicle tracking system in India is mainly used in transport industry that keeps a real-time track of all vehicles in the fleet. The tracking system consists of GPS device that brings together GPS and GSM technology using tracking software.
 
1.        What is SCADA?
SCADA or Supervisory Control And Data Acquisition is a large scale control system for automated industrial processes like municipal water supplies, power generation, steel manufacturing, gas and oil pipelines etc. SCADA also has applications in large scale experimental facilities like those used in nuclear fusion.
  
2.        Why SCADA?
SCADA is an acronym that denotes Supervisory Control And Data Acquisition. SCADA is a control system with applications in managing large-scale, automated industrial operations. Factories and plants, water supply systems, nuclear and conventional power generator systems etc are a few examples.
    
3.        SCADA Programming
SCADA or Supervisory Control and Data Acquisition is a distributed measurement and control system for large-scale industrial automation. SCADA has applications in automated operations like chemical manufacturing and transport, supply systems and power generation.
     
4.        SCADA in Future
Elsewhere on this site we saw the fundamentals of SCADA and how it has emerged. In this article we will examine some of the challenges and applications that need to be met in order for SCADA to remain relevant to the new needs.