我有以下表格:存儲過程拒絕插入數據
ThisCategoryID int IDENTITY, AUTO_INCREMENT
Title text
Type text
CategoryID int ALLOW NULLS
IsActive bit
OrderIndex int ALLOW NULLS
與此數據:
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
及以下存儲過程:
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
沒有項目在標題爲「小吃」的表格中,sp_new_category每次使用以下參數運行時都會得到-1:
@Title: Snack
@Type: Menu Section
@CategoryID: NULL
@IsActive: True
@OrderIndex: NULL
有人可以向我解釋爲什麼是這樣嗎?
您不應該使用'text'數據類型,因爲它已被棄用,並且無論如何,它似乎完全不適用於像'Snack'和'Menu Section'這樣的短字符串。如果你使用'varchar',那麼你可以使用正常的平等比較而不是你的'差異'測試。 (這似乎也是錯誤的,因爲這是基於'soundex'值) –
我同意Martin的觀點,這可能是由於IF()語句沒有返回預期結果。更改您的IF邏輯並解決問題。 – Zachary