giovedì 24 gennaio 2013

Tip: How to get directoy listed in SQL*Plus

Today I needed to launch several .sql scripts from the same directory. I needed to get the script names in a quick way, and possibly being able to copy and paste them.
The solution was using:

SQL> host dir
 El volumen de la unidad C es OS
 El número de serie del volumen es: 6A31-3504

 Directorio de C:\Users\cristina\Documents\

24/01/2013  14:02    <DIR>          .
24/01/2013  14:02    <DIR>          ..
23/01/2013  11:44           291.729 acciones.sql
23/01/2013  18:17            17.826 canales_comunicacion.sql
23/01/2013  18:17           292.138 etapas.sql
24/01/2013  12:14       126.673.515 exp1980.sql
24/01/2013  12:23        91.241.885 exp1985.sql
24/01/2013  12:29        38.205.132 exp1989.sql


SQL*Plus HOST command allows you to execute OS commands such as dir, cd, etc.

mercoledì 23 gennaio 2013

Tip: Oracle SQL Developer Data Modeler Create Sequence for an auto incrementing ID

Recently I have started using this tool from Oracle and I must admit it is brilliant for designing Star Schemas and generating DDL code.
For my project I need to define an auto incrementing ID field for several tables of my model. I obviously needed a sequence for each and I was looking for a way to define it in the Data Modeler.
Oracle Data Modeler allows you to mark any column field as auto-incrementing. Select the table in your model and double click on the column. In the example below, I want the primary key field to be auto-incrementing.


Select the option Auto-Incrementing in the Column Property panel.



If you check the DDL you will see the code for the sequence and the trigger that is fired whenever a new line is inserted into the table.

CREATE SEQUENCE CNE_CNEIDE_SEQ
    NOCACHE
    ORDER ;

CREATE OR REPLACE TRIGGER CNE_CNEIDE_TRG
BEFORE INSERT ON T_CANAL_ENTRADA
FOR EACH ROW
WHEN (NEW.CNEIDE IS NULL)
BEGIN
    SELECT CNE_CNEIDE_SEQ.NEXTVAL INTO :NEW.CNEIDE FROM DUAL;
END;
/


If you don't need the trigger, just untick the option in the Auto Increment panel.