Results 1 to 2 of 2
  1. #1
    Gray is offline Novice
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    England
    Posts
    16

    Switch Statement in WHERE Clause

    Hi

    Been trying to guess this for hours...

    I want to count the number of records returned in a SELECT subquery and alter the WHERE of the main query accordingly...I know this should be possible.. So in English... (System_Record is a Boolean)...

    if records returned by SELECT = 0 then set the WHERE condition " OR System_Record=TRUE"



    if records returned by SELECT > 0 then set the WHERE condition " AND System_Record=FALSE"

    My code so far.. don't laugh! The QBE did!
    Code:
    SELECT 
    TBL.Item_Name,
    TBL.Unique_No,
    TBL.ID, 
    (SELECT COUNT(UNIQUE_NO) FROM ADDRS_TBL AS TBL1 
    WHERE TBL1.ID=75666) As My_Count 
    FROM ADDRS_TBL AS TBL 
    WHERE (TBL.ID=75666) 
    SWITCH(My_Count=0, " OR System_Record=True", My_Count>0, " AND System_Record=False")
    Yikes!! Can someone help please?

  2. #2
    Gray is offline Novice
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    England
    Posts
    16
    Hi

    Sussed it!... For anyone else reading, this seems to work for me....

    Code:
    SELECT 
    TBL.Item_Name,
    TBL.Unique_No,
    TBL.ID,
    TBL.System_Record
    FROM Addrs As TBL 
    WHERE (TBL.ID=75666 AND EXISTS( SELECT * FROM ADDRS As TBL1 WHERE TBL1.ID=75666)) OR (System_Record=True AND NOT EXISTS( SELECT * FROM ADDRS As TBL1 WHERE TBL1.ID=75666) )
    When an invalid ID is used, my system_record is retrieved... when a valid one is used results are as expected...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using SQL switch function MS Access
    By sandlucky in forum Queries
    Replies: 18
    Last Post: 03-31-2011, 08:49 AM
  2. Use of COLLATE statement in SELECT clause
    By zurek in forum Queries
    Replies: 7
    Last Post: 03-16-2011, 06:46 AM
  3. Case or Switch in an update statement
    By allenjasonbrown@gmail.com in forum Queries
    Replies: 7
    Last Post: 11-17-2010, 01:49 AM
  4. Replies: 6
    Last Post: 04-06-2010, 03:00 PM
  5. Switch and Tables
    By UtilityRyan in forum Database Design
    Replies: 0
    Last Post: 06-12-2007, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums