WANGZHANLING 于 2014.01.22 15:59 提问

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER TRIGGER [tr_yskq1] ON [dbo].[yskq1]
FOR INSERT
AS
declare @bh varchar(10),
@date1 datetime,
@time1 varchar(30),
@timeA varchar(30),
@timeB varchar(30),
@bmmc varchar(50),
@jh bigint,
@xm varchar(30),
@bc varchar(50),
@count smallint,
@time2 datetime,
@timeC datetime,
@time3 datetime,
@time4 datetime,
@rqsj datetime,
@aa varchar(30),
@kh varchar(20),
@xs int,
@fz int
select @bh=bh,@date1=isnull(rq,'1900-01-01'),@timeA=sj,@xm=xm,@bc=bc from inserted

``````  SET @xs=CAST (SUBSTRING(@timeA,1,2)AS INT)
SET @fz=CAST (SUBSTRING(@timeA,3,2)AS INT)
``````

IF @xs0
begin
set @xs=7
if @fz set @fz=@fz+15
end
IF @xs18
begin
set @xs=19
if @fz>30
set @fz=@fz-30
end

set @time1=convert(varchar,@xs)+ convert(varchar,@fz)+'00'

--赋初值
set @count = 0
--字符型转时间型
set @time2 = cast(@time1 as datetime)
--时间推后5分钟

select @count=count(*) from yskq where rq = @date1 and sj <= convert(varchar(30),@time3,8) and sj >= convert(varchar(30),@time4,8) and bh=@bh
set @aa = convert(varchar(30),@time2,8)
--insert into t_test values (@time4,@time3,@time2,@time1,@count,@aa)
select @bmmc=bm from employee where bh=@bh
select @kh=mkkh from employee where bh=@bh
if @count <= 0
begin
--如果沒有該記錄則執行插入
insert into dbo.yskq(bh,rq,sj,xm,bmmc,bc,kh) values (@bh,@date1,@time1,@xm,@bmmc,@bc,@kh)
end