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. I want to use @@indentity Scalar variable in Stored procedure

I want to use @@indentity Scalar variable in Stored procedure

Scheduled Pinned Locked Moved Database
databasequestion
6 Posts 4 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.
  • M Offline
    M Offline
    Muhammad Fahim Baloch
    wrote on last edited by
    #1

    Hy to All; I want to use two insert statement in one stored procedure I have Two Tables "Activitity" (ActivitityId as a primery key) and "TODo" Second Table (TODOId as a primery) and (ActivitityId as a foriegn key) .... So when a value insert into "Activitity" Table ,I want to insert its primery key into "TODO" Table as Foriegn key in a single Stored procedure... So for this purpose I want to use @@indentity variable how can I use @@indentity variable in my stored procedure....

    B M N 3 Replies Last reply
    0
    • M Muhammad Fahim Baloch

      Hy to All; I want to use two insert statement in one stored procedure I have Two Tables "Activitity" (ActivitityId as a primery key) and "TODo" Second Table (TODOId as a primery) and (ActivitityId as a foriegn key) .... So when a value insert into "Activitity" Table ,I want to insert its primery key into "TODO" Table as Foriegn key in a single Stored procedure... So for this purpose I want to use @@indentity variable how can I use @@indentity variable in my stored procedure....

      B Offline
      B Offline
      Bassam Saoud
      wrote on last edited by
      #2

      if you have a column set as identity you can use SCOPE_IDENTITY() DECLARE @MyNewId INT SET @MyNewId = SCOPE_IDENTITY()

      1 Reply Last reply
      0
      • M Muhammad Fahim Baloch

        Hy to All; I want to use two insert statement in one stored procedure I have Two Tables "Activitity" (ActivitityId as a primery key) and "TODo" Second Table (TODOId as a primery) and (ActivitityId as a foriegn key) .... So when a value insert into "Activitity" Table ,I want to insert its primery key into "TODO" Table as Foriegn key in a single Stored procedure... So for this purpose I want to use @@indentity variable how can I use @@indentity variable in my stored procedure....

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

        So store the @@Identity or Scope_Identity() in a variable directly after inserting the activity record and use it in the second insert for todo, there you have your relationship

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • M Muhammad Fahim Baloch

          Hy to All; I want to use two insert statement in one stored procedure I have Two Tables "Activitity" (ActivitityId as a primery key) and "TODo" Second Table (TODOId as a primery) and (ActivitityId as a foriegn key) .... So when a value insert into "Activitity" Table ,I want to insert its primery key into "TODO" Table as Foriegn key in a single Stored procedure... So for this purpose I want to use @@indentity variable how can I use @@indentity variable in my stored procedure....

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #4

          Even you can go ahead with OUTPUT.INSERTED e.g.

          insert into Activitity
          output inserted.ActivitityId into TODo(ActivitityId)
          values(1,'somerecord')

          select * from TODo Output:

          TODOId ActivitityId
          1 1

          :)

          Niladri Biswas

          M 1 Reply Last reply
          0
          • N Niladri_Biswas

            Even you can go ahead with OUTPUT.INSERTED e.g.

            insert into Activitity
            output inserted.ActivitityId into TODo(ActivitityId)
            values(1,'somerecord')

            select * from TODo Output:

            TODOId ActivitityId
            1 1

            :)

            Niladri Biswas

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

            Now that's cool, I was not aware INSERTED worked for an ordinary insert statement, I thought it was only for triggers. Now to go play, I wonder is UPDATED works as well

            Never underestimate the power of human stupidity RAH

            N 1 Reply Last reply
            0
            • M Mycroft Holmes

              Now that's cool, I was not aware INSERTED worked for an ordinary insert statement, I thought it was only for triggers. Now to go play, I wonder is UPDATED works as well

              Never underestimate the power of human stupidity RAH

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

              Hi Mycroft, You are right. It is applicable for Insert, Update & Delete. Have a look a) OUTPUT Clause (Transact-SQL)[^] b) SQL SERVER – 2005 – OUTPUT Clause Example and Explanation with INSERT, UPDATE, DELETE[^]

              Niladri Biswas

              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