Ch 7. Working with relational data
Transactions Group of statements executed as a group. If all statements execute successfully, changes are committed. If any statement fails, all changes are rolled-back.
Transaction Process of Transaction 1. Put data into buffer cache 2. Buffer transfer to Transaction Log 3. Check point (false) 4. Transfer to data file 5. Check point (true)
Transaction
Transactions 3 Types of transactions: AutoCommit Implicit Explicit
Transactions AutoCommit Transactions : All transactions are committed automatically, basically everything we have done so far.
Transaction Create this table CREATE TABLE [dbo].[tblEmployee2] ( [EmployeeID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL, [FirstName] [nchar](50) NULL, [LastName] [nchar](50) NULL, [DOB] [datetime] NULL, [Sex] [nchar](10) NULL, )
Transactions Explicit Transaction: Manually specify when to Start Transaction and when to Commit or Rollback transaction
Transaction Create this stored procedure CREATE PROCEDURE sp_BadTransaction AS INSERT INTO tblEmployee ( FirstName,LastName,DOB,Sex) VALUES ( 'test','person','5/1/2000','male') INSERT INTO tblEmployee ( FirstName,LastName,DOB,Sex) VALUES ( 'test','person','5/1/2000',‘ThisIsTooLong')
Transaction Execute the procedure. 1 st procedure executed and inserted a record into tblEmployee table. 2 nd procedure failed and did not execute.
Transaction CREATE PROCEDURE [dbo].[sp_GoodTransaction] AS BEGIN TRY BEGIN TRANSACTION INSERT INTO tblEmployee ( FirstName,LastName,DOB,Sex) VALUES ( 'test','person','5/1/2000','male') INSERT INTO tblEmployee ( FirstName,LastName,DOB,Sex) VALUES ( 'test2','person','1/15/2003',‘ThisIsTooLong') COMMIT TRANSACTION END TRY BEGIN CATCH ROLLBACK TRANSACTION END CATCH
Transaction Execute the procedure. Both statements failed to execute. No record was inserted into tblEmployee table.
Transactions Implicit Transaction: Transaction starts automatically and is kept open until a Commit or Rollback was issued. Default is set to disabled: SET IMPLICIT_TRANSACTIONS ON to enable
Transactions SET IMPLICIT_TRANSACTIONS ON go INSERT INTO tblEmployee ( FirstName,LastName,DOB,Sex) VALUES ( 'test','person','5/1/2000','male') INSERT INTO tblEmployee ( FirstName,LastName,DOB,Sex) VALUES ( 'test2','person','1/15/2003','female') rollback transaction
Import/Populating Tables Ways to import into SQL Server 2005 Bulk Insert BCP utility
Import/Populating Tables Bulk Insert: Import data use user-specified format Able to specify field and row terminator
Import/Populating Tables USE SYBEX CREATE TABLE AirportCode ( ID int NULL, City nvarchar(50) NULL, Code nvarchar(10) NULL )
Import/Populating Tables BULK INSERT Sybex.dbo.Airportcode FROM 'c:\airportfile.txt' WITH ( FIELDTERMINATOR='|', ROWTERMINATOR='|\n' )
Import/Populating Tables BCP utility: Command-line tool Export table->file Export query->file Import into SQL Server Create format file
Import/Populating Tables BCP utility: Exercise 7.1 page 249
Import/Populating Tables Create countries table USE SYBEX CREATE TABLE countries ( countrycode char(2), Countryname varchar(50) )
Import/Populating Tables Start->run->cmd, command prompt opens up Enter “Bcp sybex.dbo.countries in countries.txt –f countries.fmt –T” at command prompt to import into sybex.dbo.countries table. Sqlcmd –E –Q “select * from sybex.dbo.countries” to verify