Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009

    Question Unanswered: Stored Procedure : Insert Return Insert

    Hi all,

    Im trying to write a stored procedure to:
    1) Insert mainly default values in Table 1.
    2) Retrieve the inserted primary key from Table 1
    3) Insert the Table 1 Primary key, along with other variables into Table 2.

    How do I get the return statement to work properly. Below is what I have so far.

    CREATE PROCEDURE usp_InsertPerson
    @FirstName varchar(20),
    @Surname varchar(20),
    @UserID bigint,
    @EntityID bigint
    Set @EntityID = (INSERT INTO Entity (EntityType) values ('1') return)
    INSERT INTO PersonName (EntityID,FirstName,Surname,UserID)
    Values (@EntityID,@Firstname,@Surname,@UserID)
    Thanks for any comments

  2. #2
    Join Date
    Nov 2004
    on the wrong server
    Provided Answers: 6
    1. where are your defaults defined? at the table level?
    2. your second SET statement makes no sense. Does the Entity table contain an Identity column or some other kind of self incrementing surrogate key?
    3. what you do here depends on your answer to number 2.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Feb 2009
    Hi Thrasymachus,

    Thankyou for your reply. In answer to your questions:

    1) Yes, the defaults are set at the Table level. The number I am trying to return is an Identity number defined at Table level.

    2) The Entity table has a column IDEntity. This column has its 'Identity Specification' set to Yes, Identity Increment = 1, Identiity Seed = 1 in the Table definition. It is also the primary key for the table if that helps.

    So if the second SET statement does not make sense, what is the correct method / syntax to add a value to a variable, where an INSERT command is used and the value, Identity Sequence number is returned?

Posting Permissions

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