Saturday, 31 May 2014

using like Operator in Procedure sql server

CREATE PROCEDURE Proc_customersearch    
@code nvarchar(10)=null,   
@name nvarchar(20)=null   
AS   
begin   
DECLARE @SQL nvarchar(500)   
if((@code is not null) and (@name is null))   
     begin   
       SET @SQL = 'SELECT CustACNumber ,Cust_Name,Contactperson,Addressline1,Addressline2,postalcode,Telephoneno, Faxnumber,EmailID,Country,IATAAreacode,ControllinglocationCode,Cust_IataCode FROM M_customercodes WHERE CustAcNumber LIKE '''  + @code + '%''
 
'   
       EXEC (@SQL)   
     end   
else if((@code is null) and (@name is  not null))   
      begin   
       SET @SQL = 'SELECT CustACNumber ,Cust_Name,Contactperson,Addressline1,Addressline2,postalcode,Telephoneno, Faxnumber,EmailID,Country,IATAAreacode,ControllinglocationCode,Cust_IataCode FROM M_customercodes WHERE Cust_Name LIKE '''  + @name + '%''' 
 
       EXEC (@SQL)   
     end   
  else if((@code is not null) and (@name is  not null))      
       begin   
          SET @SQL = 'SELECT CustACNumber ,Cust_Name,Contactperson,Addressline1,Addressline2,postalcode,Telephoneno ,Faxnumber,EmailID,Country,IATAAreacode,ControllinglocationCode,Cust_IataCode FROM M_customercodes WHERE Cust_Name LIKE '''  + @name + '%''
 
 and CustAcNumber LIKE '''  + @code + '%'' '   
          EXEC (@SQL)   
       end   
        
  end   
   

No comments:

Post a Comment