Parameter passing

  • 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.

    regards,

    thaya

  • 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

    exec(@cmd)

    That's dynamic SQL. A regular query would be

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

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply