Make SQLite abort on first error (and sing songs) - sqlite

I wanted to name this post Make SQLite abort on first error but StackOverflow's AI overlords decided it doesn't fit their conception of intelligent human behavior. For the record, I was googling exactly that, but perhaps even Google AI considered my question unworthy and didn't bother to help me. Mods, feel free to change the title according to what your AI bosses desire (if you can figure it out).
I have this script
create if not exists table entries (
id integer primary key,
start datetime not null,
end datetime not null
);
delete from entries;
insert into entries values (1, '2018-08-01 10:00', '2018-08-01 15:00');
insert into entries values (2, '2018-08-01 17:00', '2018-08-01 20:00');
insert into entries values (1, '2018-08-02 19:00', '2018-08-02 00:00');
insert into entries values (1, '2018-08-03 00:00', '2018-08-03 04:00');
insert into entries values (1, '2018-08-03 14:00', '2018-08-03 18:00');
There is a mistake in create statement. When I run the script I get
% sqlite3 db.sqlite3 <ddl.sql
Error: near line 1: near "if": syntax error
Error: near line 7: no such table: entries
Error: near line 8: no such table: entries
Error: near line 9: no such table: entries
Error: near line 10: no such table: entries
Error: near line 11: no such table: entries
Error: near line 12: no such table: entries
How do I make SQLite exit executing the script on first error it encounters? I'm looking for equivalent of set -e in Bash.

From the documentation, it looks like you can turn on the dot command .bail.
.bail on|off Stop after hitting an error. Default OFF
See also - O'Reilly Using Sqlite
Edit
To exit, you can use the .exit dot command.

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?

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

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.

Create table in multiple databases with flyway

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.

Keep getting an error and do not understand why

sales_heads will compile with no errors but when I try to compile sales_lines it comes up with 2 errors which are:
Error(3,1): PL/SQL: SQL Statement ignored
Error(3,111): PL/SQL: ORA-02289: sequence does not exist
could someone tell me where I am going wrong.
drop sequence nsale_seq;
CREATE SEQUENCE nsale_seq
START WITH 1000000000
INCREMENT BY 1
NOCACHE
NOCYCLE;
create or replace PROCEDURE sale_heads (staffID_new number, customerID_new number)
is begin
insert into SALE_HEAD (sale_num, sale_date, status, staff_id, customer_id) values (nsale_seq.NEXTVAL, sysdate, 'P', staffID_new, customerID_new);
end sale_heads;
/
create or replace PROCEDURE sales_lines (productCode_new number, quantity_new number, actualPrice_new number) is
begin
insert into SALE_LINE (actual_price, quantity, sale_num, product_code) values (actualPrice_new, quantity_new, nsale_seg.CURRVAL, productCode_new);
end sales_lines;
/
You have placed / this after your first procedure as well as after second procedure. / is used to show the end of the file or the statements to be executed!Hence, it's unable to find second procedure!!!
That's why your second procedure is getting ignored.
Please remove the / first slash after first procedure sale_heads.
I believe it'll work perfectly!

Execute Immediate with multiple SQL Statements

Here is my PL/SQL.
execute immediate '
create table Alex_Test2(col1 varchar2(100));
/
drop table Alex_Test2;
/
';
In the dynamic SQL, a table is created and then dropped immediately. The funny thing is that the table is successfully created and dropped but I got error.
Error starting at line 1 in command:
execute immediate '
Error report:
ORA-06550: line 1, column 17:
PLS-00103: Encountered the symbol "; END;" when expecting one of the following:
:= . ( # % ;
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
table ALEX_TEST2 created.
table ALEX_TEST2 dropped.
Error starting at line 6 in command:
'
Error report:
Unknown Command
The SQL statements actually typed by user. I cannot avoid the symbols like ; and /. How can I fixed this problem? Or how can I bypass this error?
However, I still need to keep the log. I expect the log is just
table ALEX_TEST2 created.
table ALEX_TEST2 dropped.
Thanks!
You are unable to manipulate the query string before executing it? If you don't have the ability to do that then what do you have the ability to do?
Sounds like you just need to do a find and replace on the query string before running it.

Resources