6

I have the following script:


use my_db;

if (2 < 3) then
    select 1;
end if;

When I execute this with command:

mysql --user=myuser --password=mypassword < script.sql

I get the following error:

ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if (2 < 3) then select 1' at line 1

Can anybody explain me why this? From mysql docs found here I think it should be working fine.

1
  • 3
    The if statement is only allowed in programming blocks, such as stored procedures, user-defined functions, and triggers. Put the conditional logic in a stored procedure and call the stored procedure from the script. Commented Mar 25, 2017 at 13:17

2 Answers 2

3

If you can change your statement, I would recommend it doing it this way:

select if(2<3, 'true','false') as amount

Or wrap your code in a procedure:

create procedure my_procedure() 
begin
  if (2 < 3) then
      select 1;
  end if;
end;

-- Execute the procedure
call my_procedure();

-- Drop the procedure
drop procedure my_procedure;
Sign up to request clarification or add additional context in comments.

4 Comments

the point is that I want to create a procedure only if it doesn't exist. So, instead of select 1, there would be a create procedure... statement.
Ok, then you would have to wrap that check code into a procedure.
Still don't understand... :). How to check if IF isn't permitted?
PostgreSQL is more friendly. postgresql.org/docs/9.1/static/sql-do.html
1

Still don't understand... :). How to check if IF isn't permitted?

https://dev.mysql.com/doc/refman/5.7/en/flow-control-statements.html says:

MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE, and REPEAT constructs for flow control within stored programs. It also supports RETURN within stored functions.

(emphasis mine)

I wouldn't bother with writing stored routines in MySQL. If you need to do conditional SQL queries, I'd recommend learning a scripting language. Python is a good choice.

#!/bin/env python

import MySQLdb

db = MySQLdb.connect()

if 2 < 3:
    cur = db.cursor()
    cur.query('select 1')
    print cur.fetchall()

7 Comments

@Ako since I wrote this answer in 2017, we've seen the release of MySQL Shell, which allows developers to do logic statements in Python or Javascript.
Thanks for the tip. Just to elaborate, using an external language to handle SQL logic is bad solution and will probably never be accepted as a standard. SQL is a standalone scripting language, why the extra complication. Py and JS require a VM, where the trust issue becomes the tide turner.
I don't think that's correct. From its earliest days, SQL was intended to be a domain-specific language, to be used as a complement to another programming language. Part 3 of the ANSI/ISO SQL standard describes the Call Level Interface (CLI) for using SQL from another language (the standard describes using SQL with Ada, C/C++, COBOL, Fortran, MUMPS, Pascal, PL/I). Part 10 of the standard describes the Object Language Bindings (OLB) for using SQL embedded in Java.
The term CLI in the SQL standard does not refer to a command-line interface tool. It refers to the call interface. In today's parlance, it's more like the API. How to programmatically open connections, start transactions and cursors, fetch results, that sort of thing.
Sorry, you lost me. Have a great day!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.