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

7 - Advance Database Objects

Stored procedures allow running multiple SQL queries and reusing code. They can accept parameters, return multiple values, and call other stored procedures. User defined functions can perform calculations and return single values or tables. Stored procedures allow DML queries and managing transactions while functions only allow select statements. Triggers automatically execute SQL in response to events and can enforce referential integrity or log database events.

Uploaded by

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

7 - Advance Database Objects

Stored procedures allow running multiple SQL queries and reusing code. They can accept parameters, return multiple values, and call other stored procedures. User defined functions can perform calculations and return single values or tables. Stored procedures allow DML queries and managing transactions while functions only allow select statements. Triggers automatically execute SQL in response to events and can enforce referential integrity or log database events.

Uploaded by

Farah Yaqoob
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

STORED PROCEDURES:

-To run multiple queries we use stored procedures


-stored procedure is a prepared SQL code that you can save, so the code can be
reused over and over again.
-So if you have an SQL query that you write over and over again, save it as a
stored procedure, and
then just call it to execute it.
-Normal queries are not saved, but stored procedure are saved in database.
-Like other programming language, they can:
1) accept input parameters
2) can call other stored procedures
3) return multiple values

BENEFITS:
-reduced client server traffic
-stronger security
-reuse of code
-easier maintenance
-improved performance
-----------------------------------------------------------------------------------
------------------------
CREATE PROCEDURE getAll
AS
SELECT * FROM Customers
GO;

EXEC getAll; -- how to execute


-----------------------------------------------------------------------------------
------------------------
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;

EXEC SelectAllCustomers @City = 'London', @PostalCode = 'WA1 1DP';


-----------------------------------------------------------------------------------
------------------------
NOTE:
-when calling procedures, if parameters are not passed it will show error
-To avaoid it use defaultly: @City nvarchar(30) = NULL when defining,
-----------------------------------------------------------------------------------
------------------------
***********************************************************************************
************************
-----------------------------------------------------------------------------------
------------------------
USER DEFINED FUNCTIONS:
-Like programming language functions, UDF can accept parameters, perform complex
calculations and
return either a value or table.
-If it returns single scalar value, function is called Scalar valued functions.
-If it returns a table, function is called Table valued functions.

SCALAR:

CREATE FUNCTION fullName (@first VARCHAR(50), @last VARCHAR(50))


RETURNS VARCHAR(200)
AS
BEGIN
RETURN (SELECT @first + space(2) + @last)
END

SELECT db.fullName('hello', 'world')


-----------------------------------------------------------------------------------
--------------------
TABLE:

CREATE FUNCTION Sales.ufn_SalesByStore (@storeid int)


RETURNS TABLE
AS
RETURN
(
SELECT P.ProductID, P.Name, SUM(SD.LineTotal) AS 'Total'
FROM Production.Product AS P
JOIN Sales.SalesOrderDetail AS SD ON SD.ProductID = P.ProductID
JOIN Sales.SalesOrderHeader AS SH ON SH.SalesOrderID = SD.SalesOrderID
JOIN Sales.Customer AS C ON SH.CustomerID = C.CustomerID
WHERE C.StoreID = @storeid
GROUP BY P.ProductID, P.Name
);
END

SELECT * FROM Sales.ufn_SalesByStore (602);


-----------------------------------------------------------------------------------
--------------------
Difference between PROCEDURES & FUNCTIONS:

A function has a return type and returns a value. A procedure does not have a
return type. But it returns values using the OUT parameters.
Doesnot allow DML queries. Only Select queries. You can use DML queries such as
insert, update, select etc� with procedures.
A function does not allow output parameters A procedure allows both
input and output parameters.
You cannot manage transactions inside a function. You can manage transactions
inside a procedure.
You cannot call stored procedures from a function You can call a function from
a stored procedure.
You can call a function using a select statement. You cannot call a procedure
using select statements.
-----------------------------------------------------------------------------------
--------------------
TRIGGERS:
-Are special kind of stored procedures that automatically executes when an event
occur in DB server.
-There are two Types DDL triggers and DML triggers.
-DML: when insert, delete, update, select.
-DDL: when create, alter, drop etc.

**create trigger that prints message when there is creation, alteration or drop of
any table. **

create trigger safety


on database
for
create_table,alter_table,drop_table
as
print'you can not create ,drop and alter table in this database'
rollback;
-----------------------------------------------------------------------------------
--------------------
BENEFITS:
-For implementing referencial integrity, we use triggers.
-control of updation
-can be used to show database events(startup/shutdown) for subscribbers.
-----------------------------------------------------------------------------------
--------------------

You might also like