请教SQLSERVER条件查询的问题
一个条件查询语句放在SQLSERVER的带参数的存储过程里面,VFP客户端输入参数值,查询出结果,但是假如某个条件没有输入条件,那么在SQLSERVER端,就没有这个查询条件,那在SQL端就要判断参数是否是空值,空值和不是空值查询语句就不一样,请问这种查询在SQLSERVER存储过程中,应该怎么写?假如数据在VFP端好写,SQL就不会了,恳请帮忙解决
alter proc t1(@i int =null ) as begin if (@i is null) begin print 1 end else begin print 2 end end go exec t1 go exec t1 1
alter proc t1(@a int =null, @b varchar(10) =null) as begin select * from (select 1 a, 'a' b union all select 2 a, 'b' b union all select 3 a, 'c' b ) t where (@a is null or @a =a ) and (@b is null or @b = b) end go exec t1 1 go exec t1 go exec t1 null, 'b'
[此贴子已经被作者于2024-6-19 11:31编辑过]
ALTER PROCEDURE [dbo].[a_test] @sqlCondition varchar(max)=null AS BEGIN declare @cSql varchar(max) set @cSql = 'select * from tableName where '+ isnull(@sqlCondition , '1=1') exec sp_executesql @cSql END
[此贴子已经被作者于2024-6-19 14:50编辑过]