0

我要完成我的任务,但我遇到了一些问题,所以我需要你的帮助我已经创建了一个表名客户端包含 6 列名称(c_id,c_name,c_transfer,c_balance,day,time)现在任务是我必须创建一个触发器,其中日期列包含 Satauarday 和星期日,它会打印对不起银行关闭,如果时间列包含 05:00 到 09:00,它将插入,但如果时间没有重新保护条件行不能插入。代码如下:

create table client
(
c_id int identity primary key,
c_name varchar(50),
c_transfer money,
c_balance money,
[date] datetime,
[day] varchar(50),
)

alter trigger transactions
ON client
for Insert 
as
begin
    if(select top 1 [day] from client order by c_id desc)='Satuarday'
    begin
    print'Sorry Bank is closed today'
    rollback;
    commit;
    end
    if(select top 1 [day] from client order by c_id desc)= 'Sunday'
    begin
    print'Sorry Bank is closed today'
    rollback;
    commit;
    end
    if(select top 1 date from client order by c_id desc)not in (DATEDIFF(hh,'09:00','05:00'))
    begin
    print'Sorry Bank time is not this...'
    rollback;
    commit;
    end
end
4

1 回答 1

0

你要找的是Inserted桌子。此表可在FOR INSERT触发器内部使用,并包含要插入的行。

GO
CREATE TRIGGER transactions
ON client
FOR INSERT AS
BEGIN
  IF EXISTS(SELECT [day] FROM inserted 
            WHERE ([day] IN ('saturday', 'sunday')) 
            OR (DATEPART(hh,[date]) <= 5) OR DATEPART(hh,[date]) >= 9)
  BEGIN
    PRINT'Sorry Bank is closed at this time.'
    ROLLBACK;
  END;
END;
于 2013-02-27T18:13:30.920 回答