I have a function that calculate time difference excluding weekends, and I have created a stored procedure to call the function. When I tested the sp, only showed message part, I am new and not sure if this is correct. And I have trouble to pass the parameters to SSRS. Please help, many thanks.
Alter Proc Test_1
#DownTime datetime,
#Uptime datetime =null --since there are some null value in uptime column
AS
Begin
print 'enter'
declare #duration int
select #duration = [dbo].[CalcWorkMinutes](#DownTime,#Uptime)
print 'abc'
;with cte_test2
as (
SELECT DownTime
,UpTime
,datediff(hh,DownTime,UpTime) AS duration
FROM Test
)
select DownTime,UpTime,duration from cte_test2
where DownTime = #DownTime and UpTime=#UpTime
and duration=#duration
--print 'pass'
print 'duration'+' '+convert(varchar,#duration)
RETURN
END
--execute [dbo].[test_1] #downtime ='2017-06-02 09:00:00.000', #Uptime ='2017-06-05 09:00:00.000'
Hmm... There's a lot going on in this one. It looks like you are trying to see what downtime was like across a certain timeframe someone will pass in? If that is the case, I'd change the parameters to be: StartDate and EndDate. You don't really need a CTE to apply the parameters... Code below... I am just taking a guess here though:
Alter Proc Test_1
#startdate datetime,
#enddate datetime --since there are some null value in uptime column
AS
Begin
print 'enter'
SELECT
DownTime
,UpTime
,[dbo].[CalcWorkMinutes](DownTime,UpTime) AS duration
FROM Test as a
where
downtime >= #startdate
and b.downtime < dateadd(day,1,#enddate)
RETURN
END
Related
I have a mysql cursor that is not returning any records when comparing a date variable to a date column in the table. All of this is taking place inside a stored procedure. Hence:
DECLARE StartDateInterval Date DEFAULT '1997-11-01';
DECLARE EndDateInterval Date DEFAULT '1997-12-31';
DECLARE End_Period Date DEFAULT '1997-12-31';
DECLARE prev_period_tournie_rating CURSOR FOR select PID, Rating, RD from rating where Rating_Date = End_Period;
SET End_Period = (StartDateInterval - interval 1 day);
OPEN prev_period_tournie_rating;
firstloop: LOOP
FETCH prev_period_tournie_rating INTO vPID, PrevRat, PrevRD;
IF done = 1 THEN LEAVE firstloop; END IF;
SET decayedRD = ROUND(`newRD`(PrevRD));
INSERT into rating (PID, Rating, RD, Rating_Date, Rating_Type) VALUES (vPID, PrevRat, decayedRD, EndDateInterval, 2);
END LOOP;
CLOSE prev_period_tournie_rating;
There is more code in the Stored Procedure that I have deleted. After the DECLARE statements, all of the above takes place inside a WHILE . . .END WHILE that iterates through 2 month intervals from 1997 to the present, hence the need for End_Period to be a variable and not coded to a single date.
When I remove the where condition the Cursor retrieves records from the rating table.
I do not pass End_Period as a paramater in the cursor DECLARE statement. It appears that some mysql dialects allow/require this and some don't. Even though I have tried to set my dialect to MariaDB (which does allow parameters, I get an error if I add End_Period as a parameter.
Rating_Date is a date column in the table rating, using the standard mysql format (YYYY-MM-DD).
Any help on solving this would be appreciated. This is my first attempt at using a Stored Procedure and it is proving to be a challenge!
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 sproc with multiple selects and result sets. The last query in the sproc needs to select data where a table created date >= the first day of the current month. I have SQL which successfully returns the first day of the month as expected. I need to select this value into a sproc variable FirstDayOfTheMonth and then reference this variable in the WHERE clause of the subsequent SELECT statement in the sproc. I included the following SQL before the final result set in the sproc but it seems that MySQL doesn't like something about it - something about its structure, positioning or syntax:
DECLARE FirstDayOfMonth INT DEFAULT 0;
SET FirstDayOfMonth = (SELECT DATE_SUB(LAST_DAY(NOW()),INTERVAL DAY(LAST_DAY(NOW()))- 1 DAY)
How can I update my existing attempt at a MySQL sproc variable so that my sproc compiles successfully with this variable declaration?
UPDATE
I tried to put the following 2 lines immediately after the BEGIN keyword in my sproc:
DECLARE FirstDayOfMonth INT DEFAULT 0;
SET FirstDayOfMonth = (SELECT DATE_SUB(LAST_DAY(NOW()),INTERVAL DAY(LAST_DAY(NOW()))- 1 DAY)
MySQL Workbench displays an error on the SET statement:
FirstDayOfMonth is not valid at this position, expecting an identifier
Any idea what I need to do differently here?
The DECLARE-statements need to be in the beginning of the procedure, before anything else, just after the BEGIN.
I can't seem to figure this out and I've been at it for sometime now any help would be great thank you.
I have to prepare a simple procedure accepting one parameter, a date value, and using date addition functions in SQL (MySql if possible) return the parameter value minus 1 day
CREATE PROCEDURE get_date
IS
BEGIN
DATE_ADD(date,INTERVAL expr type)
END;
/
EXEC get_date
This was all I could come up with so far. Any help would be greatly appreciated.
That question is not just about select statement (ie, computing minus one day); I need help with a SQL procedure.
Maybe just create a function
CREATE FUNCTION simplefunction (s datetime)
RETURNS datetime DETERMINISTIC
RETURN DATE_SUB(s, INTERVAL 1 DAY);
SELECT simplefunction (anydate);
I have already been doing some function in postgresDB, but im completly new to MySQL. I thought that it will be almost same to create procedure in MySQL, but aparently it is not. So I am asking for some help :)
In postgres I would write this function for what I need to accomplish:
DROP PROCEDURE IF EXISTS report_incomes;
DELIMITER //
CREATE FUNCTION reportincomes(IN interval INT)
RETURN report_tab TABLE
(
year_month DATE,
total DECIMAL(30,2),
total_before DECIMAL(30,2)
)
AS
BEGIN
FOR row_d IN SELECT * FROM intervals_generator('2013-01-01 00:00:00', now(), interval) LOOP
SELECT sum(cash) AS total, DATE_FORMAT(datetime, '%b %Y') AS year_month FROM incomes WHERE datetime BETWEEN row_d.start_d AND row_d.end_d//
SELECT sum(cash) AS total_before FROM incomes WHERE datetime BETWEEN DATE_SUB(row_d.start_d, INTERVAL 1 YEAR) AND DATE_SUB(row_d.end_d, INTERVAL 1 YEAR)//
END LOOP//
END//
DELIMITER ;
As you may noticed, the big part of code is already switched to MySQL, but I am kind of stuck with FOR LOOP, as it looks so different in MySQL compared to Postgres.
Of course there will be much more code added inside the LOOP, I just did not wanted to make it too complicated for explanation.
Could please anyone help me with the FOR LOOP?
Basicly, I want to execute these SELECTs for each row returned by function that generates my custom intervals, while using data from that row inside of LOOP