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. how to read returned identity value from stored procedure

how to read returned identity value from stored procedure

Scheduled Pinned Locked Moved Database
databasesysadminhelptutoriallearning
7 Posts 3 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.
  • P Offline
    P Offline
    Poonam Gandash
    wrote on last edited by
    #1

    I am inserting record in table using stored procedure. CREATE PROCEDURE add_member( @UserName Varchar( 50 ), @Password Varchar( 50 ), @dob datetime, @education Varchar(50), @designation Varchar(50), @name Varchar(50) ) AS If Exists( SELECT username FROM member WHERE username = @username ) RETURN -1 ELSE BEGIN DECLARE @ID int -- new ID value holder INSERT member ( username, password, dob, education, designation, name ) VALUES ( @username, @password, @dob, @education, @designation, @name ) -- get the new identity column value SET @id = @@IDENTITY END GO ------------------------------- I am using classic asp to add records in this way - sql = "add_member " & "'" & request.form("username") & "'" & "," & "'" & request.form("password") & "'" & "," & "'2/2/2'" & "," & request.form("education") & "," & request.form("designation") & "," & "'" & request.form("name") & "'" set rsAdd = Server.CreateObject("ADODB.Recordset") rsAdd.Open sql, con Its adding new record successfully But I am not able to read id of newly added record. Plzzzzzzz help

    A M 2 Replies Last reply
    0
    • P Poonam Gandash

      I am inserting record in table using stored procedure. CREATE PROCEDURE add_member( @UserName Varchar( 50 ), @Password Varchar( 50 ), @dob datetime, @education Varchar(50), @designation Varchar(50), @name Varchar(50) ) AS If Exists( SELECT username FROM member WHERE username = @username ) RETURN -1 ELSE BEGIN DECLARE @ID int -- new ID value holder INSERT member ( username, password, dob, education, designation, name ) VALUES ( @username, @password, @dob, @education, @designation, @name ) -- get the new identity column value SET @id = @@IDENTITY END GO ------------------------------- I am using classic asp to add records in this way - sql = "add_member " & "'" & request.form("username") & "'" & "," & "'" & request.form("password") & "'" & "," & "'2/2/2'" & "," & request.form("education") & "," & request.form("designation") & "," & "'" & request.form("name") & "'" set rsAdd = Server.CreateObject("ADODB.Recordset") rsAdd.Open sql, con Its adding new record successfully But I am not able to read id of newly added record. Plzzzzzzz help

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

      That's because you aren't returning it. You set @ID, but you need to either set an output parameter or select @ID to return it to your code.

      Bob Ashfield Consultants Ltd

      P 1 Reply Last reply
      0
      • A Ashfield

        That's because you aren't returning it. You set @ID, but you need to either set an output parameter or select @ID to return it to your code.

        Bob Ashfield Consultants Ltd

        P Offline
        P Offline
        Poonam Gandash
        wrote on last edited by
        #3

        How to return and read in asp please explain in detail Thanks in advance

        A 1 Reply Last reply
        0
        • P Poonam Gandash

          How to return and read in asp please explain in detail Thanks in advance

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

          You can either use an OUTPUT parameter in your query (search SQL Help for OUTPUT PARAMETER) or simply do a select and return the value in a recordset just like any other select statement does. An output parameter is more efficient. To be honest, its so long since I used ADODB that I can't remember the syntax for retriving output parameters, but I do remeber you have to close the recordset first, strange as it seems. Hopefully this will give you some pointers to enable you to resolve your problem after a little work on Google.

          Bob Ashfield Consultants Ltd

          1 Reply Last reply
          0
          • P Poonam Gandash

            I am inserting record in table using stored procedure. CREATE PROCEDURE add_member( @UserName Varchar( 50 ), @Password Varchar( 50 ), @dob datetime, @education Varchar(50), @designation Varchar(50), @name Varchar(50) ) AS If Exists( SELECT username FROM member WHERE username = @username ) RETURN -1 ELSE BEGIN DECLARE @ID int -- new ID value holder INSERT member ( username, password, dob, education, designation, name ) VALUES ( @username, @password, @dob, @education, @designation, @name ) -- get the new identity column value SET @id = @@IDENTITY END GO ------------------------------- I am using classic asp to add records in this way - sql = "add_member " & "'" & request.form("username") & "'" & "," & "'" & request.form("password") & "'" & "," & "'2/2/2'" & "," & request.form("education") & "," & request.form("designation") & "," & "'" & request.form("name") & "'" set rsAdd = Server.CreateObject("ADODB.Recordset") rsAdd.Open sql, con Its adding new record successfully But I am not able to read id of newly added record. Plzzzzzzz help

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Add this as you last line. Select @ID as ID

            Never underestimate the power of human stupidity RAH

            P 1 Reply Last reply
            0
            • M Mycroft Holmes

              Add this as you last line. Select @ID as ID

              Never underestimate the power of human stupidity RAH

              P Offline
              P Offline
              Poonam Gandash
              wrote on last edited by
              #6

              Then How to read this value in asp

              M 1 Reply Last reply
              0
              • P Poonam Gandash

                Then How to read this value in asp

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                The resilt can be used as either executescalar directly to a varaible or as a dataset/table using .fill.

                Never underestimate the power of human stupidity RAH

                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