Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2014
    Posts
    35

    Unanswered: Conditional logic?

    Hi Everyone,

    I am attempting to add some conditional logic to a query. The basic premise is as follows (this query snippet does work!) -

    Code:
    DECLARE @choice int
    
    SET @choice = 0
    
    IF @choice = 1
    BEGIN
    	SELECT 'True'
    END
    ELSE
    BEGIN
    	SELECT 'False'
    END
    I now want to go one step further by adding a SELECT statement that will ultimately determine how the @choice variable is set.

    For example if a product code exists in the appropriate database table I want to set @choice to 1, otherwise set it to 0.

    If anybody can suggest how I can go about this it will be greatly appreciated.

    Kind Regards,

    David

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    IF EXISTS (SELECT * FROM products WHERE product_code = '937')
      BEGIN
        SET @choice = 1;
      END
    ELSE
      BEGIN
        SET @choice = 0;
      END
    ;
    George
    Home | Blog

  3. #3
    Join Date
    Apr 2012
    Posts
    213
    Hi,

    David, you can also try something like this:

    Code:
    declare @choice int;
    set @choice = coalesce((select 1 from products where product_code = 123), 0);
    Hope this helps.

  4. #4
    Join Date
    Mar 2014
    Posts
    35
    Thanks all for the wonderful suggestions.

Posting Permissions

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