Mariadb syntax error on Declare statement - mariadb

I am creating a MariaDB stored procedure in phpmyAdmin and I keep getting a syntax error.
I have tried appending an # symbol for the variable but I am still getting the error.
Here is what my code currently looks like:
DECLARE #cNAME = VARCHAR(100);
SET #cNAME = "TEST";
SELECT * from tblUser where UserName = cNAME;
I expect that my stored procedure would get saved but it doesn't because of the syntax error.

Related

MySql mariaDB CREATE FUNCTION DELIMITER doesnt work. has error near ''

im going insane. i dont know whats wrong with this code i see nothing wrong in it. but it kept reading thee same error each time i put the $$ after the "END". please help guys...
here is my code
DELIMITER $$
CREATE FUNCTION fungsi1 (a int)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE nilaiasal INT;
DECLARE teks VARCHAR(50);
SET nilaiasal = a * a;
IF nilaiasal > 100 THEN
SET teks = 'LEBIH DARI SERATUS';
ELSEIF nilaiasal < 100 THEN
SET teks = 'KURANG DARI SERATUS';
INSERT INTO table1 (dataone,datatwo) VALUES(a,teks);
SELECT * FROM table1;
RETURN (nilaiasal);
END $$
DELIMITER ;
And the error is this
ERROR 1064 (42000): 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 15
Please help me. I've looked up create functions error and there hasnt been anyone that has a problem near ''.
You might want to consider using a stored procedue instead of a function if you want to return a result set.
You are missing END IF; as Ergest Basha suggested and you are trying to return a result set from a function which is not allowed.

Select a variable to output from within stored procedure in Teradata

I have a stored procedure I've started coding and need to return a value. In SQL Server I could just do a SELECT of the variable to return it. However this does not seem to work with Teradata and have not found a similar example on how to do this. Here is my stored procedure:
REPLACE PROCEDURE sp_Get_MyValue()
BEGIN
DECLARE mytestvar VARCHAR(40);
SELECT mycolumn INTO mytestvar FROM MyTable;
SELECT mytestvar;
END;
I get this error:
STATEMENT 2: REPLACE failed. Failed [5526 : HY000] Stored Procedure
is not created/replaced due to error(s).{Nested Failure Msg [5526 :
HY000] SPL1045:E(L10), Invalid or missing INTO clause.}
I also tried adding an OUT variable to the procedure but that did not work either:
REPLACE PROCEDURE sp_Get_MyValue(mytestvarout VARCHAR(40))
BEGIN
DECLARE mytestvar VARCHAR(40);
SELECT mycolumn INTO mytestvar FROM MyTable;
END;
With this error:
Executed as Single statement. Failed [5531 : HY000] Named-list is not
supported for arguments of a procedure. Elapsed time = 00:00:00.079
To return a single row you must define and OUT-variable and assign a value to it:
REPLACE PROCEDURE sp_Get_MyValue(OUT mytestvarout VARCHAR(40))
BEGIN
DECLARE mytestvar VARCHAR(40);
SELECT mycolumn INTO mytestvar FROM MyTable;
SET mytestvarout = mytestvar;
END;
To return a result set you need to define a kind of dummy-cursor (blame Standard SQL :-)
Returning Result Sets from a Stored Procedure

Sqlite DB Error - could not prepare statement

I am getting following error on insert statement for sqlite DB
could not prepare statement (1 near "undefined": syntax error)
I tried 2 variations of insert, for both error is same
var sql = "INSERT INTO Med(MedID) VALUES(?),";
sql += "['"+dataObj[i].MedID+"']";
var sql = "INSERT INTO Med(MedID) VALUES ('"+dataObj[i].MedID+"')";
tx.executeSql(sql);
The correct way to give parameters to an SQL statement is as follows:
var sql = "INSERT INTO Med(MedID) VALUES (?)";
tx.executeSql(sql, [dataObj[i].MedID]);
It looks like you are missing the space that is needed between the table name and the column names.
Try this:
var sql = "INSERT INTO Med (MedID) VALUES ('"+dataObj[i].MedID+"')";
tx.executeSql(sql);
Make sure your dataObj[i].MedID is also defined. Add a console.log(sql) before your executeSql statement to check the command before using it.

