Create composed key (YEAR+COUNT) field with trigger after insert in SQLITE
0
votes
1
answer
38
views
I have a table Product with two columns: name of product and price
CREATE TABLE IF NOT EXISTS YearCounters (
year INTEGER PRIMARY KEY,
counter INTEGER DEFAULT 0
);
-- Product Table
CREATE TABLE IF NOT EXISTS Products (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
price REAL
);
My objectif,Creates a unique key in the Product table, which must be composed of the year plus a number of size 10 which increments for each new product added YEAR+COUNT(10) :
INSERT INTO Products (name, price) VALUES ('Produit B', 20.15);
INSERT INTO Products (name, price) VALUES ('Produit C', 23.75);
The product table should look like this
Example:
SELECT * FROM Products;
2024000000000001|Produit B|20.15
2024000000000002|Produit C|23.75
I've developed a TRIGGER for the primary key, but it doesn't work. .
CREATE TRIGGER IF NOT EXISTS increment_counter_and_set_product_id
BEFORE INSERT ON Products
FOR EACH ROW
BEGIN
--
UPDATE YearCounters
SET counter = counter + 1
WHERE year = strftime('%Y', 'now');
--
INSERT OR IGNORE INTO YearCounters (year, counter)
VALUES (strftime('%Y', 'now'), 0);
--
SELECT counter
INTO temp_counter
FROM YearCounters
WHERE year = strftime('%Y', 'now');
--
SET NEW.id = strftime('%Y', 'now') || printf('%010d', temp_counter);
END;
Error:
I recieve the error
Parse error near line 15: near "INTO": syntax error
SELECT counter INTO temp_counter FROM Y
error here ---^
What am I doing wrong?
Asked by kiaderouiche
(1 rep)
May 27, 2024, 02:17 PM
Last activity: May 28, 2024, 10:39 AM
Last activity: May 28, 2024, 10:39 AM