Subquery error

  • Hi,

    I have the following query

    IF(SELECT 1 FROM VERSION

    WHERE PATINDEX ('7.0%', VERSION)> 0

    OR PATINDEX ('7.1%', VERSION)> 0

    OR PATINDEX ('7.2%', VERSION)> 0

    OR PATINDEX ('7.3%', VERSION)> 0

    OR PATINDEX ('7.4%', VERSION)> 0

    OR PATINDEX ('7.5%', VERSION)> 0

    OR PATINDEX ('7.6%', VERSION)> 0

    OR PATINDEX ('7.7%', VERSION)> 0)>0

    BEGIN

    if statement is giving error

    "Msg 512, Level 16, State 1, Line 1

    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

    because its trying to give the following output

    1

    1

    1

    1

    1

    1

    any solution pls

    Regards,

    antony

  • Hi Anthony,

    the question is, what do you want to do?

    Do you want to check whether at least one row is returned? Then take:

    IF exists(SELECT 1 FROM VERSION

    WHERE PATINDEX ('7.0%', VERSION)> 0

    OR PATINDEX ('7.1%', VERSION)> 0

    OR PATINDEX ('7.2%', VERSION)> 0

    OR PATINDEX ('7.3%', VERSION)> 0

    OR PATINDEX ('7.4%', VERSION)> 0

    OR PATINDEX ('7.5%', VERSION)> 0

    OR PATINDEX ('7.6%', VERSION)> 0

    OR PATINDEX ('7.7%', VERSION)> 0)

    BEGIN

    Greets

    Patrick

    [font="Arial"]Kind regards,

    Patrick Fiedler
    Consultant

    Fiedler SQL Consulting

    www.fiedler-sql-consulting.de
    [/font]

  • It's returning a 1 for each row that satisfies the predicates.

    Maybe you could use IF EXISTS?

  • yes, it works.. thanks a lot

  • IF EXISTS(SELECT 1 FROM VERSION

    WHERE VERSION LIKE '7.[01234567]%')

    BEGIN

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

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

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