ASP GridView not bind to select result

I got stored function which returns query. In ASP project I got GridView which I bind to SqlDataSource element (it named SqlDataProjectWells).
So, when I try to call it there are error appears which says something like
"error while trying to execute query"
But in pgAdmin select command works perfectly
Code calls on page load
void bindToTable(){
SqlDataProjectWells.SelectCommand = "SELECT get_zonetable()";
SqlDataProjectWells.Select(DataSourceSelectArguments.Empty);
myGridView.DataBind();
}
Stored procedure code
CREATE OR REPLACE FUNCTION get_ZoneTable()
RETURNS SETOF RECORD
AS
$$
BEGIN
return QUERY SELECT "WELLS".well_name, "ZONES".id_zones, "ZONES".top, "ZONES".botom FROM "WELLS" LEFT JOIN "ZONES" ON "WELLS".well_id = "ZONES".id_well;
/*RETURN;*/
--return result_record;
END
$$ LANGUAGE plpgsql;
Whats wrong?!
UPD:
If I use stored procedure - there are such error appears
ERROR [0A000] Error while executing the query
If I use select like this
SqlDataProjectWells.SelectCommand = "SELECT \"WELLS\".well_name, \"ZONES\".id_zones, \"ZONES\".top, \"ZONES\".botom FROM \"WELLS\" LEFT JOIN \"ZONES\" ON \"WELLS\".well_id = \"ZONES\".id_well;"
It caused error which says
A field or property with the name 'id_well' was not found on the
selected data source
Shouldn't you be calling DataBind on the GridView instead of the SqlDataSource?
Well, kinda solved problem this way. Stored procedure creates all_zones view which I use aftel in ASP code like SqlDataProjectWells.SelectCommand = "SELECT * FROM all_zones";
Stored procedure text
CREATE OR REPLACE FUNCTION get_zones_view()
RETURNS void AS
$BODY$
DECLARE
BEGIN
execute 'CREATE OR REPLACE VIEW "all_zones" AS SELECT "WELLS".well_name, "ZONES".id_zones, "ZONES".top, "ZONES".botom FROM "ZONES" JOIN "WELLS" ON "WELLS".well_id = "ZONES".id_well';
END;
$BODY$
LANGUAGE plpgsql
but I assume that it's not okay to do like that

Invalid table name error while using Execute Immediate statement with bind variables

I'm trying to get this dynamic SQL running ( using EXECUTE IMMEDIATE)
M_SQL_STATEMENT := 'SELECT MAX(:m_var1)+1 from :m_var2 RETURNING MAX(:m_var1)+1 INTO :m_var3';
EXECUTE IMMEDIATE M_SQL_STATEMENT
USING M_COLUMN_NAME, UPPER(P_TABLE_NAME), M_COLUMN_NAME
RETURNING INTO M_SEQ_NUMBER;
However, when trying to run this, I keep running into
ORA-00903: Invalid table
P_TABLE_NAME is a table name which is accepted as an input. I have confirmed that the table name & the column name are valid. I can't figure out why Oracle is throwing the error.
FWIW Altering the SQL statement to
M_SQL_STATEMENT := 'SELECT MAX(:m_var1)+1 SEQ from :m_var2 RETURNING SEQ INTO :m_var3';
still results in the same error.
You need to put the table name and column name into the dynamic SQL, so something like
M_SQL_STATEMENT := 'SELECT MAX(' || M_COLUMN_NAME || ')+1 from '
|| P_TABLE_NAME';
EXECUTE IMMEDIATE M_SQL_STATEMENT INTO M_SEQ_NUMBER;

Resources