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 149341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:06:56+00:00 2026-05-11T09:06:56+00:00

I am using MSSQL 2005 Server and I have the following SQL query. IF

  • 0

I am using MSSQL 2005 Server and I have the following SQL query.

IF @CategoryId IN (1,2,3)     BEGIN         INSERT INTO @search_temp_table          SELECT * FROM (SELECT d.DataId, (SELECT [Name] FROM Category WHERE CategoryId = d.CategoryId) AS 'Category', d.Description, d.CompanyName, d.City, d.CategoryId,                 d.CreatedOn, d.Rank, d.voteCount, d.commentCount, d.viewCount         FROM Data d                  INNER JOIN Keyword k                     ON d.DataId = k.DataId         WHERE FREETEXT(k.Keyword, @SearchQ) AND d.CategoryId=@CategoryId AND d.IsSearch=1 AND d.IsApproved=1 ) AS Search_Data     END     ELSE         BEGIN              INSERT INTO @search_temp_table              SELECT * FROM (SELECT d.DataId, (SELECT [Name] FROM Category WHERE CategoryId = d.CategoryId) AS 'Category', d.Description, d.CompanyName, d.City, d.CategoryId,                     d.CreatedOn, d.Rank, d.voteCount, d.commentCount, d.viewCount             FROM Data d                      INNER JOIN Keyword k                         ON d.DataId = k.DataId             WHERE FREETEXT(k.Keyword, @SearchQ) AND d.IsSearch=1 AND d.IsApproved=1 ) AS Search_Data         END 

In the above query I have the category condition,

d.CategoryId=@CategoryId 

which is executed when any category is passed, if no category is passed then I am not considering category condition in where clause, To implement category condition only when if the category in (1,2,3) I have used If-Clause, but can’t we do this in single where query?? that means just check if the values is there in the category (or if it’s easy then we can only check for 1,2,3 values) then that condition will be applied else query will not consider the category condition.

Is there any way, using CASE, or NOT NULL statements ??

  • 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. 2026-05-11T09:06:56+00:00Added an answer on May 11, 2026 at 9:06 am

    Similiar to marks answer you can do the following:

    WHERE FREETEXT(k.Keyword, @SearchQ)    AND d.IsSearch=1    AND d.IsApproved=1    AND ((@CategoryId NOT IN (1,2,3)) OR (d.CategoryId = @CategoryId)) ) AS Search_Data 

    This way you eliminiate the function call

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MS SQL Server 2005, I have dates stored in epoch time
I am using MS SQL server 2005 I have a table with 3 columns
I'm using MS SQL Server 2005. Is there a difference, to the SQL engine,
I am using MS SQL Server 2005 (9.0.4035) and trying to find rows that
Using MS SQL Server Management Studio 2005 - To Restore a Database: Restore Database
I've got a sql query (using MS-SQL 2005) that generates data with these columns:
Using the code below, I am returning an nvarchar field from MS SQL 2005
I have a database running on MS SQL Server 2005 and an ASP.NET 3.5
I'm using MS Sql 2005. Why does this give me the correct results (returns
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web

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.