Skip to main content

Posts

Showing posts from November, 2017

SQL query to show all records if the parameter is NULL

Sometimes we have to use a parameter that will form the basis of the condition in the Query. For e.g. if the parameter has a value, then we want to use it in the condition, otherwise we want to retrieve all the records. This can be achieved by: declare @vEname nvarchar(60) set @vEname = null Select * from Emp where Ename = isnull(@vEname,Ename) In the above script, if @vEname has some value, then the query will run based on that condition, otherwise it will run on the condition of "Ename = Ename" column, which will always return true and hence will return all the records. Happy Coding.!