I want to create a function that will return an integer variable (or boolean) after check if there are any row in table RESERVATION have an RESERVATION_ID that equal to the reser_id I give to it. But my code seem wrong at the IF statement.
This is my query code:
CREATE OR REPLACE Function fcn_check (reser_id integer) RETURNS integer AS $$
BEGIN
IF (EXISTS (SELECT * FROM TICKET_TICKET WHERE reservation_id = reser_id) )
THEN SELECT 1 AS result;
ELSE SELECT 0 AS result;
END IF;
END
$$ LANGUAGE SQL;
The error messages is:
ERROR: syntax error at or near "IF"
LINE 2: IF (SELECT * FROM TICKET_TICKET WHERE reservation_id = rese...
^
********** Error **********
ERROR: syntax error at or near "IF"
SQL state: 42601
Character: 76
Im a newbie at pgsql and look like I didnt try hard enough to slove this, but please help me.
Many Thanks!
$$ LANGUAGE SQL;
You seem to be writing PL/PgSQL, but you've declared it as SQL.
Use LANGUAGE plpgsql.
Related
As part of an exercise, I am trying to write a MySQL function that accepts a table name as an argument and returns the count of records from that table. My function looks like -
DELIMITER //
create function recordCount(tname text)
returns int deterministic
begin
DECLARE rc int;
select count(*)
into rc
from tname;
return rc;
end;
//DELIMITER
However, when I call this function like -
select recordCount('avengers');
I get the following error:
Error Code: 1146. Table 'db.tname' doesn't exist 0.000 sec
I don't understand why the function isn't looking for the table 'avengers'. I am unable to figure out what I am doing wrong, please help.
I'm using Mysql and I'm trying to make a function that is able to return me all of the lines of a table if the id is ok.
that's my code :
CREATE OR REPLACE FUNCTION lesPieces(varchar)
RETURNS TABLE my_table
DECLARE
ligne piece%ROWTYPE;
BEGIN
FOR ligne IN
SELECT *
FROM piece;
LOOP
RETURN NEXT ligne;
END LOOP;
RETURN
END;
$$ LANGUAGE plpgsql;
But Mysql returns me :
You have an error in your SQL syntax near :
'FUNCTION lesPieces(varchar)
RETURNS TABLE my_table
DECLARE
ligne piece%ROWTY' at line 1
Have you got an idea ?
------------------------------------------------------------------------------------------------------------------------
EDIT :
Thanks to #Slasko for this help :
You sure REPLACE is command ? should it be something like DROP FUNCTION IF EXISTS [function_name]; DELIMITER $$ CREATE FUNCTION [function_name(function_parameter)] ?
So I just removed the "OR REPLACE" but now the error is :
You have an error in your SQL syntax near : 'varchar) RETURNS SETOF piece AS' at line 1
This create function has many issues. For e-g RETURNS can have possible values {STRING|INTEGER|REAL|DECIMAL} as per http://dev.mysql.com/doc/refman/5.7/en/create-function-udf.html
Please refer below link for examples
http://sql-info.de/mysql/examples/create-function-examples.html
I am trying to create a mysql stored procedure with phpmyadmin:
CREATE PROCEDURE AddTableColumn()
BEGIN
IF (SELECT COUNT (table_name)
FROM information_schema.tables
WHERE table_name IN ('authors', 'publishers') = 2 )
print 'specified tables exist...';
ELSE
print 'specified tables unavailable...';
END IF;
END
above code is a snippet that should search information schema
for the availability of authors and publishers tables,
then proceed to add new column in each table if present.
the print statement was for debug purpose. when i clicked the
GO command, here's the error message :
MySQL said:
1064 - 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 'print 'avail...'; ELSE print 'unavail...'; END IF; END' at line
7.
what am i doing wrong ? i have even tried other code as a test,
all failing with the same error. code as simple as:
BEGIN
IF (6 > 4)
print 'greater';
ELSE
print 'lesser';
END IF;
END
all failed. any help will be appreciated.
'print' is not recognized in MySQL.
If you want to read back the output, just use a select in the SP.
Change part of your SP as below:
DECLARE found_status VARCHAR(255) DEFAULT NULL;
IF ....
SELECT 'specified tables exist...' INTO found_status;
ELSE
SELECT 'specified tables unavailable...' INTO found_status;
END IF;
SELECT found_status;
I tried to create one function, ie if username exist, will return random number and character as a single string, but I tried below code, throwing syntax error like below, can u help to find the issue, I know that having issue in declaring string and returning string, but unable to find the issue. Thanks to the replies in advance
DELIMITER //
create function verifyEmail(userName varchar(25))
RETURNS TEXT
BEGIN
if EXISTS(select * from userdetails where name = userName)
then
SELECT #randomPass := select concat( char(round(rand()*36)+1), char(round(rand()*36)+1), char(round(rand()*36)+1));
return #randomPass;
else
return "not_exist";
end if;
end //
DELIMITER //
#1064 - 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 'select concat( char(round(rand()*36)+1), char(round(rand()*36)+1), char(round(ra' at line 6
You are missing a closing ) on this line:
SELECT #randomPass := select concat( char(round(rand()*36)+1), char(round(rand()*36)+1), char(round(rand()*36)+1);
should be
SELECT #randomPass := select concat( char(round(rand()*36)+1), char(round(rand()*36)+1), char(round(rand()*36)+1));
Hooray, finally found answers.. This below code will help to solve the solution
DELIMITER //
create function verifyEmail(userName varchar(25))
RETURNS TEXT
BEGIN
DECLARE randomPass VARCHAR(8);
if EXISTS(select id from userdetails where name = userName)
THEN
SET randomPass = concat( char(rand()*25+65),char(rand()*25+65),char(rand()*25+65),char(rand()*25+65),char(rand()*25+65),char(rand()*25+65),char(rand()*25+65),char(rand()*25+65));
return randomPass;
else
return "not_exist";
end if;
end //
DELIMITER //
I'm working with stored procedures in mysql, so I have the following procedure:
DELIMITER ##
DROP PROCEDURE IF EXISTS generarEstadisticoRD ##
CREATE PROCEDURE generarEstadisticoRD ( mesInicial INT,anualInicial INT, mesFinal INT,anualFinal INT, codigoEntidad CHAR(3),mes INT )
BEGIN
DECLARE controlador INT;
DECLARE tipoDocumento CHAR(2);
DECLARE cursorDocumentos CURSOR FOR SELECT DISTINCT e.claseDocIdentidadFallecido
FROM EstadisticoRD e WHERE e.anual>=anualInicial AND e.anual<=anualFinal
AND e.mes >=mesInicial AND e.mes<=mesFinal AND e.codOficina=codigoEntidad;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET controlador = 1;
DROP TEMPORARY TABLE IF EXISTS estadistico;
CREATE TEMPORARY TABLE IF NOT EXISTS
estadistico( TIPO CHAR(2), MES INT );
OPEN cursorDocumentos;
cursorLoop : LOOP
FETCH cursorDocumentos INTO tipoDocumento;
IF( controlador=1 ) THEN
LEAVE cursorLoop;
END IF
/**
*Lógica
*/
INSERT INTO estadistico(`TIPO`,`MES`)
SELECT DISTINCT
c.descripcion,
IFNULL( (SELECT e.numRegistrosReportados FROM estadisticoRD e WHERE e.codOficina=codigoEntidad
AND e.claseDocIdentidadFallecido=tipoDocumento AND e.mes=mes ), 0)
FROM estadisticoRD e, claseDoc c WHERE e.codOficina=codigoEntidad AND e.claseDocIdentidadFallecido=tipoDocumento
AND c.claseDoc = e.claseDocIdentidadFallecido;
END LOOP cursorLoop;
CLOSE cursorDocumentos;
SELECT * FROM estadistico;
END ##
DELIMITER ;
I get the following messages when I try to execute the procedure:
Executed successfully in 0,001 s, 0 rows affected.
Line 2, column 1
Error code 1064, SQL state 42000: 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 'INSERT INTO estadistico(`TIPO`,`MES`)
SELECT DISTINCT c.descripcion,
' at line 24
Line 3, column 1
So, what am I doing wrong?.
UPDATE 1:
The I corrected the mistake with semicolon thanks #Daniel Victoria
But now I get the following mistake:
Error code 1267, SQL state HY000: Illegal mix of collations (latin1_spanish_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '='
Exactly I get this mistake when I do
SELECT DISTINCT e.claseDocIdentidadFallecido
FROM EstadisticoRD e WHERE ... AND e.codOficina=codigoEntidad;
why when I do e.codOficina=codigoEntidad I get this mistake, how to fixed it?.
UPDATE 2:
To solve it, I need to put COLLATE latin1_swedish_ci after to the column that has the mistake.
In this case the new query is :
SELECT DISTINCT *
FROM estadisticoRD e WHERE e.anual>=anualInicial AND e.anual<=anualFinal
AND e.mes >=mesInicial AND e.mes<=mesFinal AND e.codOficina = codigoEntidad COLLATE latin1_swedish_ci;
I hope to finish this procedure the best way.
Your are missing a semicolon (;) after the "END IF"