Commit 706477ab authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+7 −0
Changes for Developing SQL Databases - Exam 70-762 Certification - Working Files/Chapter 1/No Work Files.rtf: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural

\f0\fs24 \cf0 No work files for this lesson...}
 No newline at end of file
+37 −0
Changes for Developing SQL Databases - Exam 70-762 Certification - Working Files/Chapter 10/AFTER Trigger Example.sql: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
			
-- AFTER Trigger Example

-- Let's create a simple table 
CREATE TABLE AfterTrigTest
(
 FirstName varchar(15),
 LastName varchar(20),
 SSN varchar(11),
 State char(2)
)

-- Insert two rows into the table
INSERT INTO AfterTrigTest
VALUES
('Bob','James','484-52-6462','TN'),
('Ann','Lesley','492-67-5234','AZ')

-- Create an AFTER UPDATE trigger on the AfterTrigTest table
CREATE TRIGGER trAfterTrigTest_upd
ON AfterTrigTest
AFTER UPDATE
AS
BEGIN
PRINT  CONVERT(varchar(5),@@rowcount) + ' row(s) were updated.'
+ CHAR(13) + CHAR(13) + 'This rowcount was provided by the trAfterTrigTest trigger...'
--ROLLBACK TRANSACTION
END


-- Perform an update to test the trigger
UPDATE AfterTrigTest
SET FirstName='Robert'
WHERE FirstName='Bob'


select * from AfterTrigTest
 No newline at end of file
+36 −0
Changes for Developing SQL Databases - Exam 70-762 Certification - Working Files/Chapter 10/INSTEAD OF Trigger Example.sql: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line

-- INSTEAD OF DML Trigger Example

CREATE TABLE dbo.TriggerTest
(
 FirstName varchar(15),
 LastName varchar(20),
 SSN char(11),
 State char(2)
)


-- Let's create an INSTEAD OF trigger that will 
-- fire when an INSERT occurs on this table
-- Note!
-- The INSTEAD OF trigger usually contains code 
-- to take some action before allowing the data insert to occur
-- or be refused...

CREATE TRIGGER io_TriggerEx
ON dbo.TriggerTest
INSTEAD OF INSERT
AS
BEGIN
  PRINT 'The INSTEAD OF trigger: io_TriggerEx has fired '
  PRINT 'The data contained in the INSERT command has not been added to the table'
END



select * from dbo.TriggerTest

-- Let's execute an INSERT and see the trigger in action
INSERT INTO dbo.TriggerTest
VALUES
('Bob','James','438-76-2362','TX')
+33 −0
Changes for Developing SQL Databases - Exam 70-762 Certification - Working Files/Chapter 11/Scalar UDF Example.sql: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- Scalar UDF Example
-- We will create a function to provide a more 
-- descriptive result set for the EmailPromotion column
-- in the Person.EmailAddress table
SELECT * FROM Person.Person

-- Create a function named fnEmailPromo
-- It will accept EmailAddress as an input and
-- return a string value based on the int value
-- being passed in
-- We will use the function in a select statement
CREATE FUNCTION fnEmailPromo (@promo int)
RETURNS varchar(20)
AS
BEGIN
DECLARE @ret_promo varchar(20)
select @ret_promo=
	CASE 
		WHEN @promo=0 then 'None'
		WHEN @promo=1 then 'Local'
		WHEN @promo=2 THEN 'National'
		ELSE 'Not designated'
	END
	RETURN @ret_promo
END

-- Now, let's user our new fnEmailPromo function
-- Note that to call the new function we must include
-- dbo. in the name
SELECT FirstName + ' ' + Lastname Customer, dbo.fnEmailPromo(EmailPromotion) 'Promotion Type'
FROM Person.Person
ORDER BY LastName, FirstName
+32 −0
Changes for Developing SQL Databases - Exam 70-762 Certification - Working Files/Chapter 11/Table Valued Function Example.sql: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- TVF Example

-- Here are the three tables we will be getting our data from
select top 1 * from person.person
select top 1 * from person.emailaddress
select top 1 * from humanresources.employee

-- Our TVF will return the names and email addresses of our
-- employees based on their gender
-- Let's create our TVF

CREATE FUNCTION dbo.fnGetEmail(@gender char(1))
RETURNS TABLE
AS
RETURN
	SELECT p.FirstName, p.Lastname,e.EmailAddress,em.Gender
	FROM person.person p
	JOIN person.EmailAddress e on p.BusinessEntityID=e.BusinessEntityID
	JOIN HumanResources.Employee em on e.BusinessEntityID=em.BusinessEntityID
	WHERE em.Gender=@gender

-- Let's use our TVF
SELECT * FROM dbo.fnGetEmail('m')


-- We can add a WHERE clause to get more creative with the results
SELECT * FROM dbo.fnGetEmail('m')
WHERE LastName LIKE 'D%'



Loading