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

  • Home
  • SEARCH
  • 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 8197273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:42:25+00:00 2026-06-07T05:42:25+00:00

When you need Dynamic WHERE Clause I can use; CREATE PROCEDURE [dbo].[sp_sel_Articles] @articleId INT

  • 0

When you need Dynamic WHERE Clause I can use;

CREATE PROCEDURE [dbo].[sp_sel_Articles]
      @articleId            INT              = NULL
    , @title            NVARCHAR(250)    = NULL
    , @accessLevelId    INT              = NULL
AS
BEGIN
     SELECT *
     FROM table_Articles Art
     WHERE
          (Art.ArticleId = @articleId OR @articleId IS NULL)
          AND (Art.Title LIKE '%' + @title + '%' OR @title IS NULL)
          AND (Art.AccessLevelId = @accessLevelId OR @accessLevelId IS NULL)
END

So, I am able to invoke this procedure -for example- ONLY by ArticleId

EXEC [sp_sel_Articles] @articleId = 3

But, sometimes I’ll need to invoke by AccessLevelId and sometimes NOT by an EXACT VALUE. For example, I’ll need MORE THAN the given accesslevelId or LESS THAN.

Current procedure can ONLY handle the EXACT value by using

Art.AccessLevelId = @accessLevelId

Could also be possible to give the CONDITION type as well as the value into the procedure? It may seem very odd in this example but please just bear with me:

CREATE PROCEDURE [dbo].[sp_sel_Articles]
          @articleId            INT              = NULL
        , @title            NVARCHAR(250)    = NULL
        , @accessLevelId    INT              = NULL
        , **@accessLevelIdCondition**
    AS
    BEGIN
         SELECT *
         FROM table_Articles Art
         WHERE
              (Art.ArticleId = @articleId OR @articleId IS NULL)
              AND (Art.Title LIKE '%' + @title + '%' OR @title IS NULL)
              AND (Art.AccessLevelId **@accessLevelIdCondition** @accessLevelId OR @accessLevelId IS NULL)
    END

Perhaps an Function can be used, I don’t know. Since, there will be at least 20 Procedure that will require this flexibility, I’ll need a better, more global solution as much as possible rather than writing IF ELSE condition in every procedure.

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-06-07T05:42:26+00:00Added an answer on June 7, 2026 at 5:42 am

    Read this http://www.sommarskog.se/dynamic_sql.html before applying

      CREATE PROCEDURE [dbo].[sp_sel_Articles] 
                  @articleId            INT              = NULL 
                , @title            NVARCHAR(250)    = NULL 
                , @accessLevelId    INT              = NULL 
                , @accessLevelIdCondition varchar(100)
            AS 
            BEGIN 
                    DECLARE @SQL varchar(8000)
        SET @SQL='
             SELECT * 
             FROM table_Articles Art 
             WHERE 
                  (Art.ArticleId = '+cast(@articleId as varchar(100))+' OR '+cast(@articleId as varchar(100))+'IS NULL) 
                  AND (Art.Title LIKE ''%'' + @title + ''%'' OR @title IS NULL) 
                  AND (Art.AccessLevelId '+@accessLevelIdCondition+ cast(@accessLevelId as varchar(100))+' OR '+cast(@accessLevelId as varchar(100))+' IS NULL) '
        EXEC(@sql)
            END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create dynamic INNER JOIN (I believe I need to anyway). I
I need to create some dynamic menus in a VS2010 SDI application I'm writing.
I need to have a dynamic URL preferably from a Test Class. Can any
I need to create a dynamic array in Java, but the values type differ
I use Entity Framework 4. How can I perform a Generic Where Lambda Clause.?
Problem I have some pages that need dynamic data from website to generate output
I need to used dynamic order query in mysql and i have successfully achieved
I need to add dynamic controls and get its values in my asp.net mvc
I'm in need of a dynamic form that will be built from a list
I have several jsf pages. I need to add dynamic breadcrumbs to the header.xhtml.

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.