In my stored procedure, I have something like
WHERE W.Warehouse=@WH1
It works well when it is simple as
EXEC dbo.MyProcedure @WH1='S0'
I need to select multiple warehouses. For example.
EXEC dbo.MyProcedure @WH1='S0' OR W.Warehouse='T0' OR W.Warehouse='T0' OR W.Warehouse='Z0'
I would expect that the prefix of first warehouse would be handled in the procedure so after the WHERE clause, it should work as
W.Warehouse='S0' OR W.Warehouse='T0' OR W.Warehouse='T0' OR W.Warehouse='Z0'
However I have at least a syntax problem to define my @WH1 variable properly, and I can't put my finger on it. I am not even sure that I am using the right method. How should it be done? TIA.
None of us have any idea what your stored procedure or your tables look like. But here's an idea:
DECLARE @MyList VARCHAR(MAX);
SET @MyList = 'S0,T0,t0,W0,';
WITH CTE AS
(
SELECT Warehouse = [value]
FROM STRING_SPLIT(@MyList, ',')
WHERE LEN([value])>0
)
SELECT W.*
FROM Warehouses AS W
INNER JOIN CTE ON W.Warehouse = CTE.Warehouse
;
January 4, 2023 at 12:54 pm
Be cautious about building this kind of catch-all query. Here's some great advice from Gail Shaw on the topic. Note that she has multiple posts on the topic. It's a somewhat dangerous performance hole you might be preparing to crawl down.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
January 4, 2023 at 1:30 pm
STRING_SPLIT saves the day.... great. Thank you.
WHERE W.Warehouse IN (SELECT value FROM STRING_SPLIT(@WH1, ',') WHERE LEN([value])>0)
January 4, 2023 at 2:33 pm
STRING_SPLIT() is great. But be aware of potential performance impacts of using a table-valued function or variable directly -- You may find it better to insert the values from the STRING_SPLIT() into a temp table, & then use that. Test & see which is better.
January 4, 2023 at 3:02 pm
In my stored procedure, I have something like
WHERE W.Warehouse=@WH1It works well when it is simple as
EXEC dbo.MyProcedure @WH1='S0'I need to select multiple warehouses. For example.
EXEC dbo.MyProcedure @WH1='S0' OR W.Warehouse='T0' OR W.Warehouse='T0' OR W.Warehouse='Z0'I would expect that the prefix of first warehouse would be handled in the procedure so after the WHERE clause, it should work as
W.Warehouse='S0' OR W.Warehouse='T0' OR W.Warehouse='T0' OR W.Warehouse='Z0'However I have at least a syntax problem to define my @WH1 variable properly, and I can't put my finger on it. I am not even sure that I am using the right method. How should it be done? TIA.
Quick question, how many options will you be passing to the procedure?
😎
Further to Grant's fine comment, do you need a catch-all or can you do this with a lookup table?
January 4, 2023 at 10:21 pm
The number of options is up to 7. I build a string based using check boxes in my sheet, so it will be consistent.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply