sql - How to return all values if Param is null -
i want customize stored procedure, in case param value @townid null returns data in table. otherwise returns correct data. in table there no nulls in townid column. there nvarchar values. didn't success data if send @townid=null client side
alter procedure dbo.getcustomerspagewisewithtown @pageindex int = 1 ,@pagesize int = 10 ,@pagecount int output ,@townid int set nocount on; select row_number() on ( order ispaid desc )as rownumber ,b.id ,b.name ,b.phone1 ,b.town ,b.addess ,b.ispaid ,b.defaultimage ,t.townname #results businessfulldata b left join towns t on b.town = t.id ((b.isvisable=1) , (b.town=@townid)) declare @recordcount int select @recordcount = count(*) #results set @pagecount = ceiling(cast(@recordcount decimal(10, 2)) / cast(@pagesize decimal(10, 2))) print @pagecount select * #results rownumber between(@pageindex -1) * @pagesize + 1 and(((@pageindex -1) * @pagesize + 1) + @pagesize) - 1 drop table #results return thanks lot advise,
just add condition where clause:
v-------------v ((b.isvisable=1) , (@townid null or b.town=@townid)) also note isvisible spelled wrong.
Comments
Post a Comment