
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Tcc_TempCharge]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Tcc_TempCharge]
GO

CREATE TABLE [dbo].[Tcc_TempCharge](
	[VehicleNo] [varchar](32) NOT NULL primary key,
	[ChargeTime] [datetime] NOT NULL,
        [ChargeMoney] [int] NOT  NULL,
	[Remark] [varchar](128) NULL)
GO 



if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tg_CenterCharge]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)
drop trigger [dbo].[tg_CenterCharge]
GO


CREATE trigger [tg_CenterCharge] on [Report_CenterChargeInfo] 
for INSERT
 as
 begin
declare @vehicleNo varchar(32)
declare @inTime datetime
declare @PayMonay int
declare @NewCar varchar(20)

	select @vehicleNo = VehicleNo from inserted
	
	if (len(@vehicleNo) <2)
		return
	select @NewCar = VehicleNo from Tcc_TwofourCharge where VehicleNo = @vehicleNo
	if len(@NewCar)>2
	begin
		update a set a.ChargeTime = b.ChargeTime,a.ChargeMoney = b.ChargeMoney from Tcc_TwofourCharge a,Tcc_TempCharge b
		where a.VehicleNo = @vehicleNo and a.VehicleNo = b.VehicleNo
	end
	else
	begin
		insert into Tcc_TwofourCharge  select a.VehicleNo,a.ChargeTime,a.ChargeMoney,a.Remark from Tcc_TempCharge a where a.VehicleNo = @vehicleNo
	end
 End
GO
