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. Web Development
  3. ASP.NET
  4. SqlParameter Direction

SqlParameter Direction

Scheduled Pinned Locked Moved ASP.NET
databasecomtutorialquestionworkspace
4 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.
  • J Offline
    J Offline
    Jon G
    wrote on last edited by
    #1

    Is it possible to have a SqlParameter setup as an Output, and get it to return @@Identity without using a stored procedure? I have an INSERT statement, written as TEXT in my code (at the time being, I cannot create stored procedures). I'm trying to find out how to return the created IDENTITY that was generated. Can someone please explain to me how this works? Thanks. Jon G www.Gizmocoder.com

    M M 2 Replies Last reply
    0
    • J Jon G

      Is it possible to have a SqlParameter setup as an Output, and get it to return @@Identity without using a stored procedure? I have an INSERT statement, written as TEXT in my code (at the time being, I cannot create stored procedures). I'm trying to find out how to return the created IDENTITY that was generated. Can someone please explain to me how this works? Thanks. Jon G www.Gizmocoder.com

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi Jon. Immediately following the execution of your INSERT SqlCommand object, execute a SqlCommand object with "Select @@Identitiy" as the CommandText. Something like this (cmd is a SqlCommand object and insertedID is an int):

      cmd.CommandText = "SELECT @@Identity";
      cmd.CommandType = CommandType.Text;
      cmd.Parameters.Clear();
      insertedID = (int)cmd.ExecuteScalar();

      1 Reply Last reply
      0
      • J Jon G

        Is it possible to have a SqlParameter setup as an Output, and get it to return @@Identity without using a stored procedure? I have an INSERT statement, written as TEXT in my code (at the time being, I cannot create stored procedures). I'm trying to find out how to return the created IDENTITY that was generated. Can someone please explain to me how this works? Thanks. Jon G www.Gizmocoder.com

        M Offline
        M Offline
        Murugavel S
        wrote on last edited by
        #3

        Instead of selecting @@Identity use SCOPE_IDENTITY(). Eventhough, it returns values inserted into IDENTITY columns, SCOPE_IDENTITY will return the value of scope on which executed. For further reference, Visit SQL books online on MSDN Cheers ;)

        J 1 Reply Last reply
        0
        • M Murugavel S

          Instead of selecting @@Identity use SCOPE_IDENTITY(). Eventhough, it returns values inserted into IDENTITY columns, SCOPE_IDENTITY will return the value of scope on which executed. For further reference, Visit SQL books online on MSDN Cheers ;)

          J Offline
          J Offline
          Jon G
          wrote on last edited by
          #4

          Thanks for your input guys. I got it working great. Jon G www.Gizmocoder.com

          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