Reputation: 8345
I have the following table:
ThisCategoryID int IDENTITY, AUTO_INCREMENT
Title text
Type text
CategoryID int ALLOW NULLS
IsActive bit
OrderIndex int ALLOW NULLS
with this data:
ThisCategoryID Title Type CategoryID IsActive OrderIndex
0 Lunch Menu Section NULL True 3
2 Dessert Menu Section NULL True 1
3 Banh Mi Food Item 0 True 4
and the following stored procedure:
ALTER PROCEDURE [dbo].[sp_new_category]
@Title text,
@Type text,
@CategoryID int = null,
@IsActive bit,
@OrderIndex int = null
AS
DECLARE @Identity int
IF (SELECT count(*) FROM Category WHERE difference(title, @title) = 4) > 0
BEGIN
INSERT INTO Category (Title, Type, CategoryID, IsActive, OrderIndex) VALUES (@Title, @Type, @CategoryID, @IsActive, @OrderIndex)
SET @Identity = scope_identity()
END
ELSE
BEGIN
SET @Identity = -1
END
SELECT @Identity AS ID
RETURN @Identity
There is no item in the table with the title "Snack", yet the sp_new_category yields -1, every time I run it with the following parameters:
@Title: Snack
@Type: Menu Section
@CategoryID: NULL
@IsActive: True
@OrderIndex: NULL
Can someone explain to me why that is?
Upvotes: 0
Views: 160
Reputation: 14130
I believe your intent for the following conditional is to check if someone is trying enter an already existing item by Title (matched with SOUNDEX):
IF (SELECT count(*) FROM Category WHERE difference(title, @title) = 4) > 0
However, the way the conditional is written, you will only add items if they are similar. Try this instead:
DECLARE @Identity int
IF (SELECT count(*) FROM Category WHERE difference(title, @title) = 4) > 0
BEGIN
SET @Identity = -1
END
ELSE
BEGIN
INSERT INTO Category (Title, Type, CategoryID, IsActive, OrderIndex) VALUES (@Title, @Type, @CategoryID, @IsActive, @OrderIndex)
SET @Identity = scope_identity()
END
SELECT @Identity AS ID
RETURN @Identity
Upvotes: 2
Reputation: 21722
SELECT difference('Snack', 'Lunch')
UNION SELECT difference('Snack', 'Dessert')
UNION SELECT difference('Snack', 'Banh Mi')
3
1
2
None of your differences ever equal 4, so your insert is never run, so @identity
will always be -1
difference
is the difference in SOUNDEX encodings for text, which is an archaic hash for English names. It is not suitable for foreign words. If you tell us what you think you are accomplishing by using it, we may be able to help you.
Upvotes: 2
Reputation: 66747
Am I missing something?
The IF
statement is false so he goes to the ELSE
and puts SET @Identity = -1
. So it'll return -1
.
Upvotes: 0