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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:23:27+00:00 2026-06-01T17:23:27+00:00

I have a customer table with Cust_Id , Name , City and search is

  • 0

I have a customer table with Cust_Id, Name, City and search is based upon any or all of the above three.

Which one Should I go for ?

  1. Dynamic SQL:

    declare @str varchar(1000)
    set @str = 'Select [Sno],[Cust_Id],[Name],[City],[Country],[State] 
                from Customer where 1 = 1'
    if (@Cust_Id != '')
        set @str = @str + ' and Cust_Id    =  ''' + @Cust_Id + ''''
    if (@Name != '')
        set @str = @str + ' and Name like ''' + @Name + '%'''
    if (@City  != '')
        set @str = @str + ' and City like ''' + @City + '%'''
    exec (@str)
    
  2. Simple query:

    select
        [Sno],[Cust_Id],[Name],[City],[Country],[State]
    from
        Customer
    where
        (@Cust_Id  = '' or Cust_Id    =  @Cust_Id) and
        (@Name     = '' or Name     like @Name + '%') and
        (@City     = '' or City     like @City + '%')
    

Which one should I prefer (1 or 2) and what are advantages?

After going through everyone’s suggestion , here is what i finally got.

  DECLARE @str NVARCHAR(1000)
  DECLARE @ParametersDefinition NVARCHAR(500)
  SET @ParametersDefinition = N'@InnerCust_Id varchar(10),
                              @InnerName varchar(30),@InnerCity varchar(30)'


 SET @str = 'Select [Sno],[Cust_Id],[Name],[City],[Country],[State]
                                          from Customer where 1 = 1'

 IF(@Cust_Id != '')
    SET @str = @str + ' and Cust_Id = @InnerCust_Id'
 IF(@Name != '')
    SET @str = @str + ' and Name like @InnerName'
 IF(@City  != '')
    SET @str = @str + ' and City like @InnerCity'


-- ADD the % symbol for search based upon the LIKE keyword
SELECT  @Name = @Name + '%', @City = @City+ '%'

EXEC sp_executesql @str, @ParametersDefinition,
                      @InnerCust_Id = @Cust_Id,
                      @InnerName    = @Name,
                      @InnerCity    = @City;

Note : @Cust_Id, @Name and @City are parameters being passed to the stored procedure

References :
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/changing-exec-to-sp_executesql-doesn-t-p

http://www.sommarskog.se/dynamic_sql.html

http://msdn.microsoft.com/en-us/library/ms175170.aspx

  • 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-01T17:23:29+00:00Added an answer on June 1, 2026 at 5:23 pm

    Dynamic SQL can be a little more difficult to write, and it is vulnerable to SQL Injection if you are not careful. However, it outperforms the “non-dynamic”/Simple or query.

    Read more about it here. http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/do-you-use-column-param-or-param-is-null

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

Sidebar

Related Questions

Say we have customer table which has record CustId LastName 1 Hamlin In one
Let's say I have a Customer table which has a PrimaryContactId field and a
For example, in my WCF, if I have a Customer table and an Order
Say I have a typical customer table; Id Int Identity Primary Key, FirstName varchar(255),
I have a performance problem on a query. First table is a Customer table
I have a MySQL table that defines values for a specific customer type. |
I have a simple database named customer with a single table data.I want to
Lets say I have a Customers table and an Orders table with a one-to-many
I have 2 related MySQL tables in a one to many relationship. Customers: cust_id,
I have a table which stores a category mix for customers. The number of

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.