June 11, 1999 Leveraging SQL Server 7.0 stored procedures By Sanjiv Purba Introduction Last week I introduced you to stored procedures, database objects that the American National Standards Institute defines as part of the Transact-SQL extension. If you missed part one of this article, I suggest reading it over before you move forward. If you’re already familiar with stored procedures and get busy using them, let’s get started! Creating and Dropping Stored Procedures Stored procedures are database objects that include the name of the stored procedure, input parameters, local variables, control-of-flow statements, DML statements, DDL statements, some DCL statements, global variables and a return statement. Stored procedures are created with the CREATE PROCedure statement. A stored procedure must be dropped with the DROP PROCedure statement before it is re-created. The syntax to create a stored procedure is as follows:
The syntax to remove a stored procedure from a database is as follows:
Executing Stored Procedures Stored procedures can be executed using the following syntax:
This command can be executed at the SQL Server command prompt, inside the Query Analyzer, from inside another stored procedure, or from a client. User-Defined Variables Variables are defined in stored procedures using a DECLARE statement, and a mneumonic variable name that is prefaced with ‘@’. Some examples of this are as follows:
Global Variables Global variables can also be inspected inside stored procedures, for example, with the SELECT or PRINT statements. Commonly used global variables are @@VERSION, @@CONNECTIONS, @@ERROR, @@FETCH_STATUS, @@IDENTITY, @@NESTLEVEL, @@ROWCOUNT, @@SERVERNAME, @@TOTAL_ERRORS, and @@TRANCOUNT. Control-of-Flow Language The control-of-flow extensions that are available in SQL Server to build sophisticated stored procedures include the following statements: BEGIN..END, IF..ELSE, IF EXISTS, CASE ..END, WAITFOR, GOTO LABEL [label:], COMPUTE, WHILE, CONTINUE, BREAK, and RETURN. These are described in more detail in this section. BEGIN..END This command block is used to surround multiple Transact-SQL statements in stored procedures so that they are executed as a single statement. The syntax for this command is as follows:
IF..ELSE The IF..ELSE command block supported in the Control-of-flow dialect is the classical third-generation if-else construct. When the condition belonging to the IF evaluates to true, the statement immediately following the IF statement is executed and the statement belonging to the ELSE is skipped. If the condition belonging to the IF is false, then the statement corresponding to the ELSE is executed. IF..ELSE constructs can also be nested. The syntax for this command block is as follows:
IF EXISTS The IF EXISTS test is used to determine if a specific object exists within a database. The statement can be used to inspect the sysobjects sytem table that contains a row for every object in the database. An example for doing this is as follows:
CASE..END In this statement, a condition is evaluated at the start of the command block and a branch that matches the condition is then executed. Statements belonging to other conditions are skipped. Control is passed to the statement immediately following the CASE..END block after the statement is executed. The syntax for this command is as follows:
GOTO LABEL [label:] The first component of this construct consists of the GOTO LABEL code that forces an unconditional branch to the LABEL. The second component of this construct is a label: that marks a spot somewhere in the same stored procedure batch. Use of a GOTO is always controversial as a programming technique. It should be used consistently and clearly to avoid countless or untraceable branches. The syntax for this command is as follows:
WAITFOR The WAITFOR statement pauses active processing of the batch until a specific statement is true. This command is generally used in conjunction with a timer that stops the processing for a specified time increment before continuing. The syntax for this command is as follows:
COMPUTE The COMPUTE statement is used to calculate results and save them in a declared variable. The following example demonstrates use of the COMPUTE statement:
WHILE The WHILE statement is used to execute a set of statements until a condition is met. The condition can be a compound statement that combines multiple conditions using AND and OR. The syntax for this command is as follows:
CONTINUE The CONTINUE statement is used to pass control to the start of a WHILE statement where the condition is evaluated again. The syntax for this command is the word itself:
BREAK The BREAK statement is used to immediately and unconditionally exit a WHILE statement block. Control is passed to the statement immediately following the WHILE block. The syntax for this command is the word itself:
RETURN The RETURN statement is used to send a status back to the calling program and exit from the current program (e.g. RETURN 0). GENERATING ERRORS The Raiserror statement generates an error message from a stored procedure. An example of this is as follows:
or
ARITHMETIC AND BOOLEAN OPERATORS SQL Server supports the common arithmetic operators that most other development languages do, including + (addition), – (subtraction), / (division), * (multiplication), and % (modulo). A large set of boolean operators can be used in stored procedures, including > (greater than), = (greater than or equal to), (not equal to), and = (equal to). These can be used in stored procedures as well. Conclusion Stored procedures have been critical to the growing popularity of SQL Server since 1987. Despite the availability of other techniques, stored procedures continue to be popular in application architecture due to their power, performance boosts, security augmentation, and support for n-tier architecture. Stored procedures support sophisticated language constructs including a rich control-of-flow language, DDL statements, DML statements, DCL statements, global variables, and functions. About the author:Sanjiv Purba is a Senior Manager with Deloitte Consulting. He is the author of five books published by John Wiley, the most recent of which is Building Microsoft SQL Server 7 Applications with COM. |
Ethics and Artificial Intelligence: Driving Greater Equality
FEATURE | By James Maguire,
December 16, 2020
AI vs. Machine Learning vs. Deep Learning
FEATURE | By Cynthia Harvey,
December 11, 2020
Huawei’s AI Update: Things Are Moving Faster Than We Think
FEATURE | By Rob Enderle,
December 04, 2020
Keeping Machine Learning Algorithms Honest in the ‘Ethics-First’ Era
ARTIFICIAL INTELLIGENCE | By Guest Author,
November 18, 2020
Key Trends in Chatbots and RPA
FEATURE | By Guest Author,
November 10, 2020
FEATURE | By Samuel Greengard,
November 05, 2020
ARTIFICIAL INTELLIGENCE | By Guest Author,
November 02, 2020
How Intel’s Work With Autonomous Cars Could Redefine General Purpose AI
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
October 29, 2020
Dell Technologies World: Weaving Together Human And Machine Interaction For AI And Robotics
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
October 23, 2020
The Super Moderator, or How IBM Project Debater Could Save Social Media
FEATURE | By Rob Enderle,
October 16, 2020
FEATURE | By Cynthia Harvey,
October 07, 2020
ARTIFICIAL INTELLIGENCE | By Guest Author,
October 05, 2020
CIOs Discuss the Promise of AI and Data Science
FEATURE | By Guest Author,
September 25, 2020
Microsoft Is Building An AI Product That Could Predict The Future
FEATURE | By Rob Enderle,
September 25, 2020
Top 10 Machine Learning Companies 2021
FEATURE | By Cynthia Harvey,
September 22, 2020
NVIDIA and ARM: Massively Changing The AI Landscape
ARTIFICIAL INTELLIGENCE | By Rob Enderle,
September 18, 2020
Continuous Intelligence: Expert Discussion [Video and Podcast]
ARTIFICIAL INTELLIGENCE | By James Maguire,
September 14, 2020
Artificial Intelligence: Governance and Ethics [Video]
ARTIFICIAL INTELLIGENCE | By James Maguire,
September 13, 2020
IBM Watson At The US Open: Showcasing The Power Of A Mature Enterprise-Class AI
FEATURE | By Rob Enderle,
September 11, 2020
Artificial Intelligence: Perception vs. Reality
FEATURE | By James Maguire,
September 09, 2020
Datamation is the leading industry resource for B2B data professionals and technology buyers. Datamation's focus is on providing insight into the latest trends and innovation in AI, data security, big data, and more, along with in-depth product recommendations and comparisons. More than 1.7M users gain insight and guidance from Datamation every year.
Advertise with TechnologyAdvice on Datamation and our other data and technology-focused platforms.
Advertise with Us
Property of TechnologyAdvice.
© 2025 TechnologyAdvice. All Rights Reserved
Advertiser Disclosure: Some of the products that appear on this
site are from companies from which TechnologyAdvice receives
compensation. This compensation may impact how and where products
appear on this site including, for example, the order in which
they appear. TechnologyAdvice does not include all companies
or all types of products available in the marketplace.