USE [G_OKANDAN_IMS]
GO
/****** Object:  Trigger [dbo].[sarf_fisi_insert]    Script Date: 06.10.2015 16:27:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[sarf_fisi_insert] on [dbo].[Instance – Plaka Harcama (7925)]
for insert
as begin

declare @AMOUNT float
declare @PRICE float
declare @VAT float
declare @STOCKREF int
declare @tipi int
declare @LOGICALREF int
select @tipi=ins.[Tipi (7928)] from inserted as ins
— stok bilgisini aldım—
declare @sirket_no nvarchar(3)
declare @donem_no nvarchar(2)
select @donem_no=lg.[Logo Donem No (7136)],@sirket_no=lg.[Logo Sirket No (7135)] from [Instance – Logo Company (7133)] lg
where InstanceID=2
select @AMOUNT=stk.[Mevcut Miktar (4080)]
,@PRICE=stk.[Son Alış Fiyatı (6625)],
@VAT=stk.[Alış KDV Oranı (12964)],@STOCKREF=convert(int,RTRIM(LTRIM(stk.[Logo Stok Kodu (1076)])))
from [Instance – Stok (1071)] as stk
where stk.InstanceID=@tipi
— stok bilgisini aldım—
–insert ediyorum
— select @LOGICALREF=sf.LOGICALREF from dbo._Sarf_fisi_ as sf
DECLARE @intErrorCode INT

BEGIN TRAN

insert into GOLD.dbo.LG_015_01_STLINE(
AMOUNT,PRICE,VAT,STOCKREF)
values(@AMOUNT,@PRICE,@VAT,@STOCKREF)

SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM

— insert ediyorum —
COMMIT TRAN

PROBLEM:
IF (@intErrorCode <> 0)
BEGIN
insert into hata_sarf_fisi(miktar,fiyat,kdv,stock,tipi) values(
@AMOUNT,@PRICE,@VAT,@STOCKREF,@tipi)
ROLLBACK TRAN
END
end

select * from hata_sarf_fisi