User Defined Functions
User Defined Functions
User Defined functions can be used to perform a complex logic, can accept
parameters and return data. SQL Server supports two types of User Defined Functions as
mentioned below
Scalar Functions
The function which returns a Scalar/Single value.
SELECT DBO.MYSUM(10,20);
DROP FUNCTION MYSUM;
A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved. 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.
● An ORDER BY cannot be used in a subquery, although the main query can use an
ORDER BY.
● Subqueries that return more than one row can only be used with multiple value
operators, such as the IN operator.
● The BETWEEN operator cannot be used with a subquery; however, the BETWEEN can
be used within the subquery.
Simple Subqueries
Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows:
Note: Although Subqueries are commonly used with Select statement, these can also be used
with Insert, Update or Delete Statements
Correlated Subqueries
There are ways to incorporate the outer query’s values into the subqueries clauses. These types
of queries are called correlated subqueries, since the results from the subquery are connected,
in some form, to values in the outer query. Correlated queries are sometimes called
synchronized queries.
Eg:
Corelated sub query for average salary of those delhi employees whose salary is >100000
Stored Procedures
● A stored procedure is prepared SQL code that we save so we can reuse the code over
and over again. So if we think about a query that we write over and over again, instead
of having to write that query each time we would save it as a stored procedure and then
just call the stored procedure to execute the SQLcode that we saved as part of the
stored procedure.
● In addition to running the same SQL code over and over again we also have the ability
to pass parameters to the stored procedure.
SYNTAX
CREATE PROCEDURE <procedure_EID>
AS
BEGIN
<SQL Statement>
END
EXECUTE <procedure_EID>
EXEC <procedure_EID>
<procedure_EID>
Example 2 : Parameterized Procedure to get the details of employees of the specified city
CREATE PROCEDURE SHOWEMP @X VARCHAR(20)
AS
BEGIN
SELECT * FROM EMP WHERE CITY = @X;
END;
SYNTAX
CREATE PROCEDURE <procedure_EID>
@ var1 datatype (size), var2 datatype (size)
AS
BEGIN
[SET NOCOUNT ON/OFF]
<SQL Statement>
END
Learnvista Pvt Ltd.
2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
EXECUTE <procedure_EID> var1 , var2
DROP PROCEDURE <procedure_EID>
Transactions
• A transaction is a unit of work that is performed against a database. For example, if you are
creating a record or updating a record or deleting a record from the table, then you are
performing a transaction on the table.
Properties of Transactions
Transactions have the following four standard properties, usually referred to by
the acronym ACID:
● Atomicity: Ensures that all operations within the work unit are completed successfully;
otherwise, the transaction is aborted at the point of failure, and previous operations are
rolled back to their former state.
● Consistency: Ensures that the database properly changes state upon a successfully
committed transaction.
● Isolation: Enables transactions to operate independently of and transparent to each
other.
● Durability: Ensures that the result or effect of a committed transaction persists in case of
a system failure
Auto Increment
Auto Increment allows a unique number to be generated automatically when
a new record is added to the table.
Example :
create table emp2