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. Little help needed with a Stored Proc

Little help needed with a Stored Proc

Scheduled Pinned Locked Moved Database
databasesecurityhelpquestioncareer
5 Posts 4 Posters 1 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.
  • A Offline
    A Offline
    Aptiva Dave
    wrote on last edited by
    #1

    I am currently writing a stored proc for a database that will help create a new user. The logic I am using is something I had used before, but then I had used three separate stored procs and thought it would be more effecient to just call one stored proc to handle the whole job. Here are the tables I am using: users: userID (primary key, IsIdentity turned on) firstName lastName sex authenticationID (foreign key that points back to the authentication table) contactID (foreign key that points back to the contact table) authentication: authenticationID (primary key, IsIdentity turned on) userName password contact: contactID (primary key, IsIdentity turned on) street city state emailAddress In my stored proc, I pass in all of the parameters needed for each table (listed above, except, of course, the identity fields) and start the inserts. Since I need the contact and authentication inserts done first, I naturally would want to start with those tables, but how would I get the ID fields for each tables so I can insert them into the users table?

    L C A M 4 Replies Last reply
    0
    • A Aptiva Dave

      I am currently writing a stored proc for a database that will help create a new user. The logic I am using is something I had used before, but then I had used three separate stored procs and thought it would be more effecient to just call one stored proc to handle the whole job. Here are the tables I am using: users: userID (primary key, IsIdentity turned on) firstName lastName sex authenticationID (foreign key that points back to the authentication table) contactID (foreign key that points back to the contact table) authentication: authenticationID (primary key, IsIdentity turned on) userName password contact: contactID (primary key, IsIdentity turned on) street city state emailAddress In my stored proc, I pass in all of the parameters needed for each table (listed above, except, of course, the identity fields) and start the inserts. Since I need the contact and authentication inserts done first, I naturally would want to start with those tables, but how would I get the ID fields for each tables so I can insert them into the users table?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The sproc could return it using a output[^]-parameter. Or you could write a function instead of an sproc, and have that return the identity. Or you check the latest identity values for your transaction :)

      I are Troll :suss:

      1 Reply Last reply
      0
      • A Aptiva Dave

        I am currently writing a stored proc for a database that will help create a new user. The logic I am using is something I had used before, but then I had used three separate stored procs and thought it would be more effecient to just call one stored proc to handle the whole job. Here are the tables I am using: users: userID (primary key, IsIdentity turned on) firstName lastName sex authenticationID (foreign key that points back to the authentication table) contactID (foreign key that points back to the contact table) authentication: authenticationID (primary key, IsIdentity turned on) userName password contact: contactID (primary key, IsIdentity turned on) street city state emailAddress In my stored proc, I pass in all of the parameters needed for each table (listed above, except, of course, the identity fields) and start the inserts. Since I need the contact and authentication inserts done first, I naturally would want to start with those tables, but how would I get the ID fields for each tables so I can insert them into the users table?

        C Offline
        C Offline
        Corporal Agarn
        wrote on last edited by
        #3

        Use the @@IDENTITY function within the SP

        1 Reply Last reply
        0
        • A Aptiva Dave

          I am currently writing a stored proc for a database that will help create a new user. The logic I am using is something I had used before, but then I had used three separate stored procs and thought it would be more effecient to just call one stored proc to handle the whole job. Here are the tables I am using: users: userID (primary key, IsIdentity turned on) firstName lastName sex authenticationID (foreign key that points back to the authentication table) contactID (foreign key that points back to the contact table) authentication: authenticationID (primary key, IsIdentity turned on) userName password contact: contactID (primary key, IsIdentity turned on) street city state emailAddress In my stored proc, I pass in all of the parameters needed for each table (listed above, except, of course, the identity fields) and start the inserts. Since I need the contact and authentication inserts done first, I naturally would want to start with those tables, but how would I get the ID fields for each tables so I can insert them into the users table?

          A Offline
          A Offline
          Aptiva Dave
          wrote on last edited by
          #4

          Nevermind. Did a little bit of experimentation and got it to work. Thanks for all the input though.

          1 Reply Last reply
          0
          • A Aptiva Dave

            I am currently writing a stored proc for a database that will help create a new user. The logic I am using is something I had used before, but then I had used three separate stored procs and thought it would be more effecient to just call one stored proc to handle the whole job. Here are the tables I am using: users: userID (primary key, IsIdentity turned on) firstName lastName sex authenticationID (foreign key that points back to the authentication table) contactID (foreign key that points back to the contact table) authentication: authenticationID (primary key, IsIdentity turned on) userName password contact: contactID (primary key, IsIdentity turned on) street city state emailAddress In my stored proc, I pass in all of the parameters needed for each table (listed above, except, of course, the identity fields) and start the inserts. Since I need the contact and authentication inserts done first, I naturally would want to start with those tables, but how would I get the ID fields for each tables so I can insert them into the users table?

            M Offline
            M Offline
            Martin Arapovic
            wrote on last edited by
            #5

            I think this is what you need. Just fill the blanks....

            -- =============================================
            -- Author:		Martin Arapovic
            -- Create date: 19.01.2011
            -- Description:	....
            -- =============================================
            CREATE PROCEDURE InsertUserData 
            	(
            		-- Add the parameters for the stored procedure here
            		-- Define needed parameters
            		-- Contact Input Data
            		@Street VARCHAR(50), 
            		@City VARCHAR(50), 
            		@State VARCHAR(50), 
            		@EmailAddress VARCHAR(50),
            		--Users Input Data
            		@UserName VARCHAR(25), 
            		@Password VARCHAR(25),
            		-- User Input Data
            		@FirstName varchar(50),
            		@LastName varchar(50),
            		@Sex varchar(50)
            		-- Add other paramas if you have any
            	)
            AS
            BEGIN
            	-- SET NOCOUNT ON added to prevent extra result sets from
            	-- interfering with SELECT statements.
            	SET NOCOUNT ON;
            
            	DECLARE @ContactID INT 
            	DECLARE @AuthenticationID INT 
            	
            	--
            	-- Note: This is only skeleton of the proc that you want to implement
            	
            	--
            	-- Do all in one transaction
            	
            	BEGIN TRANSACTION
            	
            	--
                -- Insert Contact data and get ContactID
                INSERT INTO [Contact]
                       ([Street]
                       ,[City]
                       ,[State]
                       ,[EmailAddress])
                 VALUES
                       (@Street
                       ,@City
                       ,@State
                       ,@EmailAddress)
                       
                -- GET CointactID 
                SET @ContactID = SCOPE_IDENTITY() 
                 
                --
                -- Insert Authentication data and get AuthenticationID
                INSERT INTO [Authentication]
                       ([UserName]
                       ,[Password])
                 VALUES
                       (@UserName
                       ,@Password)
                       
                -- GET Authentication 
                SET @AuthenticationID = SCOPE_IDENTITY() 
                 
                -- 
                -- Insert Users data into users table.
                INSERT INTO [Users]
                       ([FirstName]
                       ,[LastName]
                       ,[Sex]
                       ,[AuthenticationID]
                       ,[ContactID])
                 VALUES
                       (@FirstName
                       ,@LastName
                       ,@Sex
                       ,@AuthenticationID
                       ,@ContactID)
            
                 -- Commit
                 COMMIT TRANSACTION
                 
                 -- This is the general example and you need to add error handling and 
                 -- other bussines logic if you have any... :)
                       	
            END
            GO
            
            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