Products
Services
Contact Us

Scripts: Asp :: Ms sql and database :: Library Article #17

Developer's Section

Using If statements inside Stored Procedures
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (4238)
Favorites:

Learn to create dynamic queries using if statements and string concatenation inside stored procedures in SQL Server.

In this tutorial we show you a way to create dynamic queries by using if statements and string concatenation inside stored procedures. This is usually the case when you are passing parameters to the stored procedure and they typically create different conditions on the where clause of the queries.

Example:

 Code Snippet 1

CREATE PROCEDURE [dbo].[USP_search_PassengerFlight]
 -- Add the parameters for the stored procedure here
   @airportID int,
   @flightID int,
   @searchCrit1 varchar(4000) = NULL, --passenger last name
   @searchCrit2 varchar(4000) = NULL, --passenger first name
   @searchCrit3 varchar(4000) = NULL, --passenger email
   @searchCrit4 varchar(4000) = NULL, --passenger address
   @searchCrit5 varchar(4000) = NULL, --passenger age
   @searchCrit6 varchar(4000) = NULL -- passenger phone
   
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
 
 Declare @unitNumber  varchar(4000); 
 Declare @lastName   varchar(4000);
 Declare @firstName   varchar(4000);
 Declare @usrEmail   varchar(4000);
 Declare @propName   varchar(4000);
 Declare @usrPhone   varchar(4000);
 
 SET @lastName = '%' + @searchCrit1 + '%';
    SET @firstName= '%' + @searchCrit2 + '%';
    SET @usrEmail = '%' + @searchCrit3 + '%';
    SET @usrAddress = '%' + @searchCrit4 + '%';
    SET @usrAge = '%' + @searchCrit5 + '%';
    SET @usrPhone = '%' + @searchCrit6 + '%';
    
    Declare @whereClause   varchar(4000) = NULL;
 
 IF @searchCrit1 IS NOT NULL
  BEGIN
   SET @whereClause = 'and (( u.lastName LIKE ''' + @lastName + ''' )';
   SET @whereClause = @whereClause + ' and ( u.active = 1 ) ';
  END
  
 if @whereClause IS NULL
  SET @whereClause = '';
 
 IF @searchCrit2 IS NOT NULL
  SET @whereClause = @whereClause + ' and ( r.pass_firstName LIKE ''' + @firstName + ''' )';
  
 IF @searchCrit3 IS NOT NULL
  SET @whereClause = @whereClause + ' and ( r.pass_email LIKE ''' + @usrEmail + ''' )';

 IF @searchCrit4 IS NOT NULL
  SET @whereClause = @whereClause + ' and ( r.pass_address LIKE ''' + @usrAddress + ''' )';

 IF @searchCrit5 IS NOT NULL
  SET @whereClause = @whereClause + ' and ( p.pass_age LIKE ''' + @usrAge + ''' )';
  
 IF @searchCrit6 IS NOT NULL
  SET @whereClause = @whereClause + ' and ( r.pass_phone LIKE ''' + @usrPhone + ''' )';

 IF @searchCrit1 IS NOT NULL and ( @searchCrit2 IS NOT NULL or @searchCrit3 IS not NULL 
or @searchCrit4 IS NOT NULL OR @searchCrit5 IS NOT NULL OR @searchCrit6 IS NOT NULL )
  SET @whereClause = @whereClause +  ')';

    Declare @SQL NVarChar(3000);

    -- Insert statements for procedure here
 if @airportID = 0
        Select @SQL = 'Select
  u.flightID,
  u.flightTime,
  u.flightDestination,
  u.flightCity,
  u.flightState,
  r.pass_firstName,
  r.pass_email,
  r.pass_address,
  r.pass_age,
  r.pass_phone,
  r.pass_code,
        p.airportID,
        p.airportName 
   From units_data u
  Inner Join passengers r 
  ON u.passengerID = r.passengerID 
        Inner Join airport p 
        ON u.airportID = p.airportID 
        Inner Join airlinelinks ul 
  ON p.airportID = ul.airportID 
  And ul.flightID = @flightID 
   Where r.flight_active = 0 ' + @whereClause + '
     order by u.flightID'
 
    else
     Select @SQL = 'Select
  u.flightID,
  u.flightTime,
  u.flightDestination,
  u.flightCity,
  u.flightState,
  r.pass_firstName,
  r.pass_email,
  r.pass_address,
  r.pass_age,
  r.pass_phone,
  r.pass_code,
        p.airportID,
        p.airportName 
   From units_data u
  Inner Join passengers r
  ON u.passengerID = r.passengerID
        Inner Join airport p
        ON u.airportID = p.airportID
   Where u.airportID  = @airportID 
And r.flight_active = 0 ' + @whereClause + '
     order by u.flightid'

 Exec sp_executesql @SQL, N'@flightID int, @airportID int', @flightID, @airportID
 

End



See other Scripts in Ms sql and database

Submit Your Scripts:


System Message:
  • Your ASP & ASP.NET script has been sent. Please allow 48 hours for processing.


If you would like to have your ASP & ASP.NET scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]