Results 1 to 2 of 2

Thread: sql query :o)

  1. #1
    Join Date
    May 2002
    Posts
    37

    Angry Unanswered: sql query :o)

    Is it possible to combine an if statement with sql...example:

    if x=y then
    SELECT *
    FROM tbl_comp
    else
    SELECT *
    FROM tbl_comp
    Where x<>y

  2. #2
    Join Date
    Dec 2001
    Location
    Toronto, Canada
    Posts
    335
    Sure, then it becomes a SQL batch, the IF statement is part of what is called TRANSACT SQL or commonly called T-SQL. There is no THEN in the IF. It is IF...BEGIN...END

    Code:
    IF  x=y BEGIN
       SELECT * 
       FROM tbl_comp 
    END
    ELSE BEGIN  
       SELECT * 
       FROM tbl_comp 
       Where x<>y
    END
    But since your statement block is singular you can leave the BEGIN..END off.
    Code:
    IF  x=y
       SELECT * 
       FROM tbl_comp 
    ELSE 
       SELECT * 
       FROM tbl_comp 
       Where x<>y
    MCDBA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •