I am running a code in a plsql package where I am using bind variable like this -
sql:= 'insert into test_table select * from '||dynamic_table||' where id = :bind1 and nvl(col1,:X)=:X';
execute immediate sql using vid,'Y','Y';
code is all compiled and running but whenever I am compiling and checking the body of my package the execute immediate get automaticlly replaced by some random letter 'u' like below-
execute immediate sql using vid,'u','Y';
Why is this happening, there is no compilation issue but correct value is not getting inserted because of this random letter u.
Related
I’m trying to make the move from SQL Server to Mariadb and I’m running into a lot of little quirks that drive me crazy at times. Chief among them are the apparent differences between executing a script from a file and pasting it into a console window. I was used to using tabs for formatting code in MSSQL, so I’m still trying to get used to the tab behavior in the Mariadb console, but at least that makes sense. The difference between var and #var is odd to me as well. The code below runs fine when execute through Pycharm, but fails when pasted into a console window. I’m running Mariadb (##version: 10.5.18-MariaDB-0+deb11u1) on a Pi Raspberry accessing it through SSH from a Windows 11 box.
All the script is doing is populating a table with random combinations of last names and male/female first names from U.S. census data for testing. Any help with the error would be greatly appreciated. I really want to understand why the difference between pasting and executing the file.
SET #loops := 10;
WHILE #loops > 0 DO
INSERT person(last_name, first_name, iso_country)
VALUES(census.random_name(0), census.random_name(FLOOR(1 + RAND() * (2 - 1 +1))), 'US');
SET #loops := #loops - 1;
END WHILE;
SELECT * FROM person;
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 'END WHILE' at line 1
I executed the file from the Pycharm IDE where it runs fine, and pasted the same code into the Mariadb console where the error was raised, and the insertions did not occur.
The mariadb command line client has it's own small parser, by default a semicolon is interpreted as end of statement.
With default delimiter ; your statement is splitted after first semicolon into WHILE #loops > 0 DO INSERT person(last_name, first_name, iso_country) VALUES(census.random_name(0), census.random_name(FLOOR(1 + RAND() * (2 - 1 +1))), 'US'); SET #loops := #loops - 1; and END WHILE. When sending both statements to the server, server will return 2 errors, but only last error is displayed.
Instead you should use a different delimiter:
DELIMITER $$
WHILE #loops > 0 DO
INSERT person(last_name, first_name, iso_country)
VALUES(census.random_name(0), census.random_name(FLOOR(1 + RAND() * (2 - 1 +1))), 'US');
SET #loops := #loops - 1;
END WHILE;$$
See also Delimiters
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.
I'm trying to connect to the multiple databases and create tables, but when migrating flyway gets syntax error.
This is the migration file I'm trying to run:
\c testdatabase;
CREATE TABLE testtable1;
\c testdatabase2;
CREATE TABLE testtable2;
Flyway gives this output:
Error Code : 0
Message : ERROR: syntax error at or near "\"
Position: 1
Line : 1
Statement : \c testdatabase
It seems like flyway does not support meta-commands like "\c" for connecting to the database. Is there any other way to do connect to the databases and create a table?
The error comes (as indicated in the error input) from the comment lines preceding your two SQL statements in the script: \c testdatabase; which are not valid SQL syntax for comments.
You could simply correct those faulty lines like the following: -- testdatabase, and generally, the error input already gives you a hint as to where lies the problem.
I have some tables and I want to insert into them by asking their name, then putting in the values for the columns. Thing is, Whenever I run this, it goes through all the inputs no matter what, even if I input an incorrect tables. Then I get an error that it expected = symbol instead of :=.
The code:
set serveroutput on;
declare
myTable varchar2;
begin
myTable = &input_table;
if myTable = 'Supervisor' then
insert into Supervisor values(&supID, &supName);
elsif myTable = 'Job' then
insert into Job values(&jobID, &jobName);
else dbms_output.put_line('Found no such table.');
end if;
end;
/
PL/SQL scripts (running in SQLPlus or SQLPlus emulators) are not interactive tools. When you run the script, Oracle first parses its text, then defines all &-variables, then ask you to fill them, and only then begins execution. Use any interactive tools instead (in fact, for your own task you have to write your own tool yourself).
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)