MySQL database stored procudure it is giving me error Erro
MySQL - database stored procudure -- it is giving me error: Error Code: 1364. Field \'cno\' doesn\'t have a default value
How do I fix it?
Create and exercise a SQL stored procedure called NewChirp(...) that the application can use to add a newly created (non-parroted) chirp to the database. The stored procedure should automatically use the current date and time to set those fields of the new chirp, and it should automatically generate the new chirp’s number by adding one to the chirper’s previous highest chirp number.
DELIMITER $$
CREATE PROCEDURE NewChirp(
new_btag VARCHAR(30),
loc_lat DECIMAL(10,6),
loc_long DECIMAL(10,6),
sentiment DECIMAL(2,1),
content VARCHAR(255))
BEGIN
DECLARE new_cno INT(11);
SET new_cno = ( SELECT MAX(cno) from Chirp) + 1;
INSERT INTO Chirp (btag, location_latitude, location_longitude, sentiment, text)
VALUES (new_btag, loc_lat, loc_long, sentiment, content);
END;
Solution
In the stored procedure
the query, SELECT MAX(cno) from Chirp , shows that the table Chirp has a column cno.
And the column may be a required column. But in the Insert query the value is not inserted for cno column.
rewrite the query to insert cno column also with the calculated value, as
INSERT INTO Chirp (cno, btag, location_latitude, location_longitude, sentiment, text)
VALUES (new_cno, new_btag, loc_lat, loc_long, sentiment, content);
new_cno is already calculated by
SET new_cno = ( SELECT MAX(cno) from Chirp) + 1;
