以下是我创建的程序。当我单独运行查询时,我在数据库中找到记录,但是当我执行该过程时,它不会获取任何记录。我哪里出错?
ALTER PROCEDURE [dbo].[GetTransferList]
@date1 datetime='2015-01-01 00:00:00.000',
@date2 datetime='2017-01-01 00:00:00.000',
@shipto varchar(50)=''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
-- Insert statements for procedure here
DECLARE
@sql NVARCHAR(MAX),
@paramlist NVARCHAR(4000),
@nl CHAR(2) = CHAR(13) + CHAR(10),
@ParamDefinition NVarchar(2000);
SET @sql = 'SELECT A.ItemDescription,A.PurchaseOrderID,A.QuantityReceived,A.Price,A.StoreID,
C.ItemType,C.BinLocation,
B.PONumber,B.ShipTo,B.StoreID
FROM [dbo].[PurchaseOrderEntry] A, [dbo].[PurchaseOrder] B,[dbo].[Item] C
WHERE A.PurchaseOrderID=B.ID AND A.ItemID=C.ID ';
IF (@date1 IS NOT NULL) AND (@date2 IS NOT NULL )
SET @sql += ' AND B.[RequiredDate] between @date1 AND @date2';
IF @shipto IS NOT NULL --!='ALL'
SET @sql += ' AND B.ShipTo = @shipto ';
SET @sql += ' GROUP BY C.BinLocation,A.Price,C.ItemType, B.ID ,A.ItemDescription,
A.PurchaseOrderID,A.QuantityReceived,A.StoreID,B.PONumber,B.ShipTo,B.StoreID'
Set @ParamDefinition =' @shipto varchar(50),
@date1 datetime,
@date2 datetime'
Execute sp_Executesql @sql,
@ParamDefinition,
@shipto,
@date1,
@date2
If @@ERROR <> 0 GoTo ErrorHandler
Set NoCount OFF
Return(0)
ErrorHandler:
Return(@@ERROR)
END
查询提取值低于,
SELECT A.ItemDescription,A.PurchaseOrderID,A.QuantityReceived,A.Price,A.StoreID,
C.ItemType,C.BinLocation,
B.PONumber,B.ShipTo,B.StoreID
FROM [dbo].[PurchaseOrderEntry] A, [dbo].[PurchaseOrder] B,[dbo].[Item] C
WHERE A.PurchaseOrderID=B.ID AND A.ItemID=C.ID
GROUP BY C.BinLocation,A.Price,C.ItemType, B.ID ,A.ItemDescription,
A.PurchaseOrderID,A.QuantityReceived,A.StoreID,B.PONumber,B.ShipTo,B.StoreID
答案 0 :(得分:1)
我怀疑你最大的问题是@shipto varchar(50)=''
。在你的过程中,你然后测试null
所以这一行......
IF @shipto IS NOT NULL
如果您不传递任何内容将等于空字符串(除非您实际传递null
值),否则不会是真的
将其更改为:
@shipto varchar(50) = null
然后你可以测试null
和你的代码
IF @shipto IS NOT NULL --!='ALL'
SET @sql += ' AND B.ShipTo = @shipto ';
如果您不提供此参数,则测试为true。
如果您想添加AND B.ShipTo = ''
,只需输入&#39;&#39;&#39;到@shipto参数。
然而,这一点并不需要是动态的,因为这会做同样的事情
b.shipto = Coalesce(@shipto, B.ShipTo)
您的日期参数也是如此。如果您实际传入空值,它们将只是null
。
由于您没有SUM
,MAX
,COUNT
等集合功能,因此您不需要该组。
所以我个人的偏好就是......
ALTER PROCEDURE [dbo].[GetTransferList]
@date1 datetime=null,
@date2 datetime=null,
@shipto varchar(50) = null
--This will make your parameters optional. IE. You dont have to provide them when you call it
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
IF(@date1 IS NULL) SET @date1 = cast('1899-1-1' as datetime) --This could be anytime in the past thatis prior to any records you hold
IF(@date2 IS NULL) SET @date2 = cast('2100-1-1' as datetime) -- A date well in the future
--You could also set directly as the default values for the parameter but if you do you will have a problem if `null` is passed in.
SELECT A.ItemDescription,A.PurchaseOrderID,A.QuantityReceived,A.Price,A.StoreID,
C.ItemType,C.BinLocation,
B.PONumber,B.ShipTo,B.StoreID
FROM [dbo].[PurchaseOrderEntry] A, [dbo].[PurchaseOrder] B,[dbo].[Item] C
WHERE A.PurchaseOrderID=B.ID AND A.ItemID=C.ID AND B.[RequiredDate] between @date1 AND @date2
AND B.ShipTo = COALESCE(@shipto, b.ShipTo)
If @@ERROR <> 0 GoTo ErrorHandler
Set NoCount OFF
Return(0)
ErrorHandler:
Return(@@ERROR)
END
现在您可以提供一个日期(返回日期之前的所有记录或日期之后的所有记录)或两个日期(返回日期之间的所有记录)或不提供任何查看全部日期。 @shipto
也是如此。
答案 1 :(得分:0)
我坚信您的日期参数与可用记录不匹配,因为在该过程中工作正常的查询没有日期条件。
proc中的Date参数标有默认值 如果前端是C#(不确定其他前端代码)。
但是case case参数都有一些日期值。 将条件的日期值分配给工作查询并进行检查。
根据Mr.Fred的要求,除非您打算使用任何汇总字段,否则可能不需要对您的查询进行分组。