473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Dynamic Where Clauses

I have a table like the following:

CREATE TABLE Customers
(
CustomerCode varchar(20) not null, Surname varchar(60) not null,
Firstname varchar(60) null, Salutation varchar(60) null,
CustomerName varchar(182) null, DateOfOldestOpenItem datetime null
)

and what I want is to have a SProc to do a search... conceptially as following:

CREATE PROC usp_CustomerNameSearch
@SearchType varchar(10),
@SearchValue varchar(182)
AS

SELECT *
FROM Customers
WHERE
-- CASE WHEN @SearchType = 'FN' THEN Firstname = @SearchValue
-- ELSE CustomerName = @SearchValue END

I know its the wrong syntax but CASE doesn't work against fields in where clauses.

Any suggestion I can achieve the same result?

(NOTE: create a large varchar variable to dynamically produce the query is NOT an option in my case unfortunately)
Sep 17 '07 #1
2 1633
ck9663
2,878 Expert 2GB
I have a table like the following:

CREATE TABLE Customers
(
CustomerCode varchar(20) not null, Surname varchar(60) not null,
Firstname varchar(60) null, Salutation varchar(60) null,
CustomerName varchar(182) null, DateOfOldestOpenItem datetime null
)

and what I want is to have a SProc to do a search... conceptially as following:

CREATE PROC usp_CustomerNameSearch
@SearchType varchar(10),
@SearchValue varchar(182)
AS

SELECT *
FROM Customers
WHERE
-- CASE WHEN @SearchType = 'FN' THEN Firstname = @SearchValue
-- ELSE CustomerName = @SearchValue END

I know its the wrong syntax but CASE doesn't work against fields in where clauses.

Any suggestion I can achieve the same result?

(NOTE: create a large varchar variable to dynamically produce the query is NOT an option in my case unfortunately)

if you need the entire table returned to you, i think a table-function is your best choice. try something like:


Expand|Select|Wrap|Line Numbers
  1.  
  2. select * from 
  3. (SELECT 'F' as key, * 
  4. FROM Customers
  5. WHERE Firstname = @SearchValue 
  6. union 
  7. SELECT 'C' as key, * 
  8. FROM Customers
  9. WHERE CustomerName = @SearchValue)  mysearch
  10. where key = substring(@SearchType,1,1)
  11.  
  12.  
or you can build your query in a string and do and EXEC (@sqlstring)
Sep 17 '07 #2
I actually don't need to return the entire table. I was trying to make it simpler to read and to be answer.

The actual script is about 400 lines with lots of subquery inside.

So both of the option you suggest will work, but will be lots and lots of work.

I just thought its the easiest if I just need to change the lines in question :)
Sep 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: diroddi | last post by:
I am having a problem using a dynamic where clause. I have a feeling that I am overlooking something very simple, although I can't seem to figure it out. The error i'm getting is: You have an...
1
by: dave | last post by:
When i hit a button in the web page, the servlet will run and should post some dynamic data into the new window (using Java Script), The new generated window will contain the info generated from...
7
by: mittal.pradeep | last post by:
What is the better table design for a data collection application. 1. Vertical model (pk, attributeName, AttributeValue) 2. Custom columns (pk, custom1, custom2, custom3...custom50) Since the...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
2
by: m.k.ball | last post by:
Thanks Rich - that's great. Before I found this group, I thought I had a reasonable understanding of SQL (well, MySQL's implementation of it, at least) but the truth is there are great chunks that...
3
by: topmind | last post by:
I am generally new to dot.net, coming from "scriptish" web languages such as ColdFusion and Php. I have a few questions if you don't mind. First, how does one go about inserting dynamic SQL...
5
by: Jyotirmoy Bhattacharya | last post by:
I'm a newcomer to Python. I have just discovered nested list comprehensions and I need help to understand how the if-clause interacts with the multiple for-clauses. I have this small program: ...
4
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I am trying to set up a dynamic search using linq. I believe the syntax is correct, but cannot confirm it because when I try to cast my Session from a List<to IQueryable<>, I get a cast error...
0
by: Jay Douglas | last post by:
Hello, I've found some posts on creating dynamic WHERE clauses in LINQ to SQL using predicate builders and extending lamda expressions. One issue with these posts is all the examples only use a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.