存储过程加if判断,sql用字符串拼接
例如:
CREATE PROCEDURE ST_ShowRoomByCatgAndStatus2(
@RCategoryId int,@Status int)
AS
DECLARE @str nvarchar(1000)
set @str=''--要赋初值,不然为NULL
if @RCategoryId!=0
BEGIN
SET @str = @str + ' and r.ST_RCategoryId=' + rtrim(@RCategoryId)--转为字符型再相加
END
if @Status!=0
BEGIN
SET @str = @str + ' and s.ST_Status=' + rtrim(@Status)
END
select @str='
select r.ST_RoomId, c.ST_Name,s.ST_Status
from ST_RoomsInfo r, ST_RoomCategory c, ST_RoomStatus s
where s.ST_RoomId=r.ST_RoomId
and c.ST_RCategoryId=r.ST_RCategoryId '+ @str
exec(@str)
GO