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. VB.NET SQL procedures to return values

VB.NET SQL procedures to return values

Scheduled Pinned Locked Moved Database
databasecsharphelp
8 Posts 6 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.
  • K Offline
    K Offline
    kakomalis
    wrote on last edited by
    #1

    Hi, I have a problem with an sql procedure. What i want to do is build a procedure that will add a record to a database table and after the addition of the record will return the id of that record. So the procedure is: CREATE PROCEDURE AddListing @Name varchar(150), @Description varchar(1000), @Keywords varchar(50), @Address varchar(200), @Telephone varchar(50), @Fax varchar(50), @Mobile varchar(50), @Email varchar(50), @URL varchar(50), @Aproved bit, @Premium bit, @UserID int, @nextID int Output --

    N E K S 5 Replies Last reply
    0
    • K kakomalis

      Hi, I have a problem with an sql procedure. What i want to do is build a procedure that will add a record to a database table and after the addition of the record will return the id of that record. So the procedure is: CREATE PROCEDURE AddListing @Name varchar(150), @Description varchar(1000), @Keywords varchar(50), @Address varchar(200), @Telephone varchar(50), @Fax varchar(50), @Mobile varchar(50), @Email varchar(50), @URL varchar(50), @Aproved bit, @Premium bit, @UserID int, @nextID int Output --

      N Offline
      N Offline
      Navi15
      wrote on last edited by
      #2

      Check out this link http://aspnet.4guysfromrolla.com/articles/062905-1.aspx Navi

      1 Reply Last reply
      0
      • K kakomalis

        Hi, I have a problem with an sql procedure. What i want to do is build a procedure that will add a record to a database table and after the addition of the record will return the id of that record. So the procedure is: CREATE PROCEDURE AddListing @Name varchar(150), @Description varchar(1000), @Keywords varchar(50), @Address varchar(200), @Telephone varchar(50), @Fax varchar(50), @Mobile varchar(50), @Email varchar(50), @URL varchar(50), @Aproved bit, @Premium bit, @UserID int, @nextID int Output --

        E Offline
        E Offline
        Eric Dahlvang
        wrote on last edited by
        #3

        It is a bad practice to get your ID using MAX(). You should define the l_id column as an IDENTITY column, and allow SQL Server to create the ID for you. Then, make your sproc like this:

        CREATE PROCEDURE AddListing
        @Name varchar(150),@Description varchar(1000),@Keywords varchar(50),
        @Address varchar(200),@Telephone varchar(50),@Fax varchar(50),
        @Mobile varchar(50),@Email varchar(50),@URL varchar(50),
        @Aproved bit,@Premium bit,@UserID int,@nextID int Output --SET @nextID = SCOPE_IDENTITY()

        INSERT INTO usr_ls_line (usr_id, l_id)
        VALUES (@UserID,@nextID)

        return @nextID

        Then, in VB.NET do something like this:

        Imports System.Data
        Imports System.Data.SqlClient

        ...
        Dim conn As New SqlConnection
        conn.ConnectionString = "Data Source=(local);" & _
        "Initial Catalog=mytestdb;" & _
        "Integrated Security=SSPI"

        Dim cmd As New SqlCommand
        cmd.Connection = conn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "AddListing"

        Dim prm1 As New SqlParameter("@Name", SqlDbType.VarChar, 150)
        prm1.Direction = ParameterDirection.Input
        cmd.Parameters.Add(prm1)
        prm1.Value = "MyStaffName"

        Dim prm2 As New SqlParameter("@Description", SqlDbType.VarChar, 1000)
        prm2.Direction = ParameterDirection.Input
        cmd.Parameters.Add(prm2)
        prm2.Value = "Whatever"

        '...

        Dim prm5 As New SqlParameter("@nextID", SqlDbType.Int) prm5.Direction = ParameterDirection.Output cmd.Parameters.Add(prm5)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()

        MsgBox("Returned ID=" + prm5.Value)

        --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        N 1 Reply Last reply
        0
        • E Eric Dahlvang

          It is a bad practice to get your ID using MAX(). You should define the l_id column as an IDENTITY column, and allow SQL Server to create the ID for you. Then, make your sproc like this:

          CREATE PROCEDURE AddListing
          @Name varchar(150),@Description varchar(1000),@Keywords varchar(50),
          @Address varchar(200),@Telephone varchar(50),@Fax varchar(50),
          @Mobile varchar(50),@Email varchar(50),@URL varchar(50),
          @Aproved bit,@Premium bit,@UserID int,@nextID int Output --SET @nextID = SCOPE_IDENTITY()

          INSERT INTO usr_ls_line (usr_id, l_id)
          VALUES (@UserID,@nextID)

          return @nextID

          Then, in VB.NET do something like this:

          Imports System.Data
          Imports System.Data.SqlClient

          ...
          Dim conn As New SqlConnection
          conn.ConnectionString = "Data Source=(local);" & _
          "Initial Catalog=mytestdb;" & _
          "Integrated Security=SSPI"

          Dim cmd As New SqlCommand
          cmd.Connection = conn
          cmd.CommandType = CommandType.StoredProcedure
          cmd.CommandText = "AddListing"

          Dim prm1 As New SqlParameter("@Name", SqlDbType.VarChar, 150)
          prm1.Direction = ParameterDirection.Input
          cmd.Parameters.Add(prm1)
          prm1.Value = "MyStaffName"

          Dim prm2 As New SqlParameter("@Description", SqlDbType.VarChar, 1000)
          prm2.Direction = ParameterDirection.Input
          cmd.Parameters.Add(prm2)
          prm2.Value = "Whatever"

          '...

          Dim prm5 As New SqlParameter("@nextID", SqlDbType.Int) prm5.Direction = ParameterDirection.Output cmd.Parameters.Add(prm5)

          conn.Open()
          cmd.ExecuteNonQuery()
          conn.Close()

          MsgBox("Returned ID=" + prm5.Value)

          --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          In this case yes, an IDENTITY column would work work, yet you can't make such a blanket statement that not using it is bad practice. Perhaps there is some special processing that must take place to assign an ID, or the ID field is not numeric.


          only two letters away from being an asset

          E 1 Reply Last reply
          0
          • N Not Active

            In this case yes, an IDENTITY column would work work, yet you can't make such a blanket statement that not using it is bad practice. Perhaps there is some special processing that must take place to assign an ID, or the ID field is not numeric.


            only two letters away from being an asset

            E Offline
            E Offline
            Eric Dahlvang
            wrote on last edited by
            #5

            Mark Nischalke wrote:

            you can't make such a blanket statement that not using it is bad practice

            Good point Mark.

            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            1 Reply Last reply
            0
            • K kakomalis

              Hi, I have a problem with an sql procedure. What i want to do is build a procedure that will add a record to a database table and after the addition of the record will return the id of that record. So the procedure is: CREATE PROCEDURE AddListing @Name varchar(150), @Description varchar(1000), @Keywords varchar(50), @Address varchar(200), @Telephone varchar(50), @Fax varchar(50), @Mobile varchar(50), @Email varchar(50), @URL varchar(50), @Aproved bit, @Premium bit, @UserID int, @nextID int Output --

              N Offline
              N Offline
              Navi15
              wrote on last edited by
              #6

              insted of using [SELECT MAX(l_id) FROM listings] use IDENT_CURRENT(listings) IDENT_CURRENT Returns the last identity value generated for a specified table Navi

              1 Reply Last reply
              0
              • K kakomalis

                Hi, I have a problem with an sql procedure. What i want to do is build a procedure that will add a record to a database table and after the addition of the record will return the id of that record. So the procedure is: CREATE PROCEDURE AddListing @Name varchar(150), @Description varchar(1000), @Keywords varchar(50), @Address varchar(200), @Telephone varchar(50), @Fax varchar(50), @Mobile varchar(50), @Email varchar(50), @URL varchar(50), @Aproved bit, @Premium bit, @UserID int, @nextID int Output --

                K Offline
                K Offline
                kumarprabhakar74
                wrote on last edited by
                #7

                dim comm as new sqlcommand("AddListing",connection) comm.commandtype=commandtype.storedprocedure 'For input parameters coom.parameters.add("@name","abc") comm.parameters.add("@nextid",sqldbtype.int).direction=parameterdirection.output comm.executenonquery comm.parameters("@nextid").value Thanks & Regards Kumar Prabhakar

                abc

                1 Reply Last reply
                0
                • K kakomalis

                  Hi, I have a problem with an sql procedure. What i want to do is build a procedure that will add a record to a database table and after the addition of the record will return the id of that record. So the procedure is: CREATE PROCEDURE AddListing @Name varchar(150), @Description varchar(1000), @Keywords varchar(50), @Address varchar(200), @Telephone varchar(50), @Fax varchar(50), @Mobile varchar(50), @Email varchar(50), @URL varchar(50), @Aproved bit, @Premium bit, @UserID int, @nextID int Output --

                  S Offline
                  S Offline
                  Sage
                  wrote on last edited by
                  #8

                  after your insert, add the following: Select SCOPE_IDENTITY() as NewestRecordsID

                  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