How to return PrimaryKey, Inserted datetime

  • Hi 

    I have a Procedure

    CREATE PROCEDURE ReturnValues(
    @ReturnValuesID INT OUT
    @MemberID INT
    @Name  VARCHAR(10)
    @InsertedDate DATETIME OUT
    )

    INSERT INTO dbTable(
    MemberID
    ,Name
    )
    VALUES(
    @MemberID
    ,@Name
    )

    I want to return the primary key and the Inserted time.

    I thought of this

    DECLARE @MyTableVar TABLE(
    PK INT
    ,InsertedDt DATETIME
    )

    INSERT INTO @MyTableVar
    OUTPUT inserted.ReturnValuesID, inserted.InsertedDate

    But I'm getting this error:
    he OUTPUT clause cannot be used in an INSERT...EXEC statement.

    Is there any better solution or what am I doing wrong?

  • If you're using the procedure in an INSERT INTO ... EXEC... Then you need to be very careful. You can't add more resultsets to the procedure, as they're break the insert. Given that, how do you want to return those two columns?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • You could do in in a merge however you may want to consider this: ALTER TABLE dbTable ADD DateCreated datetime CONSTRAINT DFdbTableDateCreated DEFAULT GetDate()

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply