0% found this document useful (0 votes)
58 views8 pages

Procedures

The document discusses stored procedures in SQL Server, including how to create procedures with input and output parameters, call procedures, modify procedures by altering or renaming them, view procedure definitions, and delete procedures. It also covers using IF/ELSE conditional statements in SQL Server to execute different code blocks based on conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views8 pages

Procedures

The document discusses stored procedures in SQL Server, including how to create procedures with input and output parameters, call procedures, modify procedures by altering or renaming them, view procedure definitions, and delete procedures. It also covers using IF/ELSE conditional statements in SQL Server to execute different code blocks based on conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DPG621S

Procedures, Conditional Constructs


Procedures
(stored procedure with no Parameter)
USE AdventureWorks2019
GO
Create procedure sp_SelectAllPersonContacts
As
select Title, FirstName, LastName
from person.Person

Execute sp_SelectAllPersonContacts
Procedures
(stored procedure with an input Parameter)
USE AdventureWorks2019
GO
Create procedure sp_CountContacts_By_Title
@title nvarchar(8)
As
select Title, FirstName, LastName
from person.Person
Where @title=Title;

Execute sp_CountContacts_By_Title 'Sra.'


Procedures
(stored procedure with an output Parameter)
USE AdventureWorks2019
GO
Create procedure sp_CountContacts_By_Title
@Title nvarchar(8),
@TitleCount int= 0 output
--The keyword output marks it as an output parameter
As
select Title, FirstName, LastName
from Person.Person
where Title = @Title
Select @TitleCount = count(*)
from Person.person
where Title=@Title
Test stored procedure
Declare @return_value int,
@TitleCount int
Execute @return_value=sp_CountContacts_By_Title
@Title='Mr.',
@TitleCount=@TitleCount output
Select 'Total Title Count' =@return_value
Modifying a Procedure
ALTER procedure sp_Select_All_PersonContact
As
SELECT Contact.Title, Contact.FirstName, Contact.LastName
FROM Person.Contact
ORDER BY Contact.LastName
Viewing the Definition of Our Stored
Procedure
Execute sp_helptext 'sp_Select_All_PersonContact’

Renaming Stored Procedures


sp_rename 'sp_Select_All_PersonContact’,
'sp_Select_All_ContactDetails’

Deleting Stored Procedures


Drop procedure sp_Select_All_ContactDetails
Condition Logic
• IF...ELSE Statement
• In SQL Server, the IF...ELSE statement is used to execute code when a
condition is TRUE, or execute different code if the condition evaluates
to FALSE.
• Syntax
IF condition
{...statements to execute when condition is TRUE...}
[ ELSE
{...statements to execute when condition is FALSE...} ]

You might also like