What is causing the syntax error in my sql stored procedure? - mariadb

I'm pretty new at stored procedures and I get the following syntax error in my code:
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 'DELIMITER //CREATE PROCEDURE sp_create_probe(IN matrix_id INT, IN oligo_id IN...' at line 2
Here's the code for my procedure:
DELIMITER //
CREATE PROCEDURE sp_create_probe(IN matrix_id INT, IN oligo_id INT)
BEGIN
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO Probes (oligo, microarray) VALUES (oligo_id, matrix_id);
SET FOREIGN_KEY_CHECKS=1;
END //
DELIMITER ;
Hope someone can help me out
EDIT:
Well I ended up fixing it. Classic noob mistake.
I had this line before the code you see above:
DROP PROCEDURE IF EXISTS sp_create_probe
Didn't copy that line into the post for some reason but it's missing the ";" at the end. That fixed it.

Related

SQR - how to use FROM [dynamic table name} within BEGIN-SELECT?

I have to create an SQR that generates a list of EEIDs, if there were any changes to the Pension data in the past day. The SQR compiles and works perfectly when I hardcode in the table names.
However, when I tried using variables for the table names, I get a compile error
I've pasted the portion of SQR that I'm trying to fix
When I start using $tableName and $auditTableName as table variables, that's when I get the error and I'm not sure what is going wrong
Can anyone help?
Please and Thank You
!***************************
begin-procedure Process-Main
!***************************
let $tableName = 'PS_PENSION_PLAN'
let $auditTableName = 'PS_AUDIT_PENSION_PLN'
let $dummy-dyn-variable = ''
begin-SELECT DISTINCT
L.EMPLID
L.EMPL_RCD
do someProcName(&L.EMPLID, &L.EMPL_RCD)
FROM [$dummy-dyn-variable]
(
SELECT DISTINCT
PP.EMPLID,
PP.EMPL_RCD,
PP.EFFDT,
'1901-01-01 12:00:00' AS AUDIT_STAMP
FROM [$dummy-dyn-variable] [$tableName] PP
UNION
SELECT DISTINCT
A.EMPLID,
A.EMPL_RCD,
A.EFFDT,
A.AUDIT_STAMP
FROM [$dummy-dyn-variable] [$auditTableName] A
)L
WHERE DATEDIFF(DAY,CAST(L.AUDIT_STAMP AS DATE),SYSDATE) = 1
ORDER BY 1,2
end-SELECT
end-procedure
Edit:
does the UNION have anything to do with this?
I keep receiving is this error:
(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 102 in cursor 1:
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'FROM'.
(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 8180 in cursor 1:
[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.
Edit2:
Ok, initial problem solved with [$dummy-dyn-variable], which led to the next problem with the DO command. I've updated the code above with DO someProcName(param_a, param_b)
I am now getting an error saying:
(SQR 2002) DO arguments do not match procedure's
Weird part, if I remove the dynamic table variables and hardcode the table names in the FROM section, then it compiles properly without errors. This makes me believe that the error is not related to my someProcName (maybe?)
am I missing something here?

Create a stored procedure using RMySQL

Background: I am developing a rscript that pulls data from a mysql database, performs a logistic regression and then inserts the predictions back into the database. I want the entire system to be self contained in the script in case of database failure. This includes all mysql stored procedures that the script depends on to aggregate the data on the backend since these would be deleted in such a database failure.
Question: I'm having trouble creating a stored procedure from an R script. I am running the following:
mySQLDriver <- dbDriver("MySQL")
connect <- dbConnect(mySQLDriver, group = connection)
query <-
"
DROP PROCEDURE IF EXISTS Test.Tester;
DELIMITER //
CREATE PROCEDURE Test.Tester()
BEGIN
/***DO DATA AGGREGATION***/
END //
DELIMITER ;
"
sendQuery <- dbSendQuery(connect, query)
dbClearResult(dbListResults(connect)[[1]])
dbDisconnect(connect)
I however get the following error that seems to involve the DELIMITER change.
Error in .local(conn, statement, ...) :
could not run statement: 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 'DELIMITER //
CREATE PROCEDURE Test.Tester()
BEGIN
/***DO DATA AGGREGATION***/
EN' at line 2
What I've Done: I have spent quite a bit of time searching for the answer, but have come up with nothing. What am I missing?
Just wanted to follow up on this string of comments. Thank you for your thoughts on this issue. I have a couple Python scripts that need to have this functionality and I began researching the same topic for Python. I found this question that indicates the answer. The question states:
"The DELIMITER command is a MySQL shell client builtin, and it's recognized only by that program (and MySQL Query Browser). It's not necessary to use DELIMITER if you execute SQL statements directly through an API.
The purpose of DELIMITER is to help you avoid ambiguity about the termination of the CREATE FUNCTION statement, when the statement itself can contain semicolon characters. This is important in the shell client, where by default a semicolon terminates an SQL statement. You need to set the statement terminator to some other character in order to submit the body of a function (or trigger or procedure)."
Hence the following code will run in R:
mySQLDriver <- dbDriver("MySQL")
connect <- dbConnect(mySQLDriver, group = connection)
query <-
"
CREATE PROCEDURE Test.Tester()
BEGIN
/***DO DATA AGGREGATION***/
END
"
sendQuery <- dbSendQuery(connect, query)
dbClearResult(dbListResults(connect)[[1]])
dbDisconnect(connect)

Haskell Sqlite 3 triggers - Syntax error

I've written a simple trigger using HDBC and Sqlite3.
calculateNoOfStocksTraded::Database.HDBC.Sqlite3.Connection->IO Integer
calculateNoOfStocksTraded conn=do
run conn "CREATE TRIGGER calStocks\
\AFTER INSERT ON historicalData\
\FOR EACH ROW\
\BEGIN\
\UPDATE company\
\SET noOfStocks=300.0;\
\END " []
It keeps saying that there's a syntax error. I don't know how to figure it out. pls help me to locate the error
The pieces of your SQL strings are being run together without spaces between them (because that's how the string breaks with \ work in Haskell). Try adding a space at the end of each line of the query, before the \.

Im getting an Warning: Procedure created with compilation errors

I'm beginner of pl/sql and I'm learning.
This is my table.
select * from s;
SLNO ENAME SAL
------ -------------------- ----------
1 Shiv 8000
2 Pankaj 9000
3 Prasath 10000
This is my procedure:
set serveroutput on
create or replace procedure p1(n number)
is wname varchar(20);
begin
select ename into wname from s where eid=n;
dbms_output.put_line('------');
dbms_output.put_line('employee name:='||wname);
dbms_output.put_line('------');
end;
I'm getting a warning:
Procedure created with compilation errors.
If I execute the above query. Can anyone please suggest where I'm going wrong please..
You can use the following command.
show errors procedure procedure_name;
to get the list of errors and check that.
There is no column named EID on your table S.
put parameter type IN.
Give procedure like create or replace procedure p1(n IN s.eid%TYPE)
It will work.
I think there's a typo in your program,
try
SET SETVEROUTPUT ON
all in capitals.

ERROR: The symbol "(" was substituted for "VARCHAR2" to continue

I am trying to compile this package body:
CREATE OR REPLACE PACKAGE BODY package_1 AS
PROCEDURE procedure_1 (P_HOST IN VARCHAR2, P_USER IN VARCHAR2, P_NAME IN VARCHAR2)
IS
BEGIN
SELECT HOSTNAME, USERS, PS_NAME
INTO P_HOST, P_USER, P_NAME
FROM PS_COLLECT
WHERE NOT EXISTS
(
SELECT HOSTNAME, USERS, PS_NAME
FROM PS_MASTER
WHERE PS_MASTER.HOSTNAME = PS_COLLECT.HOSTNAME
AND PS_MASTER.USERS = PS_COLLECT.USERS
AND PS_MASTER.PS_NAME = PS_COLLECT.PS_NAME
);
END procedure_1;
END package_1;
But I am getting this error
The symbol "(" was substituted for "VARCHAR2" to continue.
I am a newbie int PL/SQL..please help :)
I wouldn't expect this to cause the exact error you're seeing, but one definite problem with your code is that you are specifying IN mode for the parameters, but then trying to assign values to them inside the procedure. It looks like you should change the mode to OUT for all three parameters (in both the header and the body).
If that alone does not resolve your issue, I suggest posting the modified text of both the CREATE PACKAGE and CREATE PACKAGE BODY statements.

Resources