IF Not (EXISTS(SELECT * FROM sysobjects WHERE name='SummaryAccountingDocumentDetails'))
Begin
CREATE TABLE [dbo].[SummaryAccountingDocumentDetails](
	[Id] [UNIQUEIDENTIFIER] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_SummaryAccountingDocumentDetails_Id]  DEFAULT (NEWID()),
	[AccountingdocumentId] [UNIQUEIDENTIFIER] NULL,
	[Type] [CHAR](1) NOT NULL,
	[Code] [VARCHAR](20) NOT NULL,
	[Debtor] [MONEY] NOT NULL,
	[Creditor] [MONEY] NOT NULL,
	[SyncTime] [DATETIME] NULL,
	[Store_Code] [VARCHAR](3) NULL,
        [Status] [CHAR](1) NULL DEFAULT(0),
	[DocumentId] [BIGINT] NULL,
 CONSTRAINT [PK_SummaryAccountingDocumentDetails] PRIMARY KEY CLUSTERED 
(
  [Id] ASC
)
) ON [PRIMARY]


IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_SummaryAccountingDocumentDetails_SummaryAccountingDocuments]') AND parent_object_id = OBJECT_ID(N'[dbo].[SummaryAccountingDocumentDetails]'))
ALTER TABLE [dbo].[SummaryAccountingDocumentDetails]  WITH CHECK ADD  CONSTRAINT [FK_SummaryAccountingDocumentDetails_SummaryAccountingDocuments] FOREIGN KEY([AccountingdocumentId])
REFERENCES [dbo].[SummaryAccountingDocuments] ([Id])
ON DELETE CASCADE


IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_AccountingDocumentDetails_AccountingDocuments]') AND parent_object_id = OBJECT_ID(N'[dbo].[AccountingDocumentDetails]'))
ALTER TABLE [dbo].[AccountingDocumentDetails] CHECK CONSTRAINT [FK_AccountingDocumentDetails_AccountingDocuments]
End