|
DBA Procedures: Resources 4. Insert data into new table. Connect as system
manager. c) To see what Oracle roles the user has been given.
TIPS: 1) In SQLPlus type: HELP INDEX # this will give you a list of the commands. HELP COMMAND_NAME will give you more specifics. 2) Database export within Perl 3) Using the import utility. The following script shows how you can run sql scripts with sqlplus from within a Perl program (you will want to scroll down towards the bottom of the file) 4) Getting Oracle to do calculations: From SqlPlus type: SELECT (3 + 3) FROM DUAL; or SELECT 3 + 3 FROM DUAL;
5) From NewsGroups:
When you recieve the formula in a string format, the only options is to
create a stored PL/SQL function
and work with dynamic SQL or Ref Cursors. I used Ref Cursors for a problem
that is quite similar to
yours. Hopelfully this example PL/SQL function makes it clear
CREATE OR REPLACE FUNCTION CALCULATE_FORMULA(formula in varchar2) return
number
AS
TYPE dynamic_cursor_type IS REF CURSOR;
dynamic_cursor dynamic_cursor_type;
cursor_source VARCHAR2(200);
result number := 0;
BEGIN
cursor_source := 'select '||formula||' from dual';
OPEN dynamic_cursor FOR cursor_source;
FETCH dynamic_cursor INTO result;
CLOSE dynamic_cursor;
return result;
EXCEPTION
when others
then
raise_application_error(-20110, 'CALCULATION ERROR. PLEASE CHECK
YOUR FORMULA!', TRUE);
END;
# Note: formula is where you put math to be done
6) Working with Functions:
SQL> CREATE OR REPLACE FUNCTION hello_func (
name_ IN VARCHAR2) RETURN VARCHAR2
IS
BEGIN
RETURN 'Hello ' || name_;
END hello_func;
/
SQL> Function Created.
SQL> select hello_func('David') "Hello String" from dual;
Hello String
-------------------------
Hello David
7) # Spooling and turning off headers type:
set echo off
set heading off
set verify off
set feedback off
set serveroutput on
spool D.LOG
SELECT '=== Exec of D.SQL ' || TO_CHAR(SYSDATE, 'DD.MM.YYYY') || ' ===' FROM
DUAL
/
ACCEPT inp PROMPT 'Input something? (Y/N)'
prompt === End of Script D.SQL===
SPOOL OFF
--------------------------------------------------------------
Look at Later:
DECLARE
-- Declare the OLE objects
MyApplication OLE2.OBJ_TYPE;
MyDocuments OLE2.OBJ_TYPE;
MyDocument OLE2.OBJ_TYPE;
-- Declare handle to the OLE argument list
args OLE2.LIST_TYPE;
var varchar2(100);
BEGIN
-- Create the Word.Application object and make Word visible
-- by setting the 'Visible' property to true, by default it is
invisible.
MyApplication:=OLE2.CREATE_OBJ('Word.Application');
OLE2.SET_PROPERTY(MyApplication, 'Visible', 'True'); -- Comment this
line to hide the application.
-- get a handle on Documents collection
MyDocuments:=OLE2.GET_OBJ_PROPERTY(MyApplication, 'Documents');
-- Open a new document
MyDocument :=OLE2.INVOKE_OBJ(MyDocuments,'Add');
OLE2.DESTROY_ARGLIST(args);
message('Adding Document Variable');
-- Create a document variable MyVar and populate with the value 12
args:=OLE2.CREATE_ARGLIST;
OLE2.ADD_ARG(args, 'MyVar');
OLE2.ADD_ARG(args, '12');
OLE2.INVOKE(MyDocument,'Variables.Add',args);
OLE2.DESTROY_ARGLIST(args);
message('Reading Document Variable');
-- Read the document variable and display on Forms message line
args:=OLE2.CREATE_ARGLIST;
OLE2.ADD_ARG(args, 'MyVar');
var:=OLE2.INVOKE_CHAR(MyDocument,'Variables',args);
message(var);
OLE2.DESTROY_ARGLIST(args);
-- Release the OLE objects
OLE2.INVOKE(MyDocument,'Close');
OLE2.RELEASE_OBJ(MyDocument);
OLE2.RELEASE_OBJ(MyDocuments);
OLE2.RELEASE_OBJ(MyApplication);
exception
when others then
message(sqlerrm||sqlcode);
OLE2.DESTROY_ARGLIST(args);
OLE2.INVOKE(MyDocument,'Close');
-- Release the OLE objects
OLE2.RELEASE_OBJ(MyDocument);
OLE2.RELEASE_OBJ(MyDocuments);
OLE2.RELEASE_OBJ(MyApplication);
END;
--------------------------------------------
Look at Later:
Build a table into the Oracle database, and called "MYTABLE1" something like this.
ID
(type: number) User Name(type: varchar2) Phone(type: varchar2) Email(type: varchar2)
100 Colin Tong 999-999-8888 [email protected]
111 John White 888-888-8888 [email protected]
101 Don Wod 416-333-3344 [email protected]
Access and retrieve data
1) Instantiate OO4O Object, OraSession and interface OraDatabase for
connecting to ORACLE.
First of all, create the OraSession Object by using CreateObject, then create the
OraDatabase Object by opening a connection to Oracle, as shown below.
<%
Set OraSession = CreateObject("OracleInProcServer.XOraSession")
Set OraDatabase = OraSession.OpenDatabase("", _
"username/password", Cint(0))
%>
The "username" and "password" are your relational database's user name and
password.
2) Create an OraDynaset Object to execute SQL statement. You may use either
CreateDynaset or DbCreateDynaset to create the recordset.
<%
'execute SQL
Set OraDynaset = OraDatabase.DbCreateDynaset( _
"select * from mytable1", cint(0))
%>
3) Retrieve data and remove created object
<%
Do While(OraDynaset.EOF = FALSE)
Response.write(OraDynaset.Fields("ID"))
Response.write(OraDynaset.Fields("UserName"))
... others ...
... ...
OraDynaset.MoveNext
Loop
'remove OraSession
Set OraSession = Nothing
%>
Edit data record
We are going to use the methods of OraDynaset to implement the editing data purpose.
1) Create OraDynaset object with SQL Statement
<%
'Create the OraDynaset Object for ID= fID record.
Set OraDynaset = OraDatabase.CreateDynaset(_
"select * from MYTABLE1 where ID= "& fID, cint(0))
%>
The fID is the value of the ID field that you want to update or insert.
2) Execute OraDynaset for updating or adding
<%
'update the field of the record(ID=fID) using Edit method.
'or use the AddNew to insert a new record
OraDynaset.Edit
OraDynaset.Fields("Phone").Value = fPhone
OraDynaset.Update
' remove the created session
Set OraSession = Nothing
%>
Delete data record.
Some of you might already know how to delete record(s) from Oracle DB
using OraDynaset if you really understand the methods that we used
(Edit, Update and AddNew) at above sections. Actually, we simply use the method
Delete of OraDynaset for deleting.
<%
'Delete all records that with above condition.
OraDynaset.Delete
%>
Sample codes for search and update data records from Oracle8i
1) Searching
<%
'*************************************************************
'RetrieveRecProc.asp -Retrieve records using OO4O in ASP
'Original Author: Colin Tong
'Modified Date: 9/26/2001
'Note: You are free to use this code, however, please keep
'the original author name.
'*************************************************************
%>
<%
'Declare variables as OLE Objects.
Dim OraSession
Dim OraDatabase
Dim OraDynaset
'Create the OraSession Object.
Set OraSession = CreateObject("OracleInProcServer.XOraSession")
'Create the OraDatabase Object by opening a connection to Oracle.
'Be sure your own username and password to access your Oracle db
Set OraDatabase = OraSession.OpenDatabase("", "user/password", _
Cint(0))
'Create the OraDynaset Object to execute SQL statement
Set OraDynaset = OraDatabase.DbCreateDynaset(_
"select * from mytable1", cint(0))
%>
2) Updating
<%
'**************************************************************
'UpdateRecProc.asp -Update a record using OO4O in ASP
'Original Author: Colin Tong 'Modified Date: 9/26/2001
'Note: You are free to use this code, however please keep
'the original author name.
'**************************************************************
%>
<%
'Declare variables as OLE Objects. Dim OraSession
Dim OraDatabase
Dim OraDynaset
'get field values from submitted form
fID = request.form("ID")
fUserName = request.form("UserName")
fPhone = request.form("Phone")
fEmail = request.form("Email")
'Create the OraSession Object
Set OraSession = CreateObject("OracleInProcServer.XOraSession")
'Create the OraDatabase Object by opening a connection to Oracle
Set OraDatabase = OraSession.OpenDatabase("", "user/password", _
Cint(0))
'Create the OraDynaset Object for ID= fID record
Set OraDynaset = OraDatabase.CreateDynaset(_
"select * from MYTABLE1 where ID= "& fID, cint(0))
'update the field of the record(ID=fID) using Edit method
Do While(OraDynaset.EOF = FALSE)
OraDynaset.Edit
OraDynaset.Fields("UserName").Value = fUserName
OraDynaset.Fields("Phone").Value = fPhone
OraDynaset.Fields("Email").Value = fEmail
OraDynaset.Update
OraDynaset.MoveNext
Loop
%>
Update A Record in MYTABLE1 Table (Oracle) Using oo4oThe record (ID=<%=fID%>) has been updated successfully!<% 'remove OraSession Set OraSession = Nothing %> ------------------------------------------------- Forms Developer and Reports Developer Built-in Package [ole_odref60.pdf] (pg44) /* ** Start Excel, perform some operations on the ** spreadsheet, then close the application. */ DECLARE AppID PLS_INTEGER; BEGIN AppID := DDE.App_Begin('c:\excel\excel.exe emp.xls', DDE.App_Mode_Normal); ... DDE.App_End(AppID); END; ------------------------------------ DDE.App_Focus example /* ** Start Excel, then activate the application window */ DECLARE AppID PLS_INTEGER; BEGIN AppID := DDE.App_Begin('c:\excel\excel.exe', DDE.App_Mode_Maximized); DDE.App_Focus(AppID); ----------------------------- DDE.Execute example /* ** Initiate Excel, then perform a recalculation */ DECLARE ConvID PLS_INTEGER; BEGIN ConvID := DDE.Initiate('EXCEL', 'abc.xls'); DDE.Execute(ConvID, '[calculate.now()]', 1000); END; -------------------------------------------- DDE.Poke example /* ** Open a DDE Conversation with MS Excel on topic ** abc.xls and end data "foo" to cell at row 2, ** column 2 */ DECLARE ConvID PLS_INTEGER; BEGIN ConvID = DDE.Initiate('EXCEL', 'abc.xls'); DDE.Poke(ConvID, 'R2C2', 'foo', DDE.CF_TEXT, 1000); END; ---------------------------------------------- DDE.Request example /* ** Open a DDE Conversation with MS Excel for Windows on ** topic abc.xls then request data from 6 cells ** between row 2, column 2 and row 3, column 4 */ DECLARE ConvID PLS_INTEGER; Buffer VARCHAR2(80); BEGIN ConvID := DDE.Initiate('EXCEL', 'abc.xls'); DDE.Request (ConvID, 'R2C2:R3C4', Buffer, DDE.Cf_Text, 1000); END; ------------------------------------------------------- DDE.Terminate example /* ** Open a DDE Conversation with MS Excel on topic ** abc.xls perform some operations, then terminate ** the conversation */ DECLARE ConvID PLS_INTEGER; BEGIN ConvID := DDE.Initiate('EXCEL', 'abc.xls'); ... DDE.Terminate(ConvID); END; ------------------------------------------------------------------ Make Your ASP Work With An Oracle Database |
||||||