SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TBREC_CFG_ACTION](
	[GUID] [bigint] IDENTITY(1,1) NOT NULL,
	[PROFILE_ID] [bigint] NOT NULL,
	[ACTIVE] [bit] NOT NULL,
	[SEQUENCE] [tinyint] NOT NULL,
	[ENDPOINT_ID] [bigint] NOT NULL,
	[ENDPOINT_AUTH_ID] [bigint] NULL,
	[ENDPOINT_PARAMS_ID] [smallint] NULL,
	[SQL_CONNECTION_ID] [smallint] NULL,
	[TYPE_ID] [tinyint] NOT NULL,
	[PREPROCESSING_QUERY] [nvarchar](max) NULL,
	[HEADER_QUERY] [nvarchar](max) NULL,
	[BODY_QUERY] [nvarchar](max) NULL,
	[POSTPROCESSING_QUERY] [nvarchar](max) NULL,
	[ERROR_ACTION_ID] [tinyint] NOT NULL,
	[ADDED_WHO] [nvarchar](50) NOT NULL,
	[ADDED_WHEN] [datetime] NOT NULL,
	[CHANGED_WHO] [nvarchar](50) NULL,
	[CHANGED_WHEN] [datetime] NULL,
 CONSTRAINT [PK_TBREC_CFG_ACTION_GUID] PRIMARY KEY CLUSTERED 
(
	[GUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] ADD  CONSTRAINT [DF_TBREC_CFG_ACTION_ACTIVE]  DEFAULT ((0)) FOR [ACTIVE]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] ADD  CONSTRAINT [DF_TBREC_CFG_ACTION_SEQUENCE]  DEFAULT ((0)) FOR [SEQUENCE]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] ADD  CONSTRAINT [DF_TBREC_CFG_ACTION_TYPE_ID]  DEFAULT ((1)) FOR [TYPE_ID]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] ADD  CONSTRAINT [DF_TBREC_CFG_ACTION_ERROR_ACTION_ID]  DEFAULT ((0)) FOR [ERROR_ACTION_ID]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] ADD  CONSTRAINT [DF_TBREC_CFG_ACTION_ADDED_WHO]  DEFAULT (suser_sname()) FOR [ADDED_WHO]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] ADD  CONSTRAINT [DF_TBREC_CFG_ACTION_ADDED_WHEN]  DEFAULT (sysdatetime()) FOR [ADDED_WHEN]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION]  WITH CHECK ADD  CONSTRAINT [FK_TBREC_CFG_ACTION_TBDD_CONNECTION] FOREIGN KEY([SQL_CONNECTION_ID])
REFERENCES [dbo].[TBDD_CONNECTION] ([GUID])
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] CHECK CONSTRAINT [FK_TBREC_CFG_ACTION_TBDD_CONNECTION]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION]  WITH CHECK ADD  CONSTRAINT [FK_TBREC_CFG_ACTION_TBREC_CFG_ENDPOINT] FOREIGN KEY([ENDPOINT_ID])
REFERENCES [dbo].[TBREC_CFG_ENDPOINT] ([GUID])
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] CHECK CONSTRAINT [FK_TBREC_CFG_ACTION_TBREC_CFG_ENDPOINT]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION]  WITH CHECK ADD  CONSTRAINT [FK_TBREC_CFG_ACTION_TBREC_CFG_ENDPOINT_AUTH] FOREIGN KEY([ENDPOINT_AUTH_ID])
REFERENCES [dbo].[TBREC_CFG_ENDPOINT_AUTH] ([GUID])
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] CHECK CONSTRAINT [FK_TBREC_CFG_ACTION_TBREC_CFG_ENDPOINT_AUTH]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Primärschlüssel' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'GUID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Profil-Referenz' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'PROFILE_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Ein-/Ausschalter' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'ACTIVE'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Reihenfolge im Profil' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'SEQUENCE'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Endpoint-Referenz' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'ENDPOINT_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Auth-Referenz' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'ENDPOINT_AUTH_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Parameterguppe' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'ENDPOINT_PARAMS_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'SQL-Verbindungs-Referenz' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'SQL_CONNECTION_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'HTTP-Methode (0 = NONE; 1 = GET; 2 = POST; 3 = PUT; 4 = PATCH; 5 = DELETE; 6 = HEAD; 7 = OPTIONS; 8 = CONNECT; 9 = TRACE)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'TYPE_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'SQL vor Request' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'PREPROCESSING_QUERY'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'SQL fuer Header-Erzeugung' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'HEADER_QUERY'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'SQL fuer Body-Erzeugung' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'BODY_QUERY'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'SQL nach Request' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'POSTPROCESSING_QUERY'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Fehlerbehandlung (0 = STOP; 1 = CONTINUE)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'ERROR_ACTION_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Angelegt von' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'ADDED_WHO'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Angelegt am' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'ADDED_WHEN'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Geaendert von' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'CHANGED_WHO'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Geaendert am' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ACTION', @level2type=N'COLUMN',@level2name=N'CHANGED_WHEN'
GO

--------------------------------------------------------------------------------------

CREATE OR ALTER TRIGGER [dbo].[TBREC_CFG_ACTION_AFT_UPD] ON [dbo].[TBREC_CFG_ACTION]
FOR UPDATE
AS
	UPDATE [TBREC_CFG_ACTION] 
	SET    [CHANGED_WHEN] = (SYSDATETIME())
	FROM   [INSERTED]
	WHERE  [TBREC_CFG_ACTION].[GUID] = [INSERTED].[GUID]
GO

ALTER TABLE [dbo].[TBREC_CFG_ACTION] ENABLE TRIGGER [TBREC_CFG_ACTION_AFT_UPD]
GO