473,466 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Display no records if parameters are empty

I have a stored procedure which accepts up to four optional parameters. This works fine, but if all the text boxes which (feed the parameters)are empty then I get all records displayed which I do not want.

Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[sp_threekeywordsearch]
  2. @keyword1 VARCHAR(255) = null,
  3. @keyword2 VARCHAR(255) = null ,
  4. @keyword3 VARCHAR(255) = null,
  5. @composer varchar(255) = null
  6.  
  7.  
  8. AS
  9. SELECT description, TrackTitle,CDTitle,Source
  10. FROM dbo.NON_STOP_PLAYLIST
  11. where   (keywords like '%' + @keyword1 + '%' or @keyword1 IS NULL)  
  12.     and  
  13.     (keywords like '%' + @keyword2 + '%' or @keyword2 IS NULL)  
  14. and  
  15.     (keywords like '%' + @keyword3 + '%' or @keyword3 IS NULL)  and
  16.     (composer like '%' + @composer + '%' or @composer IS NULL)  
Nov 4 '10 #1
6 1919
NeoPa
32,556 Recognized Expert Moderator MVP
As the logic you wish to apply for all NULLs is the opposite of the logic used for NULLs when any keywords are present I suggest you will need to use CASE to handle all NULLs as a special situation.
Nov 4 '10 #2
Hi NeoPa,

Do you have a simple example of the syntax for this ?

Bill
Nov 5 '10 #3
Dear Frnd...

this may help you..

Expand|Select|Wrap|Line Numbers
  1. if(@keyword1 is null and @keyword2 is null  and  @keyword3=null and @composer is null)
  2.     SELECT description, TrackTitle,CDTitle,Source FROM dbo.NON_STOP_PLAYLIST where 1<>1
  3. else
  4. SELECT description, TrackTitle,CDTitle,Source 
  5. FROM dbo.NON_STOP_PLAYLIST 
  6. where   (keywords like '%' + @keyword1 + '%' or @keyword1 IS NULL)   
  7.     and   
  8.     (keywords like '%' + @keyword2 + '%' or @keyword2 IS NULL)   
  9. and   
  10.     (keywords like '%' + @keyword3 + '%' or @keyword3 IS NULL)  and 
  11.     (composer like '%' + @composer + '%' or @composer IS NULL)
Nov 5 '10 #4
NeoPa
32,556 Recognized Expert Moderator MVP
CASE (Transact-SQL) gives you examples as well as the full syntax specification.
Nov 5 '10 #5
its very bad.. not posting reply, if someone posted an answer found worked or not .... I'm leaving out from this forum.. I feel so bad
Nov 8 '10 #6
MMcCarthy
14,534 Recognized Expert Moderator MVP
Sandeep your post has been displayed. The moderation queue can be a little slow sometimes. You can avoid this by registering as a member.
Nov 9 '10 #7

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

Similar topics

5
by: Miguel Dias Moura | last post by:
Hello, I have a search form in an ASP.NET/VB page. The form has the input text box and the button "search". The keywords are passed in the URL to results.aspx. Here is an example:...
2
by: yoshitha | last post by:
hi, i want to display records in datagrid. strtestids() is a string array. it contains some testids. based on these ids i've to fill the datagrid. i wrote code like this
0
by: Tony | last post by:
vs 2002 - vb - access 2002 - xp - web forms I want to display records, one at a time. I have about 25 fields to display. I've created SQL and retrieved records. I placed data in 25 separate...
3
by: Kai Zhang | last post by:
I am trying to display some database records in datagrid using dataset. the records need to be displayed are couple of thousands, but the records in database that the SQL query needs to exam are...
3
by: Bob Sanderson | last post by:
I am trying to create a form for a MySQL database similar to a spreadsheet. The idea is to display a list of records, with the last line of the list being an input form. When the user enters data...
5
by: dorandoran | last post by:
How do I display records using object oriented method? All I have the link to their api help http://www.e-topco.com/oa/oa_help/OpenAir_SOAP.htm and few example. I am want display all the...
2
by: kurtzky | last post by:
i created a form that should function as follows: i will enter a number in a textbox..then it should query from the database all the records which has that number..these records will have a...
1
Soniad
by: Soniad | last post by:
Hello, I have stored procedure in which i am passing parameters (varchar(8000)), these parameters can have values or can be empty (''), i have written blocks of codes for each parameter , which...
8
by: jawad malik | last post by:
I want that each time i execute query it display records in a way that the top record will be positioned last and so on. First time when i executed query it will display like this name A B ...
0
by: Rahul Metha | last post by:
o Select table from drop down list. o Display records in Grid – Display all records belong to table in grid. o Table Fields – This section will have all the fields which are belongs to table.
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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,...
0
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...
0
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 ...

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.