0

I have 5 facilities in my division, but the code below in a stored procedure only pulls documents when the "facilityID = 4" unless if I change it manually to represent other ID numbers. So I need the facilityID variable below to point to more than one value like: [Set @FacilityID = 1,2,3,4,5] but of course it's not allowing me. If it was characters, I would be able to put them in quotes. Any advice please?

declare @facilityID tinyint,
@facilitydivisionID tinyint,
SET @facilityID = 4
SET @facilitydivisionID = 6
Aaron Bertrand
182k28 gold badges407 silver badges626 bronze badges
asked Apr 25, 2014 at 19:24

1 Answer 1

3

You can use a table-valued parameter.

CREATE TYPE dbo.Facilities AS TABLE
(
 FacilityID TINYINT PRIMARY KEY
);

Then your stored procedure can say:

CREATE PROCEDURE dbo.whatever
 @FacilityDivisionID TINYINT,
 @Facilities dbo.Facilities READONLY
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ... FROM dbo.wherever AS w
 INNER JOIN @Facilities AS f
 ON f.FacilityID = w.FacilityID
 WHERE w.FacilityDivisionID = @FacilityDivisionID;
END
GO

Then you can call it from T-SQL like this:

DECLARE @t dbo.Facilities;
INSERT @t VALUES(1),(2),(3),(4),(5);
DECLARE @d TINYINT = 6;
EXEC dbo.whatever @FacilityDivisionID = @d, @Facilities = @t;

Or from C# using a DataTable and a parameter type as Structured. You can search to find plenty of examples of using TVPs from application code.

answered Apr 25, 2014 at 19:38
5
  • Thank you, thats great as I didnt know that I could declare a variable that way by pointing it to a table as a readonly. Since I have the "Facilities" table already in my database that stores the 5 facilitie's information based on the facilityID, then I have to create the Table Valued Parameter with a different name, correct? Thanks in advance Commented Apr 25, 2014 at 19:47
  • Yes, you will need a different name. Commented Apr 25, 2014 at 20:05
  • I keep getting error 137 that the scalar variable needs to be declared, although I declared it as a read only like you showed me above, not sure what am I missing? I will attach the code once I figuer out how to do this here, in my select statement in the stored procedure: I have multipple joins with other tables. Thanks again. Commented Apr 28, 2014 at 6:32
  • I'm looking at the help for the code block, but I still dont understand how to post my code here, as Indenting four spaces or writing <pre> <code> infront of my code will edit it like the way you have above, please advice, thanks Commented Apr 28, 2014 at 16:34
  • @Shayma please don't post code in a comment. If you have further problems getting your new solution to work, please post a new question. Commented Apr 28, 2014 at 16:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.