USP_REPORT_NAMINGOPPORTUNITYAVAILABILITY
Parameters
Parameter | Parameter Type | Mode | Description |
---|---|---|---|
@PURPOSEID | uniqueidentifier | IN | |
@CONTACTID | uniqueidentifier | IN | |
@TYPEID | uniqueidentifier | IN | |
@SITEID | uniqueidentifier | IN | |
@FACILITYID | uniqueidentifier | IN | |
@CAMPAIGNID | uniqueidentifier | IN | |
@AREAID | uniqueidentifier | IN | |
@UNITID | uniqueidentifier | IN | |
@NAMINGOPPORTUNITYQUERYID | uniqueidentifier | IN | |
@NAMINGOPPORTUNITYID | uniqueidentifier | IN | |
@REPORTUSERID | nvarchar(128) | IN | |
@ALTREPORTUSERID | nvarchar(128) | IN |
Definition
Copy
create procedure dbo.USP_REPORT_NAMINGOPPORTUNITYAVAILABILITY
(
@PURPOSEID uniqueidentifier = null,
@CONTACTID uniqueidentifier = null,
@TYPEID uniqueidentifier = null,
@SITEID uniqueidentifier = null,
@FACILITYID uniqueidentifier = null,
@CAMPAIGNID uniqueidentifier = null,
@AREAID uniqueidentifier = null,
@UNITID uniqueidentifier = null,
@NAMINGOPPORTUNITYQUERYID uniqueidentifier = null,
@NAMINGOPPORTUNITYID uniqueidentifier = null,
@REPORTUSERID nvarchar(128) = null,
@ALTREPORTUSERID nvarchar(128) = null
)
with execute as owner
as
set nocount on;
-- Retrieve the table for the selection, if there is one.
declare @DBOBJECTNAME nvarchar(128);
declare @DBOBJECTTYPE smallint;
declare @CURRENTAPPUSERID uniqueidentifier;
set @CURRENTAPPUSERID = dbo.UFN_APPUSER_GETREPORTAPPUSERID(@REPORTUSERID, @ALTREPORTUSERID);
if @NAMINGOPPORTUNITYQUERYID is not null begin
if not exists(select ID from dbo.IDSETREGISTER where ID = @NAMINGOPPORTUNITYQUERYID) raiserror('ID set does not exist in the database.', 15, 1);
select @DBOBJECTNAME = DBOBJECTNAME, @DBOBJECTTYPE = OBJECTTYPE from dbo.IDSETREGISTER where ID = @NAMINGOPPORTUNITYQUERYID;
if @DBOBJECTTYPE = 1 set @DBOBJECTNAME = @DBOBJECTNAME + '()';
else if @DBOBJECTTYPE = 2 set @DBOBJECTNAME = @DBOBJECTNAME + '(''' + convert(nvarchar(36), @NAMINGOPPORTUNITYQUERYID) + ''')';
end
declare @SQLTOEXEC nvarchar(max);
set @SQLTOEXEC =
'
select
NAMINGOPPORTUNITY.NAME,
NAMINGOPPORTUNITY.QUANTITY,
NAMINGOPPORTUNITY.REMAINING,
NAMINGOPPORTUNITY.MINIMUMAMOUNT,
CONTACT_NF.NAME as CONTACT
from
dbo.NAMINGOPPORTUNITY
inner join dbo.DESIGNATIONLEVEL on DESIGNATIONLEVEL.ID = NAMINGOPPORTUNITY.PURPOSEID
left join dbo.SITE on SITE.ID = NAMINGOPPORTUNITY.SITEID
left join dbo.NAMINGOPPORTUNITYFACILITY on NAMINGOPPORTUNITYFACILITY.ID = NAMINGOPPORTUNITY.FACILITYID
outer apply dbo.UFN_CONSTITUENT_DISPLAYNAME(NAMINGOPPORTUNITY.CONTACTID) CONTACT_NF
' + nchar(13);
begin
if not @NAMINGOPPORTUNITYQUERYID is null
begin
set @SQLTOEXEC = @SQLTOEXEC +
'inner join dbo.' + @DBOBJECTNAME + ' as NAMINGOPPORTUNITYQUERY on NAMINGOPPORTUNITY.[ID] = NAMINGOPPORTUNITYQUERY.[ID]' + nchar(13);
end
end
set @SQLTOEXEC = @SQLTOEXEC +
'
where
(@PURPOSEID is null or (DESIGNATIONLEVEL.ID = @PURPOSEID)) and
(@CONTACTID is null or (NAMINGOPPORTUNITY.CONTACTID = @CONTACTID)) and
(@TYPEID is null or (NAMINGOPPORTUNITY.NAMINGOPPORTUNITYTYPECODEID = @TYPEID)) and
(@SITEID is null or (SITE.ID = @SITEID)) and
(@FACILITYID is null or (NAMINGOPPORTUNITY.FACILITYID = @FACILITYID)) and
(@CAMPAIGNID is null or (NAMINGOPPORTUNITY.CAMPAIGNID = @CAMPAIGNID)) and
(@AREAID is null or (NAMINGOPPORTUNITYFACILITY.AREACODEID = @AREAID)) and
(@UNITID is null or (NAMINGOPPORTUNITYFACILITY.UNITCODEID = @UNITID)) and
(@NAMINGOPPORTUNITYID is null or (NAMINGOPPORTUNITY.ID = @NAMINGOPPORTUNITYID)) and
dbo.UFN_SITEALLOWEDFORUSER(@CURRENTAPPUSERID, NAMINGOPPORTUNITY.SITEID) = 1
order by
NAMINGOPPORTUNITY.MINIMUMAMOUNT desc
' + nchar(13);
exec sp_executesql @SQLTOEXEC,
N'@PURPOSEID uniqueidentifier, @CONTACTID uniqueidentifier, @TYPEID uniqueidentifier, @SITEID uniqueidentifier, @FACILITYID uniqueidentifier, @CAMPAIGNID uniqueidentifier, @AREAID uniqueidentifier, @UNITID uniqueidentifier, @NAMINGOPPORTUNITYQUERYID uniqueidentifier, @NAMINGOPPORTUNITYID uniqueidentifier, @CURRENTAPPUSERID uniqueidentifier',
@PURPOSEID=@PURPOSEID, @CONTACTID=@CONTACTID, @TYPEID=@TYPEID, @SITEID=@SITEID, @FACILITYID=@FACILITYID, @CAMPAIGNID=@CAMPAIGNID, @AREAID=@AREAID, @UNITID=@UNITID, @NAMINGOPPORTUNITYQUERYID=@NAMINGOPPORTUNITYQUERYID, @NAMINGOPPORTUNITYID=@NAMINGOPPORTUNITYID, @CURRENTAPPUSERID=@CURRENTAPPUSERID;