The following code is showing LEAST() and GREATEST() function with both BIGINT UNSIGNED parameters.
Tested with MariaDB 10.3.12 and DBeaver 21.3.5
Is this bug of MariaDB?
Is there any solution?
Please help me.
-----------------------------------------------------------------------------------
Code to reproduce :
-----------------------------------------------------------------------------------
DELIMITER $$
BEGIN NOT ATOMIC
DECLARE ABC BIGINT UNSIGNED DEFAULT 18446744073709551615; -- BIGINT UNSIGNED MAX
DECLARE XYZ BIGINT UNSIGNED DEFAULT 0; -- BIGINT UNSIGNED MIN
SELECT LEAST(ABC,XYZ), GREATEST(ABC,XYZ), LEAST(ABC,0), GREATEST(ABC,0);
END;
$$
DELIMITER ;
------------------------------------------------------------------------------
Unexpected result :
------------------------------------------------------------------------------
|Bug? |Bug? |Valid |Valid |
|LEAST(ABC,XYZ) |GREATEST(ABC,XYZ) |LEAST(ABC,0) |GREATEST(ABC,0) |
|--------------------|------------------|-------------|----------------------|
|18446744073709551615|0 |0 |18446744073709551615 |
------------------------------------------------------------------------------
Related
I'm using MariaDB to import some files in XML.
Here is a snippet of the code I'm using:
CREATE TABLE invoices (
InvoiceNumber VARCHAR(20),
InvoiceStatus CHAR (1),
InvoiceDate CHAR (10),
Period CHAR (2)
)
;
DROP TABLE if EXISTS temptbl;
create table temp02 (
InvoiceNumber VARCHAR(20) xpath='InvoiceNo',
InvoiceStatus CHAR(1) xpath='DocumentStatus/InvoiceStatus',
InvoiceDate CHAR (10) xpath='InvoiceDate',
Period CHAR (2) xpath='Period'
)
engine=CONNECT table_type=XML file_name='..\\importmaridb\\month01.xml'
tabname='AuditFile' option_list='rownode=SourceDocuments/SalesInvoices/Invoice';
INSERT INTO invoices
SELECT * FROM temptbl;
I then repeat 12x the code of importing to table "temptbl" changing only the file name to reflect the other months.
I would like to have a loop to iterate the files each time.
I believe part of the solution would be to create a table with the file names and an auto increment column, where I would loop through the numbers.
I've attempted to define a variable and substituting in the code, like file_name=#path. But MariaDB gives me an error a syntax error.
| VARIABLE_NAME | VARIABLE_VALUE | VARIABLE_TYPE | CHARACTER_SET_NAME |
+---------------+----------------+---------------+--------------------+
| path | ..\importmaridb| VARCHAR | utf8mb4 |
| | \month01.xml | | |
Can someone give me some pointers, and even if this is possible?
I feel like such a noob for asking this, but I can't for the life of me figure this one out. I'm trying to create a stored procedure here:
create or replace procedure add_cart(#user_id int, #product_id int, #quantity int)
begin
declare cart_id int;
start transaction;
select id into cart_id from carts where user_id = #user_id;
if cart_id is null then
insert into carts (user_id, updated_at, inserted_at) values (#user_id, now(), now());
select id into cart_id from carts where user_id = #user_id;
end if;
insert into cart_products (cart_id, product_id, quantity) values (cart_id, #product_id, #quantity);
commit;
end
But I keep getting this SyntaxError:
[2022-06-14 10:29:29] [42000][1064] (conn=12) 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 '#user_id int, #product_id int, #quantity int)
[2022-06-14 10:29:29] [42000][1064] 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 '#user_id int, #product_id int, #quantity int)
[2022-06-14 10:29:29] begin
[2022-06-14 10:29:29] declare cart_id int;
[2022-06-14 10:29:29] ...' at line 1
MariaDB version 10.6.0 if that's relevant.
Thanks :)
Nvm, got it. It was the # signs :)
Under mariadb-10.3.7 on an InnoDB engine, I'm trying to have the following structure:
drop sequence if exists user_account_id_seq;
create sequence user_account_id_seq start with 1 increment by 10;
drop table if exists user_account;
create table user_account (
-- id bigint(20) unsigned not null auto_increment,
id bigint(20) unsigned not null default (next value for user_account_id_seq),
version int(10) unsigned not null,
created_on datetime,
updated_on datetime,
firstname varchar(255) not null,
lastname varchar(255) not null,
password varchar(100),
password_salt varchar(50),
readable_password varchar(50),
email varchar(50) not null,
confirmed_email bit(1) not null check (confirmed_email in (0, 1)),
work_phone varchar(20),
unique key email (email),
primary key (id)
);
But it doesn't want to remove a sequence:
--------------
drop sequence if exists user_account_id_seq
--------------
--------------
commit
--------------
--------------
create sequence user_account_id_seq start with 1 increment by 10
--------------
ERROR 1050 (42S01) at line 4: Table '`useraccount`.`user_account_id_seq`' already exists
+ /usr/bin/mariadb/install/bin/mysql useraccount --protocol=tcp -h mysql -P 3306 -u root -v
--------------
I then manually tried these commands to show how puzzling this message is:
MariaDB [useraccount]> create sequence user_account_id_seq start with 1 increment by 10;
ERROR 1813 (HY000): Tablespace for table '`useraccount`.`user_account_id_seq`' exists. Please DISCARD the tablespace before IMPORT
MariaDB [useraccount]> alter table `useraccount`.`user_account_id_seq` discard tablespace;
ERROR 1146 (42S02): Table 'useraccount.user_account_id_seq' doesn't exist
I got this three tables
CREATE TABLE
(
FirstTableId Int primary key identity,
Something nvarchar(20)
)
CREATE TABLE SecondTable
(
SecondTableId Int primary key identity,
Something nvarchar(20)
)
CREATE TABLE Relation
(
RelationId int primary key identity,
RelationSomething nvarchar(20),
FirstTableId Int,
SecondTableId Int,
FOREIGN KEY (FirstTableId) REFERENCES FirstTable(FirstTableId),
FOREIGN KEY (SecondTableId) REFERENCES SecondTable(SecondTableId),
)
I create this procedure to insert data into FirstTable and Relation:
CREATE PROC InsertInto (#FirstTableId int)
AS
INSERT INTO FirstTable VALUES ('example')
SELECT #FirstTableId = SCOPE_IDENTITY()
INSERT INTO Relation (RelationSomething, FirstTableId, SecondTableId) VALUES ('example', #FirstTableId, 2)
I pass values from dropdownlists for example data, and do not pass anything for #FirstTable, because I expect that it get SCOPE_IDENTITY(), but I got error like this: "Must declare the scalar variable "#FirstTableId"? How can I solve this and make it's work?
You need to declare the variable in the body and not the Stored Procedure definition. When you declare it in the definition, that means you will pass the value when you call the Stored Procedure.
CREATE PROC InsertInto ()
AS
DECLARE #FirstTableId int;
INSERT INTO FirstTable VALUES ('example')
SET #FirstTableId = SCOPE_IDENTITY()
INSERT INTO Relation (RelationSomething, FirstTableId, SecondTableId) VALUES ('example', #FirstTableId, 2)
Do you have a typo in your actual code? Because in your question you say the procedure parameter is #FirstTable and then later say the error is about #FirstTableId, and in the example procedure the name of the parameter is #FirstTableId.
If you do not need to input or output anything from the parameter, declare and use the variable in the procedure.
If you are trying to use an output parameter, then you would declare the parameter as output:
create proc InsertInto (#FirstTableId int output) as
begin;
set nocount, xact_abort on;
insert into FirstTable
values ('example');
select #FirstTableId = scope_identity();
insert into Relation (FirstTableId, SecondTableId) values
(#FirstTableId, 2);
end;
go
and use it like so:
declare #id int;
exec insertinto #id output;
select #id as IdOutput;
returns
+----------+
| IdOutput |
+----------+
| 1 |
+----------+
and the row from the relation table:
select * from relation;
returns
+--------------+---------------+
| firsttableid | secondtableid |
+--------------+---------------+
| 1 | 2 |
+--------------+---------------+
rextester demo: http://rextester.com/VPS78362
We are tying to use the Σ symbol in our plsql packages. When we compile the packages and execute , the Σ symbol turns to 'S'. Is there any method to avoid this ?
Here is an example:
declare
-- Local variables here
i varchar2(10);
begin
dbms_output.put_line('hello - Σ ');
end;
output
hello - S
That's not a valid ASCII character so you need to use unicode, e.g.:
dbms_output.put_line('hello - ' || unistr('\03A3') || ' ');
Note that dbms_output may not show the correct character depending on your character set.
PL/SQL source code is saved to database in database character set. Databases that use UTF-8 can use UTF-8 also in PL/SQL source code:
with nls_parameters as (
SELECT 1 as depth, 'SESSION' as "LEVEL", parameter, value FROM nls_session_parameters
union all
SELECT 2 as depth, 'INSTANCE' as "LEVEL", parameter, value FROM nls_instance_parameters
union all
SELECT 3 as depth, 'DATABASE' as "LEVEL", parameter, value FROM nls_database_parameters
)
select "LEVEL", parameter, '''' || value || '''' as value
from nls_parameters
where parameter = 'NLS_CHARACTERSET'
order by parameter, depth
;
LEVEL PARAMETER VALUE
-------- ---------------- ----------
DATABASE NLS_CHARACTERSET 'AL32UTF8'
Example:
$ cat /tmp/foo.sql
create procedure foo is
begin
dbms_output.put_line('hello - Σ ');
dbms_output.put_line('μεταφρασμένο από το Google!');
end;
/
$
SQL> #/tmp/foo
Procedure created.
SQL> exec foo
hello - Σ
μεταφρασμένο από το Google!
PL/SQL procedure successfully completed.
SQL>
Another example:
$ cat /tmp/foo.sql
create procedure fooΣ is
Σ number := 100;
begin
dbms_output.put_line('hello - Σ = ' || Σ);
dbms_output.put_line('μεταφρασμένο από το Google!');
end;
/
SQL> #/tmp/foo
Procedure created.
SQL> exec fooΣ
hello - Σ = 100
μεταφρασμένο από το Google!
PL/SQL procedure successfully completed.
SQL>
Note that I'm using linux terminal that understands UTF-8.