请教下大家,存储过程中怎么做判断处理?
请教下大家,存储过程中怎么做判断处理?
举个例子,比如传入的参数叫id,判断id参数是否为空,非空的话找到指定一条记录,空的话返回整个表……
换句话说,怎么根据传入的参数情况,判断WHERE条件?
[解决办法]
对你需要判断的内容放到if里面,
[解决办法]
if @id IS NULL
select * from tb
else
select * from tb where id=@id
SELECT *
FROM dbo.TB
WHERE 1 = 1
AND (@id IS NULL OR id = @id)
AND (@name IS NULL OR name = @name)
AND (@age IS NULL OR age = @age)