I am a Newbee using MySQL Workbench. I have a table Called requests. It has a column called STATUS and another called EXPIREDDATE. I want to create a Stored procedure that Inputs the text "Exipred" into the column STATUS if the date in EXPIREDDATE exceeds Todays date. The begining of the code is below. Thanks.
CREATE PROCEDURE `Add Expired` ( IF expireddate => todays date THEN status = "expired")
BEGIN
END
Inputs the text "Expired" in to a column A if the date in column B exceeds Todays date
You are describing an update statement with filtering:
update mytable set a = 'expired' where b > current_date
You can easily turn this to a stored procedure - although it wouldn't be very helpful (you can just run the query).
Related
I have a table with sensor data and a table that gets data from an app.
Every time the app saves data to it's table, it saves two datetime values to "start time" and "end time" columns. Based on that time gap, I have to get some values from the sensor data table (start temp, maximum temp, max humidity...).
I am currently using a before insert trigger and using new.colName = (select from sensor ...).
First column inserts fine but after that I get "Error Code: 1193 Unknown system variable 'NEW.max_temp'".
Im sure this is because i have something wrong in my trigger syntax, but I am not able to determine what is causing this.
The values im filling are originally Null when the app inserts it's data and the columns exist im trying to SET NEW.XXXX exist.
My Trigger:
DELIMITER $$
CREATE DEFINER = CURRENT_USER TRIGGER `Cegasa`.`FAB_BEFORE_INSERT`
BEFORE INSERT ON `fabrication`
FOR EACH ROW
SET NEW.start_temp = (SELECT temp FROM sensors WHERE (NEW.fab_start_date) <= date ORDER BY date LIMIT 1);
SET NEW.max_temp = (SELECT temp FROM sensors WHERE (NEW.fab_end_date) >= date ORDER BY date DESC LIMIT 1);
DELIMITER;
First SET statement works fine but second one gives the error. I can provide further table structure if necessary but I think it has more to do with my syntax.
I have a set of date (start date & end date) in DB, how to validate for duplicate date set between user input and DB. (Mysql)
i have try a few method but it only compare either start or end date only. not both at the same time.
I think you are looking for the following logic.
Filter using between SQL command
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
use the following logic:
User registered new START DATE and END DATE
Search for START DATE using BETWEEN into DATABASE
Search for END DATE using BETWEEN into DATABASE
If those values exists into ranges you can show duplicated message.
Regards
C.
I am in desperate need of some help!
I am currently working on a project for SQL in a course I am doing (Due on Friday)
One of the questions is to create a stored procedure which shows dates of sales_orders within a certain range.
I have created the following stored procedure for this question, but all I am getting is blank rows? From what I can tell it seems to be reading the dates wrong, but I have no idea why? Can anyone please help? I am a newbie at SQL.
DELIMITER //
CREATE PROCEDURE customer_order_range (start_date DATE, end_date DATE)
BEGIN
SELECT *
FROM customer_order
WHERE `date` >= start_date
AND `date` <= end_date;
END //
DELIMITER ;
CALL customer_order_range ( 'start_date' = '2020-02-01', 'end_date' = '2020-03-05');
It returns blank rows. My customer_order table has a date column, which is stored as a DATE value.
Any help would be super appreciated!
Conor
I've got it!
This piece of the code is wrong:
CALL customer_order_range ( 'start_date' = '2020-02-01', 'end_date' = '2020-03-05');
It should instead be:
CALL customer_order_range ('2020-02-01','2020-03-05');
The problem arose when I put the parameter names in with their values when calling the stored procedure. Thanks everyone! :)
Remove the delimiters and the columns names like below :
CALL customer_order_range ('2020-02-01', '2020-03-05');
I have a PHP/MySQL project having 15 tables. One of table name tbl_user_log, where all user log will saved. I want to empty or truncate this table in every 3 month.How could I do this using trigger or any solution is applicable.
You can set a cronjob to a certain route of your project to perform following sql:
DELETE FROM tbl_user_log
See this website: http://setcronjob.com
Or you can simply use mysql EVENTs.
It may helps you
declare #tbl nvarchar(max)='table_name'
if (
(select create_date FROM
sys.tables where name=#tbl)
<= (SELECT DATEADD(month,-3, GETDATE()))
begin
truncate table #tbl
end
else
select 'table creation date is not more than 3 months'
better you can put this in a procedure and u can directly pass table name as input and can wrk efficently
I'm using SQL Server 2008. I'm looking for a creative way to save and update a list of dates in our database.
I'm going to collect a list of dates from the application and I will need to check if each value already exists, if not add, and then delete any dates not in the list that are already stored in the database.
The easiest thing I can think of is to delete all dates associated to this particular request and then iterate over each item in my list and insert into database.
Does anyone have a more elegant idea?
You can use merge. You can also load the dates into a temporary table and do an insert such as:
with toinsert as (
select thedate
from #newdates nd left outer join
alldates ad
on nd.thedate = ad.thedate
where ad.thedate is null
)
insert into alldates(thedate)
select thedate
from toinsert
The toinsert alias uses a left outer join to do a "not in". I often find that this works better. Regardless of how you set up the queries (like this or with a merge), you should put in an index on the dates. It should make things go faster.
I would use a combination of table valued parameters and the NOT EXISTS function. So pass your dates from your application to a stored procedure as a paramater, the stored procedure will then return a list of all the dates inserted back to your application.#
The first step is to create the type so you can pass a list of dates to your procedure:
CREATE TYPE dbo.DateListType AS TABLE (Date DATETIME)
Next create your procedure. I have assumed you date table is called dbo.Dates, you'll obviously need to substitute your table in for this.
CREATE PROCEDURE dbo.InsertDates (#Dates dbo.DateListType READONLY)
AS
BEGIN
DECLARE #Inserted TABLE (Date DATETIME)
INSERT INTO dbo.Dates
OUTPUT inserted.Date INTO #Inserted
SELECT Date
FROM #Dates d
WHERE NOT EXISTS
( SELECT 1
FROM dbo.Dates
WHERE Dates.Date = d.Date
)
SELECT Date
FROM #Inserted
END
Not sure what your application is coded in so unfortunately can't suggest any code to call the procedure