» »

MariaDB - IF / ELSE IF / ELSE - kakšna je pravilna sintaksa?

MariaDB - IF / ELSE IF / ELSE - kakšna je pravilna sintaksa?

HotBurek ::

Evo, imam super preprost problem.

Baza je: MariaDB 10.3.15
GUI je: DBeaver 6.1.5

Probam pa naredit IF/ELSEIF/, ki bo glede na input prebral podatke iz različne tabele. In je problem, ker je nekaj narobe s sintakso in ne najdem kaj.

Se pravi, kakšna je pravilna sintaksa?

Tole je pa primer, ki ga poženem in dobim errorje za vsak IF, ELSE IF in END IF.

USE db1;

SET @val1 = 0;

IF @val1 = 0 THEN

	SELECT * FROM db1.table1;

ELSE IF @val1 = 1 THEN

	SELECT * FROM db1.table2;

END IF;
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

videc ::

IF condition1 THEN
{...statements to execute when condition1 is TRUE...}

[ ELSEIF condition2 THEN
{...statements to execute when condition1 is FALSE and condition2 is TRUE...} ]

[ ELSE
{...statements to execute when both condition1 and condition2 are FALSE...} ]

END IF;

https://www.techonthenet.com/mariadb/lo...

OracleDev ::

Prva stvar je verjetno ELSEIF mora biit napisan skupaj, drugo pa verjetno napaka tudi drugje v kodi (deklariranje spremenljivk, select ki ne ve kam dati rezultate,...).

HotBurek ::

Ok, popravljen v ELSEIF. Pa so isti trije errorji.

Za prvi IF, za vsak ELSEIF, ter na koncu še END IF.

In to je celotna koda, ker si prirapvljam kveri, da bom naredil pol proceduro...


SQL Error [1064] [42000]: (conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
(conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
(conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
Query is: IF @val1 = 0 THEN

SELECT 1
java thread: DBeaver: SQL script execution
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
Query is: IF @val1 = 0 THEN

SELECT 1
java thread: DBeaver: SQL script execution


SQL Error [1064] [42000]: (conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ELSEIF @val1 = 1 THEN
(conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ELSEIF @val1 = 1 THEN
(conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ELSEIF @val1 = 1 THEN
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ELSEIF @val1 = 1 THEN

SELECT 1' at line 1
Query is: ELSEIF @val1 = 1 THEN

SELECT 1
java thread: DBeaver: SQL script execution
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ELSEIF @val1 = 1 THEN

SELECT 1' at line 1
Query is: ELSEIF @val1 = 1 THEN

SELECT 1
java thread: DBeaver: SQL script execution


SQL Error [1064] [42000]: (conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF' at line 1
(conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF' at line 1
(conn=7771) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF' at line 1
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF' at line 1
Query is: END IF
java thread: DBeaver: SQL script execution
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF' at line 1
Query is: END IF
java thread: DBeaver: SQL script execution
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

HotBurek ::

Problem rešen, potrebno je bilo dodati DELIMITER. Neki zarad podpičji.

https://mariadb.com/kb/en/library/delim...

Sample je sedaj takle (ki bo pol imel še ELSEIF in tabele ad01, ad02 , ..., ad10, ad11 itn...):
USE db1;

DELIMITER //

CREATE DEFINER=`root`@`localhost` PROCEDURE `db1`.`spCheckIfAdExists`(IN catval INT, IN subcatval INT, IN adid INT)
BEGIN
	
	IF catval = 0 THEN
	
		IF subcatval = 0 THEN
		
			#SELECT cat.id, subcat.id, ad00.id FROM db1.Category AS cat
			SELECT COUNT(*) AS 'count', IFNULL(usr.id, -1) AS 'userId'
			FROM db1.Category AS cat
			INNER JOIN db1.Subcategory AS subcat
			ON cat.id = subcat.categoryId
			INNER JOIN db1.Ad00 AS ad00
			ON subcat.id = ad00.subcategoryId
			INNER JOIN db1.Users AS usr
			ON ad00.userId = usr.id
			WHERE cat.categoryValue = catval AND subcat.subcategoryValue = subcatval AND ad00.id = adid;
		
		END IF;
	
	END IF;

END;

DELIMITER ;
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

Zgodovina sprememb…

  • spremenilo: HotBurek ()


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[PHP]Zajem podatkov iz baze

Oddelek: Programiranje
353792 (3429) cobrica
»

[MySql]Sintaksa poizvedbe

Oddelek: Programiranje
61476 (1397) KernelPanic
»

php, napake in napake :S

Oddelek: Izdelava spletišč
171472 (1229) preem
»

SQL Error

Oddelek: Izdelava spletišč
141183 (1044) Lukas991
»

spet mysql - problemi z ... who knows what

Oddelek: Izdelava spletišč
5748 (724) pivmik

Več podobnih tem