Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. syntax error for simple IF - THEN

syntax error for simple IF - THEN

Scheduled Pinned Locked Moved Database
helpmysqlcomannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I am trying to creat a simple IF - THEN in MySQL but getting a synax error message. can anyone help please... here is my statement:

    IF EXISTS (SELECT payroll_id FROM payroll WHERE location_id = param_location AND payroll_month = param_month AND payroll_year = param_year) THEN
    SET param_payroll_id = 0;
    END IF;

    Thanks, Jassim[^]

    Technology News @ www.JassimRahma.com

    T 1 Reply Last reply
    0
    • J Jassim Rahma

      Hi, I am trying to creat a simple IF - THEN in MySQL but getting a synax error message. can anyone help please... here is my statement:

      IF EXISTS (SELECT payroll_id FROM payroll WHERE location_id = param_location AND payroll_month = param_month AND payroll_year = param_year) THEN
      SET param_payroll_id = 0;
      END IF;

      Thanks, Jassim[^]

      Technology News @ www.JassimRahma.com

      T Offline
      T Offline
      thatraja
      wrote on last edited by
      #2

      Never used MySql but based on my search, you can't use that syntax in Mysql. Alternate way is, just store the result of query into some variable & use it for comparison to achieve the output. Check this sample MySQL Stored Procedures IF NOT EXISTS()[^] BTW, always include the complete error message(just 'Syntax error' is not enough) in your question which is better for members to give you answers quickly.

      thatraja

      Coming soon1 | Coming soon2 | Coming soon3New

      J 1 Reply Last reply
      0
      • T thatraja

        Never used MySql but based on my search, you can't use that syntax in Mysql. Alternate way is, just store the result of query into some variable & use it for comparison to achieve the output. Check this sample MySQL Stored Procedures IF NOT EXISTS()[^] BTW, always include the complete error message(just 'Syntax error' is not enough) in your question which is better for members to give you answers quickly.

        thatraja

        Coming soon1 | Coming soon2 | Coming soon3New

        J Offline
        J Offline
        Jassim Rahma
        wrote on last edited by
        #3

        I tried below code but getting same error for the IF--THEN

        SET param_payroll_exists = (SELECT COUNT(*) FROM payroll WHERE location_id = param_location AND payroll_month = param_month AND payroll_year = param_year);

        IF EXISTS (param_payroll_exists > 0) THEN
        SET param_payroll_id = 0;
        END IF;

        the exact error is: You have an error in your SQL syntax. Check the manual that corresponds to you MySQL server for the right syntax to use near 'param_payroll_exists > 0) THEN ..........

        Technology News @ www.JassimRahma.com

        P T 2 Replies Last reply
        0
        • J Jassim Rahma

          I tried below code but getting same error for the IF--THEN

          SET param_payroll_exists = (SELECT COUNT(*) FROM payroll WHERE location_id = param_location AND payroll_month = param_month AND payroll_year = param_year);

          IF EXISTS (param_payroll_exists > 0) THEN
          SET param_payroll_id = 0;
          END IF;

          the exact error is: You have an error in your SQL syntax. Check the manual that corresponds to you MySQL server for the right syntax to use near 'param_payroll_exists > 0) THEN ..........

          Technology News @ www.JassimRahma.com

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Jassim Rahma wrote:

          IF EXISTS (param_payroll_exists > 0) THEN

          Do you really mean EXISTS here?

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          1 Reply Last reply
          0
          • J Jassim Rahma

            I tried below code but getting same error for the IF--THEN

            SET param_payroll_exists = (SELECT COUNT(*) FROM payroll WHERE location_id = param_location AND payroll_month = param_month AND payroll_year = param_year);

            IF EXISTS (param_payroll_exists > 0) THEN
            SET param_payroll_id = 0;
            END IF;

            the exact error is: You have an error in your SQL syntax. Check the manual that corresponds to you MySQL server for the right syntax to use near 'param_payroll_exists > 0) THEN ..........

            Technology News @ www.JassimRahma.com

            T Offline
            T Offline
            thatraja
            wrote on last edited by
            #5

            You didn't get Peter's reply which's fair enough? Just remove EXISTS from that query & run. It's over.

            thatraja

            Coming soon1 | Coming soon2 | Coming soon3New

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups