USP_REPORT_PROJECTINVENTORY

Returns data for the sponsorship project inventory report.

Parameters

Parameter Parameter Type Mode Description
@DIMENSION1 nvarchar(100) IN
@DIMENSION1VALUE nvarchar(255) IN
@DIMENSION1VALUE3 nvarchar(255) IN
@DIMENSION2 nvarchar(100) IN
@DIMENSION2VALUE nvarchar(255) IN
@DIMENSION2VALUE3 nvarchar(255) IN
@DIMENSION3 nvarchar(100) IN
@DIMENSION3VALUE nvarchar(255) IN
@DIMENSION3VALUE3 nvarchar(255) IN

Definition

Copy


            CREATE procedure dbo.USP_REPORT_PROJECTINVENTORY (
                @DIMENSION1 as nvarchar(100) = null,
                @DIMENSION1VALUE as nvarchar(255) = null,
                @DIMENSION1VALUE3 as nvarchar(255) = null,
                @DIMENSION2 as nvarchar(100) = null,
                @DIMENSION2VALUE as nvarchar(255) = null,
                @DIMENSION2VALUE3 as nvarchar(255) = null,
                @DIMENSION3 as nvarchar(100) = null,
                @DIMENSION3VALUE as nvarchar(255) = null,
                @DIMENSION3VALUE3 as nvarchar(255) = null
                )
            with execute as owner
            as
            set nocount on;

            declare @SQLTOEXEC nvarchar(max);
            declare @FROMCLAUSE nvarchar(max)='';
            declare @COLUMNSELECT nvarchar(max)='';
            declare @WHERECLAUSE nvarchar(max)='';
            declare @REVENUEJOIN nvarchar(max);
            declare @GROUPBYCLAUSE nvarchar(max);
            declare @SELECT nvarchar(max);
            declare @FROM nvarchar(max);
            declare @COLUMN nvarchar(max);
            declare @WHERE nvarchar(max);
            declare @HAVING nvarchar(max) = '';
            declare @HAVINGCLAUSE nvarchar(max) = '';
            declare @LOCATIONCOUNT tinyint = 0;
            declare @LOCATIONTYPEENUM tinyint = 2;

            set @FROMCLAUSE = 'from dbo.SPONSORSHIPOPPORTUNITY OPP ';
            set @FROMCLAUSE = @FROMCLAUSE + 'inner join dbo.SPONSORSHIPOPPORTUNITYPROJECT SOP on SOP.ID = OPP.ID '
            set @WHERECLAUSE = ' where SOP.ID is not null '

            set @COLUMNSELECT = 'select ';

            -- count how many dimension parameters have location type specified

            if @DIMENSION1 = @LOCATIONTYPEENUM
            begin
                set @LOCATIONCOUNT = @LOCATIONCOUNT + 1;
            end
            if @DIMENSION2 is not null and @DIMENSION2 = @LOCATIONTYPEENUM
            begin
                set @LOCATIONCOUNT = @LOCATIONCOUNT + 1;
            end

            if @DIMENSION3 is not null and @DIMENSION3 = @LOCATIONTYPEENUM
            begin
                set @LOCATIONCOUNT = @LOCATIONCOUNT + 1;
            end


            -- only process dimension if it is not null and is not Location Type 

            if @DIMENSION1 is not null
            begin
                if @DIMENSION1 <> @LOCATIONTYPEENUM
                begin

                    exec dbo.USP_REPORT_CHILDINVENTORY_PROCESSDIMENSION 
                        @DIMENSION1
                        '@DIMENSION1VALUE'
                        @FROM output
                        @COLUMN output
                        @WHERE output
                        'DIMENSION1',
                        @HAVING output

                    set @FROMCLAUSE = @FROMCLAUSE + @FROM;

                    if @COLUMNSELECT = 'select '
                        set @COLUMNSELECT =  @COLUMNSELECT + @COLUMN ;
                    else
                        set @COLUMNSELECT =  @COLUMNSELECT + ',' + @COLUMN ;

                    set @WHERECLAUSE += @WHERE + ' and @DIMENSION1VALUE3 is null'
                    set @GROUPBYCLAUSE = 'DIMENSION1' ;

                    if @DIMENSION1VALUE is not null
                        set @HAVINGCLAUSE = @HAVINGCLAUSE + @HAVING;

                --

                    -- set these to null for the next row

                    --

                    set @FROM = '';
                    set @COLUMN = '';
                    set @WHERE = '';
                    set @HAVING = '';
                end
            end
            -- only process dimension if it's not null and is not Location Type

            if @DIMENSION2 is not null 
            begin
                if @DIMENSION2 <> @LOCATIONTYPEENUM
                begin
                    exec dbo.USP_REPORT_CHILDINVENTORY_PROCESSDIMENSION
                    @DIMENSION2
                    '@DIMENSION2VALUE'
                    @FROM output
                    @COLUMN output
                    @WHERE output
                    'DIMENSION2',
                    @HAVING output

                    if @FROM is not null
                    begin
                        set @FROMCLAUSE = @FROMCLAUSE + @FROM;
                    end
                    if @COLUMNSELECT = 'select '
                        set @COLUMNSELECT =  @COLUMNSELECT + @COLUMN ;
                    else
                        set @COLUMNSELECT =  @COLUMNSELECT + ',' + @COLUMN ;

                    set @WHERECLAUSE =  @WHERECLAUSE + @WHERE;
                    if @GROUPBYCLAUSE is not null
                        set @GROUPBYCLAUSE = @GROUPBYCLAUSE + ', DIMENSION2';
                    else
                        set @GROUPBYCLAUSE = 'DIMENSION2';

                    if @HAVINGCLAUSE is not null and @DIMENSION2VALUE is not null
                        set @HAVINGCLAUSE = @HAVINGCLAUSE + @HAVING;

                    --

                    -- set these to null for the next row

                    --

                    set @FROM = '';
                    set @COLUMN = '';
                    set @WHERE = '';
                    set @HAVING = '';
                end
            end
            else
            begin
                    set @WHERECLAUSE =  @WHERECLAUSE + ' and @DIMENSION2VALUE is null';
            end
            --

            if @DIMENSION3 is not null 
            begin
                if @DIMENSION3 <> @LOCATIONTYPEENUM
                begin
                        exec dbo.USP_REPORT_CHILDINVENTORY_PROCESSDIMENSION
                        @DIMENSION3,
                        '@DIMENSION3VALUE'
                        @FROM output
                        @COLUMN output
                        @WHERE output
                        'DIMENSION3',
                        @HAVING output

                        if @FROM is not null
                        begin
                            set @FROMCLAUSE = @FROMCLAUSE + @FROM;
                        end

                        if @COLUMNSELECT = 'select '
                            set @COLUMNSELECT =  @COLUMNSELECT + @COLUMN ;
                        else
                            set @COLUMNSELECT =  @COLUMNSELECT + ',' + @COLUMN ;

                        set @WHERECLAUSE =  @WHERECLAUSE + @WHERE;
                        if @GROUPBYCLAUSE is not null
                            set @GROUPBYCLAUSE = @GROUPBYCLAUSE + ', DIMENSION3' ;
                        else
                            set @GROUPBYCLAUSE = 'DIMENSION3';

                        if @HAVINGCLAUSE is not null and @DIMENSION3VALUE is not null
                                set @HAVINGCLAUSE = @HAVINGCLAUSE + @HAVING;
                        --

                        -- set these to null for the next row

                        --

                        set @FROM = '';
                        set @COLUMN = '';
                        set @WHERE = '';
                        set @HAVING = '';

                end
            end
            else
            begin
                    set @WHERECLAUSE =  @WHERECLAUSE + ' and @DIMENSION3VALUE is null';
            end
            --After we know how many location types have been set, max of 3 dimensions, we can now

            -- construct the sql to handle the location types.


            -- construct the sql query

            -- handle processing of location types

            If @LOCATIONCOUNT > 0
            begin
                exec dbo.USP_REPORT_CHILDINVENTORY_PROCESSLOCATIONTYPE_DIMENSION
                @LOCATIONCOUNT,
                @DIMENSION1,
                @DIMENSION2,
                @DIMENSION3,
                @COLUMNSELECT,
                @FROMCLAUSE,
                @WHERECLAUSE,
                @GROUPBYCLAUSE,
                @SQLTOEXEC output

            end
            else
      begin
                set @SELECT = 'select count(*) CNT, '+ @GROUPBYCLAUSE;

                set @SQLTOEXEC = @SELECT + ' from (' + @COLUMNSELECT + ' ' + @FROMCLAUSE + ' ' + @WHERECLAUSE + ') as QRY group by ' + @GROUPBYCLAUSE + @HAVINGCLAUSE;
            end

            -- execute the query

            exec sp_executesql @SQLTOEXEC,
                               N'@DIMENSION1VALUE nvarchar(255), @DIMENSION2VALUE nvarchar(255), @DIMENSION3VALUE nvarchar(255),  @DIMENSION1VALUE3 nvarchar(255), @DIMENSION2VALUE3 nvarchar(255), @DIMENSION3VALUE3 nvarchar(255)',
                               @DIMENSION1VALUE=@DIMENSION1VALUE, @DIMENSION2VALUE=@DIMENSION2VALUE, @DIMENSION3VALUE=@DIMENSION3VALUE, @DIMENSION1VALUE3=@DIMENSION1VALUE3, @DIMENSION2VALUE3=@DIMENSION2VALUE3, @DIMENSION3VALUE3=@DIMENSION3VALUE3 ;