Lab Manual 10 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Department of Computer Science & Information Technology

Khwaja Fareed University of Engineering & Information Technology

Course: Database Systems

Lab Instructor:
Ms. Hira Kanwal

Student Name

Student Roll #

Department

Batch/Year/Section

For Lab. Instructor

Marks Signature

KFUEIT Department of CS/IT

lxxx
Lab Manual # 10 Creating & Manipulating Databases

10.1. Objective
1. Roll back or Commit transaction
2. Stored Procedures

10.2. Transaction

A transaction is a unit of work that is performed against a database. This work can be
performed manually, such as an UPDATE statement or INSERT statement or an Update or
Delete. These are all transactions.

SQL Server supports transaction control. Below is a short description of each transaction.
Note that transaction controls are only used with DML commands.

1. BEGIN TRANSACTION - the starting point of a transaction

2. ROLLBACK TRANSACTION - roll back a transaction either because of a mistake or


a failure

3. COMMIT TRANSACTION - save changes to the database

Example:
begin tran

delete from customers

Mistakenly deleted all the data from a table. You can roll back your transaction by writing:

rollback tran

10.3. Stored Procedures

Stored Procedure in SQL Server can be defined as the set of logical group of SQL statements
which are grouped to perform a specific task.

Advantages

1. It increases the performance of the database.


2. It reduces the amount of information sent to the database server.
3. Stored procedure provides an important layer of security between the user
interface and the database.

Example 1:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE SelectProduct
-- Add the parameters for the stored procedure here
@ProductId int

AS

KFUEIT Department of CS/IT


81
Lab Manual # 10 Creating & Manipulating Databases

BEGIN

SET NOCOUNT ON;

SELECT * from Products where ProductId = @ProductId


END
GO

To Execute the above Procedure write:

Exec SelectProduct 1

Example 2:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertData]
-- Add the parameters for the stored procedure here
@ProductName varchar(50), @ProductCategory varchar(50), @Price money,
@Instock int, @SoldOut int

AS
BEGIN
SET NOCOUNT ON;

Insert into Products


(ProductName,ProductCategory,Price,InStock,SoldOut)
values (@ProductName,@ProductCategory,@Price,@InStock,@SoldOut)
END

To Execute the procedure write:

Execute InsertData New, Neww, 340, 20, 12

10.3.1. SET Quoted Identifier ON/OFF

It specifies how SQL Server treats the data that is defined in Single Quotes and Double
Quotes.

When it is set to ON any character set that is defined in the double quotes “”is treated as
a Table Name, Proc Name, Column Name….etc), And any character set that is defined in
the single quotes ‘’ is treated as a literal.
When it is set to OFF any character set that is defined either in Single Quotes or in Double
Quotes is treated as a literal.

 SET QUOTED_IDENTIFIER ON
DROP TABLE "bbbb"

 SET QUOTED_IDENTIFIER OFF


DROP TABLE "bbbb"

KFUEIT Department of CS/IT


82
Lab Manual # 10 Creating & Manipulating Databases

The second one gives an error.

10.3.2. SET ANSI_NULLS ON/OFF:

The ANSI_NULLS option specifies that how SQL Server handles the comparison operations
with NULL values.

When it is set to ON any comparison with NULL using = and <> will yield to false value.
When it is set to OFF any comparison with NULL using = and <> will work as usual i.e.
NULL = NULL returns true and 1= NULL returns false.

By default, both ANSI_NULLS and QUOTED_IDENTIFIER are set to ON.

Example:
SET ANSI_NULLS OFF
IF NULL = NULL
PRINT 'same'
ELSE
PRINT 'different'

10.3.3. SET NOCOUNT ON/OFF

When SET NOCOUNT is ON, the count (indicating the number of rows affected by a
Transact-SQL statement) is not returned.

When SET NOCOUNT is OFF, the count is returned. It is used with any SELECT, INSERT,
UPDATE, DELETE statement.

SET NOCOUNT ON only saves 9-bytes per query.

10.4. LAB TASK

10.4.1. Write stored procedures (select, insert, update, delete) for all the tables in the ACDB.

KFUEIT Department of CS/IT


83

You might also like