如何针对存储过程测试查询

时间:2012-10-31 05:33:02

标签: sql stored-procedures sql-server-2008-r2 ssms

-- =============================================

ALTER PROCEDURE [dbo].[NST_GetCboCurrencyAfterUpdate]
@AccCurrency nvarchar(3)='',

@AccSgroup nvarchar(6)= '',

@AccInternalCie int=null

AS

BEGIN

SET NOCOUNT ON;

    IF @AccInternalCie is not null and @AccCurrency <> '' and @AccSgroup <> ''
    Begin
    SELECT * FROM TblAccounts Where 
   AccInternalCie = @AccInternalCie and AccCurrency = @AccCurrency and AccSgroup = @AccSgroup ORDER BY AccDescription

   End 


    Else if @AccInternalCie is not null and @AccCurrency <> ''
   Begin
   SELECT * FROM TblAccounts Where 
   AccInternalCie = @AccInternalCie and AccCurrency = @AccCurrency ORDER BY AccDescription
   END

   Else if @AccInternalCie is not null and @AccSgroup <> ''
   Begin
   SELECT * FROM TblAccounts Where 
   AccInternalCie = @AccInternalCie and AccSgroup = @AccSgroup ORDER BY AccDescription
   END

    Else if @AccCurrency <> '' and @AccSgroup <> ''
   Begin
   SELECT * FROM TblAccounts Where 
   AccCurrency = @AccCurrency and AccSgroup = @AccSgroup ORDER BY AccDescription
   END
  Else if @AccSgroup <> '' 
   Begin
   SELECT * FROM TblAccounts Where 
    AccSgroup = @AccSgroup 
    Print @AccSgroup
   END
    Else if @AccCurrency <> '' 
   Begin
   SELECT * FROM TblAccounts Where 
    AccCurrency = @AccCurrency ORDER BY AccDescription
   END
     Else if @AccInternalCie is not null 
   Begin
   SELECT * FROM TblAccounts Where 
   AccInternalCie = @AccInternalCie 
   END

end 

我有以下存储过程,如何在SQL SERVER MANAGEMENT STUDIO中使用调试进行测试如何执行特定查询

Select * from TblAccounts where AccSGroup = '1000'

假设只有AccSGroup不等于''so

   Else if @AccSgroup <> ''       
    Begin
    SELECT * FROM TblAccounts 
    Where AccSgroup = @AccSgroup 
    Print @AccSgroup
   END
应该执行

查询并且必须绕过休息

1 个答案:

答案 0 :(得分:1)

我认为您应该更改存储过程,将输入参数设置为null,只要它们等于''

在执行任何逻辑之前,在程序开始时这样做。

if @AccCurrency ='' 
BEGIN
     set @AccCurrency = null
END
if @AccSgroup ='' 
BEGIN
     set @AccSgroup = null
END

然后更改您的逻辑以删除对&lt;&gt;的检查''而是检查是否为空

所以如下更改行..

IF @AccInternalCie is not null and @AccCurrency <> '' and @AccSgroup <> ''

到此..

IF @AccInternalCie is not null and @AccCurrency  is not null and @AccSgroup  is not null 

然后使用此测试您的存储过程..

exec [dbo].[NST_GetCboCurrencyAfterUpdate]
@AccCurrency =null,
@AccSgroup = 'your value',
@AccInternalCie =null

这应该可以获得所需的结果!!

更改数据库图层代码以检查空值/空值,并在设置参数值时改为传递DBNull.Value。 实施例

if(AccCurrency.Trim().Equals(string.Empty))
{
    ParameterAccCrurrency.Value = DBNull.Value;
}
else
{
    ParameterAccCrurrency.Value = AccCurrency;
}