phpmyadmin sql event getting error when trying to declare variable - sql

I'm new to sql so bear with me, but I'm trying to create an event that will populate the row of a table at a specific time with values from 1 to x where x is a pre-declared variable. I'm attempting to use a delimiter for the first time and I'm not really sure where all my semi-colons should be at but the following is what I assumed after a while of reading:
DELIMITER $$
DECLARE day_m INT DEFAULT 0;
SET day_m = DATE_FORMAT(CURRENT_DATE, '%e');
WHILE day_m <= DATE_FORMAT(LAST_DAY(CURRENT_DATE), '%e')
BEGIN
INSERT INTO monthly_scheduling (days) values (day_m)
SET day_m = day_m + 1
END;
$$
DELIMITER ;
The error I'm getting is as follows: MySQL said: #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 '$$ DECLARE day_m INT DEFAULT 0; SET day_m = DATE_FORMAT(CURRENT_DATE, ' at line 1
I've been trying all sorts of things but I can't get the event to create because of the error I seem to keep getting at the DECLARE section. Any help would be much appreciated.

Related

How do you select an unique id in an DB2 table that was generated using DB2 generate unique() function?

I would like to select the information from a DB2 table, using that unique id that was generated using DB2 generate unique() function as key in a simple select query.
Using the field directly, gives an SQL code of -333, using HEX(unique id) in select gives SQL code of +100.
How can i get the information from the table?
This depends on the data type of your parameter.
If you know its hex representation, then:
DECLARE GLOBAL TEMPORARY TABLE SESSION.GEN_UNIQUE (U) AS (VALUES GENERATE_UNIQUE())
WITH DATA WITH REPLACE ON COMMIT PRESERVE ROWS NOT LOGGED;
SELECT HEX(U) AS HEX_VAL
FROM SESSION.GEN_UNIQUE
WHERE U = HEXTORAW ('20210531172108210028000000');
|HEX_VAL |
|--------------------------|
|20210531172108210028000000|
If you have it as CHAR(13) FOR BIT DATA, then it can be used as usual:
--#SET TERMINATOR #
BEGIN
DECLARE L_VAR CHAR(13) FOR BIT DATA;
DECLARE L_DUMMY INT;
DECLARE EXIT HANDLER FOR NOT FOUND
BEGIN
RESIGNAL SQLSTATE '70001' SET MESSAGE_TEXT = 'Oops!';
END;
SET L_VAR = HEXTORAW('20210531172108210028000000');
SELECT 1 INTO L_DUMMY
FROM SESSION.GEN_UNIQUE
WHERE U = L_VAR;
END
#

Getting a syntax error when declaring a table in SQL

New to SQL and trying to run this code and I get the error "#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 '#Qtable TABLE(Qid INT) END' at line 3". The code runs fine if I take out the DECLARE statement;
SET #query_id = (SELECT ID from ua820988_dev.users WHERE `email` = 'example#gmail.com');
CREATE PROCEDURE query_requests()
BEGIN
DECLARE #Qtable TABLE(Qid INT)
END;
SELECT * from ua820988_dev.requests WHERE `match` = #query_id;
SELECT * from ua820988_dev.requests_archive WHERE `match` = #query_id;
I'm hoping to eventually put the results from the 2nd and 3rd SELECT statements into the table, but this is the watered down version for now just trying to get the code to run. I'm running SQL 5.6 on MariaDB 10.2.

How do you use variables in UDF's in mySQL

So i am trying to create a function in phpmyadmin using mySQL
So first i program it in SQL Server and then copy-paste it into phpmyadmin and when i try to insert the function it keeps telling me that there is a syntax error.
Here's the code:
CREATE FUNCTION check_verkoper (
#var VARCHAR(32)
)
RETURNS bit
AS
BEGIN
IF (SELECT verkoper FROM gebruiker WHERE gebruikersnaam = #var) = 1
BEGIN
RETURN 1
END
RETURN 0
END
go
ALTER TABLE verkoper
ADD CONSTRAINT CHK_verkoper CHECK(dbo.check_verkoper(gebruikersnaam) = 1);
GO
So it keeps telling me this error:
Something went wrong in the syntax near:
#var VARCHAR(32)
)
RETURNS bit
AS
BEGIN
IF (SELECT verkoper FROM gebruiker
on line 2
Can anyone help this student?

Create trigger error: 'Incorrect Syntax near 'dbo'

I'm currently learning some regarding SQL triggers. I am trying to create a column on a test table which will show the current date when the row is updated. The column datatype is type DateTime So far I have:
CREATE TRIGGER lastUpdated
ON [dbo].[ImportTest]
AFTER UPDATE
AS
BEGIN
IF NOT UPDATE(LAST_UPD)
BEGIN
SET [dbo].[ImportTest].[LAST_UPD] = CURRENT_TIMESTAMP
END
END
GO
However, I get the following error trying to execute:
Msg 102, Level 15, State 1, Procedure lastUpdated, Line 29
Incorrect syntax near 'dbo'.
Any help would be appreciated.
You cannot update a column like assigning value to variable. Try this
IF NOT UPDATE(LAST_UPD)
BEGIN
UPDATE IT
SET [LAST_UPD] = CURRENT_TIMESTAMP
FROM [dbo].[ImportTest] IT
WHERE EXISTS (SELECT 1
FROM inserted i
WHERE i.somecol = it.somecol
And ...)
END

solution for generating a running sequence number by using stored procedure in sql server 2008

how to generate a running sequence number from 0001 to 9999 by through using a function in sql server, if u call that function in sql server, it should to be provide a running sequence number when ever if it reach 9999 again it should to be reset 0001
can any help on this please to perform this above task
There are plenty of good articles about that. Try Google.
For instance see here and there
A function can't update, insert or delete, so you can't store the variable and check it's value next time you call the function. So there is no way of doing this using a function.
You can simply get the sequence number using following procedure:
CREATE PROCEDURE proc_GetSeqNumber
AS
BEGIN
DECLARE #idt INT
SET #idt = 0
WHILE (#idt < 9999)
BEGIN
SELECT #idt = #idt + 1
PRINT #idt
END
END
The only way a database can remember a value is if it is stored in a table (in Oracle, you could use a sequence, but SQL Server doesn't use those.) So I would create a single table with a single value and the function would read the value, increment it, and if need be reset it.
if #ItemsCount=0
begin
set #generatedSeNumber=1
end
else
begin
SET #sql= 'insert into Senumber values(('+#SeNumber+'))'
Exec (#sql)