Thursday, 12 September 2013

output in insert statement sql server

 Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. These results can be returned to the processing application for use in such things as confirmation messages, archiving, and other such application requirements.

CREATE PROCEDURE [dbo].[create_batch_header]
    @MeasureCode nvarchar(50),
    @ExternalSystemCode varchar(50),
    @TotalRecordCount int,
    @StartDate datetime,
    @EndDate datetime
AS
declare @BatchIdTable table(BatchId int);
declare @msg nvarchar(1000),
        @ErrorCode int;
BEGIN
  INSERT INTO stg.BatchHeader
              (ExternalSystemCode,
               StatusCode,
               TotalRecordCount,
               MeasureCode,
               StartDate,
               EndDate)
       OUTPUT inserted.BatchId into @BatchIdTable
       VALUES (@ExternalSystemCode,
               dbo.get_constructing_batch_status(),
               @TotalRecordCount,
               @MeasureCode,
               @StartDate,
               @EndDate);
 
  SELECT BatchId from @BatchIdTable;
END

No comments:

Post a Comment