触发分配

时间:2013-02-27 18:01:33

标签: sql sql-server

我想完成我的任务,但我遇到了一些问题,所以我需要你的帮助 我已经让一个表名客户端包含6列名称(c_id,c_name,c_transfer,c_balance,day,time)现在分配是我必须创建一个触发器,其中是day列包含Satauarday和Sunday它打印出来对不起Bank关闭了如果时间列包含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

1 个答案:

答案 0 :(得分: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;