0% found this document useful (0 votes)
10 views

Unit 4 Notes - Bcom

My notes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit 4 Notes - Bcom

My notes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Built-in Functions in SQL

Numeric Functions
Input Value
Function
Argument Returned
Absolute
value of m,
The
absolute
ABS
m = value value of a
(m)
number is
the number
without its
sign.
Remainder of
MOD m = value, n =
m divided by
( m, n ) divisor
n
POWER m = value, n = m raised to
( m, n ) exponent the nth power
m = value, n =
ROUND m rounded to
number of
( m [, the nth
decimal places,
n]) decimal place
default 0
m = value, n =
TRUNC m truncated
number of
( m [, to the nth
decimal places,
n]) decimal place
default 0
n = angle
SIN ( n ) expressed in sine (n)
radians
n = angle
COS ( n ) expressed in cosine (n)
radians
n = angle
TAN
expressed in tan (n)
(n)
radians
SQRT ( n n = value positive
) square root of
n
Here are some examples of the use of some of
these numeric functions:
1. select round (83.28749, 2) from marks;
2. select sqrt (3.67) from marks;
3. select power (2.512, 5) from marks;
String Functions
Input
Function Value Returned
Argument
First letter of each
s= word is changed
INITCAP
character to uppercase and
(s)
string all other letters
are in lower case.
s= All letters are
LOWER (
character changed to
s)
string lowercase.
UPPER s= All letters are
(s) character changed to
string uppercase.
s1 and s2 Concatenation of
CONCAT are s1 and s2.
( s1, s2 ) character Equivalent to s1 ||
strings s2
s= Returns the
LENGTH
character number of
(s)
string characters in s.
Here are some examples of the use of String
functions:
1. select concat ('Alan', 'Turing') as
"NAME" from customers;
2. select initcap (‘now is the time for all
good men’) from customers;

Group Functions
Input
Function Value Returned
Argument
AVG
col =
( [ DISTINC The average value
column
T | ALL ] of that column
name
col )
Number of rows
COUNT returned including
none
(*) duplicates and
NULLs
COUNT Number of rows
col =
( [ DISTINC where the value of
column
T | ALL ] the column is not
name
col ) NULL
MAX
col =
( [ DISTINC Maximum value in
column
T | ALL ] the column
name
col )
MIN col = Minimum value in
( [ DISTINC column
T | ALL ]
name the column
col )
SUM
col =
( [ DISTINC Sum of the values
column
T | ALL ] in the column
name
col )

Date and Time Functions


