I get error 137 that I must declare a scalar variable (@facilities), although I'm declaring it in the Create stored procedure as a readonly variable, so I wasn't sure what I'm missing here. any advice will be appreciated. I created a table valued parameter to hold multipple integer values, that I can call later in my stored procedure. the goal is to get all the documents implementing in each facility for the one division (6) for a specific date. Please see a section of the code below:
use DevDocMgt_new
CREATE TYPE dbo.Facilities AS TABLE
(FacilityID TINYINT PRIMARY KEY);
go
declare @F dbo.Facilities;
Insert @F Values (1), (2), (3), (4), (5);
go
Create PROCEDURE dbo.GetImpDocsTwo
(
@facilities dbo.Facilities ReadOnly,
@facilitydivisionID tinyint = 6)
AS
BEGIN
SET NOCOUNT ON;
SELECT di.DocumentNumber, di.DocumentVersion, di.DocumentTitle,
ts.TransmittalNumber, ts.TransmittalVersion,
CASE WHEN TransmittalSiteLeadImpDate IS NOT NULL
AND (TransmittalSiteLeadFacility = @facilities OR TransmittalSiteLeadFacility = @facilityDivisionID)
THEN TransmittalSiteLeadImpDate ELSE TransmittalImpDate END AS FacilityImpDate
FROM dbo.DocumentInfo di
INNER JOIN dbo.TransmittalSheet ts ON di.DocumentTransmittalImp = ts.TransmittalID
LEFT OUTER JOIN dbo.TransmittalSiteLead tsl ON ts.TransmittalID = tsl.TransmittalSiteLeadTSID
Inner Join @facilities as F on f.facilityID = tsl.TransmittalSiteLeadFacility
-- Select documents implementing in the facility
WHERE (tsl.TransmittalSiteLeadFacility = @facilities OR
tsl.TransmittalSiteLeadFacility = @facilitydivisionID)
end
1 Answer 1
The problem is here:
WHERE (tsl.TransmittalSiteLeadFacility = @facilities
You are trying to compare a column on the left to a table on the right. This is like saying:
FROM sys.objects
WHERE object_id = sys.columns
...which obviously makes no sense.
That where clause doesn't belong there at all. You've already ensured that the facilities listed in the incoming table-valued parameter are matched to TransmittalSheet by using it in the JOIN clause. There is no need to filter on that match again.
My guess after several rounds of teeth-pulling is you want this:
CREATE PROCEDURE dbo.GetImpDocsThree
@facilities dbo.Facilities ReadOnly,
@facilitydivisionID tinyint = 6
AS
BEGIN
SET NOCOUNT ON;
;WITH f AS
(
SELECT FacilityID FROM @faclities
UNION ALL SELECT @facilitydivisionID
)
SELECT di.DocumentNumber, di.DocumentVersion, di.DocumentTitle,
ts.TransmittalNumber, ts.TransmittalVersion,
FacilityImpDate = COALESCE(ts.TransmittalSiteLeadImpDate, ts.TransmittalImpDate)
FROM dbo.DocumentInfo AS di
INNER JOIN dbo.TransmittalSheet AS ts
ON di.DocumentTransmittalImp = ts.TransmittalID
INNER JOIN f
ON tsl.TransmittalSiteLeadFacility = f.FacilityID;
END
GO
Or just insert 6 into the TVP up front too, and ditch the additional single facility variable altogether.
-
Now the stored procedure is compiling without any error but I dont get any data back. The actual working sp was producing data from joining an existing Facility table, TransmittalSheet table, DocumentInfo and TransmittalSiteLead tables with date range variables, that had also one variable facilityID that points to one facility at a time. So now that I created the TVP dbo.Facilities, it doesnt have other information inserted other than the facility Id numbers (1,2,3,4,5).Shayma Ahmad– Shayma Ahmad2014年04月28日 18:20:15 +00:00Commented Apr 28, 2014 at 18:20
-
Ok i will do thatShayma Ahmad– Shayma Ahmad2014年04月28日 18:29:47 +00:00Commented Apr 28, 2014 at 18:29
-
Awsome, your third stored procedure above (GetImpDocsThree) returns rows for me now but I just have to get it to work with date range variables so I can tell its pulling the correct rows I need. For some reason when I add the "Start and End" variables, still doesn't like it but I'm working on it.Shayma Ahmad– Shayma Ahmad2014年04月30日 19:00:40 +00:00Commented Apr 30, 2014 at 19:00
-
It seems SQLfiddle doesnt like my Schema building, so no luck yet creating my sample data there. Thanks for everything again.Shayma Ahmad– Shayma Ahmad2014年04月30日 19:02:36 +00:00Commented Apr 30, 2014 at 19:02
-
Can you please check my link for a sample database sqlfiddle.com/#!3/b2643/5/1 , as I entered few rows for each table, hopefully it's helpful. I will submit a new question for this too. ThanksShayma Ahmad– Shayma Ahmad2014年05月07日 00:01:45 +00:00Commented May 7, 2014 at 0:01
6
into@f
as well, instead of complicating things and treating that separately? When all it really is is an additional facility to filter on.