Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Returning 2 output parameters in Stored Procedure

Returning 2 output parameters in Stored Procedure

Scheduled Pinned Locked Moved Database
databasehelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dayekh
    wrote on last edited by
    #1

    Hi guys, I'm trying to get 2 values from a stored procedure: one telling me the execution was successful and another for telling me what the newly inserted ROW ID is. I currently have the following: USE [xyz] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[spName] ( @vchName VarChar (50), @intSuccess Numeric(9) OUT ) AS SET XACT_ABORT ON BEGIN TRANSACTION INSERT INTO Table ( vchName ) VALUES ( @vchName ) IF @@ERROR <> 0 BEGIN SELECT @intSuccess = 0 ROLLBACK END ELSE SELECT @intSuccess = 1 /* SET NOCOUNT ON */ COMMIT TRANSACTION SET XACT_ABORT OFF RETURN @intSuccess Currently it returns the value "@intSuccess" ONLY. I want to add the line "SELECT @@IDENTITY AS inLatestID" right under the insert statement so I can get the latest ID value of the record that was just newly inserted. I think I will need to return another value alongside @intSuccess, but I'm not sure how that works in the Stored Procedure

    A 1 Reply Last reply
    0
    • D Dayekh

      Hi guys, I'm trying to get 2 values from a stored procedure: one telling me the execution was successful and another for telling me what the newly inserted ROW ID is. I currently have the following: USE [xyz] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[spName] ( @vchName VarChar (50), @intSuccess Numeric(9) OUT ) AS SET XACT_ABORT ON BEGIN TRANSACTION INSERT INTO Table ( vchName ) VALUES ( @vchName ) IF @@ERROR <> 0 BEGIN SELECT @intSuccess = 0 ROLLBACK END ELSE SELECT @intSuccess = 1 /* SET NOCOUNT ON */ COMMIT TRANSACTION SET XACT_ABORT OFF RETURN @intSuccess Currently it returns the value "@intSuccess" ONLY. I want to add the line "SELECT @@IDENTITY AS inLatestID" right under the insert statement so I can get the latest ID value of the record that was just newly inserted. I think I will need to return another value alongside @intSuccess, but I'm not sure how that works in the Stored Procedure

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      You can have multiple out parameters, but you do not need to return success/failure as an out parameter. You can use standard try/catch (sql 2005/8) or raiseerror for earlier versions to check for errors. Check out BOL for more details

      Bob Ashfield Consultants Ltd

      D 1 Reply Last reply
      0
      • A Ashfield

        You can have multiple out parameters, but you do not need to return success/failure as an out parameter. You can use standard try/catch (sql 2005/8) or raiseerror for earlier versions to check for errors. Check out BOL for more details

        Bob Ashfield Consultants Ltd

        D Offline
        D Offline
        Dayekh
        wrote on last edited by
        #3

        I suppose that makes sense. Normally I check if the return value is > 0, I suppose if a Stored Procedure executes successfully, then my parameter should be > 0 and I can just check that. Thanks for your reply.

        A 1 Reply Last reply
        0
        • D Dayekh

          I suppose that makes sense. Normally I check if the return value is > 0, I suppose if a Stored Procedure executes successfully, then my parameter should be > 0 and I can just check that. Thanks for your reply.

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          A stored proc (or any other sql query) returns zero for success and non-zero for an error

          Bob Ashfield Consultants Ltd

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups