且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

存储过程中的空值参数

更新时间:2023-02-07 12:12:51

 声明  @ Query   varchar ( 1000 )

设置  @ Query  = ' 从TableName1中选择ColumnName1,columnName2,....,其中ColumnName1 ="Test"和'

 IF  isull( @ parm ' ')= ' '
设置  @ Query  =  @ Query  + ' (从TableName2中选择CoulmnName2)中的CoulmnName2'
 ELSE 
设置  @ Query  =  @ Query  + '  CoulmnName2 =' +  @ parm 

执行  @ Query  


您好,

如果可以的话请尝试...

如果@parm为NULL
  开始
   和columnname在(从otherTable中选择idfield)
   别的
     和columnname = @parm
  END 



问候,
Al Moje


您可以执行以下操作:

和(isnull(@ parm,0)= 0或(从otherTable中选择idfield的)中的列名)

这对两种情况都适用.


Hello everyone,

I have a question about a query in a stored procedure.
When the used parmeter @parm is null in my where clause the syntax should be

and columnname in (select idfield from otherTable)


if the @parm is filled with a value the syntax should be:

and columnname = @parm



How to handle this problem in the where clause?

Declare @Query varchar(1000)

Set @Query = 'Select ColumnName1, columnName2, ..... From TableName1 Where ColumnName1 = ''Test'' and '

IF isnull(@parm, '') = ''
	Set @Query = @Query + 'CoulmnName2 in (Select CoulmnName2 from TableName2)'
ELSE
	Set @Query = @Query + 'CoulmnName2 = ' + @parm 

Execute @Query


Hi,

Try this if could help...

IF @parm  is NULL        
  BEGIN        
   and columnname in (select idfield from otherTable) 
   else
     and columnname = @parm
  END  



Regards,
Al Moje


You can do something like the following:

and (isnull(@parm,0) = 0 OR columnname in (select idfield from otherTable))

This will work for both the conditions.