Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Monday, October 2, 2017

Exclude Week Ends in the report.



---->>> Below query will exclude Saturday and Sunday exclusion 
                 
SELECT (TRUNC(SYSDATE)-4) DAY1, (TRUNC(SYSDATE)-1) DAY2,
((TRUNC(SYSDATE)-1) - (TRUNC(SYSDATE)-4))-2*FLOOR(((TRUNC(SYSDATE)-1) - (TRUNC(SYSDATE)-4))/7)-DECODE(SIGN(TO_CHAR((TRUNC(SYSDATE)-1),'D')-
        TO_CHAR((TRUNC(SYSDATE)-4),'D')),-1,2,0)+DECODE(TO_CHAR((TRUNC(SYSDATE)-4),'D'),7,1,0)-
        DECODE(TO_CHAR((TRUNC(SYSDATE)-1),'D'),7,1,0) as Working_Days
FROM DUAL


------>>> Below query will exclude Friday and Saturday exclusion

SELECT (TRUNC(SYSDATE)-8) DAY1, (TRUNC(SYSDATE)-1) DAY2,
((TRUNC(SYSDATE)-1) - (TRUNC(SYSDATE)-8))-2*FLOOR(((TRUNC(SYSDATE)-1) - (TRUNC(SYSDATE)-8))/7)-DECODE(SIGN(TO_CHAR((TRUNC(SYSDATE)-1),'D')-
        TO_CHAR((TRUNC(SYSDATE)-8),'D')),-1,2,0)+DECODE(TO_CHAR((TRUNC(SYSDATE)-8),'D'),7,2,0)-
        DECODE(TO_CHAR((TRUNC(SYSDATE)-1),'D'),7,2,0) as Working_Days

FROM DUAL

Friday, January 11, 2013

Oracle Error Messages


For Oracle Error messages Click on Below Link

http://www.techonthenet.com/oracle/errors/index.php

Wednesday, January 9, 2013

Synonyms


synonym is an alternative name for objects such as tables, views, sequences, stored procedures, and other database objects.

Creating or replacing a synonym

The syntax for creating a synonym is:

create [or replace] [public] synonym [schema .] synonym_name
  for [schema .] object_name [@ dblink];

The or replace phrase allows you to recreate the synonym (if it already exists) without having to issue a DROP synonym command.
The public phrase means that the synonym is a public synonym and is accessible to all users. Remember though that the user must first have the appropriate privileges to the object to use the synonym.
The schema phrase is the appropriate schema. If this phrase is omitted, Oracle assumes that you are referring to your own schema.
The object_name phrase is the name of the object for which you are creating the synonym. It can be one of the following:
  • table
  • view
  • sequence
  • stored procedure
  • function
  • package
  • materialized view
  • java class schema object
  • user-defined object
  • synonym

For Example

create public synonym suppliers
for app.suppliers;

This first example demonstrates how to create a synonym called suppliers. Now, users of other schemas can reference the table called suppliers without having to prefix the table name with the schema named app. For example:

select * from suppliers;

If this synonym already existed and you wanted to redefine it, you could always use the or replace phrase as follows:

create or replace public synonym suppliers
for app.suppliers;

Dropping a synonym

It is also possible to drop a synonym. The syntax for dropping a synonym is:

drop [public] synonym [schema .] synonym_name [force];

The public phrase allows you to drop a public synonym. If you have specified public, then you don't specify a schema.
The force phrase will force Oracle to drop the synonym even if it has dependencies. It is probably not a good idea to use the force phrase as it can cause invalidation of Oracle objects.

For Example

drop public synonym suppliers;

This drop statement would drop the synonym called suppliers that we defined earlier.

Change a user's password in Oracle


Question: How do I change the password for a user in Oracle?
Answer: To change a user's password in Oracle, you need to execute the alter user command.
The syntax for changing a password is:

alter user user_name identified by new_password;

user_name is the user whose password you wish to change.
new_password is the new password to assign.

For Example

If you wanted to reset the password for a user named smithj, and you wanted to set the new password to autumn, you would run the following command:

alter user smithj identified by autumn;

Create Roles in Oracle


role is a set or group of privileges that can be granted to users or another role. This is a great way for database administrators to save time and effort.

