Oracle
Oracle Dummy table name
Case Statement
CASE
WHEN T1.TYPE = 1 THEN 'Type 1'
WHEN T1.TYPE = 2 THEN 'Type 2'
ELSE 'Unknown'
END
Substring
SUBSTR( T1.DESCRIPTION , <starting position> , <length> )
Dates
Current date
Selecting on a date
WHERE dEffectiveOrRenewal BETWEEN '01-Jun-2007' AND '01-Oct-2007'
Date to string
TO_CHAR( <date> , '<format>' )
String to date
TO_DATE( <string> , '<format>' )
Time between two dates
NumToDsInterval( EndDate - StartDate , <unit of measure> )
Where unit of measure can be: 'day' , 'hour' , 'minute' , 'second'
Alias a Column
SELECT T1.TYPE ActionDescription FROM SomeTable
SELECT T1.TYPE "Action Description" FROM SomeTable
Notice the second example's use of double quotes. That is not a typo.
Linking to a Remote Server
CREATE DATABASE LINK <alias>
CONNECT TO <username> IDENTIFIED BY <password>
USING '<TNS Name Entry>';
--
SELECT sName FROM NamesTable@<alias>;
Listing all Objects in the database
SELECT * FROM ALL_TABLES ; -- All Tables
SELECT * FROM USER_VIEWS ; -- All Views
SELECT * FROM USER_DB_LINKS ; -- All DB Links
SELECT * FROM USER_SOURCE ; -- All Packages and Procedures
Returning a recordset from a package/stored proc
Spec:
TYPE returnCursor IS REF CURSOR;
PROCEDURE SP_GET_RECORDSET( parm1 IN VARCHAR2 , parm2 OUT returnCursor );
Body:
PROCEDURE SP_GET_RECORDSET( parm1 IN VARCHAR2 , parm2 OUT returnCursor )
IS
BEGIN
OPEN parm2 FOR
SELECT field1, field2, field3
FROM sometable
END;
Reading an Oracle recordset from VBScript
Dim objConnection
Dim objCommand
Dim objRs
Dim sIsoSelectedMonth
Dim sFolderName
sFolderName = "0000000000000000000010000008417"
Set objConnection = CreateObject( "ADODB.Connection" )
objConnection.Open "DSN=dsnname;Uid=userid;Pwd=password"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "{ call InstanceName.PackageName.SP_GET_RECORDSET( ? ) }"
objCommand.CommandType = 1 ' Text command
objCommand.Parameters( 0 ).Direction = 1 ' Input parm
objCommand.Parameters( 0 ).Value = sFolderName
Set objRs = CreateObject("ADODB.RecordSet")
Set objRs = objCommand.Execute()
Do Until objRs.EOF
Msgbox( objRs( 0 ) & "," & objRs( 1 ) & "," & objRs( 2 ) )
objRs.MoveNext
Loop
Calculating Elapsed Time
elapsed_minutes = round( to_number( logoff_time - logon_time ) * 1440 )
1440 is the number of minutes per day
Streamlined Cursor
PROCEDURE SP_IMPORT_BRKR_CRM_TO_MSTORM( p_FolderId IN VARCHAR , p_PlanId IN VARCHAR )
IS
CURSOR l_BrokerCursor IS
SELECT inst_prod_id InstalledProdId
FROM ps_lx_inst_prod
WHERE lx_as_plan_id = p_PlanId;
BEGIN
FOR l_BrokerRecord IN l_BrokerCursor
LOOP
UPDATE GI_TRACKER_V4_CRM
SET sBroker1 = l_BrokerRecord.InstalledProdId
WHERE eFolderId = p_FolderId;
END LOOP;
END;
This is a steamlined way of doing cursors. There is no need to open, fetch, or close the cursor. Nor is there a need to declare the record brought back from the cursor.
Deleting Duplicate Rows
DELETE FROM myTable
WHERE rowid NOT IN (
SELECT MIN( rowid )
FROM myTable
GROUP BY column1, column2, column3...
)
Limiting Returned Oracle Rows
SELECT * FROM myTable WHERE RowNum < 10
Running a Stored Procedure With a Parameter
set serveroutput on
declare
short date := '2-jan-09';
BEGIN
namesearch_pkg.create_patient_key( '8a00969c0614681801061888acb31652' , short , 'BIRTHDATE' );
END;
DB2 (AS/400 version)
DB2 Dummy Table Name
SELECT 'Hello' FROM sysibm.sysdummy1
Limiting Returned DB2 Rows
SELECT * FROM myTable FETCH FIRST 10 ROWS ONLY
Get Table Descriptions
select DBXFIL , DBXTXT
from QSYS.QADBXFIL
where DBXLIB = 'MCAFILQ3' --<-- change for different schemas
and upper(DBXTXT) like('%AUDIT%')
order by DBXFIL
Get a Table's Columns
select DBIFIL, DBIFLD , DBITXT , DBITYP , DBIFLN , DBINSC
from QSYS.QADBILLB
where DBILIB = 'MCAFILT2' --<-- change for different schemas
and TRIM( DBIFIL ) = 'MCPAUT' --<-- change for different tables
Setting a session's schema
Correlated Delete
DELETE FROM TBL1 T
WHERE NOT EXISTS
(
SELECT *
FROM TBL2
WHERE T2KEY = T.T1KEY
)
Limit the number of results
SELECT * FROM my_table FETCH FIRST 20 ROWS ONLY
Comments (0)
You don't have permission to comment on this page.