CREATE FUNCTION throws SQL Error (1064) (42000) - mariadb

I'm trying to create a stored function in a MariaDB database.
I copied the function I'm trying to create from the MariaDB Docs:
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT;
SET x = 42;
RETURN x;
END
//
DELIMITER ;
Unfortunately, I get the following error:
SQL Error [1064] [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 '' at line 3
What baffles me most is that the given code is supposed to resolve the very error code I'm getting according to the MariaDB docs
The solution is to specify a distinct delimiter for the duration of the process, using the DELIMITER command

It turned out, the client I used to issue the command, DBeaver, was causing the trouble.
After switching over to MySqlWorkbench everything worked as expected.
Apparently, DBeaver didn't recognise the delimiters correctly..

I think you may have forgotten to select the database you want this routine to be stored into.
So try adding a use as the first line
use `test`;
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT;
SET x = 42;
RETURN x;
END
//
DELIMITER ;

Related

Send multiple parameters to SQL Server using dbBind in R

I am working on a program that has to send data to Microsoft SQL Server through R. Since SQL Server doesn't have any INSERT OR IGNORE INTO like PostgreSQL, I have to check if the value is already in the table. However, I don't know if I'm doing it right. How do I send multiple parameters (or the same parameter twice) into SQL Server using dbBind?
Here is my code:
statement <- "IF NOT EXISTS (SELECT * FROM dbo.nodes WHERE node_id=?) INSERT INTO dbo.nodes (node_id) VALUES (?)"
insertnew <- dbSendQuery(conn, statement)
test_p1 <- list(5, 6)
test_p2 <- list(test_p1, test_p1)
dbBind(insertnew, params=test_p2)
When I run it it gives me the following error:
Error in result_bind(res#ptr, params, batch_rows) :
RAW() can only be applied to a 'raw', not a 'double'
Using RStudio's Show Traceback feature, it shows this:
result_bind(res#ptr, params, batch_rows)
.local(res, params, ...)
dbBind(insertnew, params = test_p1)
Does anybody know what I'm doing wrong here? If the parameters should be something other than a list of lists, what should they be? Is there a way to name parameters to use twice in the same statement? What is RAW() and what is it trying to apply it to?
dbo.nodes is a table with one column, node_id NVARCHAR(1000) NOT NULL PRIMARY KEY.
conn is a DBIConnection object created earlier using dbConnect(odbc::odbc(), ...). It works for executing other statements so there shouldn't be a problem there.
Adding the argument batch_rows=1 to dbBind doesn't change anything (not that I understand what that is supposed to do).
Thanks in advance for the help!
test_p1 = c(5, 6)
test_p2 = unname(data.frame(I(test_p1), I(test_p1)))
In case this helps someone looking this up later, changing it from a list of lists to a data frame made from vectors worked.

procedure created with compilation error

CREATE OR REPLACE PROCEDURE ex9a(n NUMBER ,c CHAR) IS
pi NUMBER(7,4):=3.14;
v_record Areas%rowtype;
BEGIN
IF c='R' THEN
DBMS_OUTPUT.PUT_LINE('CHOICE : R');
v_record.Input_Value:= n;
v_record.Circle_Area:=pi*n*n;
v_record.Square_Area:=null;
v_record.Sphere_Area:=2*pi*n;
v_record.Sphere_Volume:=(3/4)*r*r;
v_record.Cube_Volume:=null;
END IF;``
END;
/
I am getting procedure created with compilation errors.I want to compute area of a circle and insert it into the table .
When I give SHOW ERRORS , it lists the errors as
1) plsql statement
2) 'R' must be declared .
(Even after I gave 'then'. I forgot the line number)
It would help if you would list the errors, and the lines on which they occur.
However, one obvious issue is that your IF statement has no THEN. It should be:
IF c = 'R' THEN

PLS-00103 error when creating object type

I am trying to create and assign variables using following code to create object types in plsql (11g) but facing some errors:
begin
execute immediate 'drop type picu_obj force';
execute immediate 'drop type picu_obj_tab force';
execute immediate 'create type picu_obj as object(Customer_ID varchar2(32767),Customer_Name varchar2(32767),Server_Name varchar2(32767),Time_stamp varchar2(32767))';
execute immediate 'create type picu_obj_tab is table of picu_obj;';
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
end;
The above code gives following errors:
ERROR at line 6:
ORA-06550: line 6, column 10:
PLS-00103: Encountered the symbol "PICU_OBJ_TAB" when expecting one of the
following:
:= . ( # % ;
The symbol ":=" was substituted for "PICU_OBJ_TAB" to continue.
Please suggest what I am doing wrong here.
There are two problems with this code:
First: In Oracle 11g you can not use varchar2(32767) the maximum length is 4000 for a varchar there. So even if the code did run, it wouldn't create the types.
Secondly: the PL/SQL code is validated/compiled when you run it. But as you use dynamic SQL to create the types, the PL/SQL compiler can't see those types when it tries to compile the lines:
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
and that's the error you are seeing.
You have to create the types before you run PL/SQL code that uses them.

PLS-00103: Encountered the symbol

i am trying to compile this function
CREATE OR REPLACE FUNCTION sql_error_msg (err_num PLS_INTEGER)
RETURN VARCHAR2 IS
BEGIN
RETURN sqlerrm(-err_num);
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END sql_error_msg
/
but i am getting this error:
FUNCTION SQL_ERROR_MSG
On line: 8
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
;
The symbol ";" was substituted for "end-of-file" to continue.
Just add ; at the end of the function.
END sql_error_msg;
/
I was trying to execute PL/SQL that "creates or replaces" a function, using <jdbc:initialize-database> tag. I was given the PLS-00103 and it turned out PL/SQL is not currently supported this way.
You have to execute the script in a different way.
I am going to try directly with JDBC as per http://www.coderanch.com/t/298532/JDBC/databases/run-SQL-Script-file-JDBC
and work out a solutiom from there.

PL/SQL script giving errors

I've to work on some older EDW scripts, which I think are in PL/SQL and queries fech data from Oracle table as well. But there is some problem with them, the part which declares variables, as shown in the image gives error. I'm unable to understand why?
Below is some part of script,
VARIABLE begin_exp_date varchar2(8)
VARIABLE end_exp_date varchar2(8)
VARIABLE begin_cal_key number
Declare
begin
:begin_exp_date := 'begin_exp_date';
:end_exp_date := 'end_exp_date';
:begin_cal_key := 'begin_cal_key';
end;
These lines produce error ORA-00900: Invalid SQL statement.
Any help?
If you plug the script in SQL*Plus, it will be executed without the ORA-00900 error. I guess you received the error when it was run in Toad.
If it is indeed PL/SQL, it should be more like this
DECLARE
begin_exp_date varchar2(8);
end_exp_date varchar2(8);
begin_cal_key number;
BEGIN
begin_exp_date := 'begin_exp_date';
-- and so on
END;
You can set constant values to the variables in the DECLARE section if you want. Note that you've defined begin_cal_key as a NUMBER so cannot assign the string 'begin_cal_key'

Resources