USP_DATAFORMTEMPLATE_ADD_MKTSEGMENTATIONFINDERNUMBER

The save procedure used by the add dataform template "Marketing Effort Finder Number Add Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier INOUT The output parameter indicating the ID of the record added.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@SEGMENTATIONID uniqueidentifier IN Input parameter indicating the context ID for the record being added.
@MIN bigint IN Range
@MAX bigint IN Quantity
@VENDORID uniqueidentifier IN Reserved for
@ISRESERVED bit IN Is reserved?
@INUSE bit IN In use?

Definition

Copy


CREATE procedure dbo.[USP_DATAFORMTEMPLATE_ADD_MKTSEGMENTATIONFINDERNUMBER]
(
  @ID uniqueidentifier = null output,
  @CHANGEAGENTID uniqueidentifier = null,
  @SEGMENTATIONID uniqueidentifier,
  @MIN bigint,
  @MAX bigint,
  @VENDORID uniqueidentifier = null,
  @ISRESERVED bit = 1,
  @INUSE bit = 0
)
as
  set nocount on;

  declare @CURRENTDATE datetime;

  --Check to see if the finder number range is for a house file

  if @VENDORID = '00000000-0000-0000-0000-000000000001' set @VENDORID = null;

  if (select [ISHISTORICAL] from dbo.[MKTSEGMENTATION] where [ID] = @SEGMENTATIONID) = 1
    begin
      raiserror('BBERR_MKTSEGMENTATION_ISHISTORICAL', 13, 1);
      return 1;
    end

  --Check to make sure MAX is greater than MIN

  if @MIN > @MAX
    begin
      raiserror('BBERR_MKTSEGMENTATIONFINDERNUMBER_INVALIDRANGE', 13, 1);
      return 1;
    end

  --Check to make sure MAX and MIN fit inside a gap

  if not exists (
    select *
    from dbo.[UFN_MKTSEGMENTATIONFINDERNUMBER_GAPS](@ID) as [GAPS]
    where [GAPS].[MIN] <= @MIN
    and ([GAPS].[MAX] >= @MAX or [GAPS].[MAX] is null))
    begin
      raiserror('BBERR_MKTSEGMENTATIONFINDERNUMBER_OVERLAPPINGRANGE', 13, 1);
      return 1;
    end

  begin try
    if @ID is null set @ID = newid();

    if @CHANGEAGENTID is null  
      exec dbo.[USP_CHANGEAGENT_GETORCREATECHANGEAGENT] @CHANGEAGENTID output;

    set @CURRENTDATE = getdate();

    insert into dbo.[MKTSEGMENTATIONFINDERNUMBER] (
      [ID],
      [SEGMENTATIONID],
      [MAX],
      [MIN],
      [VENDORID],
      [ISRESERVED],
      [INUSE],
      [ADDEDBYID],
      [CHANGEDBYID],
      [DATEADDED],
      [DATECHANGED]
    ) values (
      @ID,
      @SEGMENTATIONID,
      @MAX,
      @MIN,
      @VENDORID,
      @ISRESERVED,
      @INUSE,
      @CHANGEAGENTID,
      @CHANGEAGENTID,
      @CURRENTDATE,
      @CURRENTDATE
    );
  end try

  begin catch
    exec dbo.[USP_RAISE_ERROR];
    return 1;
  end catch

  return 0;