All IF-statement returns "Syntax error"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luttkens
    New Member
    • Jul 2007
    • 23

    All IF-statement returns "Syntax error"

    I used to have no problems using IF-statement in SQL-queries. Now nothing works, it always returns "Syntax error". This query also returns an error.

    [CODE=mysql]IF (1=1) THEN
    SELECT 'True'
    ELSE
    SELECT 'False'
    END IF[/CODE]

    What is wrong? I know it doesn't do anything, but why do I get syntax error?
    Last edited by mwasif; Jul 27 '07, 04:19 PM. Reason: Added code tag
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, luttkens. Welcome to TSDN!

    Try this instead:
    [code=mysql]
    SELECT
    IF
    (
    {condition},
    TRUE,
    FALSE
    );
    [/code]

    You can only use IF ... END IF blocks in stored procedures.


    P.S., Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

    Comment

    • luttkens
      New Member
      • Jul 2007
      • 23

      #3
      Thanks!

      What if i want to do something like this:

      Code:
      IF [condition] THEN
           INSERT INTO ....
      ELSE
           UPDATE ....
      END IF

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by luttkens
        Thanks!

        What if i want to do something like this:

        Code:
        IF [condition] THEN
             INSERT INTO ....
        ELSE
             UPDATE ....
        END IF
        I thought pb just explained that to you. Are you looking for the if function instead?

        Comment

        • luttkens
          New Member
          • Jul 2007
          • 23

          #5
          Originally posted by r035198x
          I thought pb just explained that to you. Are you looking for the if function instead?
          I don't think so. I tried the code like this:

          Code:
          SELECT   IF
              (
                  3>1,
                  INSERT INTO table1(field1) VALUES ('blabla'),
                  INSERT INTO table1(field1) VALUES ('BLABLA')
              );
          and it still doesn't work. I reckon his code only works if you want to return a string, like you do with the SELECT statement.

          What I want to do is checking if a name in a table exists. If false, I want to create a new row and return the ID for the row. If true I just want to return the ID of that existing row.

          In SQL-server it works fine just writing like a did in my previous post.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, luttkens.

            Try this instead:

            [code=mysql]
            INSERT
            INTO
            `table1`
            (
            `field1`
            )
            VALUES
            (
            IF(
            3 > 1,
            'someText',
            'someOtherText'
            )
            )
            [/code]

            Comment

            • luttkens
              New Member
              • Jul 2007
              • 23

              #7
              Thanks for helping me! Problem solved!

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, luttkens.

                Originally posted by luttkens
                Thanks for helping me! Problem solved!
                Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

                Comment

                Working...