Input Value
Function Argume Returne
nt d
d = date,
n= Date d
ADD_MONTHS ( d,
number plus n
n)
of months
months
LAST_DAY ( d ) d = date Date of
the last
day of
the
month
containin
gd
Number
of
months
MONTHS_BETWE d and e
by which
EN ( d, e ) are dates
e
precedes
d
Date of
d = date,
the first
NEXT_DAY ( d, day = day
day of
day ) of the
the week
week
after d
Current
SYSDATE none date and
time
Date Formats
Forma Descript Range of
t ion Values
Day of
DD the 1 - 31
month
Name of
the day
SUN, ...,
DY in 3
SAT
uppercas
e letters
DAY Complet SUNDA
e name Y, ...,
of the SATURD
day in AY
uppercas
e,
padded
to 9
characte
rs
Number
MM of the 1 - 12
month
Name of
the
month in
uppercas JANUAR
MONT e padded Y, ...,
H to a DECEM
length of BER
9
characte
rs
Two or
YY or four 71 or
YYYY digit 1971
year
HH:MI Hours : 10:28:53
:SS Minutes
:
Seconds
Here are some examples of the use of the
Date functions:
1. select to_char ( sysdate, 'MON DD,
YYYY' ) from dual;
2. select to_char ( sysdate, 'HH12:MI:SS
AM' ) from dual;
3. select to_char ( new_time ( sysdate,
'CDT', 'GMT'), 'HH24:MI' ) from dual;
4. select greatest ( to_date ( 'JAN 19, 2000',
'MON DD, YYYY' ), to_date ( 'SEP 27,
1999', 'MON DD, YYYY' ), to_date ( '13-
Mar-2009', 'DD-Mon-YYYY' ) ) from
dual;
5. select next_day ( sysdate, 'FRIDAY' )
from dual;
SQL - Sub Queries
A Subquery or Inner query or a Nested query
is a query within another SQL query and
embedded within clauses, most commonly in
the WHERE clause. It is used to return data
from a table, and this data will be used in the
main query as a condition to further restrict
the data to be retrieved. Subqueries can be
used with the SELECT, INSERT, UPDATE,
and DELETE statements along with the
operators like =, <, >, >=, <=, IN,
BETWEEN, etc.
There are a few rules that subqueries must
follow −
 Subqueries must be enclosed within
parentheses.
 A subquery can have only one column in
the SELECT clause, unless multiple columns
are in the main query for the subquery to
compare its selected columns.
 An ORDER BY command cannot be used
in a subquery, although the main query can
use an ORDER BY. The GROUP BY
command can be used to perform the same
function as the ORDER BY in a subquery.
 Subqueries that return more than one row
can only be used with multiple value
operators such as the IN operator.
 The SELECT list cannot include any
references to values that evaluate to a BLOB,
ARRAY, CLOB, or NCLOB.
 A subquery cannot be immediately
enclosed in a set function.
 The BETWEEN operator cannot be used
with a subquery. However, the BETWEEN
operator can be used within the subquery.
Subqueries with the SELECT Statement
Syntax:
SELECT column_name [, column_name ]
FROM table1 [, table2 ] WHERE
column_name OPERATOR (SELECT
column_name [, column_name ] FROM
table1 [, table2 ] [WHERE])
Example
Consider the CUSTOMERS table having the
following records –
ID NAME AGE ADDRESS SALARY
1 Ramesh 35 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
Now, let us check the following subquery
with a SELECT statement.
SQL> SELECT * FROM CUSTOMERS WHERE ID
IN (SELECT ID FROM CUSTOMERS WHERE
SALARY > 4500);
SELECT ID FROM CUSTOMERS WHERE
SALARY > 4500

ID
4
5
7
SELECT * FROM CUSTOMERS WHERE ID
IN…….

Output:
ID NAME AGE ADDRESS SALARY
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
7 Muffy 24 Indore 10000.00
Subqueries with the INSERT Statement
Subqueries also can be used with INSERT
statements. The INSERT statement uses the
data returned from the subquery to insert into
another table. The selected data in the
subquery can be modified with any of the
character, date or number functions.
Syntax
INSERT INTO table_name [ (column1 [, column2
]) ] SELECT [ *|column1 [, column2 ] FROM
table1 [, table2 ] [ WHERE VALUE OPERATOR
]
Example
Consider a table CUSTOMERS_BKP with
similar structure as CUSTOMERS table. Now
to copy the complete CUSTOMERS table
into the CUSTOMERS_BKP table, we can
use the following syntax.
SQL> INSERT INTO CUSTOMERS_BKP
SELECT * FROM CUSTOMERS WHERE ID IN
(SELECT ID FROM CUSTOMERS);
Subqueries with the UPDATE
Statement
The subquery can be used in conjunction with
the UPDATE statement. Either single or
multiple columns in a table can be updated
when using a sub query with the UPDATE
statement.
Syntax
UPDATE table SET column_name = new_value [
WHERE OPERATOR [ VALUE ] (SELECT
COLUMN_NAME FROM TABLE_NAME)
[ WHERE) ];
Example
Assuming, we have CUSTOMERS_BKP
table available which is backup of
CUSTOMERS table. The following example
updates SALARY by 0.25 times in the
CUSTOMERS table for all the customers
whose AGE is greater than or equal to 27.
SQL> UPDATE CUSTOMERS SET
SALARY = SALARY * 0.25 WHERE AGE
IN (SELECT AGE FROM
CUSTOMERS_BKP WHERE AGE >= 27);
This would impact two rows and finally
CUSTOMERS table would have the
following records.
ID NAME AGE ADDRESS SALARY
1 Ramesh 35 Ahmedabad 125.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 2125.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00

Subqueries with the DELETE


Statement
The subquery can be used in conjunction with
the DELETE statement like with any other
statements mentioned above.
Syntax
DELETE FROM TABLE_NAME [ WHERE
OPERATOR [ VALUE ] (SELECT
COLUMN_NAME FROM TABLE_NAME)
[ WHERE) ];
Example
Assuming, we have a CUSTOMERS_BKP
table available which is a backup of the
CUSTOMERS table. The following example
deletes the records from the CUSTOMERS
table for all the customers whose AGE is
greater than or equal to 27.
SQL> DELETE FROM CUSTOMERS
WHERE AGE IN (SELECT AGE FROM
CUSTOMERS_BKP WHERE AGE >= 27);
This would impact two rows and finally the
CUSTOMERS table would have the
following records.
ID NAME AGE ADDRESS SALARY
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
BUILT-IN FUNCTIONS WITH
SUBQUERIES
We can use subqueries in conditional logic (in
conjunction with WHERE, JOIN/ON,
or CASE). The following query returns all of
the entries from the earliest date in the
dataset.
Syntax:
SELECT * FROM
tutorial.sf_crime_incidents_2014_01 WHERE
Date = (SELECT MIN(date)FROM
tutorial.sf_crime_incidents_2014_01);
The above query works because the result of
the subquery is only one cell. Most
conditional logic will work with subqueries
containing one-cell results.

******
SQL JOIN
A JOIN clause is used to combine rows from two
or more tables, based on a related column between
them.
Basic SQL JOIN types
SQL Server supports many kinds of different
joins including (4 MAIN TYPES) INNER
JOIN, SELF JOIN, CROSS JOIN,
and OUTER JOIN. In fact, each join type
defines the way two tables are related in a
query. OUTER JOINS can further be divided
into (3 SUBPOINTS) LEFT OUTER
JOINS, RIGHT OUTER JOINS,
and FULL OUTER JOINS.
 SQL INNER JOIN creates a result table
by combining rows that have matching values
in two or more tables.
 SQL SELF JOIN joins the table to itself
and allows comparing rows within the same
table.
 SQL CROSS JOIN creates a result table
containing paired combination of each row of
the first table with each row of the second
table.
 SQL LEFT OUTER JOIN includes in a
result table unmatched rows from the table
that is specified before the LEFT OUTER
JOIN clause.
 SQL RIGHT OUTER JOIN creates a
result table and includes into it all the records
from the right table and only matching rows
from the left table.
 FULL (OUTER) JOIN returns all
records when there is a match in either left or
right table.
Let's look at a selection from the "Orders"
table:
OrderID CustomerID OrderPlaced
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Then, look at a selection from the "Customers"
table:
CustomerI CustomerNa ContactNam Countr
D me e y
1 Alfreds Maria German
Futterkiste Anders y
2 Ana Trujillo Ana Trujillo Mexico
Emparedados y
helados
3 Antonio Antonio Mexico
Moreno Moreno
Taquería
The "CustomerID" column in the "Orders" table
refers to the "CustomerID" in the "Customers"
table. The relationship between the two tables
above is the "CustomerID" column.
Then, we can create the following SQL statement
(that contains an INNER JOIN), that selects
records that have matching values in both tables:

Example
SELECT Orders.OrderID,
Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID
=Customers.CustomerID;
Output:
OrderI CustomerNam OrderDat
D e e
10308 Ana Trujillo 9/18/1996
Emparedadosy
helados
10365 Antonio Moreno 11/27/1996
Taquería
10383 Around the 12/16/1996
Horn
10355 Around the 11/15/1996
Horn
10278 Berglunds 8/12/1996
snabbköp

1. INNER JOIN
INNER JOIN statement returns only those
records or rows that have matching values
and is used to retrieve data that appears in
both tables.
Syntax
SELECT column_name(s) FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example
SELECT Orders.OrderID,
Customers.CustomerName FROM Orders
INNER JOIN Customers ON Orders.CustomerID
= Customers.CustomerID;
2. FULL OUTER JOIN
The FULL OUTER JOIN returns a result
that includes rows from both left and right
tables. In case, no matching rows exist for the
row in the left table, the columns of the right
table will have nulls. Correspondingly, the
column of the left table will have nulls if
there are no matching rows for the row in the
right table.

Syntax:
SELECT column_name(s) FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Example
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
FULL OUTER JOIN Orders ON Customers.Cust
omerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

3. LEFT OUTER JOIN


The LEFT OUTER JOIN gives the output
of the matching rows between both tables. In
case, no records match from the left table, it
shows those records with null values.

Syntax
SELECT column_name(s) FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Example
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
LEFT JOIN Orders ON Customers.CustomerID =
Orders.CustomerID
ORDER BY Customers.CustomerName;
4. RIGHT OUTER JOIN
The RIGHT OUTER JOIN works by the
same principle as the LEFT OUTER JOIN.
The RIGHT OUTER JOIN selects data
from the right table (Table B) and matches
this data with the rows from the left table
(Table A). The RIGHT JOIN returns a result
set that includes all rows in the right table,
whether or not they have matching rows from
the left table. In case, a row in the right table
does not have any matching rows in the left
table, the column of the left table in the result
set will have nulls.
Syntax:
SELECT column_name(s) FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example:
SELECT Orders.OrderID, Employees.LastName,
Employees.FirstName FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID
= Employees.EmployeeID
ORDER BY Orders.OrderID;

5. SELF JOIN
The SELF JOIN allows you to join a table to
itself. This implies that each row of the table
is combined with itself and with every other
row of the table. The SELF JOIN can be
viewed as a join of two copies of the same
table. The table is not actually copied, but
SQL performs the command as though it
were.
Syntax:
SELECT column_name(s) FROM table1 T1,
table1 T2 WHERE condition;
Example:
SELECT A.CustomerName AS CustomerNam
e1,
B.CustomerName AS CustomerName2, A.C
ity
FROM Customers A, Customers B
WHERE A.CustomerID = B.CustomerID
AND A.City = B.City
ORDER BY A.City;

6. CROSS JOIN
The CROSS JOIN command in SQL, also
known as a cartesian join, returns all
combinations of rows from each table.
Envision that you need to find all
combinations of size and color. In that case, a
CROSS JOIN will be an asset. Note, that this
join does not need any condition to join two
tables. In fact, CROSS JOIN joins every row
from the first table with every row from the
second table and its result comprises all
combinations of records in two tables.

Syntax:
SELECT column_name(s) FROM table1
CROSS JOIN table2;
Example:
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
CROSS JOIN Orders;
******

What is PL/SQL block?


In PL/SQL, the code is not executed in single line format, but it is
always executed by grouping the code into a single element called
Blocks. Blocks contain both PL/SQL as well as SQL instruction. All these
instruction will be executed as a whole rather than executing a single
instruction at a time.

Block Structure
PL/SQL blocks have a pre-defined structure in which the code is to be
grouped. Below are different sections of PL/SQL blocks.

1. Declaration section
2. Execution section
3. Exception-Handling section

The below picture illustrates the different PL/SQL block and their
section order.
Declaration Section
This is the first section of the PL/SQL blocks. This section is an optional
part. This is the section in which the declaration of variables, cursors,
exceptions, subprograms, pragma instructions and collections that are
needed in the block will be declared. Below are few more
characteristics of this part.

 This particular section is optional and can be skipped if no


declarations are needed.
 This should be the first section in a PL/SQL block, if present.
 This section starts with the keyword ‘DECLARE’ for triggers and
anonymous block. For other subprograms, this keyword will not
be present. Instead, the part after the subprogram name
definition marks the declaration section.
 This section should always be followed by execution section.

Execution Section
Execution part is the main and mandatory part which actually executes
the code that is written inside it. Since the PL/SQL expects the
executable statements from this block this cannot be an empty block,
i.e., it should have at least one valid executable code line in it. Below
are few more characteristics of this part.

 This can contain both PL/SQL code and SQL code.


 This can contain one or many blocks inside it as a nested block.
 This section starts with the keyword ‘BEGIN’.
 This section should be followed either by ‘END’ or Exception-
Handling section (if present)

Exception-Handling Section
The exception is unavoidable in the program which occurs at run-time
and to handle this Oracle has provided an Exception-handling section
in blocks. This section can also contain PL/SQL statements. This is an
optional section of the PL/SQL blocks.
 This is the section where the exception raised in the execution
block is handled.
 This section is the last part of the PL/SQL block.
 Control from this section can never return to the execution block.
 This section starts with the keyword ‘EXCEPTION’.
 This section should always be followed by the keyword ‘END’.

The Keyword ‘END’ marks the end of PL/SQL block.

PL/SQL Block Syntax


Below is the syntax of the PL/SQL block structure.

DECLARE --optional
<declarations>

BEGIN --compulsory
<executable statements. At least
one executable statement is compulsory>
EXCEPTION --optional
<exception handles>

END; -- compulsory
/
Note: A block should always be followed by ‘/’ which sends the
information to the compiler about the end of the block.

Cursor is a Temporary Memory or Temporary Work


Station. It is Allocated by Database Server at the Time
of Performing DML(Data Manipulation Language)
operations on the Table by the User. Cursors are used
to store Database Tables.
There are 2 types of Cursors: Implicit Cursors, and
Explicit Cursors. These are explained as following
below.
1. Implicit Cursors: Implicit Cursors are also known as
Default Cursors of SQL SERVER. These Cursors
are allocated by SQL SERVER when the user
performs DML operations.
2. Explicit Cursors: Explicit Cursors are Created by
Users whenever the user requires them. Explicit
Cursors are used for Fetching data from Table in
Row-By-Row Manner.
How To Create Explicit Cursor?
1.Declare Cursor Object
Syntax:
DECLARE cursor_name CURSOR FOR SELECT
* FROM table_name
Query:
DECLARE s1 CURSOR FOR SELECT * FROM
studDetails
2. Open Cursor Connection
Syntax:
OPEN cursor_connection
Query:
OPEN s1
Fetch Data from the Cursor There is a total of 6
methods to access data from the cursor. They
are as follows:
1.FIRST is used to fetch only the first row from
the cursor table.
2.LAST is used to fetch only the last row from the
cursor table.
3.NEXT is used to fetch data in a forward
direction from the cursor table.
4.PRIOR is used to fetch data in a backward
direction from the cursor table.
5.ABSOLUTE n is used to fetch the exact n th row
from the cursor table.
6.RELATIVE n is used to fetch the data in an
incremental way as well as a decremental way.
Syntax:
FETCH NEXT/FIRST/LAST/PRIOR/ABSOLUTE
n/RELATIVE n FROM cursor_name
Query:This ad will end in 14

FETCH FIRST FROM s1


FETCH LAST FROM s1
FETCH NEXT FROM s1
FETCH PRIOR FROM s1
FETCH ABSOLUTE 7 FROM s1
FETCH RELATIVE -2 FROM s1
Close cursor connection
Syntax:
CLOSE cursor_name
Query:
CLOSE s1
SQL TRIGGER

A trigger is a stored procedure in a database that automatically invokes


whenever a special event in the database occurs. For example, a trigger
can be invoked when a row is inserted into a specified table or when
specific table columns are updated in simple words a trigger is a collection
of SQL statements with particular names that are stored in system memory.
It belongs to a specific class of stored procedures that are automatically
invoked in response to database server events. Every trigger has a table
attached to it.
Because a trigger cannot be called directly, unlike a stored procedure, it is
referred to as a special procedure. A trigger is automatically called
whenever a data modification event against a table takes place, which is the
main distinction between a trigger and a procedure. On the other hand, a
stored procedure must be called directly.
The following are the key differences between triggers and stored
procedures:
1. Triggers cannot be manually invoked or executed.
2. There is no chance that triggers will receive parameters.
3. A transaction cannot be committed or rolled back inside a trigger.
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Explanation of Syntax
1. Create trigger [trigger_name]: Creates or replaces an existing trigger
with the trigger_name.
2. [before | after]: This specifies when the trigger will be executed.
3. {insert | update | delete}: This specifies the DML operation.
4. On [table_name]: This specifies the name of the table associated with
the trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger will be
executed for each affected row.
6. [trigger_body]: This provides the operation to be performed as the trigger
is fired
Why Do We Employ Triggers?
When we need to carry out some actions automatically in certain desirable
scenarios, triggers will be useful. For instance, we need to be aware of the
frequency and timing of changes to a table that is constantly changing. In
such cases, we could create a trigger to insert the required data into a
different table if the primary table underwent any changes.

Different Trigger Types in SQL Server


Two categories of triggers exist:
1. DDL Trigger
2. DML Trigger
3. Logon Triggers

DDL Triggers

The Data Definition Language (DDL) command events such as


Create_table, Create_view, drop_table, Drop_view, and Alter_table cause
the DDL triggers to be activated.
SQL Server
create tigger safety
on database
for
create_table,alter_table,drop_table
as
print 'you can not create,drop and alter tab
Output:
DML Triggers

The Data uses manipulation Language (DML) command events that begin
with Insert, Update, and Delete set off the DML triggers. corresponding to
insert_table, update_view, and delete_table.
SQL Server
create trigger deep
on emp
for
insert,update ,delete
as
print 'you can not insert,update and delete this table i'
rollback;
Output:

Logon Triggers

logon triggers are fires in response to a LOGON event.

Difference between Function and Procedure

A function calculates the results of a program based on the inputs provided,


whereas a procedure is used to perform some tasks in a specific order.
There are many other differences between functions and procedures, which
we will discuss in this article.
What is Function?
A function, in the context of computer programming languages, is a set of
instructions which takes some input and performs certain tasks. In SQL, a
function returns a value. In other words, a function is a tool in SQL that is
used to calculate anything to produce an output for the provided inputs. In
SQL queries, when a function is called, it returns the resulting value. It also
controls to the calling function. However, in a function, we cannot use some
DML statements like Insert, Delete, Update, etc.
Also, a function can be called through a procedure. Based on definition,
there are two types of functions namely, predefined
function and userdefined function. Another important point about
functions is that they may or may not return a value, i.e. a function can
return a null valued as well.

What is a Procedure?
A procedure is a set of instructions which takes input and performs a
certain task. In SQL, procedures do not return a value. In Java, procedures
and functions are same and also called subroutines.
In SQL, a procedure is basically a precompiled statement which is stored
inside the database. Therefore, a procedure is sometimes also called
a stored procedure. A procedure always has a name, list of parameters,
and compiled SQL statements. In SQL, a procedure does not return any
value.

Stored Procedure Syntax


CREATE PROCEDURE procedure_name
AS
sql_statement
GO;

Execute a Stored Procedure


EXEC procedure_name;

Now, let us discuss the differences between function and procedure in


detail.
Difference between Function and Procedure
Following are the important differences between SQL Function and SQL
Procedure−

Key Function Procedure

A function is used to A procedure is used to


Definition calculate result using perform certain task in
given inputs. order.

A function can be A procedure cannot be


Call called by a called by a function.
procedure.

DML statements DML statements can be


DML cannot be executed executed within a
within a function. procedure.

A function can be A procedure cannot be


SQL, Query
called within a query. called within a query.

Whenever a function A procedure is compiled


is called, it is first once and can be called
SQL, Call
compiled before multiple times without
being called. being compiled.

A function returns a A procedure returns the


value and control to control but not any value
SQL, Return
calling function or to calling function or
code. code.

A function has no A procedure has support


try-catch
support for try-catch for try-catch blocks.

A select statement A select statement can't


SELECT can have a function have a procedure call.
call.

Explicit A function cannot A procedure can use


Transaction have explicit explicit transaction
Handling transaction handling. handling.

Conclusion
The most significant difference that you should note here is that a function
is used to calculate the result using the given inputs, while a procedure is
used to perform a certain task in order.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy