+ Reply to Thread
Results 1 to 5 of 5

Thread: Out parameter Vs Select and ExecuteScalar() in Stored Procedure

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Out parameter Vs Select and ExecuteScalar() in Stored Procedure

    I was just wondering if there is any advantage of using an out parameter over just using a select statement after inserting a record and returning an autonumber PK field inside a Stored Procedure.

    So, would there be any difference between doing this:-
    Code:
    CREATE PROCEDURE AddSomething
    (
    name VARCHAR(100),
    OUT id INT
    )
    
    BEGIN
    
    INSERT INTO tbl
    (Name)
    VALUES
    (
    name
    )
    ;
    
    SELECT LAST_INSERT_ID() INTO id;
    
    END
    
    And then retrieving the value in .NET using a parameter object or doing this:-
    Code:
    CREATE PROCEDURE AddSomething
    (
    name VARCHAR(100)
    )
    
    BEGIN
    
    INSERT INTO tbl
    (Name)
    VALUES
    (
    name
    )
    ;
    
    SELECT LAST_INSERT_ID();
    
    END
    
    And just using command.ExecuteScalar(); to retrieve the value.

    Does one have an advantage over the other?

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    I don't see any advantage of one over the other, and generally use the latter. I'd save OUT parameters for when a store procedure would need to return a result set AND some other value(s) -- or when a sproc needs to return a value to another sproc.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by Wolffy View Post
    I don't see any advantage of one over the other, and generally use the latter. I'd save OUT parameters for when a store procedure would need to return a result set AND some other value(s) -- or when a sproc needs to return a value to another sproc.
    Cool. That makes sense...

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Tho, I suppose, using an OUT parameter would be just a tiny bit more efficient since you then use ExecNonQuery and don't seed to setup a result set.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by Wolffy View Post
    Tho, I suppose, using an OUT parameter would be just a tiny bit more efficient since you then use ExecNonQuery and don't seed to setup a result set.
    You've always got to complicate things, haven't you Wolffy!!

+ Reply to Thread

Similar Threads

  1. Replies: 3
    Last Post: June 3rd, 2010, 07:28 AM
  2. Stored Procedure 2
    By Centurion in forum ASP Development
    Replies: 13
    Last Post: June 25th, 2009, 12:37 PM
  3. SQL Job or stored procedure
    By todd2006 in forum SQL Development
    Replies: 5
    Last Post: February 11th, 2009, 02:20 PM
  4. stored procedure
    By todd2006 in forum ASP Development
    Replies: 7
    Last Post: February 5th, 2009, 03:02 PM
  5. stored procedure
    By todd2006 in forum Microsoft SQL Server
    Replies: 1
    Last Post: February 5th, 2009, 01:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

SEO by vBSEO