stored procedures

5
USE KoumoundourosFinancial GO -- ====================================================================== ======= -- 1. Write a stored procedure that given a Celsius temp returns a Fahrenheit. -- F = (9/5.0) * C + 32 -- ====================================================================== ======= --first problem --sp_rename GetFahrenheightCalc, GetFahrenheitCalc --ALTER PROCEDURE GetFahrenheitCalc CREATE PROCEDURE GetFahrenheitCalc @Celsius dec(5,2), @Fahrenheit dec(5,2) output AS SET @Fahrenheit = (9/5.0) * @Celsius + 32 DECLARE @Fahrenheit_calculation dec(5,2) EXECUTE GetFahrenheitCalc 32.00, @Fahrenheit_calculation output PRINT @Fahrenheit_calculation

Upload: jimmy-koumoundouros

Post on 21-Apr-2017

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stored Procedures

USE KoumoundourosFinancial

GO

-- =============================================================================

-- 1. Write a stored procedure that given a Celsius temp returns a Fahrenheit.

-- F = (9/5.0) * C + 32

-- =============================================================================

--first problem

--sp_rename GetFahrenheightCalc, GetFahrenheitCalc

--ALTER PROCEDURE GetFahrenheitCalc

CREATE PROCEDURE GetFahrenheitCalc

@Celsius dec(5,2),

@Fahrenheit dec(5,2) output

AS

SET @Fahrenheit =

(9/5.0) * @Celsius + 32

DECLARE @Fahrenheit_calculation dec(5,2)

EXECUTE GetFahrenheitCalc 32.00, @Fahrenheit_calculation output

PRINT @Fahrenheit_calculation

-- =============================================================================

-- 2. Write a stored procedure to print out a list of C to F where C goes

-- from 1 to 100.

-- =============================================================================

--second problem

Page 2: Stored Procedures

create procedure GetFahrenheitCalc2

as

begin

declare @i as integer

set @i = 1

while @i <= 100

begin

print (9/5.0) * @i + 32

set @i = @i + 1

end

end

execute GetFahrenheitCalc2

-- =============================================================================

-- 3. Write a stored procedure that given a city will return author’s first

-- and last names from the pub database that live in that city.

-- (note: create the code in your database accessing tables in pubs as

-- pubs..tablename).

-- =============================================================================

--third problem

create procedure AuthorCity

@city varchar(50)

as

select Au_lname, au_fname from authors

where city = @city

Page 3: Stored Procedures

execute AuthorCity 'Oakland'

USE pubs

GO

select Au_lname, au_fname from authors

where city = 'Oakland'

-- =============================================================================

-- 4. Modify the previous code to display the title that they wrote too.

-- =============================================================================

--fourth problem

create procedure AuthorTitles

@city varchar(50)

as

select Au_lname, au_fname, title from authors

join titleauthor on authors.au_id = titleauthor.au_id

join titles on titles.title_id=titleauthor.title_id

where city = @city

execute AuthorTitles 'Oakland'

USE pubs

GO

Page 4: Stored Procedures

select Au_lname, au_fname, title from authors

join titleauthor on authors.au_id = titleauthor.au_id

join titles on titles.title_id=titleauthor.title_id

where city = 'Oakland'

-- =============================================================================

-- 5. Write a stored procedure that given a city returns the total number of

-- authors that live in that city through an output parameter.

-- =============================================================================

--fifth problem

create procedure NumberOfAuthors

@city varchar(50),

@total int OUTPUT

as

select @total=count(*) from pubs..authors where city = @city

declare @total int

execute NumberOfAuthors 'Oakland', @total OUTPUT

print @total