Wrox Programmer Forums
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 30th, 2007, 02:25 AM
Authorized User
Join Date: Jul 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Best Searching Criteria

I have a table


 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


 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


   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?

Old September 30th, 2007, 07:14 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

It looks about right but you can simplify the WHERE clause to get rid of the CASE statements:
(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)

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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.