Call now: (800) 766-1884  


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA

 SQL Server Scripts
 Scripts Menu




  SQL Server Tips by Gama and Naughter


SP with output in the message window

Of course the first stored procedure to be analyzed has to be “Hello World!”:

--typical hello world with output in the message window
PRINT 'Hello World!'

The stored procedure can be created in Enterprise Manager by right clicking in the stored procedure branch of the selected database. One of the options is “New stored procedure” and typing the code in the edit window. As an alternative, the code executes in SQL Query analyzer with the same result.

Note: calling an SP requires the keyword EXEC, unless it is only one SQL statement. For example:

Valid Valid Valid Error helloworld helloworld
helloworld EXEC helloworld
EXEC helloworld helloworld
helloworld Table 27.1 – correct and incorrect ways to call multiple statements

SP with output in the grid window (rowset)

If an application connects to SQL Server through ADO and executes the SP it will get no output from the previous example because ADO can get data from an SP via an output parameter or a recordset. Let us rewrite the SP so that it will return a row of data:

--typical hello world with output in the grid window
SELECT 'Hello World!' AS [Hello]

SP with multiple output rows in the grid window

An SP can return the entire contents of a table, which might consist of one or more columns and zero or more rows. If the SP returns the data from an existing table, be it temporary or not, the number of rows will depend on the contents of the table and the filter conditions, if there are any. Another possible scenario is when each row of data has a different source and they are all combined into one rowset by using the UNION clause.

--typical hello world with output in the grid window, several rows of output
SELECT 'Hello' AS [Hello]
SELECT 'dear' AS [Hello]
SELECT 'World!' AS [Hello]

SP with Input parameter, output in the message window

SP’s have input parameters that will allow data passed during the call and used internally. In this example, the SP has an input parameter “name” and the output is a salute plus the input. It is a variation of “Hello World!” and the twist is that it returns “Hello <name>!” where <name> is the input parameter data.

--SP with one input parameter, output in the message window
PRINT 'Hello '+@name+'!'

SP with Input parameter, output in the grid window

The output can also be a rowset as in one of the previous examples.

--SP with one input parameter, output in the grid window
select 'Hello '+@name+'!' AS [Hi]

SP with Input and Output parameters

The output of an SP could also be returned in an output parameter. This way a variable is used to store the output from the SP. This example is very simple: it changes the first character of the input to upper case and the rest to lower case.

--Capitalizes first char, changes the rest to lower case
--SP with one input parameter and one output parameter
SET @nameCap=UPPER(LEFT(@name,1))+LOWER(RIGHT(@name,LEN(@name)-1))

SP with a parameter that is both Input and Output

In some situations, there is no need to store the output into an extra variable because the output must replace the input.

--Capitalizes first char, changes the rest to lower case
--SP with one input/output parameter
SET @name=UPPER(LEFT(@name,1))+LOWER(RIGHT(@name,LEN(@name)-1))

SP with named parameters

The SP cap1 from the first example is fine for this example, as well. So far these examples called SP’s with the parameters specified by their location, that is, the position of the values to be passed to the SP defines which parameters will receive those values. It is very easy to call an SP with named parameters:

* The advantages of the order of the parameters becoming irrelevant are:
* Adding parameters to the SP without having to supply unnecessary ones when calling.
* Determining missing parameters for error handling or as optional parameters.
* Using default parameters without having to fill them with NULL’s.

The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter


Burleson Consulting Remote DB Administration







Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.

Burleson Consulting
SQL Server database support


Copyright © 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter