Select Statement When Returns Nothing

  • Hi,

    I have One SQL Query,

    Select FormID from Tablename

    Where Action in (1,2,3,4)

    And Status = 'Y'

    The above query Will return me Something if it satisfy the condition otherwise nothing: something like below:  

    I don't want Such output. If select statement returns NOTHING then it should return one Constant value Say 1.

    Please Reply.

     

     


    Kindest Regards,

    Vishal Prajapati

    DBA at Extentia Infromation Technology

  • This can probably be obtained in may ways. You could try the statement below:

    select isnull(t.FormId, dt.FormId)

    from

    (select 1 as FormId) dt

    left join Tablename t

    on t.Action in (1,2,3,4) and t.Status = 'Y'

  • hi,

    use yoir Query like this ::

    Select (case when action in (1,2,3,4) and     

                            status='y' then FormID

                            else  1 end ) Result from Tablename

  • Vishal,

    if no rows match, how many rows do you want returned? IF you wish only one row, I'd do it like so...

    Select FormID from Tablename

    Where Action in (1,2,3,4)

    And Status = 'Y'

    UNION

    SELECT 1 AS FormID

    WHERE NOT EXISTS

    (

    Select *

    from Tablename

    Where Action in (1,2,3,4)

    And Status = 'Y'

    )

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

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