Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:14:39+00:00 2026-05-23T21:14:39+00:00

I use SQL Server 2008 Express with Advanced Services. I have a view: IF

  • 0

I use SQL Server 2008 Express with Advanced Services. I have a view:

IF EXISTS (select * from sys.views where object_id = object_id(N'[dbo].[vw_PersonDetails]'))
    DROP VIEW vw_PersonDetails
GO

CREATE VIEW vw_PersonDetails
AS
    SELECT
        p.PersonID, p.Title, 
        p.FirstName, p.LastName,
        a.AddressLine1, a.AddressLine2, a.AddressLine3, a.AddressLine4,
        a.Country, a.PostalCode, 
        a.PhoneNumber, a.Email, p.EntryDate
    FROM  
        [dbo].[Persons] p
    INNER JOIN 
        [dbo].[Address] a ON p.PersonID = a.PersonID
GO

Now I have to search this view with each of the columns as options.

Ex:

IF (@firstName != NULL OR @firstName != '') AND 
   (@lastName != NULL OR @lastName != '') AND 
   (@addressLine1 != NULL OR @addressLine1 != '') AND 
   (@postalCode != NULL OR @postalCode != '') AND 
   (@country != NULL OR @country != '') AND
   (@phoneNumber != NULL OR @phoneNumber != '') AND 
   (@email != NULL OR @email != '') AND 
   (@entryDate != NULL)
BEGIN
    SELECT * 
    FROM dbo.vw_PersonDetails 
    WHERE 
        (FirstName LIKE [dbo].[GetSearchString](@firstName) OR
        LastName LIKE [dbo].[GetSearchString](@lastName) OR
        AddressLine1 LIKE [dbo].[GetSearchString](@addressLine1) OR
        Country LIKE [dbo].[GetSearchString](@country) OR
        PostalCode LIKE [dbo].[GetSearchString](@postalCode) OR
        PhoneNumber LIKE [dbo].[GetSearchString](@phoneNumber) OR
        Email LIKE [dbo].[GetSearchString](@email) OR
        EntryDate = @entryDate
       )
END

Now is there any other options except writing a never ending IF-ELSE-IF trail in a stored procedure or building query dynamically. Please help.

Another question is that what will be better: Writing such a stored procedure or making a dynamic query from code.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:14:40+00:00Added an answer on May 23, 2026 at 9:14 pm

    You can do it in one simple query:

    SELECT * from dbo.vw_PersonDetails WHERE (
        (@firstName IS NULL OR @firstName = '' OR FirstName like @firstName) AND
        ... (same thing for other parameters)
    

    This will work due to SQL Server being intelligent enough to short circuit the evaluation in the expected way. However, this is not actually guaranteed by the specification.

    If you want to be on the safe side, you can force evaluation order by doing this:

    SELECT * from dbo.vw_personDetails WHERE (
        (CASE 
            WHEN @firstname IS NULL THEN 1
            WHEN @firstname='' THEN 1
            WHEN FirstName like @firstName THEN 1
            ELSE 0
         END=1) AND
        .... (same thing for other parameters)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an application that uses SQL Server 2008 R2 Express with Advanced Services.
I have installed Visual Web Developer Express 2010, and SQL Server 2008 R2 Express
Basic Question - I have installed SQL Server Express 2008 with integrated SP1 .
I'm configuring a simple service broker implementation. Locally, we use SQL Server 2008 Express
i use sql server 2008 for publisher and sql server 2005 express for subscriber
I have a Sql Server 2008 Express database file that's currently being used by
I have added Full Text Search to my sql server 2008 express database and
I have a virtual windows server with SQL Server 2008 R2 Express. for the
What is the best way to use SQL Server 2008 as a development database,
Is SQL Server 2008 a good option to use as an image store for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.