SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[KDVTANIMSUBE]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[KDVTANIMSUBE](
	[KDVTANIMSUBEID] [int] IDENTITY(1,1) NOT NULL,
	[SUBEID] [int] NOT NULL,
	[ORAN] [float] NOT NULL,
	[ALKOLLU] [int] NOT NULL,
	[ALISHESAPID] [int] NULL,
	[ALISIADEHESAPID] [int] NULL,
	[SATISHESAPID] [int] NULL,
	[SATISIADEHESAPID] [int] NULL,
	[TEVKALISHESAPID] [int] NULL,
	[TEVKALISIADEISHESAPID] [int] NULL,
	[TEVKSATISHESAPID] [int] NULL,
	[TEVKSATISIADEHESAPID] [int] NULL,
	[TEVKALISIADEHESAPID] [int] NULL,
	[ACIKLAMA] [nvarchar](50) NULL,
	[GELIRENTKONTROLHESABIID] [int] NULL,
 CONSTRAINT [PK_KDVTANIMSUBE] PRIMARY KEY CLUSTERED 
(
	[KDVTANIMSUBEID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO


IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[KDVTANIMSUBE]') AND name = N'IX_KDVTANIMSUBE_SUBEID_ORAN_ALKOLLU_ACIKLAMA')
CREATE UNIQUE NONCLUSTERED INDEX IX_KDVTANIMSUBE_SUBEID_ORAN_ALKOLLU_ACIKLAMA ON dbo.KDVTANIMSUBE
	(
	SUBEID,
	ORAN,
	ALKOLLU,
	ACIKLAMA
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

IF NOT EXISTS (SELECT TOP 1 * FROM KDVTANIMSUBE WHERE ALISHESAPID IS NOT NULL OR ALISIADEHESAPID IS NOT NULL OR SATISHESAPID IS NOT NULL OR SATISIADEHESAPID IS NOT NULL OR TEVKALISHESAPID IS NOT NULL OR TEVKALISIADEHESAPID IS NOT NULL OR TEVKSATISHESAPID IS NOT NULL OR TEVKSATISIADEHESAPID IS NOT NULL)
INSERT INTO KDVTANIMSUBE (SUBEID, ORAN, ALKOLLU, ACIKLAMA, ALISHESAPID, ALISIADEHESAPID, SATISHESAPID, SATISIADEHESAPID, TEVKALISHESAPID, TEVKALISIADEHESAPID, TEVKSATISHESAPID, TEVKSATISIADEHESAPID) 
SELECT     SUBELER.SUBEID, ORAN, ALKOLLU, ACIKLAMA, ALISHESAPID, ALISIADEHESAPID, SATISHESAPID, SATISIADEHESAPID, TEVKALISHESAPID, TEVKALISIADEHESAPID, TEVKSATISHESAPID, TEVKSATISIADEHESAPID
FROM         KDVTANIM CROSS JOIN SUBELER
WHERE (ALISHESAPID IS NOT NULL OR ALISIADEHESAPID IS NOT NULL OR SATISHESAPID IS NOT NULL OR SATISIADEHESAPID IS NOT NULL OR TEVKALISHESAPID IS NOT NULL OR TEVKALISIADEHESAPID IS NOT NULL OR TEVKSATISHESAPID IS NOT NULL OR TEVKSATISIADEHESAPID IS NOT NULL) AND
  NOT EXISTS (SELECT KDVTANIMSUBEID FROM KDVTANIMSUBE 
            WHERE SUBEID = SUBELER.SUBEID AND ORAN = KDVTANIM.ORAN AND ALKOLLU = KDVTANIM.ALKOLLU) 
GO