Tsql 插入到不在IF块中工作-T-SQL
我正在制定一个程序,将商品数量(p_count的值)从旧商店转移到新商店Tsql 插入到不在IF块中工作-T-SQL,tsql,Tsql,我正在制定一个程序,将商品数量(p_count的值)从旧商店转移到新商店 SET @countOnOldStore = (SELECT "count" FROM ProductStore WHERE StoreId = @p_oldStoreId AND ProductId = @p_productID) SET @countOnNewStore = (SELECT "count" FROM ProductStore WHERE StoreId = @p_newStoreID AND P
SET @countOnOldStore = (SELECT "count" FROM ProductStore WHERE StoreId = @p_oldStoreId AND ProductId = @p_productID)
SET @countOnNewStore = (SELECT "count" FROM ProductStore WHERE StoreId = @p_newStoreID AND ProductId = @p_productID)
SET @ShiftedCount = @countOnOldStore - @p_count
SET @newStoreAfterShift = @countOnNewStore + @p_count
IF @ShiftedCount > 0
BEGIN
DELETE FROM ProductStore WHERE storeId = @p_oldStoreId and productID = @p_productID
INSERT INTO ProductStore (storeId,productId,"count") VALUES (@p_oldStoreId,@p_productID,@ShiftedCount)
DELETE FROM ProductStore WHERE storeId = @p_newStoreID and productID = @p_productID
INSERT INTO ProductStore (storeId,productId,"count") VALUES (@p_newStoreID,@p_productID,@newStoreAfterShift)
END
ELSE
PRINT 'ERROR'
嗯。。。第二次插入无效。我想不出来。上面说
Cannot insert the value NULL into column 'count', table 'dbo.ProductStore'; column does not allow nulls. INSERT fails.
谁能看到问题并向我解释一下吗?它的学校项目看起来整个查询应该是:
UPDATE ProductStore
SET [count] = [count] + CASE
WHEN storeId = @p_NewStoreID THEN @p_Count
ELSE -@p_Count END
WHERE
productID = @p_ProductID and
storeID in (@p_NewStoreID,@p_OldStoreID)
如果以下任一值为
NULL
,则总数将为NULL
:
SET @newStoreAfterShift = @countOnNewStore + @p_count
检查两个值(
@countOnNewStore,@p\u count
)是否为NULL看起来您没有给@p\u count赋值,所以它是NULL
,@ShiftedCount和@newStoreAfterShift也是如此。尝试打印@newStoreAfterShift值。它似乎为null,ProductStore不接受null或列计数尝试打印@ShiftedCount
,@newStoreAfterShift
。错误日志很清楚,您正在插入null,但这是不允许的。为什么您要删除
和插入
,而不仅仅是执行更新
?我想处理storeId中没有productId的情况,我需要更新。彼得·霍瓦思:我想我能应付一比一。不用谢;)