Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 30th, 2007, 02:25 AM
Authorized User
 
Join Date: Jul 2007
Location: Karachi, Sindh, Pakistan.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Best Searching Criteria

I have a table

 GO

 CREATE TABLE [dbo].[Speech]
 (
  [SpeechId] [int] IDENTITY(1,1) NOT NULL CONSTRAINT PkSpeech_SpeechId PRIMARY KEY,
  [UniqueName] [varchar](52) NOT NULL,
  [NativeName] [nvarchar](52) NOT NULL,
  [Place] [nvarchar](52) NOT NULL,
  [Type] [smallint] NOT NULL,
  [LanguageId] [char](2) NOT NULL CONSTRAINT FkSpeech_LanguageId FOREIGN KEY (LanguageId) REFERENCES Language(LanguageId) ON UPDATE CASCADE ON DELETE CASCADE,
  [SpeakerId] [int] NOT NULL CONSTRAINT FkSpeech_SpeakerId FOREIGN KEY (SpeakerId) REFERENCES Speaker(SpeakerId) ON DELETE CASCADE,
  [IsFavorite] [bit] NOT NULL,
  [IsVisible] [bit] NOT NULL,
  [CreatedDate] [datetime] NOT NULL DEFAULT GETDATE(),
  [ModifiedDate] [datetime] NULL
 )

Now I want to search the Table Speech

Sometimes by : SpeechId
Sometimes by : SpeakerId
Sometimes by : LanguageId
Sometimes by : SpeechId And LanguageId
Sometimes by : SpeakerId And LanguageId

All can have conditions with IsVisible, IsFavorite and Type columns.

for example

I need all Speeches with
any particular SpeakerId and LanguageId
with IsVisible equals to true
and IsFvaorite No Matter
and Type equals to Audio

For these type of queries I think the solution is

GO

 CREATE PROCEDURE [dbo].[sprocGetSpeech]

  @speechId int = NULL,
  @uniqueName varchar(52) = NULL,
  @nativeName nvarchar(52) = NULL,
  @place nvarchar(52) = NULL,
  @type smallint = NULL,
  @languageId char(2) = NULL,
  @speakerId int = NULL,
  @isFavorite bit = NULL,
  @isVisible bit = NULL

 AS

  SELECT
   SpeechId,
   UniqueName,
   NativeName,
   Place,
   Type,
   LanguageId,
   SpeakerId,
   IsFavorite,
   IsVisible,
   CreatedDate,
   ModifiedDate
  FROM
   Speech
  WHERE
   SpeechId = @speechId
   AND UniqueName = CASE WHEN @uniqueName IS NULL THEN [UniqueName] ELSE @uniqueName END
   AND NativeName = CASE WHEN @nativeName IS NULL THEN [NativeName] ELSE @NativeName END
   AND Place = CASE WHEN @place IS NULL THEN [Place] ELSE @place END
   AND Type = CASE WHEN @type IS NULL THEN [Type] ELSE @type END
   AND LanguageId = CASE WHEN @languageId IS NULL THEN [LanguageId] ELSE @languageId END
   AND SpeakerId = CASE WHEN @speakerId IS NULL THEN [SpeakerId] ELSE @speakerId END
   AND IsFavorite = CASE WHEN @isFavorite IS NULL THEN [IsFavorite] ELSE @isFavorite END
   AND IsVisible = CASE WHEN @isVisible IS NULL THEN [IsVisible] ELSE @isVisible END


Can anyone tell me?

Is it right way to do?
Do you have any better solution?
If my solution is better then Is there any performance loss with that query?


Reply With Quote
  #2 (permalink)  
Old September 30th, 2007, 07:14 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It looks about right but you can simplify the WHERE clause to get rid of the CASE statements:
Code:
(UniqueName = @UniqueName OR @UniqueName IS NULL)
So this part will be true if the UniqueName field matches the @UniqueName parameter or if the @UniqueName parameter is null.

--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AND/OR criteria in a JOIN fizzerchris Access 5 July 18th, 2007 02:37 PM
Searching Criteria Issue phungleon SQL Server ASP 18 June 29th, 2005 02:29 PM
Criteria in Query lryckman Access 1 June 23rd, 2004 11:11 AM
what is the criteria to get one record yylee Access 1 April 29th, 2004 04:19 PM
Query Criteria Clive Astley Access 4 March 25th, 2004 03:27 AM



All times are GMT -4. The time now is 10:00 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.