我有这个存储过程:
@upc AS VARCHAR(13) = NULL,
@cert_code AS VARCHAR(15) = NULL,
@vendor AS INT = NULL,
@count AS INT = 0 OUTPUT,
@store AS VARCHAR(3) = NULL,
@corporate AS VARCHAR = Null,
@zone AS VARCHAR = Null,
@region AS VARCHAR = Null,
@class AS VARCHAR = Null
AS
BEGIN -- Added for new functionality
SET NOCOUNT ON;
If @store IS NOT NULL
BEGIN
-- Get the storeID from Stores
DECLARE @storeID AS INT
SELECT @storeID = StoreID
FROM Enterprise..Stores
WHERE Store = @store
END
IF @storeID IS NOT NULL
BEGIN
IF @cert_code IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsStore
WHERE (StoreID = @storeID) AND (upc = @upc)
END
ELSE
BEGIN
IF @vendor IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsStore
WHERE (StoreID = @storeID) AND (cert_code = @cert_code)
END
ELSE
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsStore
WHERE (StoreID = @storeID)
AND (cert_code = @cert_code) AND (vendor = @vendor)
END
END
END
END
If @zone IS NOT NULL
BEGIN
IF @cert_code IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsZone
WHERE (upc = @upc)
END
ELSE
BEGIN
IF @vendor IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsZone
WHERE (cert_code = @cert_code)
END
ELSE
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsZone
WHERE (cert_code = @cert_code) AND (vendor = @vendor)
END
END
END
IF @region IS NOT NULL
BEGIN
IF @cert_code IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsRegion
WHERE (upc = @upc)
END
ELSE
BEGIN
IF @vendor IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsRegion
WHERE (cert_code = @cert_code)
END
ELSE
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsRegion
WHERE (cert_code = @cert_code) AND (vendor = @vendor)
END
END
END
IF @class IS NOT NULL
BEGIN
IF @cert_code IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsClass
WHERE (upc = @upc)
END
ELSE
BEGIN
IF @vendor IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsClass
WHERE (cert_code = @cert_code)
END
ELSE
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsClass
WHERE (cert_code = @cert_code) AND (vendor = @vendor)
END
END
END
IF @corporate IS NOT NULL
BEGIN
IF @cert_code IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsMaster
WHERE (upc = @upc)
END
ELSE
BEGIN
IF @vendor IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsMaster
WHERE (cert_code = @cert_code)
END
ELSE
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsMaster
WHERE (cert_code = @cert_code) AND (vendor = @vendor)
END
END
END
GO
当我@store IS NOT NULL
时,它会按预期计算并返回INT
,但是@corporate
,@zone
,@region
,{{1}之一设置为NOT NULL,我得到的输出为null而不是预期的INT。
这里有什么问题?
答案 0 :(得分:2)
我不知道它是否是错误或其他错误,但请检查IF和ELSE的开始和结束
从我看到的过程在这组代码中结束,因为END匹配主存储过程的BEGIN。
@upc AS VARCHAR(13) = NULL,
@cert_code AS VARCHAR(15) = NULL,
@vendor AS INT = NULL,
@count AS INT = 0 OUTPUT,
@store AS VARCHAR(3) = NULL,
@corporate AS VARCHAR = Null,
@zone AS VARCHAR = Null,
@region AS VARCHAR = Null,
@class AS VARCHAR = Null
AS
BEGIN -- Added for new functionality
SET NOCOUNT ON;
If @store IS NOT NULL
BEGIN
-- Get the storeID from Stores
DECLARE @storeID AS INT
SELECT @storeID = StoreID
FROM Enterprise..Stores
WHERE Store = @store
END
IF @storeID IS NOT NULL
BEGIN
IF @cert_code IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsStore
WHERE (StoreID = @storeID) AND (upc = @upc)
END
ELSE
BEGIN
IF @vendor IS NULL
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsStore
WHERE (StoreID = @storeID) AND (cert_code = @cert_code)
END
ELSE
BEGIN
SELECT @count = COUNT(*)
FROM Enterprise..ProductsStore
WHERE (StoreID = @storeID)
AND (cert_code = @cert_code) AND (vendor = @vendor)
END
END
END
END -- This End is wrong should not be here
如果这只是一个拼写错误,请正确发布代码。 还有其他人指出将@count参数初始化为零 似乎程序的剩余部分从未执行过,因为@count未初始化,它返回Null
答案 1 :(得分:1)
您的所有查询都不能为您的@count
变量分配空值,因为它们都使用count()
聚合。这意味着没有任何查询被运行。
我确实认为end
之前的if @zone
是错误的。但我非常确定过程定义末尾的end
必须是批处理的结尾。也许我错了,你的过程实际上比你预期的更早结束了?
您当然可以将大量分支压缩到您的查询中。这是我的尝试。我减少了大部分的缩进和开始/结束对。
declare @storeID as int;
if @store is not null
begin
-- Get the storeID from Stores
SELECT @storeID = StoreID
FROM Enterprise..Stores
WHERE Store = @store;
if @storeID is not null
SELECT @count = COUNT(*)
FROM Enterprise..ProductsStore
WHERE StoreID = @storeID and
(
@cert_code is null and upc_code = @upc_code
or cert_code = @cert_code and (vendor = @vendor or @vendor is null)
);
end
if @zone is not null
SELECT @count = COUNT(*)
FROM Enterprise..ProductsZone
WHERE @cert_code is null and upc_code = @upc_code
or cert_code = @cert_code and (vendor = @vendor or @vendor is null);
if @region is not null
SELECT @count = COUNT(*)
FROM Enterprise..ProductsRegion
WHERE @cert_code is null and upc_code = @upc_code
or cert_code = @cert_code and (vendor = @vendor or @vendor is null);
if @class is not null
SELECT @count = COUNT(*)
FROM Enterprise..ProductsClass
WHERE @cert_code is null and upc_code = @upc_code
or cert_code = @cert_code and (vendor = @vendor or @vendor is null);
答案 2 :(得分:1)
为什么要放varchar? 请使用以下代码。
@corporate AS VARCHAR(max) = NULL,
@zone AS VARCHAR(max) = NULL,
@region AS VARCHAR(max) = NULL,
@class AS VARCHAR(max) = NULL