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!) -

    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

  • Soemthing like this maybe ?

    DECLARE @choice int;

    SET @choice = 0;

    if exists (select 1 from yourtable)

    set @choice = 1;

  • Quick questions, which product code? Is there only one product code or should it be any product code? What's the role of the variable, wouldn't it be better to have a column with a choice flag?

    The problem itself is simple, straight forward solution is to use the sign of th count of codes, produces a list of codes with either 0 as non-existing or 1 for existing codes.

    😎

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply