Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Parameter passing Expand / Collapse
Posted Wednesday, February 6, 2008 4:00 PM


Group: General Forum Members
Last Login: Monday, April 12, 2010 11:09 PM
Points: 15, Visits: 44
hi guys,
i know how to pass input parameter to stored procs.
why do we use output parameters in stored procs?
would anyone suggest an example for that kind of stored procs?

other question is about dynamic sql
can i able to use the where clause in dynamic sql?
eg: 'select gemName,gemType,State
from GEmTable where country=%A'

when i used dynamic sql in my scripts using where clause,i had an error message.

thank you very much for your time.

Post #452480
Posted Wednesday, February 6, 2008 8:03 PM



Group: Administrators
Last Login: Today @ 1:27 PM
Points: 34,363, Visits: 18,581
An output parameter can be used to assign a variable the result of a stored procedure. Less used now that we have user-defined functions.

For dynamic Sql, it's something like

declare @cmd varchar(8000), @a char(2)
select @a = 'US'

select @cmd = 'select gemName,gemType,State
from GEmTable where country=' + @A


That's dynamic SQL. A regular query would be

select gemName, gemType, State from GemTable where country = @a

Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #452526
Posted Thursday, February 7, 2008 5:44 AM



Group: General Forum Members
Last Login: Today @ 4:52 AM
Points: 17,172, Visits: 32,134
I'm pretty sure I'm old fashioned, but I still use OUTPUT parameters in some situations. Let's say you have a situation where the application normally creates a row in a table and then creates rows in the child table. The usual call is to spcTable1 which returns the SCOPE_IDENTITY for the row it inserted and the app then passes this on to spcTable2. But what if you also have to support a set based approach where you receive an XML file or something and need to process the same procedures in a single stored proc:

Declare @MyId int
EXEC spcTable1 @Param1 = 'SomeValue', @Param2 = @MyId OUTPUT
--this executes the procedure and captures the output which you then pass on to the next proc

EXEC spcTable2 @Param3 = @MyId

It's a simplistic example, but it gives you the idea.

"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server Query Performance Tuning
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #452671
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse