By admin on December 14, 2011
Here is the syntax to create a sequence. This can be used to create primary keys or to generate a number sequence.
The below sequence will start with 1 and increment by 1. There is no MAXVALUE defined so the sequence’s max value will default to 999999999999999999999999999. The CACHE is set to 50 so it will cache up to 50 values to help with performance.
CREATE SEQUENCE SOME_SEQUENCE
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 50;
To use the above sequence, you can use the <sequence name>.nextval in a statement.
Example:
insert into temp (some_number) values (some_sequence.nextval);
Posted in Uncategorized | Tagged CACHE, MAXVALUE, MINVALUE |
By admin on December 14, 2011
The following function will add a new column called SOME_NEW_COLUMN with a NUMBER datatype to all tables in the current schema. This function does not take any parameters.
CREATE OR REPLACE FUNCTION ADD_COLUMN_FUNCTION
RETURN INTEGER
IS
temp_String varchar2(200);
temp_Table_Name varchar2(30);
CURSOR cursor_User_Tables IS
SELECT TABLE_NAME
FROM USER_TABLES;
BEGIN
FOR rec IN cursor_User_Tables
LOOP
temp_Table_Name := rec.TABLE_NAME;
temp_String := ‘ALTER TABLE ‘ || temp_Table_Name || ‘ ADD SOME_NEW_COLUMN NUMBER’;
EXECUTE IMMEDIATE temp_String;
END LOOP;
RETURN 1;
END;
/
You can execute the above function by running the following.
DECLARE
X NUMBER;
BEGIN
X := ADD_COLUMN_FUNCTION;
END;
/
If you wanted the function to add the column to a single table, your function would look like this.
CREATE OR REPLACE FUNCTION ADD_COLUMN_FUNCTION_2 (var_Table_Name VARCHAR2)
RETURN INTEGER
IS
temp_String varchar2(200);
BEGIN
temp_String := ‘ALTER TABLE ‘ || var_Table_Name || ‘ ADD SOME_NEW_COLUMN_2 NUMBER’;
EXECUTE IMMEDIATE temp_String;
RETURN 1;
END;
/
You can execute the above function by running the following where TEMP2 is the table name we are adding the column to.
DECLARE
X NUMBER;
BEGIN
X := ADD_COLUMN_FUNCTION_2(‘TEMP2′);
END;
/
Posted in Uncategorized | Tagged BEGIN, DECLARE, LOOP, RETURN |
By admin on December 14, 2011
The following stored procedure will add a new column called SOME_NEW_COLUMN with a NUMBER datatype to all tables in the current schema. This stored procedure does not take any parameters.
CREATE OR REPLACE PROCEDURE ADD_COLUMN_STORED_PROCEDURE IS
temp_String varchar2(200);
temp_Table_Name varchar2(30);
CURSOR cursor_User_Tables IS
SELECT TABLE_NAME
FROM USER_TABLES;
BEGIN
FOR rec IN cursor_User_Tables
LOOP
temp_Table_Name := rec.TABLE_NAME;
temp_String := ‘ALTER TABLE ‘ || temp_Table_Name || ‘ ADD SOME_NEW_COLUMN NUMBER’;
EXECUTE IMMEDIATE temp_String;
END LOOP;
END;
/
If you wanted the stored procedure to add the column to a single table, your stored procedure would look like this.
CREATE OR REPLACE PROCEDURE ADD_COLUMN_STORED_PROCEDURE_2 (var_Table_Name VARCHAR2) IS
temp_String varchar2(200);
BEGIN
temp_String := ‘ALTER TABLE ‘ || var_Table_Name || ‘ ADD SOME_NEW_COLUMN_2 NUMBER’;
EXECUTE IMMEDIATE temp_String;
END;
/
Posted in Uncategorized | Tagged BEGIN, CURSOR, LOOP, NUMBER |
By admin on November 17, 2011
Here is a simple way to upgrade all sequence generator datatypes so that version 8.1 Informatica mappings will work with version 8.6.
UPDATE OPB_WIDGET_FIELD
SET wgt_prec = 19, wgt_datatype = -5
WHERE field_name IN ( ‘CURRVAL’,'NEXTVAL’)
AND wgt_datatype = 4;
Posted in Informatica | Tagged SET |
By admin on November 17, 2011
Here is the statement to add a column to a table.
ALTER TABLE SAMPLE_TABLE ADD ANOTHER_DATE DATE;
Here is the statement to add multiple columns to a table.
ALTER TABLE SAMPLE_TABLE ADD (
ANOTHER_STRING VARCHAR2(100 CHAR),
ANOTHER_NUMBER NUMBER(32,12)
);
Posted in Oracle |
By admin on November 12, 2011
Here are a few examples of how to analyze a table and index.
ANALYZE TABLE SAMPLE_TABLE COMPUTE STATISTICS; –completely computes statistics
ANALYZE INDEX SAMPLE_TABLE_PK COMPUTE STATISTICS; –completely computes statistics
ANALYZE TABLE SAMPLE_TABLE ESTIMATE STATISTICS SAMPLE 100 ROWS; –estimates statistics based on the set number of rows
ANALYZE TABLE SAMPLE_TABLE ESTIMATE STATISTICS SAMPLE 15 PERCENT; –estimates statistics based on the set percentage of rows
Posted in Oracle | Tagged PERCENT |
By admin on November 12, 2011
Here is an example of how to truncate a table in Oracle. To do a truncate, you must be connected to the schema the table is in.
TRUNCATE TABLE SAMPLE_TABLE;
Posted in Oracle |
By admin on November 11, 2011
Here is an example of how to delete data from a table. To do a delete, you must do a commit for the delete to be permanent. You can do a delete in any schema that has access to the necessary table.
DELETE FROM SAMPLE_TABLE;
COMMIT;
Here is an example of how to delete data from a table by using a filter.
DELETE FROM SAMPLE_TABLE WHERE SOME_NUMBER = 20;
COMMIT;
Posted in Oracle |
By admin on November 11, 2011
Here is an example of how to add a datafile in Oracle. The datafile size is 2 GB. You will need to replace the ‘/u01/oracle/’ path with a valid directory.
ALTER TABLESPACE ”SAMPLEDATAFILE” ADD DATAFILE ‘/u01/oracle/some_folder.dbf’ size 2048M;
Posted in Oracle | Tagged GB |
By admin on November 11, 2011
Here is how to create a backup of an existing table (with data).
CREATE TABLE SAMPLE_TABLE_COPY AS SELECT * FROM SAMPLE_TABLE;
Here is how to create a backup of an existing table (without data).
CREATE TABLE SAMPLE_TABLE_COPY AS SELECT * FROM SAMPLE_TABLE WHERE ROWNUM < 1;
Posted in Oracle |