http://sqlfiddle.com/#!3/b2643/5/1
http://sqlfiddle.com/#!3/b2643/5/0
This link above has the sample database tables I created on SQLfiddle.
I have 3 main tables in my stored procedure:
TransmittalSheet
, DocumentInfo
and TransmittalSiteLead
. Each TS can have many documents released with it and also many site lead staffs for each Transmittal that implements at different times.
Our users run reports through the use of a VB application but its pulling the data from a SQL Server in the back end via stored procedures. Currently, they can pull all docs implemening per facility individually/ seperately, Sometimes these documents are shared or implemented in multipple facilities at the same date.
My goal is to get all the documents implementing for all facilities (5 of them) at once for the one division (West) within specific date range, then at the end I would like to take care of duplicates or I mean showing the same shared document once.
With the help of Aaron Bertrand, he was able to show me the use of Table Valued Parameter, now I get partial results, versus all documents that I see when I run the original individual stored procedure that has one variable pointing to one facility at a time, so I wasn't sure what I'm missing in my query, please advice
CREATE TYPE dbo.Facilities AS TABLE
(
FacilityID TINYINT PRIMARY KEY
);
go
declare @F dbo.Facilities;
Insert @F Values (1), (2), (3), (4), (5), (7);
-- Facility 6 is considered the Division go
create PROCEDURE [dbo].[GetImpDocsThree]
@facilities dbo.Facilities ReadOnly,
@facilitydivisionID tinyint = 6,
@startdate smalldatetime = '06/16/2005',
@enddate smalldatetime = '04/28/14'
AS
BEGIN
SET NOCOUNT ON;
;WITH f AS
(
SELECT FacilityID FROM @facilities
UNION ALL SELECT @facilitydivisionID
)
SELECT di.DocumentNumber, di.DocumentVersion, di.DocumentTitle, f.FacilityID,tsl.TransmittalSiteLeadFacility,
ts.TransmittalNumber, ts.TransmittalVersion,
FacilityImpDate = COALESCE(tsl.TransmittalSiteLeadImpDate, ts.TransmittalImpDate)
FROM dbo.DocumentInfo AS di
INNER JOIN dbo.TransmittalSheet AS ts
ON di.DocumentTransmittalImp = ts.TransmittalID
LEFT OUTER JOIN dbo.TransmittalSiteLead AS tsl ON ts.TransmittalID = tsl.TransmittalSiteLeadTSID
INNER JOIN f
ON tsl.TransmittalSiteLeadFacility = f.FacilityID
-- TSSiteLeadImpDate sometimes have different dates than TransmittalImpDate
-- Select documents implementing within the date range
Where transmittalSiteLeadImpDate
BETWEEN @startdate AND @enddate
order by di.DocumentNumber
END
-- Above runs successfully but it returns partial data as I need something like this for the last part in the date range query as it was working for the individual sp:
/*
-- Select documents implementing within the date range
where ((SELECT dbo.TransmittalSiteLead.TransmittalSiteLeadImpDate
FROM dbo.TransmittalSiteLead
--WHERE dbo.DocumentInfo.DocumentTransmittalImp = dbo.TransmittalSiteLead.TransmittalSiteLeadTSID
where (dbo.TransmittalSiteLead.TransmittalSiteLeadFacility = f.facilityID OR
dbo.TransmittalSiteLead.TransmittalSiteLeadFacility = @facilitydivisionID))
BETWEEN @startdate AND @enddate)
order by di.DocumentNumber
END
*/
DocumentInfo
andTransmittalSheet
. Also if you could describe exactly what you want. Are there any documents missing from the current query? The way I understand the question, you should be getting the documents that you need. For the second part, are you only returning information about the document, or is there information from other tables as well? You might review theDISTINCT
clause to see if it does what you need.