Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Searching down multiple hierachies

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-09-21 : 10:11:03
Georg writes "Hello there. Thanks for having this forum open. It's been a great help over the last few months of me getting to know MSSQL Server ;).

I have the following situation:

The application we've designed for our bosses had to take info from a message sent to our system and store, amongst other things, information on a person. This person had to have available to it the possibility to store no, one, or multiple addresses with it. So, we created a table structure with a relationship between two tables (t_Patient and t_Address). Because we have many types of persons in our system, but have the need to contain different types of information about them, we are using t_Address to be able to contain multiple types of addresses (that is, t_Patient is linked, but so are many other "Person" tables).

Now my bosses are asking for a search form to be able to search for these persons in the database. I've come as so far as to be able to search with a multitude of parameters, but only to include one address to be searched on (I only need to return one associated address with a bit set to 1).

Finally, the question:

How can I make this an efficient (1 million+ records) stored procedure without breaking my neck?

Any help would be greatly appreciated.

Thank you,
Georg.

P.S. this is what I've got so far:

CREATE PROCEDURE pr_PatientsSearch
@orderby INT = 1,
@d_birth DATETIME = NULL,
@s_firstname VARCHAR(50) = NULL,

...

@s_street1 VARCHAR(100) = NULL,
@s_email2 VARCHAR(100) = NULL

AS

SELECT p.u_patientid AS pu_patientid,
ISNULL(p.s_mrno, '') AS ps_mrno,
p.d_birth AS pd_birth,
p.d_death AS pd_death,
p.d_lastedit AS pd_lastedit,

...

a.u_addressid AS au_addressid,
a.u_entityid AS as_entityid,
ISNULL(a.s_street1, '') AS as_street1,
ISNULL(a.b_delete, 0) AS ab_delete,
ISNULL(a.b_default, 0) AS ab_default

...

FROM t_Patient p
LEFT OUTER JOIN
t_Address a
ON p.u_patientid = a.u_entityid
WHERE (p.[b_delete] = 0)
AND
(a.[b_default]=1 OR a.[b_default]=NULL)
AND
(p.[d_birth] = COALESCE(@d_birth, p.[d_birth]))
AND
(p.[s_acctno] LIKE COALESCE(@s_acctno, p.[s_acctno]))

...

AND
(a.[s_street2] LIKE COALESCE(@s_street2, a.[s_street2]))
(a.[s_city] LIKE COALESCE(@s_city, a.[s_city]))
AND
(a.[s_statecode] = COALESCE(@s_statecode, a.[s_statecode]))

...

AND
(a.[s_email2] LIKE COALESCE(@s_email2, a.[s_email2]))
ORDER BY
CASE @orderby
WHEN 1
THEN
p.s_lastname+ ', ' +p.s_firstname

...


END ASC,
CASE @orderby
WHEN 2
THEN
p.s_lastname+ ', ' +p.s_firstname

...

END DESC
GO
"
   

- Advertisement -