Professional Documents
Culture Documents
Lab Manual 10 PDF
Lab Manual 10 PDF
Lab Manual 10 PDF
Lab Instructor:
Ms. Hira Kanwal
Student Name
Student Roll #
Department
Batch/Year/Section
Marks Signature
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.
Example:
begin tran
Mistakenly deleted all the data from a table. You can roll back your transaction by writing:
rollback tran
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
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
BEGIN
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;
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"
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.
Example:
SET ANSI_NULLS OFF
IF NULL = NULL
PRINT 'same'
ELSE
PRINT 'different'
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.
10.4.1. Write stored procedures (select, insert, update, delete) for all the tables in the ACDB.