ASP/ADO: Return a value from a Stored Procedure

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default ASP/ADO: Return a value from a Stored Procedure

    I generally use the following code to call a stored procedure:

    sSQL = "Exec MySP " & param1 & ", " & param2
    [Set oRS =] oConn.Execute (sSQL)

    .... or something like this.

    AFAIK it's quite an efficient way to query a DB via ADO - Less overhead than
    a command object.

    However, I'm not sure how I should handle where I want the SP to return a
    value.

    For example, I want to do an Insert. I'd like to be able to return one of
    several return values:

    Scope_Identity() if OK
    '1' if record already exists
    '0' if any other error

    I can do this currently by returning a derived recordset, ie, 'Select
    Scope_Identity() as ReturnVal' or 'Select 1 as ReturnVal'.
    I can then query this recordset to find out the result.

    I know that SP's can have OUTPUT parameters, and that you these can be used
    in conjunction with the Command Object/Parameters.

    The question is, can my original technique be modified to handle these
    output parameters?

    If not, what are the relative merits in using a Command object, vs return a
    recordset contain the value?

    Thanks in advance


    CJM Guest

  2. Similar Questions and Discussions

    1. Trouble getting stored procedure return value!?
      This is my first stored procedure so go easy on me. Procedure runs fine except I can't get a value into the return value @RecordCount. CREATE...
    2. Can't get return Value from Stored Procedure
      I'm trying to get the @@IDENTITY value back from my stored procedure in T-SQL to use it in later code. I haven't been able to get it to work. ...
    3. Using stored procedure to return a whole row of data, without using record set?
      I now know that I cannot use client application written in embedded SQL to receive record sets, that only an application using CLI can receive...
    4. Stored Procedure has both return value and data set (SqlDataReader)
      Greetings! I met the same question as in ADO a few months ago. I'm working on MS SQL Server 2000. I have a stored procedure that returns a...
    5. Getting Return Value of Stored Procedure
      Hello Friends ! I have the Following Code, that Executes a Stored Procedure and Attempt to read a Returned Integer Value from the StoredProc....
  3. #2

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    [url]http://www.aspfaq.com/params.htm[/url]

    (FTI, this is one of the few articles on my site not written by me.)

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)




    "CJM" <cjmnews04@newsgroups.nospam> wrote in message
    news:#pXpJCcaEHA.2840@TK2MSFTNGP11.phx.gbl...
    > I generally use the following code to call a stored procedure:
    >
    > sSQL = "Exec MySP " & param1 & ", " & param2
    > [Set oRS =] oConn.Execute (sSQL)
    >
    > ... or something like this.
    >
    > AFAIK it's quite an efficient way to query a DB via ADO - Less overhead
    than
    > a command object.
    >
    > However, I'm not sure how I should handle where I want the SP to return a
    > value.
    >
    > For example, I want to do an Insert. I'd like to be able to return one of
    > several return values:
    >
    > Scope_Identity() if OK
    > '1' if record already exists
    > '0' if any other error
    >
    > I can do this currently by returning a derived recordset, ie, 'Select
    > Scope_Identity() as ReturnVal' or 'Select 1 as ReturnVal'.
    > I can then query this recordset to find out the result.
    >
    > I know that SP's can have OUTPUT parameters, and that you these can be
    used
    > in conjunction with the Command Object/Parameters.
    >
    > The question is, can my original technique be modified to handle these
    > output parameters?
    >
    > If not, what are the relative merits in using a Command object, vs return
    a
    > recordset contain the value?
    >
    > Thanks in advance
    >
    >

    Aaron [SQL Server MVP] Guest

  4. #3

    Default Re: ASP/ADO: Return a value from a Stored Procedure


    "Aaron [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
    news:e$V1mDcaEHA.3356@tk2msftngp13.phx.gbl...
    > [url]http://www.aspfaq.com/params.htm[/url]
    >
    > (FTI, this is one of the few articles on my site not written by me.)
    >
    Thanks Aaron.

    I take it that the article was a long way of saying 'use the command object
    for returned values'? lol. I suspected as much, and it's not a problem
    really.

    Thanks

    Chris


    CJM Guest

  5. #4

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    Hi Chris,

    I think you original way cannot get a return value from the store
    procedure. You have to use Command object and OutPut parameter, or return a
    recordset. Based on my experience, there wouldn't be a big lost on
    performance when we use command object.

    Luke

    [MSFT] Guest

  6. #5

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    Hopefully I am not missing anything in your text, but can't you just
    do a select at the end of your insert statement. Please see psuedo
    Stored Proc SQL:

    if not exists (select ... from Table where) begin
    Insert into Table () Values
    select 0 as RetVal, 'OK' as ErrMsg
    end else begin
    select 1, 'record already exists' as ErrMsg
    end

    Just add the not exists to your existing stored procs.

    HTH

    "CJM" <cjmnews04@newsgroups.nospam> wrote in message news:<#pXpJCcaEHA.2840@TK2MSFTNGP11.phx.gbl>...
    > I generally use the following code to call a stored procedure:
    >
    > sSQL = "Exec MySP " & param1 & ", " & param2
    > [Set oRS =] oConn.Execute (sSQL)
    >
    > ... or something like this.
    >
    > AFAIK it's quite an efficient way to query a DB via ADO - Less overhead than
    > a command object.
    >
    > However, I'm not sure how I should handle where I want the SP to return a
    > value.
    >
    > For example, I want to do an Insert. I'd like to be able to return one of
    > several return values:
    >
    > Scope_Identity() if OK
    > '1' if record already exists
    > '0' if any other error
    >
    > I can do this currently by returning a derived recordset, ie, 'Select
    > Scope_Identity() as ReturnVal' or 'Select 1 as ReturnVal'.
    > I can then query this recordset to find out the result.
    >
    > I know that SP's can have OUTPUT parameters, and that you these can be used
    > in conjunction with the Command Object/Parameters.
    >
    > The question is, can my original technique be modified to handle these
    > output parameters?
    >
    > If not, what are the relative merits in using a Command object, vs return a
    > recordset contain the value?
    >
    > Thanks in advance
    CST Guest

  7. #6

    Default Re: ASP/ADO: Return a value from a Stored Procedure


    "CST" <cont@b-50.com> wrote in message
    news:bd314cc7.0407200342.17361cd3@posting.google.c om...
    > Hopefully I am not missing anything in your text, but can't you just
    > do a select at the end of your insert statement. Please see psuedo
    > Stored Proc SQL:
    >
    > if not exists (select ... from Table where) begin
    > Insert into Table () Values
    > select 0 as RetVal, 'OK' as ErrMsg
    > end else begin
    > select 1, 'record already exists' as ErrMsg
    > end
    >
    > Just add the not exists to your existing stored procs.
    >
    > HTH
    Yes, you can. This is the way I currently do it. I was just wondering if
    there are better ways without using a command object, and it appears there
    aren't.

    Chris


    CJM Guest

  8. #7

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    You wouldn't have to use the command object. Say the insert statement
    returns 2 fields (errnbr, errdesc)

    set rs = adoConn.Execute(Some Stored Proc)

    strErr = rs("ErrNbr")
    strErrDesc = rs("ErrDesc")

    rs.close
    set rs = nothing

    Much easier and a better way than the cmd object.




    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    C T Guest

  9. #8

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    Blimey! This is getting confusing...

    I currently use the method that yourself & CST are using! That much is clear
    from my original post.

    CJM


    CJM Guest

  10. #9

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    Sorry for the confusion, I was working off of the example provided by
    CST (cool initials BTW). Are you still having problems or do you need
    clerification on my post?



    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    C T Guest

  11. #10

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    C T wrote:
    > You wouldn't have to use the command object. Say the insert statement
    > returns 2 fields (errnbr, errdesc)
    >
    > set rs = adoConn.Execute(Some Stored Proc)
    >
    > strErr = rs("ErrNbr")
    > strErrDesc = rs("ErrDesc")
    >
    > rs.close
    > set rs = nothing
    >
    > Much easier and a better way than the cmd object.
    >
    It may be easier (I disagrree that it is "much" easier), but I disagree that
    it is better to retrieve a relatively large resultset from the database and
    marshal it across processes into a needlessly large COM object (the
    recordset) when you could be retrieving two values.

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  12. #11

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    > Yes, you can. This is the way I currently do it. I was just wondering if
    > there are better ways without using a command object, and it appears there
    > aren't.
    What do you mean? If you are returning a resultset, and not using output /
    return values, then you can avoid a command object.

    CREATE PROCEDURE dbo.foo
    AS
    BEGIN
    SET NOCOUNT ON
    IF NOT EXISTS (SELECT ... FROM table WHERE ...)
    BEGIN
    INSERT Table VALUES(...)
    SELECT 0
    ELSE
    SELECT 1
    END
    GO

    Now from ASP:

    set rs = conn.execute("EXEC dbo.foo")
    response.write rs(0)

    If you are using explicit output variables or return values, then yes, you
    do need to use a command object, because the connection object does not
    provide that functionality on its own.

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)


    Aaron [SQL Server MVP] Guest

  13. #12

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    C T wrote:
    > You wouldn't have to use the command object. Say the insert statement
    > returns 2 fields (errnbr, errdesc)
    >
    > set rs = adoConn.Execute(Some Stored Proc)
    >
    > strErr = rs("ErrNbr")
    > strErrDesc = rs("ErrDesc")
    >
    > rs.close
    > set rs = nothing
    >
    > Much easier and a better way than the cmd object.
    >
    >
    I forgot to mention that this method leaves you open to SQL Injection. As
    seen in these links,

    [url]http://www.nextgenss.com/papers/advanced_sql_injection.pdf[/url]
    [url]http://www.nextgenss.com/papers/more_advanced_sql_injection[/url].

    even the Replace() defensive measure that is so often touted can be
    defeated. The best way to prevent SQL Injection is to use a Command object
    to pass parameters (or, if no return or output parameters are involved, the
    "stored-procedure-as-connection-method")

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  14. #13

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    Not to sound like an arse, but I don't think that CJM is returning a
    large resultset. I may be wrong, but I think that it is just 1 record
    stating if the insert was successful or not. I don't think that this
    will have that big of an impact on performance. Then again, I don't
    fully understand the differences between overhead for both the exec vs
    the cmd object.



    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    C T Guest

  15. #14

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    C T wrote:
    > Not to sound like an arse, but I don't think that CJM is returning a
    > large resultset.
    Any resultset is large compared to the data it contains, especially if it
    contains only numeric data (if you're retrieving very large text fields,
    then the data may exceed the metadata). It is always expensive to marshal a
    resultset across processes into a COM recordset.

    Recordsets are designed to be used where scrolling is necessary, i.e., where
    multiple records are involved. They are overkill when you are retrieving a
    few numeric values in a single record.

    It is up to the developer what kinds of tradeoffs re. performance,
    resources, and ease of programming he wishes to make. Just remember that on
    a web server, an application is not working in isolation. While an app may
    be performing adequately when run by itself, it may impact performance of
    the web server as a whole.

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  16. #15

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    This thread doesnt seem to be dying a death, but I think my original
    question has been answered.

    To summarise, there are two suggested methods:

    1) Using a Command object/output parameters
    2) Using a simple Select statement to return a value, eg. 'Select 1'

    From what I understand, there is not a lot in it. A Command object may be
    unnecessary for many operations, and there clearly are ways to avoid using
    them. However, Bob suggests that using a Recordset to return a single value
    is not making efficient use of the [large] Recordset object either.

    So I guess you have to look at each situation individually, and choose an
    approach based on the contributing factors. In most cases for me, I dont
    suppose it makes much of a difference. As such, I intend to use both
    approaches, if only to make sure I am up to speed on each technique, should
    I need to be more discriminating in the future.

    I home this summarises it well.

    Thanks to all.

    Chris


    CJM Guest

  17. #16

    Default Re: ASP/ADO: Return a value from a Stored Procedure

    Hi Chris,

    Thanks very much for sharing it in the community. :)

    Best regards,
    Yanhong Huang
    Microsoft Community Support

    Get Secure! ¨C [url]www.microsoft.com/security[/url]
    Register to Access MSDN Managed Newsgroups!
    -http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
    p&SD=msdn

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Yan-Hong Huang[MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139