Creating a Role

To create a role, you must have CREATE ROLE system privileges.
The syntax for creating a role is:

CREATE ROLE role_name
[ NOT IDENTIFIED | 
IDENTIFIED {BY password | USING [schema.] package | EXTERNALLY | GLOBALLY } ;

The role_name phrase is the name of the new role that you are creating. This is how you will refer to the grouping of privileges.
The NOT IDENTIFIED phrase means that the role is immediately enabled. No password is required to enable the role.
The IDENTIFIED phrase means that a user must be authorized by a specified method before the role is enabled.
The BY password phrase means that a user must supply a password to enable the role.
The USING package phrase means that you are creating an application role - a role that is enabled only by applications using an authorized package.
The EXTERNALLY phrase means that a user must be authorized by an external service to enable the role. An external service can be an operating system or third-party service.
The GLOBALLY phrase means that a user must be authorized by the enterprise directory service to enable the role.

Note

If both the NOT IDENTIFIED and IDENTIFIED phrases are omitted in the CREATE ROLE statement, the role will be created as a NOT IDENTIFIED role.

For Example

CREATE ROLE test_role;

This first example creates a role called test_role.

CREATE ROLE test_role
IDENTIFIED BY test123;

This second example creates the same role called test_role, but now it is password protected with the password of test123.

Grant Privileges (on Tables) to Roles

You can grant roles various privileges to tables. These privileges can be any combination of select, insert, update, delete, references, alter, and index. Below is an explanation of what each privilege means.

PrivilegeDescription
SelectAbility to query the table with a select statement.
InsertAbility to add new rows to the table with the insert statement.
UpdateAbility to update rows in the table with the update statement.
DeleteAbility to delete rows from the table with the delete statement.
ReferencesAbility to create a constraint that refers to the table.
AlterAbility to change the table definition with the alter table statement.
IndexAbility to create an index on the table with the create index statement.

The syntax for granting privileges on a table is:

grant privileges on object to role_name

For example, if you wanted to grant select, insert, update, and delete privileges on a table called suppliers to a role named test_role, you would execute the following statement:

grant select, insert, update, delete on suppliers to test_role;

You can also use the all keyword to indicate that you wish all permissions to be granted. For example:

grant all on suppliers to test_role;

Revoke Privileges (on Tables) to Roles

Once you have granted privileges, you may need to revoke some or all of these privileges. To do this, you can execute a revoke command. You can revoke any combination of select, insert, update, delete, references, alter, and index.
The syntax for revoking privileges on a table is:

revoke privileges on object from role_name;

For example, if you wanted to revoke delete privileges on a table called suppliers from a role named test_role, you would execute the following statement:

revoke delete on suppliers from test_role;

If you wanted to revoke all privileges on a table, you could use the all keyword. For example:

revoke all on suppliers from test_role;

Grant Privileges (on Functions/Procedures) to Roles

When dealing with functions and procedures, you can grant roles the ability to execute these functions and procedures. The Execute privilege is explained below:

PrivilegeDescription
ExecuteAbility to compile the function/procedure.
Ability to execute the function/procedure directly.

The syntax for granting execute privileges on a function/procedure is:

grant execute on object to role_name;

For example, if you had a function called Find_Value and you wanted to grant execute access to the role named test_role, you would execute the following statement:

grant execute on Find_Value to test_role;

Revoke Privileges (on Functions/Procedures) to Roles

Once you have granted execute privileges on a function or procedure, you may need to revoke these privileges from a role. To do this, you can execute a revoke command.
The syntax for the revoking privileges on a function or procedure is:

revoke execute on object from role_name;

If you wanted to revoke execute privileges on a function called Find_Value from a role named test_role, you would execute the following statement:

revoke execute on Find_Value from test_role;

Granting the Role to a User

Now, that you've created the role and assigned the privileges to the role, you'll need to grant the role to specific users.
The syntax to grant a role to a user is:

GRANT role_name TO user_name;

For Example

GRANT test_role to smithj;

This example would grant the role called test_role to the user named smithj.

The SET ROLE statement

The SET ROLE statement allows you to enable or disable a role for a current session.
When a user logs into Oracle, all default roles are enabled, but non-default roles must be enabled with the SET ROLE statement.
The syntax for the SET ROLE statement is:

SET ROLE
( role_name [ IDENTIFIED BY password ] | ALL [EXCEPT role1, role2, ... ] | NONE );

The role_name phrase is the name of the role that you wish to enable.
The IDENTIFIED BY password phrase is the password for the role to enable it. If the role does not have a password, this phrase can be omitted.
The ALL phrase means that all roles should be enabled for this current session, except those listed in the EXCEPT phrase.
The NONE phrase disables all roles for the current session. (including all default roles)

For Example

SET ROLE test_role IDENTIFIED BY test123;

This example would enable the role called test_role with a password of test123.

Setting a role as DEFAULT Role

A default role means that the role is always enabled for the current session at logon. It is not necessary to issue the SET ROLE statement. To set a role as a DEFAULT role, you need to issue the ALTER USER statement.
The syntax for setting a role as a DEFAULT role is:

ALTER USER user_name
DEFAULT ROLE
( role_name | ALL [EXCEPT role1, role2, ... ] | NONE );

The user_name phrase is the name of the user whose role you are setting as DEFAULT.
The role_name phrase is the name of the role that you wish to set as DEFAULT.
The ALL phrase means that all roles should be enabled as DEFAULT, except those listed in the EXCEPT phrase.
The NONE phrase disables all roles as DEFAULT.

For Example

ALTER USER smithj
DEFAULT ROLE
test_role;
This example would set the role called test_role as a DEFAULT role for the user named smithj.

ALTER USER smithj
DEFAULT ROLE
ALL;

This example would set all roles assigned to smithj as DEFAULT.

ALTER USER smithj
DEFAULT ROLE
ALL EXCEPT test_role;

This example would set all roles assigned to smithj as DEFAULT, except for the role called test_role.

Dropping a Role

It is also possible to drop a role. The syntax for dropping a role is:

DROP ROLE role_name;

For Example

DROP ROLE test_role;

This drop statement would drop the role called test_role that we defined earlier.

Grant/Revoke Privileges


You can grant users various privileges to tables. These privileges can be any combination of select, insert, update, delete, references, alter, and index. Below is an explanation of what each privilege means.

PrivilegeDescription
SelectAbility to query the table with a select statement.
InsertAbility to add new rows to the table with the insert statement.
UpdateAbility to update rows in the table with the update statement.
DeleteAbility to delete rows from the table with the delete statement.
ReferencesAbility to create a constraint that refers to the table.
AlterAbility to change the table definition with the alter table statement.
IndexAbility to create an index on the table with the create index statement.

The syntax for granting privileges on a table is:

grant privileges on object to user;

For example, if you wanted to grant select, insert, update, and delete privileges on a table called suppliers to a user name smithj, you would execute the following statement:

grant select, insert, update, delete on suppliers to smithj;

You can also use the all keyword to indicate that you wish all permissions to be granted. For example:

grant all on suppliers to smithj;

If you wanted to grant select access on your table to all users, you could grant the privileges to the public keyword. For example:

grant select on suppliers to public;

Revoke Privileges on Tables

Once you have granted privileges, you may need to revoke some or all of these privileges. To do this, you can execute a revoke command. You can revoke any combination of select, insert, update, delete, references, alter, and index.
The syntax for revoking privileges on a table is:

revoke privileges on object from user;

For example, if you wanted to revoke delete privileges on a table called suppliers from a user named anderson, you would execute the following statement:

revoke delete on suppliers from anderson;

If you wanted to revoke all privileges on a table, you could use the all keyword. For example:

revoke all on suppliers from anderson;

If you had granted privileges to public (all users) and you wanted to revoke these privileges, you could execute the following statement:

revoke all on suppliers from public;

Grant Privileges on Functions/Procedures

When dealing with functions and procedures, you can grant users the ability to execute these functions and procedures. The Execute privilege is explained below:

PrivilegeDescription
ExecuteAbility to compile the function/procedure.
Ability to execute the function/procedure directly.

The syntax for granting execute privileges on a function/procedure is:

grant execute on object to user;

For example, if you had a function called Find_Value and you wanted to grant execute access to the user named smithj, you would execute the following statement:

grant execute on Find_Value to smithj;

If you wanted to grant all users the ability to execute this function, you would execute the following:

grant execute on Find_Value to public;

Revoke Privileges on Functions/Procedures

Once you have granted execute privileges on a function or procedure, you may need to revoke these privileges from a user. To do this, you can execute a revoke command.
The syntax for the revoking privileges on a function or procedure is:

revoke execute on object from user;

If you wanted to revoke execute privileges on a function called Find_Value from a user named anderson, you would execute the following statement:

revoke execute on Find_Value from anderson;

If you had granted privileges to public (all users) and you wanted to revoke these privileges, you could execute the following statement:

revoke execute on Find_Value from public;

Oracle System Tables


Below is an alphabetical listing of the Oracle system tables that are commonly used.
System TableDescription
ALL_ARGUMENTSArguments in object accessible to the user
ALL_CATALOGAll tables, views, synonyms, sequences accessible to the user
ALL_COL_COMMENTSComments on columns of accessible tables and views
ALL_CONSTRAINTSConstraint definitions on accessible tables
ALL_CONS_COLUMNSInformation about accessible columns in constraint definitions
ALL_DB_LINKSDatabase links accessible to the user
ALL_ERRORSCurrent errors on stored objects that user is allowed to create
ALL_INDEXESDescriptions of indexes on tables accessible to the user
ALL_IND_COLUMNSCOLUMNs comprising INDEXes on accessible TABLES
ALL_LOBSDescription of LOBs contained in tables accessible to the user
ALL_OBJECTSObjects accessible to the user
ALL_OBJECT_TABLESDescription of all object tables accessible to the user
ALL_SEQUENCESDescription of SEQUENCEs accessible to the user
ALL_SNAPSHOTSSnapshots the user can access
ALL_SOURCECurrent source on stored objects that user is allowed to create
ALL_SYNONYMSAll synonyms accessible to the user
ALL_TABLESDescription of relational tables accessible to the user
ALL_TAB_COLUMNSColumns of user's tables, views and clusters
ALL_TAB_COL_STATISTICSColumns of user's tables, views and clusters
ALL_TAB_COMMENTSComments on tables and views accessible to the user
ALL_TRIGGERSTriggers accessible to the current user
ALL_TRIGGER_COLSColumn usage in user's triggers or in triggers on user's tables
ALL_TYPESDescription of types accessible to the user
ALL_UPDATABLE_COLUMNSDescription of all updatable columns
ALL_USERSInformation about all users of the database
ALL_VIEWSDescription of views accessible to the user
DATABASE_COMPATIBLE_LEVELDatabase compatible parameter set via init.ora
DBA_DB_LINKSAll database links in the database
DBA_ERRORSCurrent errors on all stored objects in the database
DBA_OBJECTSAll objects in the database
DBA_ROLESAll Roles which exist in the database
DBA_ROLE_PRIVSRoles granted to users and roles
DBA_SOURCESource of all stored objects in the database
DBA_TABLESPACESDescription of all tablespaces
DBA_TAB_PRIVSAll grants on objects in the database
DBA_TRIGGERSAll triggers in the database
DBA_TS_QUOTASTablespace quotas for all users
DBA_USERSInformation about all users of the database
DBA_VIEWSDescription of all views in the database
DICTIONARYDescription of data dictionary tables and views
DICT_COLUMNSDescription of columns in data dictionary tables and views
GLOBAL_NAMEglobal database name
NLS_DATABASE_PARAMETERSPermanent NLS parameters of the database
NLS_INSTANCE_PARAMETERSNLS parameters of the instance
NLS_SESSION_PARAMETERSNLS parameters of the user session
PRODUCT_COMPONENT_VERSIONversion and status information for component products
ROLE_TAB_PRIVSTable privileges granted to roles
SESSION_PRIVSPrivileges which the user currently has set
SESSION_ROLESRoles which the user currently has enabled.
SYSTEM_PRIVILEGE_MAPDescription table for privilege type codes. Maps privilege type numbers to type names
TABLE_PRIVILEGESGrants on objects for which the user is the grantor, grantee, owner, or an enabled role or PUBLIC is the grantee
TABLE_PRIVILEGE_MAPDescription table for privilege (auditing option) type codes. Maps privilege (auditing option) type numbers to type names

Transactions

Commit


The syntax for the COMMIT statement is:
COMMIT [WORK] [COMMENT text];
The Commit statement commits all changes for the current session. Once a commit is issued, other users will be able to see your changes.


 Rollback



The syntax for the ROLLBACK statement is:
ROLLBACK [WORK] [TO [SAVEPOINT] savepoint_name];
The Rollback statement undoes all changes for the current session up to the savepoint specified. If no savepoint is specified, then all changes are undone.



Set Transaction



There are three transaction control functions. These are:
  1. SET TRANSACTION READ ONLY;
  2. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
  3. SET TRANSACTION USE ROLLBACK SEGMENT name;


Lock Table




The syntax for a Lock table is:
LOCK TABLE tables IN lock_mode MODE [NOWAIT];
Tables is a comma-delimited list of tables.
Lock_mode is one of:
  • ROW SHARE
  • ROW EXCLUSIVE
  • SHARE UPDATE
  • SHARE
  • SHARE ROW EXCLUSIVE
  • EXCLUSIVE
NoWait specifies that the database should not wait for a lock to be released.

Sequences (Autonumber)


In Oracle, you can create an autonumber field by using sequences. A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.
The syntax for a sequence is:
CREATE SEQUENCE sequence_name
  MINVALUE value
  MAXVALUE value
  START WITH value
  INCREMENT BY value
  CACHE value;
For example:
CREATE SEQUENCE supplier_seq
  MINVALUE 1
  MAXVALUE 999999999999999999999999999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;
This would create a sequence object called supplier_seq. The first sequence number that it would use is 1 and each subsequent number would increment by 1 (ie: 2,3,4,...}. It will cache up to 20 values for performance.
If you omit the MAXVALUE option, your sequence will automatically default to:
MAXVALUE 999999999999999999999999999
So you can simplify your CREATE SEQUENCE command as follows:
CREATE SEQUENCE supplier_seq
  MINVALUE 1
  START WITH 1
  INCREMENT BY 1
  CACHE 20;
Now that you've created a sequence object to simulate an autonumber field, we'll cover how to retrieve a value from this sequence object. To retrieve the next value in the sequence order, you need to use nextval.
For example:
supplier_seq.nextval
This would retrieve the next value from supplier_seq. The nextval statement needs to be used in an SQL statement. For example:
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(supplier_seq.nextval, 'Kraft Foods');
This insert statement would insert a new record into the suppliers table. The supplier_id field would be assigned the next number from the supplier_seq sequence. The supplier_name field would be set to Kraft Foods.

Frequently Asked Questions


One common question about sequences is:
Question: While creating a sequence, what does cache and nocache options mean? For example, you could create a sequence with a cache of 20 as follows:
CREATE SEQUENCE supplier_seq
  MINVALUE 1
  START WITH 1
  INCREMENT BY 1
  CACHE 20;
Or you could create the same sequence with the nocache option:
CREATE SEQUENCE supplier_seq
  MINVALUE 1
  START WITH 1
  INCREMENT BY 1
  NOCACHE;
Answer: With respect to a sequence, the cache option specifies how many sequence values will be stored in memory for faster access.
The downside of creating a sequence with a cache is that if a system failure occurs, all cached sequence values that have not be used, will be "lost". This results in a "gap" in the assigned sequence values. When the system comes back up, Oracle will cache new numbers from where it left off in the sequence, ignoring the so called "lost" sequence values.
Note: To recover the lost sequence values, you can always execute an ALTER SEQUENCE command to reset the counter to the correct value.
Nocache means that none of the sequence values are stored in memory. This option may sacrifice some performance, however, you should not encounter a gap in the assigned sequence values.

Question: How do we set the LASTVALUE value in an Oracle Sequence?
Answer: You can change the LASTVALUE for an Oracle sequence, by executing an ALTER SEQUENCE command.
For example, if the last value used by the Oracle sequence was 100 and you would like to reset the sequence to serve 225 as the next value. You would execute the following commands.
alter sequence seq_name
increment by 124;

select seq_name.nextval from dual;

alter sequence seq_name
increment by 1;
Now, the next value to be served by the sequence will be 225.