?
Solved

How to force a return record even if none are found matching my criteria?

Posted on 2005-03-29
7
Medium Priority
?
214 Views
Last Modified: 2006-11-18
I'm trying to force a select query to return a record with default values IF the criteria does NOT find a matching value.

Example:

SELECT     Field1 + Field2 + Field3 + Field4 AS TotalFields, UniqueID
FROM       MyTable
WHERE     (UniqueID = 'DM') AND (SomeDateField = '3/31/2004')

If the select does not return any records, I need it to be forced to return at least one record with default values (that I can specify).

Is this possible?

Rob.
0
Comment
Question by:RobAinscough
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 13654239
SELECT     Field1 + Field2 + Field3 + Field4 AS TotalFields, UniqueID
FROM       MyTable
WHERE     (UniqueID = 'DM') AND (SomeDateField = '3/31/2004')
IF @@ROWCOUNT=0
          SELECT 0 AS TotalFields,'' AS UniqueID
0
 
LVL 17

Accepted Solution

by:
mokule earned 500 total points
ID: 13654261
or
IF EXISTS( SELECT     Field1 + Field2 + Field3 + Field4 AS TotalFields, UniqueID
FROM       MyTable
WHERE     (UniqueID = 'DM') AND (SomeDateField = '3/31/2004')
)
    SELECT     Field1 + Field2 + Field3 + Field4 AS TotalFields, UniqueID
    FROM       MyTable
    WHERE     (UniqueID = 'DM') AND (SomeDateField = '3/31/2004')
ELSE
          SELECT 0 AS TotalFields,'' AS UniqueID
0
 

Author Comment

by:RobAinscough
ID: 13654714
The IF EXISTS does work, thank you.  However, I'm trying to CREATE VIEW using the above construct and apparently VIEWs do NOT support the use of IF EXISTS...

hmmmm...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 13655036
>>However, I'm trying to CREATE VIEW using the above construct <<
It is always good to know ahead of time.  

Consider adding it to the original table and only displaying if the condition does not return any rows.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 13655056
Friendly reminder:  This question has been classified as abandoned:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21305266.html
0
 

Author Comment

by:RobAinscough
ID: 13655354
mokule answered my question with the information I originally provided so I've awarded the points, not his/her fault that my additional requirements couldn't work with that solution.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 13655568
Fair enough.
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question