473,770 Members | 4,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Searching multiple entities at a time


Dear Group

Can anyone provide a sample query for the following scenario?

Let's assume I want to search for an order someone placed which might
be an individual or company. An individuals first name is stored in
column FirstName And the individuls last name in column LastName of the
contact table and the company name is stored in column CompanyName of
the company table.

If a user issues a query with CName parameter how can I search all
these columns and tables at the same time to see if there's a match? I
might add that there's a column in the orders table that holds
0=Contact 1=Company but ideally I don't want to use this column.

Thanks very much for your help and efforts.

Martin

Aug 27 '05 #1
2 1091
Stu
Just a dirty stab at it:
DECLARE @Cname varchar(20)
SELECT NameResult = FirstName & ' ' & LastName,
Type = 'Contact'
FROM contact
WHERE FirstName LIKE '%' + @CNAME + '%'
OR LastName LIKE '%' + @CNAME + '%'
UNION ALL
SELECT NameResult = CompanyName,
Type = 'Company'
FROM company
WHERE CompanyName LIKE '%' + @CNAME + '%'

/*untested */

HTH,
Stu

Aug 27 '05 #2
(th************ @hotmail.com) writes:
Can anyone provide a sample query for the following scenario?

Let's assume I want to search for an order someone placed which might
be an individual or company. An individuals first name is stored in
column FirstName And the individuls last name in column LastName of the
contact table and the company name is stored in column CompanyName of
the company table.

If a user issues a query with CName parameter how can I search all
these columns and tables at the same time to see if there's a match? I
might add that there's a column in the orders table that holds
0=Contact 1=Company but ideally I don't want to use this column.


Since this is likely to be a commaon query, I would consider a redesign,
and have a table Customers. If you then need to have different data
for individuals and companies, you can have sub-tables to Customers
that holds this information.

The query Stu suggested will give the correct result, but it will not
perform well if there are plenty of customers. Partly this is because he
wrote:

LIKE '%' + @CNAME + '%'

Sometimes it is good to permit users to use parts within the name, but
a seacch like:

LIKE @CNAME + '%'

can make use of an index. So it is better to write the search this
way. If users need to search for parts within a name, they can always
add an initial % themselves.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Aug 27 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1572
by: Matthew Burgess | last post by:
I'm not sure that XML allows me to do what I want, but here goes: Given that I have files a.xml and b.xml who both need to access an entity defined in c.ent how do I go about it? Ideally I'd like a syntax similar to: --- a.xml (or b.xml) --- <!DOCTYPE ... >
1
1683
by: Dalan | last post by:
I'm not certain if this is doable in the manner explained below, but surely there have been others who have done something similar. So whatever insight and assistance that can be provided will be much appreciated. I working with a separate Update Query (well several) that consolidate various goods from separate tables for access and selection on a sales/order invoice form. The data which is available is the TagNo,ItemDescrip, Quantity...
1
3269
by: Kathie G via AccessMonster.com | last post by:
Hello, I have a delima that I am not sure how to resolve. Records get entered manually or through an import. The records contain, RecordID, Client ID, Specimen Date, Visit Date, Client DOB, and other info on the client. The problem is, we bill off this information, and it is possible that the same client can be entered more than once. So I need to have a unique record based on Client, Visit (or) Specimen Date, and date entered. ...
9
2778
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I have been commissioned to create a web-based application for a client. It has a formsaunthentication...
33
2515
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following criteria: I have a list of about 100 numbers which correspond to the ID field and also another 40 say numbers corresponding to the numbers in the PRODUCT field. I want to show the rows that correspond to both these criteria.
5
5402
by: Jeff | last post by:
We are using .Net and the wsdl Utility to generate proxies to consume web services built using the BEA toolset. The data architects on the BEA side create XML schemas with various entities in separate files for ease of maintainability. These schemas are all part of the same namespace. When defining a web service that access more than one of these entities, the wsdl file generated by BEA contains multiple schema elements with the same...
3
8450
by: ian_jacobsen | last post by:
First let me start by saying that this problem is not consistently reproducible. I have a windows service that creates reports for a group of entities. This service can process multiple groups at a single time, where each group is running in a separate thread. I have noticed mixed behavior when running multiple groups at one time (in separate threads). I have received the following two messages logged as errors. - A Crystal Reports...
60
4939
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
52
6351
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.