【发布时间】:2019-02-17 16:29:59
【问题描述】:
我需要使用游标完成此问题的第二部分 (https://imgur.com/yyoZxsw),但我的代码正在使用相同的值更新每一行。基本上我需要检查时间和超时是否在某个范围内,例如上午 9 点到下午 12 点之间,付款将是 350。另外,如果它是从上午 10 点到下午 16 点,我需要在 2 个范围之间进行计算。
我尝试了下面的代码,但它不起作用。预计会通过timein 和timeout 计算要支付的金额到amtpaid 列。
create table babysitter (
babysitterid char(5) not null primary key,
datein date not null,
timein time not null,
dateout date not null,
timeout time not null,
noofhrswrk int,
amtpaid int
);
insert into babysitter values('BS001', '18-Jan-2019', '10:00', '18-Jan-
2019', '16:00', '', '')
insert into babysitter values('BS002', '15-Jan-2019', '13:00', '15-Jan-
2019',
'20:00', '', '')
insert into babysitter values('BS003', '21-Jan-2019', '21:00', '21-Jan-
2019',
'07:00', '', '')
insert into babysitter values('BS004', '11-Jan-2019', '08:00', '11-Jan-
2019', '13:00', '', '')
declare @timein time
declare @timeout time
declare @hoursworked datetime
declare Calculate_No_Hrs cursor for
select timein, timeout, noofhrswrk from babysitter
open Calculate_No_Hrs
fetch next from Calculate_No_Hrs into @timein, @timeout, @hoursworked
while (@@FETCH_STATUS = 0)
begin
update babysitter
set noofhrswrk = abs(datediff(hour, timeout, timein))
fetch next from Calculate_No_Hrs into @timein, @timeout, @hoursworked
end
close Calculate_No_Hrs
deallocate Calculate_No_Hrs ---end first question
--------------------------------------------------------------
declare @timein time
declare @timeout time
declare @amount int
declare @hourswrk int
declare @pay int
set @pay = 0
declare Amt_Paid cursor for
select timein, timeout, noofhrswrk, amtpaid
from babysitter
open Amt_Paid
fetch next from Amt_Paid into @timein, @timeout, @hourswrk, @amount
while (@@FETCH_STATUS = 0)
begin
if (@timein >= '09:00' and @timeout <= '12:00')
begin
set @amount = 350 * @hourswrk
set @pay += @amount
-- update babysitter
-- set amtpaid = @amount
end
if (@timein >= '12:00' and @timeout <= '17:00')
begin
set @amount = 400 * @hourswrk
set @pay += @amount
-- update babysitter
-- set amtpaid = @amount
end
if (@timein >= '17:00' and @timeout <= '21:00')
begin
set @amount = 500 * @hourswrk
set @pay += @amount
-- update babysitter
-- set amtpaid = @amount
end
if (@timein >= '21:00' and @timeout <= '00:00')
begin
set @amount = 600 * @hourswrk
set @pay += @amount
-- update babysitter
-- set amtpaid = @amount
end
if (@timein >= '00:00' and @timeout <= '07:00')
begin
set @amount = 800 * @hourswrk
-- update babysitter
-- set amtpaid = @amount
end
update babysitter
set amtpaid = @pay
fetch next from Amt_Paid into @timein, @timeout, @hourswrk, @amount
end
close Amt_Paid
deallocate Amt_Paid
【问题讨论】:
-
为什么要使用光标来执行此操作?我建议你可以在没有人的情况下做到这一点。就像我在您的另一个(现已删除)问题中提到的那样,将您的问题的上下文放在您的问题中;不在场外资源上。另外,不要忘记,我们无权访问您的数据(或作业),因此如果没有 DDL 和 DML,我们将无法运行上述内容。
-
@Larnu 我认为这只是使用光标的问题练习是要求之一。 @Cat_img.jpeg 我没有检查您的代码是否有效,但如果您不在
update语句中指定babysiterid,那将毫无意义。 -
您需要确定每个单独范围内的工作小时数,并将适当的费率应用于这些小时数,例如从 10:00 到 14:00 工作的人以 350 卢比/小时的价格工作了 2 小时,以 400 卢比/小时的价格工作了 2 小时,对吧?
-
@HABO 是的,我在第一部分完成了这项工作,并将其存储在名为
noofhrswrk的列中 -
@Larnu 我已经用我的数据更新了帖子,但是对于第三行,我从 21:00 到 07:00 得到了 14 小时而不是 10 小时
标签: sql-server tsql database-cursor