select * from Doctorslist where case when @Name is not null then DoctorName end = case when @Name is not null then @Name end and case when @Country is not null then Country end = case when @Country is not null then @Country end and case when @State is not null then State end = case when @State is not null then @State end
ISNULL
select * from Doctorslist where DoctorName = isnull(@Name,DoctorName) and Country = isnull(@Country,Country) and State = isnull(@State,State)
like isnull(@Name,name)%
@Name ='T%'
@DoctorName varchar(255),@Degree varchar(255),@Speciality varchar(255),@TelNumber varchar(255),@District varchar(255),@State varchar(255),@Country varchar(255),@Email varchar(255)ASBEGINselect DoctorName,Degree,Speciality,TelNumber,Taluk,District,State,Country from Doctorslist where DoctorName = isnull(@DoctorName,DoctorName)and TelNumber = isnull(@TelNumber,TelNumber)and State = isnull(@State,State) and Country = isnull(@Country,Country)and Speciality = isnull(@Speciality,Speciality)and Degree = isnull(@Degree,Degree)and TelNumber = isnull(@TelNumber,TelNumber)and District = isnull(@District,District)and Email = isnull(@Email,Email)END
--@WhereStatement should be in format: -- [Name] = 'SomeName' --or -- [Speciality] = 'SomSpeciality' --and so on... CREATE PROCEDURE usp_GetDoctorDetails @WhereStatement VARCHAR(300) NULL AS BEGIN DECLARE @sql VARCHAR(MAX) SET @sql = N'SELECT * FROM DoctorsList ' IF (NOT @WhereStatement IS NULL) SET @sql = @sql + @WhereStatement EXEC(@sql) END
if @Name is not null set @sql = @sql + ' And (DoctorName = @Name)'
if @Name is not null set @sql = @sql + ' And DoctorName =''' + @Name + ''''
set @sql = @sql + ' And DoctorName Like =''' + @DoctorName + '''%'
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)