Reputation: 13233
I have a SQL statement that displays the following values, but the number of rows can be anything.
SELECT NameID,Name,ValueID,Value FROM Options WHERE OptionID = 10000
Which results to:
NameID | Name | ValueID | Value
100 | Color | 10000 | Black
101 | Size | 10005 | Large
Or sometimes even:
NameID | Name | ValueID | Value
100 | Color | 10000 | Black
101 | Size | 10005 | Large
102 | Height | 10009 | Tall
103 | Width | 10006 | Wide
I'm trying to write a Stored Procedure that can allow me to check if ALL of these value's exist based on OptionID
Therefore,
OptionID
of course to only show all the options inside the Options
table for this OptionID
NameID
and ValueID
The general logic needed would be something like this:
if ALL NameID and ValueID Pairs with-in an OptionID Exists
(as shown above in the results)
Then Return 'Unique Combination'
Else Return 'Combination Exists'
Boolean would be fine as well. I've tried using a PIVOT to see if it can be done like this, but that was a bit more than i think i needed, but maybe not.. Any suggestions?
Upvotes: 1
Views: 2258
Reputation: 8078
You can use an if exists statement.
if exists(SELECT NameID,Name,ValueID,Value FROM Options WHERE OptionID = @optionID and NameId = @NameID and ValueID = @ValueID)
begin
--do whatever you want
end
else
begin
--do whatever else you want
end
Updated answer
Declare @Loop as int,@POS int, @PAirs varchar(max),@CurrentPair varchar(max)
Declare @NameID as int,@ValueID as int, @Loop2 as int,@Pos2 int
SELECT @Pos = CHARINDEX(';', @Pairs, 1)
print @Pos
SELECT @Loop = CASE WHEN LEN(@Pairs) > 0 THEN 1 ELSE 0 END
WHILE (SELECT @Loop) = 1
BEGIN
SELECT @Pos = CHARINDEX(';', @Pairs, 1)
IF @Pos > 0
BEGIN
set @CurrentPair = SUBSTRING(@Pairs, 1, @Pos - 1) --Here it is a comma delimited string such as 100,20000
set @Pairs = SUBSTRING(@Pairs, @Pos + 1, LEN(@Pairs) - @Pos) --This takes the current pair away from original string
SELECT @Loop2 = CASE WHEN LEN(@CurrentPair) > 0 THEN 1 ELSE 0 END
While (Select @Loop2) = 1
Begin
SELECT @Pos2 = CHARINDEX('2', @CurrentPair, 1)
If @Pos2 > 0
Begin
set @NameID = SUBSTRING(@CurrentPair, 1, @Pos2 - 1)
set @ValueID = SUBSTRING(@CurrentPair, @Pos2 + 1, LEN(@CurrentPair) - @Pos2)
if exists(SELECT NameID,Name,ValueID,Value FROM Options
WHERE NameId = @NameID and ValueID = @ValueID)
begin
--pair found
end
else
begin
--pair not found
end
End
Else
Begin
set @Loop2 = 0
End
End
END
ELSE
BEGIN
set @Loop = 0
END
END
Upvotes: 2
Reputation: 40319
I would do something based on SELECT... EXCEPT SELECT...
. First, create and populate a temporary table with the passed-in name/value pairs, and then run something like
IF exists (-- Everything passed in
SELECT select NameId, Name, ValueId, Value
from @TempTable
-- Then remove everything that is in the table
except select NameId, Name, ValueId, Value
from Options
where OptionId = @OptionId)
RETURN 'All items passed were not found for this OptionId'
ELES
RETURN 'All items passed were found within this OptionId'
Fuss with this, I mistrust the meaning of those return labels.
Upvotes: 0