Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Location
    Houston, TX
    Posts
    32

    Unanswered: Simpled Stored Procedure help

    I'm sure there's a bafflingly simple solution. Somewhere between the first Insert and the second insert, I just need to get the new UserID from the first entry to be used in the second entry.

    I'm not entirely familiar with writing stored procedures yet.

    Code:
    CREATE PROCEDURE proc_NewUser
    	@Username nvarchar(20),		@PW nvarchar(20),
    	@SecQ Int,					@SecAnswer nvarchar(30),
    	@UserID Int = NULL,			@FirstName nvarchar(20),
    	@LastName nvarchar(30),		@EmailAddr nvarchar(50)
    		
    AS
    
    BEGIN
    	BEGIN
    		INSERT INTO tblUsers
    		(
    			Username,
    			PW,
    			SecQ,
    			SecAnswer
    		)
    		VALUES
    		(
    			@Username,
    			@PW,
    			@SecQ,
    			@SecAnswer
    		)
    	 
                 
    		INSERT INTO tblUserInfo
    		(
    			UserID,
    			FirstName,
    			LastName,
    			EmailAddr
    		)
    		VALUES
    		(
    			@UserID,
    			@FirstName,
    			@LastName,
    			@EmailAddr
    		)
    	END
    END

  2. #2
    Join Date
    Feb 2004
    Location
    Bangalore, India
    Posts
    250
    after the first insert,

    set @UserID = Scope_identity() and use it in the next insert (assuming that the user table has an identity column)
    Cheers....

    baburajv

  3. #3
    Join Date
    Nov 2011
    Location
    Houston, TX
    Posts
    32
    Hi there! I tried that and it kept giving me syntax errors.

    Does it help to mention I'm doing this in the Visual Studio IDE and not in SSMS?

  4. #4
    Join Date
    Nov 2011
    Location
    Houston, TX
    Posts
    32
    Quote Originally Posted by baburajv View Post
    after the first insert,

    set @UserID = Scope_identity() and use it in the next insert (assuming that the user table has an identity column)

    actually it did work lol

    I'm a moron. I forgot the word SET. How could I beeee so... blind.

    Thank you for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •