USP_DATALIST_SELECTIONCOUNTS

Retrieves the selection and intersection counts of the specified selections.

Parameters

Parameter Parameter Type Mode Description
@SELECTIONID1 uniqueidentifier IN Selection ID 1
@SELECTIONID2 uniqueidentifier IN Selection ID 1

Definition

Copy


CREATE procedure dbo.[USP_DATALIST_SELECTIONCOUNTS]
(
  @SELECTIONID1 uniqueidentifier,
  @SELECTIONID2 uniqueidentifier
)
as
  set nocount on;

  declare @SELECTIONSQL1 nvarchar(max);
  declare @SELECTIONSQL2 nvarchar(max);
  declare @SQL nvarchar(max);
  declare @PARAMS nvarchar(max) = '@COUNT int output';
  declare @SELECTION1COUNT int;
  declare @SELECTION2COUNT int;
  declare @INTERSECTIONCOUNT int;


  --Get the "select" statement sql for each selection...

  select
    @SELECTIONSQL1 = dbo.[UFN_SELECTIONHELPER_BUILDIDSELECTSQL](@SELECTIONID1),
    @SELECTIONSQL2 = dbo.[UFN_SELECTIONHELPER_BUILDIDSELECTSQL](@SELECTIONID2);

  if @SELECTIONSQL1 is null or @SELECTIONSQL2 is null
    raiserror('Selection does not exist in the database.', 13, 1);


  --Get the count of the first selection...

  set @SQL = 'select @COUNT = count(*) from (' + @SELECTIONSQL1 + ') as [T]';
  exec sp_executesql @SQL, @PARAMS, @COUNT = @SELECTION1COUNT output;

  --Get the count of the second selection...

  set @SQL = 'select @COUNT = count(*) from (' + @SELECTIONSQL2 + ') as [T]';
  exec sp_executesql @SQL, @PARAMS, @COUNT = @SELECTION2COUNT output;

  --Get the intersection count of the two selections...

  set @SQL = 'select @INTERSECTIONCOUNT = count(*)' + char(13) +
             'from (' + char(13) +
             dbo.[UFN_SELECTIONMERGE_BUILDIDSELECTSQL](@SELECTIONID1, @SELECTIONID2, 1) +
             ') as [MERGEDSELECTIONS];';
  exec sp_executesql @SQL, N'@INTERSECTIONCOUNT int output', @INTERSECTIONCOUNT = @INTERSECTIONCOUNT output;


  --Return the results...

  select
    @SELECTION1COUNT - @INTERSECTIONCOUNT as [SELECTION1COUNT],
    @SELECTION2COUNT - @INTERSECTIONCOUNT as [SELECTION2COUNT],
    @INTERSECTIONCOUNT as [INTERSECTIONCOUNT];

  return 0;