Technical Article

AUTOGENERATE Stored procedures example

,

run the script as it is on your server.

inspect the database tables and structure. notice a table called 'gautengdata' that does not have a primary key.

inspect the store procs.

run:

USE [splogexample]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[_SPLOG_SPConstructor]

SELECT 'Return Value' = @return_value

GO

with default parameters

refresh the stored procedures

inspect the stored procedures.

think about this:

a true programmer knows that the path of least effort (resistance) is usually the road to rapid application development and successful delivery.

all of this means lots of money...

have fun!

Chris

USE master
GO

-- Drop the database if it already exists
IF EXISTS ( SELECT name
 FROM sys.databases
 WHERE name = N'splogexample' ) 
 DROP DATABASE splogexample
GO

CREATE DATABASE splogexample
GO
USE splogexample
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[_SPLOG_AutoGenStatements]')
 AND type IN ( N'P', N'PC' ) ) 
 BEGIN
 EXEC dbo.sp_executesql @statement = N'-- =============================================
--PART OF THE MACRO CODE EXECUTION BY CHRIS MORTON
--CONTACT CBMORTON@GMAIL.COM
--NO LIABLITITY CAN BE CLAIMED
--VERSION 1.1 BETA OCTOBER 23 2008
-- =============================================
CREATE PROCEDURE [dbo].[_SPLOG_AutoGenStatements]
@Tablename VARCHAR(50),
@AuthorName VARCHAR(50) = ''Chris Morton'',
@Prefix VARCHAR(3) = NULL,
@CreateLog BIT,
@TestMode BIT
AS 
BEGIN
DECLARE @ColumnParameter VARCHAR(4000)
DECLARE @CreatedDate VARCHAR(50)
SET @CreatedDate = CONVERT(VARCHAR(50), GETDATE(), 111) 
DECLARE @SPName VARCHAR(100)
DECLARE @SPHeader VARCHAR(200)
DECLARE @SPDescription VARCHAR(1000)
SET @SPDescription = ''-- =============================================''
+ CHAR(10) + CHAR(13) + ''-- AUTHOR: '' + @AuthorName + CHAR(10)
+ CHAR(13) + ''-- CREATED DATE: '' + @CreatedDate + CHAR(10)
+ CHAR(13)
+ ''-- AUTOGENERATED BY SQL CODE GENERATION TOOL VERSION 1.1 BETA - NO LIABILITY CAN BE CLAIMED FROM CHRIS MORTON - CONTACT cbmorton@gmail.com''
+ CHAR(10) + CHAR(13)
+ ''-- ============================================='' + CHAR(10)
+ CHAR(13)
DECLARE @Statement VARCHAR(4000)
DECLARE @SPText VARCHAR(8000)
DECLARE @ParameterName VARCHAR(100)
DECLARE @ParameterDataType VARCHAR(50)
DECLARE @ParameterList VARCHAR(2000)
DECLARE @WhereString VARCHAR(300)
DECLARE @PK VARCHAR(50)
SET @WhereString = CHAR(10) + CHAR(13) + ''WHERE ''
DECLARE @SelectStatement VARCHAR(4000)
SET @SelectStatement = ''SELECT''
DECLARE @InsertStatement VARCHAR(4000)
SET @InsertStatement = ''INSERT INTO''
DECLARE @InsertStatementValues VARCHAR(2000)
DECLARE @UsageUpdateStatement VARCHAR(150)
SET @UsageUpdateStatement = ''''

DECLARE @TableMetaData AS TABLE
(
ID INT IDENTITY(1, 1),
ColumnName VARCHAR(50) NOT NULL,
DataType VARCHAR(50) NOT NULL,
Length VARCHAR(4) NULL,
isPK BIT NULL
)

INSERT INTO @TableMetaData
(
ColumnName,
DataType,
Length,
isPK

)
SELECT a.Column_name AS columnname,
Data_type AS datatype,
Character_maximum_length AS length,
CASE b.table_name
WHEN b.table_name THEN 1
ELSE 0
END AS ispk
FROM INFORMATION_SCHEMA.columns a
LEFT JOIN INFORMATION_SCHEMA.key_column_usage b ON a.Column_name = b.Column_name
AND a.table_name = b.table_name
AND b.constraint_Name LIKE ''PK%''
WHERE a.table_name = @Tablename
DECLARE @NumColumns INT
SET @NumColumns = ( SELECT COUNT(1)
FROM @TableMetaData
)
DECLARE @ColumnName VARCHAR(50)
DECLARE @Counter INT 




--selectByID statement
-- SET @UsageUpdateStatement = CHAR(10) + CHAR(13)
-- + ''UPDATE [SPLOG] SET [USAGE] = [USAGE] + 1 WHERE SPNAME = ''''''

SET @ParameterList = ''''
SET @ParameterName = ''''
SET @SelectStatement = ''SELECT''
SET @PK = ( SELECT ColumnName
FROM @TableMetaData
WHERE ispk = 1
)
SET @Counter = 1
WHILE @Counter <= @NumColumns
BEGIN
SET @ColumnName = ( SELECT ColumnName
FROM @TableMetaData
WHERE ID = @Counter
)
SET @ParameterDataType = ISNULL(( SELECT DataType
+ ISNULL(CASE LOWER(Datatype)
WHEN ''varchar'' THEN ''('' + Length + '')''
WHEN ''char'' THEN ''('' + Length + '')''
WHEN ''varbinary'' THEN ''('' + Length + '')''
WHEN ''binary'' THEN ''('' + Length + '')''
WHEN ''nchar'' THEN ''('' + Length + '')''
WHEN ''nvarchar'' THEN ''('' + Length + '')''
WHEN ''decimal'' THEN ''('' + Length + '')''
END, '''')
FROM @TableMetaData
WHERE ID = @Counter
), ''datatype error'')




IF ( SELECT ispk
FROM @TableMetaData
WHERE id = @Counter
) = 1 
BEGIN
SET @ParameterName = ''@'' + @ColumnName + '' ''
+ @ParameterDataType + CHAR(10) + CHAR(13)
END
ELSE 
BEGIN
SET @ParameterName = ''--@'' + @ColumnName + '' ''
+ @ParameterDataType + CHAR(10) + CHAR(13)
END
SET @parameterlist = @parameterlist + @parametername


SET @Counter = @Counter + 1
END
SET @counter = 1
SET @WhereString = @WhereString + @PK + '' = @'' + @PK
SET @SPName = ISNULL(@Prefix, '''') + ''select'' + @TableName + ''ByID''
+ CHAR(10)
-- IF @CreateLog = 1 
-- BEGIN
-- SET @usageUpdateStatement = @usageUpdateStatement
-- + SUBSTRING(@SPName, 0, LEN(@SpName)) + ''''''''
-- END
-- ELSE 
-- BEGIN
-- SET @usageUpdateStatement = ''''
-- END
SET @SPHeader = ''CREATE PROCEDURE '' + @SPName + CHAR(10) + CHAR(13)
WHILE @Counter <= @NumColumns
BEGIN
SET @ColumnName = ISNULL(( SELECT ColumnName
FROM @TableMetaData
WHERE ID = @Counter
), ''column error'')
IF @Counter = @NumColumns 
BEGIN
SET @SelectStatement = @SelectStatement + CHAR(10)
+ CHAR(13) + '' ['' + @ColumnName + '']'' + +CHAR(10)
+ CHAR(13) + ''FROM '' + CHAR(10) + CHAR(13) + ''[''
+ @TableName + '']''
END
ELSE 
BEGIN
SET @SelectStatement = @SelectStatement + CHAR(10)
+ CHAR(13) + '' ['' + @ColumnName + ''],''
END
SET @Counter = @Counter + 1
END
SET @Statement = @SelectStatement + @WhereString
SET @SPText = ISNULL(@SPDescription, ''Description Error'')
+ ISNULL(@SPHeader, ''Header Error'') + ISNULL(@ParameterList, '''')
+ CHAR(10) + CHAR(13) + ''AS'' + CHAR(10) + CHAR(13) + ''BEGIN''
+ CHAR(10) + CHAR(13) + ISNULL(@Statement, ''Statement Error'')
+ @UsageUpdateStatement + CHAR(10) + CHAR(13) + ''END''
INSERT INTO #sp ( sptext, spname )
SELECT @SPText AS sptext,
@SPName AS spname


--select all statement
-- SET @UsageUpdateStatement = CHAR(10) + CHAR(13)
-- + ''UPDATE [SPLOG] SET [USAGE] = [USAGE] + 1 WHERE SPNAME = ''''''

SET @SelectStatement = ''SELECT''
SET @ParameterList = ''''
SET @ParameterName = ''''
SET @Counter = 1
SET @SPName = ISNULL(@Prefix, '''') + ''selectALL'' + @TableName + CHAR(10)
-- IF @CreateLog = 1 
-- BEGIN
-- SET @usageUpdateStatement = @usageUpdateStatement
-- + SUBSTRING(@SPName, 0, LEN(@SpName)) + ''''''''
-- END
-- ELSE 
-- BEGIN
-- SET @usageUpdateStatement = ''''
-- END
SET @SPHeader = ''CREATE PROCEDURE '' + @SPName + CHAR(13)
WHILE @Counter <= @NumColumns
BEGIN
SET @ColumnName = ( SELECT ColumnName
FROM @TableMetaData
WHERE ID = @Counter
)
SET @ParameterDataType = ISNULL(( SELECT DataType
+ ISNULL(CASE LOWER(Datatype)
WHEN ''varchar'' THEN ''('' + Length + '')''
WHEN ''char'' THEN ''('' + Length + '')''
WHEN ''varbinary'' THEN ''('' + Length + '')''
WHEN ''binary'' THEN ''('' + Length + '')''
WHEN ''nchar'' THEN ''('' + Length + '')''
WHEN ''nvarchar'' THEN ''('' + Length + '')''
WHEN ''decimal'' THEN ''('' + Length + '')''
END, '''')
FROM @TableMetaData
WHERE ID = @Counter
), ''datatype error'')
IF @Counter = @NumColumns 
BEGIN
SET @SelectStatement = @SelectStatement + CHAR(10)
+ CHAR(13) + '' ['' + @ColumnName + '']'' + +CHAR(10)
+ CHAR(13) + ''FROM '' + CHAR(10) + CHAR(13) + ''[''
+ @TableName + '']''
END
ELSE 
BEGIN
SET @SelectStatement = @SelectStatement + CHAR(10)
+ CHAR(13) + '' ['' + @ColumnName + ''],''
END
SET @Counter = @Counter + 1
END

SET @Counter = 1
WHILE @Counter <= @NumColumns
BEGIN
SET @ColumnName = ( SELECT ColumnName
FROM @TableMetaData
WHERE ID = @Counter
)
SET @ParameterDataType = ISNULL(( SELECT DataType
+ ISNULL(CASE LOWER(Datatype)
WHEN ''varchar'' THEN ''('' + Length + '')''
WHEN ''char'' THEN ''('' + Length + '')''
WHEN ''varbinary'' THEN ''('' + Length + '')''
WHEN ''binary'' THEN ''('' + Length + '')''
WHEN ''nchar'' THEN ''('' + Length + '')''
WHEN ''nvarchar'' THEN ''('' + Length + '')''
WHEN ''decimal'' THEN ''('' + Length + '')''
END, '''')
FROM @TableMetaData
WHERE ID = @Counter
), ''datatype error'') 
IF ( SELECT ispk
FROM @TableMetaData
WHERE id = @Counter
) = 1 
BEGIN
SET @ParameterName = ''--@'' + @ColumnName + '' ''
+ @ParameterDataType + CHAR(10) + CHAR(13)
END
ELSE 
BEGIN
SET @ParameterName = ''--@'' + @ColumnName + '' ''
+ @ParameterDataType + CHAR(10) + CHAR(13)
END
SET @parameterlist = @parameterlist + @parametername


SET @Counter = @Counter + 1
END
SET @Statement = @SelectStatement
SET @SPText = ISNULL(@SPDescription, ''Description Error'')
+ ISNULL(@SPHeader, ''Header Error'') + ISNULL(@ParameterList, '''')
+ CHAR(10) + CHAR(13) + ''AS'' + CHAR(10) + CHAR(13) + ''BEGIN''
+ CHAR(10) + CHAR(13) + ISNULL(@Statement, ''Statement Error'')
+ @UsageUpdateStatement + CHAR(10) + CHAR(13) + ''END''
INSERT INTO #sp ( sptext, spname )
SELECT @SPText AS sptext,
@SPName AS spname
--INSERT STATEMENT

-- SET @UsageUpdateStatement = CHAR(10) + CHAR(13)
-- + ''UPDATE [SPLOG] SET [USAGE] = [USAGE] + 1 WHERE SPNAME = ''''''

SET @SPName = ISNULL(@Prefix, '''') + ''insert'' + @TableName + CHAR(10)

-- IF @CreateLog = 1 
-- BEGIN
-- SET @usageUpdateStatement = @usageUpdateStatement
-- + SUBSTRING(@SPName, 0, LEN(@SpName)) + ''''''''
-- END
-- ELSE 
-- BEGIN
-- SET @usageUpdateStatement = ''''
-- END

SET @SPHeader = ''CREATE PROCEDURE '' + @SPName + CHAR(13)

SET @Counter = 1
WHILE @Counter <= @NumColumns
BEGIN
SET @ColumnName = ( SELECT ColumnName
FROM @TableMetaData
WHERE ID = @Counter
)
SET @ParameterDataType = ( SELECT DataType
+ ISNULL(CASE LOWER(Datatype)
WHEN ''varchar'' THEN ''('' + Length + '')''
WHEN ''char'' THEN ''('' + Length + '')''
WHEN ''varbinary'' THEN ''('' + Length + '')''
WHEN ''binary'' THEN ''('' + Length + '')''
WHEN ''nchar'' THEN ''('' + Length + '')''
WHEN ''nvarchar'' THEN ''('' + Length + '')''
WHEN ''decimal'' THEN ''('' + Length + '')''
END, '''')
FROM @TableMetaData
WHERE ID = @Counter
)



SET @ParameterName = ''@'' + @ColumnName 
IF @Counter = 1 
BEGIN

IF ( SELECT ispk
FROM @TableMetaData
WHERE id = @Counter
) = 1 
BEGIN

SET @InsertStatement = @InsertStatement + '' ''
+ ''['' + @TableName + '']'' + CHAR(10) + CHAR(13) + '' (''
+ CHAR(10) + CHAR(13) + ''--[''
+ @ColumnName + ''],'' + CHAR(10) + CHAR(13)

SET @InsertStatementValues = ''--''
+ @ParameterName + '','' + CHAR(10)
+ CHAR(13) 

SET @ParameterList = ''--'' + @ParameterName
+ '' '' + @ParameterDataType + '', ''
+ CHAR(10) + CHAR(13) 
END
ELSE 
BEGIN

SET @InsertStatement = @InsertStatement + '' ''
+ @TableName + CHAR(10) + CHAR(13) + '' (''
+ CHAR(10) + CHAR(13) + ''['' + @ColumnName
+ ''],'' + CHAR(10) + CHAR(13)

SET @InsertStatementValues = +@ParameterName
+ '','' + CHAR(10) + CHAR(13) 

SET @ParameterList = ''@'' + @ParameterName
+ '' '' + @ParameterDataType + '', ''
+ CHAR(10) + CHAR(13)
END 

END
IF @Counter > 1
and @Counter < @NumColumns 
BEGIN

IF ( SELECT ispk
FROM @TableMetaData
WHERE id = @Counter
) = 1 
BEGIN

SET @InsertStatement = @InsertStatement
+ '' --['' + @ColumnName + ''],'' + CHAR(10)
+ CHAR(13)

SET @InsertStatementValues = @InsertStatementValues
+ '' --'' + @ParameterName + '','' + CHAR(10)
+ CHAR(13)

SET @ParameterList = ''--'' + @ParameterName
+ '' '' + @ParameterDataType + '', ''
+ CHAR(10) + CHAR(13) 
END
ELSE 
BEGIN

SET @InsertStatement = @InsertStatement + '' [''
+ @ColumnName + ''],'' + CHAR(10) + CHAR(13) 
SET @InsertStatementValues = @InsertStatementValues
+ '' '' + @ParameterName + '','' + CHAR(10)
+ CHAR(13) 
SET @ParameterList = @ParameterList
+ @ParameterName + '' ''
+ @ParameterDataType + '','' + CHAR(10)
+ CHAR(13)
END


END
IF @Counter = @NumColumns 
BEGIN

IF ( SELECT ispk
FROM @TableMetaData
WHERE id = @Counter
) = 1 
BEGIN

SET @InsertStatement = SUBSTRING(@InsertStatement, 0, LEN(@InsertStatement) - 1)
+ CHAR(10) + CHAR(13) + '')'' + CHAR(10)
+ CHAR(13) + ''VALUES'' + CHAR(10) + CHAR(13)
+ ''(''
SET @InsertStatementValues = @InsertStatementValues
+ '')''
SET @ParameterList = SUBSTRING(@ParameterList, 0, LEN(@ParameterList) - 3)
+ ''--'' + @ParameterName + '' ''
+ @ParameterDataType 
END
ELSE 
BEGIN

SET @InsertStatement = @InsertStatement + ''[''
+ @ColumnName + ''])'' + CHAR(10) + CHAR(13)
+ ''VALUES'' + CHAR(10) + CHAR(13) + ''(''
SET @InsertStatementValues = @InsertStatementValues
+ '' '' + @ParameterName + '')''
SET @ParameterList = @ParameterList
+ @ParameterName + '' ''
+ @ParameterDataType
END

-- SET @InsertStatement = @InsertStatement + ''[''
-- + @ColumnName + ''])'' + CHAR(10) + CHAR(13)
-- + ''VALUES'' + CHAR(10) + CHAR(13) + ''(''
-- SET @InsertStatementValues = @InsertStatementValues
-- + '' '' + @ParameterName + '')''
-- SET @ParameterList = @ParameterList + @ParameterName
-- + '' '' + @ParameterDataType
END
SET @Counter = @Counter + 1
END

SET @Statement = @InsertStatement + @InsertStatementValues
SET @SPText = ISNULL(@SPDescription, ''Description Error'')
+ ISNULL(@SPHeader, ''Header Error'') + ISNULL(@ParameterList, '''')
+ CHAR(10) + CHAR(13) + ''AS'' + CHAR(10) + CHAR(13) + ''BEGIN''
+ CHAR(10) + CHAR(13) + ISNULL(@Statement, ''Statement Error'')
+ @UsageUpdateStatement + CHAR(10) + CHAR(13) + ''END''
INSERT INTO #sp ( sptext, spname )
SELECT @SPText AS sptext,
@SPName AS spname

--update statement

-- SET @UsageUpdateStatement = CHAR(10) + CHAR(13)
-- + ''UPDATE [SPLOG] SET [USAGE] = [USAGE] + 1 WHERE SPNAME = ''''''

SET @WhereString = CHAR(10) + CHAR(13) + ''WHERE ''
SET @ColumnParameter = ''''

SET @SPName = ISNULL(@Prefix, '''') + ''update'' + @TableName + CHAR(10)
-- IF @CreateLog = 1 
-- BEGIN
-- SET @usageUpdateStatement = @usageUpdateStatement
-- + SUBSTRING(@SPName, 0, LEN(@SpName)) + ''''''''
-- END
-- ELSE 
-- BEGIN
-- SET @usageUpdateStatement = ''''
-- END
SET @SPHeader = ''CREATE PROCEDURE '' + @SPName + CHAR(13)
DECLARE @UpdateStatement VARCHAR(8000)
SET @UpdateStatement = ''UPDATE ['' + @TableName + '']'' + CHAR(10) + CHAR(13)
+ ''SET ''
SET @Counter = 1
WHILE @Counter <= @NumColumns
BEGIN
SET @ColumnName = ( SELECT ColumnName
FROM @TableMetaData
WHERE ID = @Counter
)
SET @ParameterName = ''@'' + @ColumnName
SET @ParameterDataType = ( SELECT DataType
+ ISNULL(CASE LOWER(Datatype)
WHEN ''varchar'' THEN ''('' + Length + '')''
WHEN ''char'' THEN ''('' + Length + '')''
WHEN ''varbinary'' THEN ''('' + Length + '')''
WHEN ''binary'' THEN ''('' + Length + '')''
WHEN ''nchar'' THEN ''('' + Length + '')''
WHEN ''nvarchar'' THEN ''('' + Length + '')''
WHEN ''decimal'' THEN ''('' + Length + '')''
END, '''')
FROM @TableMetaData
WHERE ID = @Counter
)
IF @Counter = 1 
BEGIN
SET @ParameterList = @ParameterName + '' ''
+ @ParameterDataType + '', '' + CHAR(10) + CHAR(13) 
END
IF @Counter = @NumColumns 
BEGIN
SET @ColumnParameter = @ColumnParameter + @ColumnName
+ '' = '' + @ParameterName + CHAR(10) + CHAR(13)
SET @ParameterList = @ParameterList + @ParameterName
+ '' '' + @ParameterDataType + CHAR(10) + CHAR(13)
END


IF @Counter > 1
and @Counter < @NumColumns 
BEGIN
SET @ColumnParameter = @ColumnParameter + @ColumnName
+ '' = '' + @ParameterName + '','' + CHAR(10)
+ CHAR(13)
SET @ParameterList = @ParameterList + @ParameterName
+ '' '' + @ParameterDataType + '','' + CHAR(10)
+ CHAR(13)
END
SET @Counter = @Counter + 1
END

SET @PK = ( SELECT ColumnName
FROM @TableMetaData
WHERE ispk = 1
)
SET @WhereString = @WhereString + @PK + '' = @'' + @PK 
SET @Statement = @UpdateStatement + @ColumnParameter + @WhereString
SET @SPText = ISNULL(@SPDescription, ''Description Error'')
+ ISNULL(@SPHeader, ''Header Error'') + ISNULL(@ParameterList, '''')
+ CHAR(10) + CHAR(13) + ''AS'' + CHAR(10) + CHAR(13) + ''BEGIN''
+ CHAR(10) + CHAR(13) + ISNULL(@Statement, ''Statement Error'')
+ @UsageUpdateStatement + CHAR(10) + CHAR(13) + ''END''
INSERT INTO #sp ( sptext, spname )
SELECT @SPText AS sptext,
@SPName AS spname
END












' 
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[DocumentType]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[DocumentType]
 (
 [DocumentTypeID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [DocumentTypeName] [varchar](50) NOT NULL,
 [VirtualDirectory] [varchar](250) NOT NULL,
 [URL] [varchar](250) NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_DocumentType] PRIMARY KEY CLUSTERED ( [DocumentTypeID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Message]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Message]
 (
 [MessageID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [Text] [varchar](8000) NOT NULL,
 [Subject] [varchar](200) NOT NULL,
 [Title] [varchar](50) NOT NULL,
 [Class] [varchar](50) NULL,
 CONSTRAINT [PK_Message] PRIMARY KEY CLUSTERED ( [MessageID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Errors]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Errors]
 (
 [ErrorID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [UserID] [uniqueidentifier] NOT NULL,
 [ErrorMessage] [bigint] NOT NULL,
 [IPAddress] [char](12) NULL,
 [LogDate] [datetime] NOT NULL,
 [Hostname] [varchar](50) NULL,
 [Logged] [bit] NOT NULL,
 [ClearLogDate] [datetime] NULL,
 CONSTRAINT [PK_Errors] PRIMARY KEY CLUSTERED ( [ErrorID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[FeedbackQuestion]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[FeedbackQuestion]
 (
 [FeedbackQuestionID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [QuestionText] [varchar](200) NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_FeedbackQuestion] PRIMARY KEY CLUSTERED ( [FeedbackQuestionID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[FeedbackCategory]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[FeedbackCategory]
 (
 [FeedbackCategoryID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [FeedbackCategoryName] [varchar](50) NOT NULL,
 CONSTRAINT [PK_FeedbackCategory] PRIMARY KEY CLUSTERED ( [FeedbackCategoryID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Action]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Action]
 (
 [ActionID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [ActionName] [varchar](50) NOT NULL,
 CONSTRAINT [PK_Action] PRIMARY KEY CLUSTERED ( [ActionID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Manufacturer]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Manufacturer]
 (
 [ManufacturerID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [ManufacturerName] [varchar](50) NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_Manufacturer] PRIMARY KEY CLUSTERED ( [ManufacturerID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[AttributeType]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[AttributeType]
 (
 [AttributeTypeID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [AttributeTypeName] [varchar](50) NOT NULL,
 CONSTRAINT [PK_AttributeType] PRIMARY KEY CLUSTERED ( [AttributeTypeID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Contract]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Contract]
 (
 [ContractID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [ContractName] [varchar](50) NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_Contracts] PRIMARY KEY CLUSTERED ( [ContractID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Address]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Address]
 (
 [AddressID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [CityID] [bigint] NOT NULL,
 [ProvinceID] [bigint] NOT NULL,
 [BusinessCustomer] [bit] NOT NULL,
 [Address1] [varchar](50) NOT NULL,
 [Address2] [varchar](50) NULL,
 [Address3] [varchar](50) NULL,
 [PostalCode] [char](4) NOT NULL,
 [PhoneNumber] [char](10) NULL,
 CONSTRAINT [PK_Address] PRIMARY KEY CLUSTERED ( [AddressID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[AnalysisCode]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[AnalysisCode]
 (
 [AnalysisCodeID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [AnalysisCode] [varchar](10) NULL,
 CONSTRAINT [PK_AnalysisCode] PRIMARY KEY CLUSTERED ( [AnalysisCodeID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Channel]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Channel]
 (
 [ChannelID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [Channel] [varchar](6) NULL,
 CONSTRAINT [PK_Channel] PRIMARY KEY CLUSTERED ( [ChannelID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[gautengdata]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[gautengdata]
 (
 [ID] [int] IDENTITY(1, 1)
 NOT NULL,
 [AccNo] [nvarchar](255) NULL,
 [AccName] [nvarchar](255) NULL,
 [Surname] [nvarchar](255) NULL,
 [Msisdn] [nvarchar](255) NULL,
 [AnalysisCode] [nvarchar](255) NULL,
 [Tarplan] [nvarchar](255) NULL,
 [PackageDescription] [nvarchar](255) NULL,
 [ActivationDate] [datetime] NULL,
 [ContractStart] [datetime] NULL,
 [ContractTermDate] [datetime] NULL,
 [EligibleUpgradeDate] [datetime] NULL,
 [LastUpgDate] [datetime] NULL,
 [ContactNo] [nvarchar](255) NULL,
 [Channel] [nvarchar](255) NULL,
 [DealerID] [nvarchar](255) NULL,
 [PC4] [nvarchar](255) NULL,
 [PostalCode] [nvarchar](255) NULL,
 [Region] [nvarchar](255) NULL
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Dealer]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Dealer]
 (
 [DealerID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [Dealer] [varchar](6) NULL,
 CONSTRAINT [PK_Dealer] PRIMARY KEY CLUSTERED ( [DealerID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[ApplicationDocumentBatch]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[ApplicationDocumentBatch]
 (
 [ApplicationDocumentBatchID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [CreatedDate] [datetime] NOT NULL,
 [DownloadedDate] [datetime] NULL,
 [ProcessedDate] [datetime] NULL,
 [Processed] [bit] NOT NULL,
 [Pending] [bit] NULL,
 [Filename] [varchar](100) NOT NULL,
 CONSTRAINT [PK_ApplicationDocumentBatch] PRIMARY KEY CLUSTERED ( [ApplicationDocumentBatchID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Status]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Status]
 (
 [StatusID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [StatusName] [varchar](50) NOT NULL,
 CONSTRAINT [PK_Status] PRIMARY KEY CLUSTERED ( [StatusID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Document]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Document]
 (
 [DocumentID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [DocumentTypeID] [bigint] NOT NULL,
 [FileName] [varchar](100) NOT NULL,
 [UploadedDate] [datetime] NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_Document] PRIMARY KEY CLUSTERED ( [DocumentID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[ApplicationDocument]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[ApplicationDocument]
 (
 [ApplicationDocumentID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [ApplicationID] [bigint] NOT NULL,
 [DocumentID] [bigint] NOT NULL,
 [DocumentTypeID] [bigint] NOT NULL,
 [ApplicationDocumentBatchID] [bigint] NULL,
 [UserID] [uniqueidentifier] NOT NULL,
 [SentDate] [datetime] NULL,
 [RecievedDate] [datetime] NULL,
 [FileName] [varchar](100) NULL,
 [Processed] [bit] NOT NULL,
 [Pending] [bit] NOT NULL,
 CONSTRAINT [PK_ApplicationDocument] PRIMARY KEY CLUSTERED ( [ApplicationDocumentID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[CommunicationHistory]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[CommunicationHistory]
 (
 [CommunicationHistoryID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [MessageID] [bigint] NOT NULL,
 [ApplicationID] [bigint] NOT NULL,
 [StatusID] [bigint] NOT NULL,
 [LogDate] [datetime] NOT NULL,
 [EmailBody] [varchar](8000) NULL,
 [FaxBody] [varchar](8000) NULL,
 [Pending] [bit] NOT NULL,
 CONSTRAINT [PK_CommunicationHistory] PRIMARY KEY CLUSTERED ( [CommunicationHistoryID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[MessageDocument]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[MessageDocument]
 (
 [MessageDocumentID] [bigint] NOT NULL,
 [DocumentID] [bigint] NOT NULL,
 [MessageID] [bigint] NOT NULL,
 CONSTRAINT [PK_AttachmentGroup] PRIMARY KEY CLUSTERED ( [MessageDocumentID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[CustomerFeedbackQuestionResponse]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[CustomerFeedbackQuestionResponse]
 (
 [FeedbackResponseID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [CustomerFeedbackID] [bigint] NOT NULL,
 [FeedbackQuestionID] [bigint] NOT NULL,
 [CustomerResponse] [varchar](1000) NOT NULL,
 CONSTRAINT [PK_CustomerFeedbackQuestionResponse] PRIMARY KEY CLUSTERED ( [FeedbackResponseID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[CustomerQuestion]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[CustomerQuestion]
 (
 [CustomerQuestionID] [bigint] NOT NULL,
 [CustomerFeedbackID] [bigint] NOT NULL,
 [FeedbackCategoryID] [bigint] NOT NULL,
 [Question] [varchar](1000) NOT NULL,
 CONSTRAINT [PK_CustomerQuestions] PRIMARY KEY CLUSTERED ( [CustomerQuestionID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[ApplicationHistory]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[ApplicationHistory]
 (
 [ApplicationHistoryID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [ApplicationID] [bigint] NOT NULL,
 [AgentID] [uniqueidentifier] NOT NULL,
 [StatusID] [bigint] NOT NULL,
 [ActionID] [bigint] NOT NULL,
 [LogDate] [datetime] NOT NULL,
 [Note] [varchar](500) NULL,
 CONSTRAINT [PK_ApplicationHistory] PRIMARY KEY CLUSTERED ( [ApplicationHistoryID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Phone]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Phone]
 (
 [PhoneID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [ManufacturerID] [bigint] NOT NULL,
 [PhoneName] [varchar](50) NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_Phone] PRIMARY KEY CLUSTERED ( [PhoneID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Attribute]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Attribute]
 (
 [AttributeID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [AttributeTypeID] [bigint] NOT NULL,
 [AttributeName] [varchar](50) NOT NULL,
 [Description] [varchar](200) NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_Attributes] PRIMARY KEY CLUSTERED ( [AttributeID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[ContractAttribute]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[ContractAttribute]
 (
 [ContractAttributeID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [ContractID] [bigint] NOT NULL,
 [AttributeID] [bigint] NOT NULL,
 [Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_ContractAttribute] PRIMARY KEY CLUSTERED ( [ContractAttributeID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Application]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Application]
 (
 [ApplicationID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [CustomerID] [bigint] NOT NULL,
 [AgentID] [uniqueidentifier] NOT NULL,
 [StatusID] [bigint] NOT NULL,
 [LogDate] [datetime] NOT NULL,
 [ExistingContractID] [bigint] NOT NULL,
 [ExistingPhoneID] [bigint] NOT NULL,
 CONSTRAINT [PK_Application] PRIMARY KEY CLUSTERED ( [ApplicationID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Company]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Company]
 (
 [CompanyID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [AddressID] [bigint] NOT NULL,
 [CompanyName] [varchar](50) NOT NULL,
 [ContactName] [varchar](100) NULL,
 [ContactNumber] [char](10) NULL,
 CONSTRAINT [PK_Company] PRIMARY KEY CLUSTERED ( [CompanyID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[Customer]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[Customer]
 (
 [CustomerID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [AnalysisCodeID] [bigint] NOT NULL,
 [DealerID] [bigint] NOT NULL,
 [ChannelID] [bigint] NOT NULL,
 [ProvinceID] [bigint] NOT NULL,
 [AccountHolder] [varchar](150) NOT NULL,
 [AccountName] [varchar](150) NOT NULL,
 [AccountNumber] [varchar](150) NOT NULL,
 [TarriffPlan] [varchar](200) NULL,
 [PackageDescription] [varchar](200) NULL,
 [VodacomNumber] [char](10) NOT NULL,
 [EmailAddress] [varchar](50) NULL,
 [ContactDetails] [varchar](200) NULL,
 [FirstName] [varchar](50) NULL,
 [LastName] [varchar](50) NULL,
 [IDNumber] [char](13) NULL,
 [PostalCode] [varchar](50) NULL,
 [PC4] [varchar](50) NULL,
 [EligibleUpgradeDate] [datetime] NULL,
 [ContractTermDate] [datetime] NULL,
 [ContractStart] [datetime] NULL,
 [ActivationDate] [datetime] NULL,
 [LastUpgradeDate] [datetime] NULL,
 [ImportedDate] [datetime] NOT NULL
 CONSTRAINT [DF_Customer_ImportedDate] DEFAULT ( GETDATE() ),
 [LastModifiedDate] [datetime] NULL,
 [LastModifiedBy] [uniqueidentifier] NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ( [CustomerID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[CallLog]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[CallLog]
 (
 [CallLogID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [CustomerID] [bigint] NOT NULL,
 [AgentID] [uniqueidentifier] NOT NULL,
 [CreatedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_CallLog] PRIMARY KEY CLUSTERED ( [CallLogID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[CustomerFeedback]')
 AND type IN ( N'U' ) ) 
 BEGIN
 CREATE TABLE [dbo].[CustomerFeedback]
 (
 [CustomerFeedbackID] [bigint] IDENTITY(1, 1)
 NOT NULL,
 [CustomerID] [bigint] NOT NULL,
 [AgentID] [uniqueidentifier] NOT NULL,
 [LogDate] [datetime] NOT NULL,
 CONSTRAINT [PK_CustomerFeedback] PRIMARY KEY CLUSTERED ( [CustomerFeedbackID] ASC )
 WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF ) ON [PRIMARY]
 )
 ON [PRIMARY]
 END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS ( SELECT *
 FROM sys.objects
 WHERE object_id = OBJECT_ID(N'[dbo].[_SPLOG_SPConstructor]')
 AND type IN ( N'P', N'PC' ) ) 
 BEGIN
 EXEC dbo.sp_executesql @statement = N'-- =============================================
--PART OF THE MACRO CODE EXECUTION BY CHRIS MORTON
--CONTACT CBMORTON@GMAIL.COM
--NO LIABLITITY CAN BE CLAIMED
--VERSION 1.1 BETA OCTOBER 23 2008
-- =============================================
CREATE PROCEDURE [dbo].[_SPLOG_SPConstructor] 
@AuthorName VARCHAR(50) = ''Chris Morton'',
@CreateLog BIT = 1,
@Prefix VARCHAR(3) = NULL,
@TestMode BIT = 0
AS 
BEGIN

CREATE TABLE #sp
(
spid BIGINT IDENTITY(1, 1),
sptext TEXT NOT NULL,
spname VARCHAR(100) NOT NULL
)
    
DECLARE @tables TABLE
(
id BIGINT IDENTITY(1, 1),
[table] VARCHAR(50)
)
    
INSERT INTO @tables ( [table] )
SELECT TABLE_NAME AS [table]
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_TYPE = ''BASE TABLE''
AND TABLE_NAME <> ''sysdiagrams''
AND table_name <> ''SPLOG''
    
    
DECLARE @tablename VARCHAR(50)
DECLARE @Counter BIGINT 
SET @counter = 1
WHILE @counter <= ( SELECT MAX(id)
FROM @tables
)
BEGIN
    
SET @tablename = ( SELECT [TABLE]
FROM @tables
WHERE id = @counter
) 
    
EXEC dbo._SPLOG_AutoGenStatements @tablename, @AuthorName, @Prefix, @CreateLog, @TestMode 
    
SET @Counter = @counter + 1
    
END
    
DECLARE @sqlstring VARCHAR(8000)
DECLARE @spname VARCHAR(100)
SET @counter = 1
WHILE @counter <= ( SELECT MAX(spid)
FROM #sp
)
BEGIN
    
SET @sqlstring = ( SELECT sptext
FROM #sp
WHERE spid = @counter
) 
SET @spname = ( SELECT spname
FROM #sp
WHERE spid = @counter
)
    
BEGIN TRY
    
IF @testmode = 0 
BEGIN
EXEC ( @sqlstring
)
PRINT ''The stored procedure '' + @spname
+ '' was created successfully on ''
+ CONVERT(VARCHAR, GETDATE())
END
ELSE 
BEGIN
PRINT ''The stored procedure '' + @spname
+ '' was constructed successfully on ''
+ CONVERT(VARCHAR, GETDATE())
END
END TRY
BEGIN CATCH
DECLARE @errormessage VARCHAR(200)
SET @errormessage = ( SELECT ERROR_MESSAGE()
)
PRINT ''The stored procedure '' + @spname
+ '' was not created.'' + CHAR(10) + CHAR(13)
+ ''The Error was:'' + CHAR(10) + CHAR(13)
+ @errormessage
END CATCH
    
    
    
SET @counter = @counter + 1
END
    
IF @CreateLog = 1 
BEGIN
    
IF NOT EXISTS ( SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = ''SPLOG'' ) 
BEGIN
CREATE TABLE SPLOG
(
spid BIGINT IDENTITY(1, 1),
sptext TEXT NOT NULL,
spname VARCHAR(200) NOT NULL,
createddate DATETIME DEFAULT GETDATE(),
usage BIGINT NOT NULL DEFAULT 0
)
    
INSERT INTO splog ( sptext, spname )
SELECT sptext,
RTRIM(spname)
FROM #sp
    
    
END
ELSE 
BEGIN
    
INSERT INTO splog ( sptext, spname )
SELECT sptext,
spname
FROM #sp
    
END
    
END
    
DROP TABLE #sp
    
END







' 
 END
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Document_DocumentType]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Document]') ) 
 ALTER TABLE [dbo].[Document]
 WITH CHECK
 ADD CONSTRAINT [FK_Document_DocumentType] FOREIGN KEY ( [DocumentTypeID] ) REFERENCES [dbo].[DocumentType] ( [DocumentTypeID] )
GO
ALTER TABLE [dbo].[Document]
 CHECK CONSTRAINT [FK_Document_DocumentType]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ApplicationDocument_Application]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ApplicationDocument]') ) 
 ALTER TABLE [dbo].[ApplicationDocument]
 WITH CHECK
 ADD CONSTRAINT [FK_ApplicationDocument_Application] FOREIGN KEY ( [ApplicationID] ) REFERENCES [dbo].[Application] ( [ApplicationID] )
GO
ALTER TABLE [dbo].[ApplicationDocument]
 CHECK CONSTRAINT [FK_ApplicationDocument_Application]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ApplicationDocument_ApplicationDocumentBatch]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ApplicationDocument]') ) 
 ALTER TABLE [dbo].[ApplicationDocument]
 WITH CHECK
 ADD CONSTRAINT [FK_ApplicationDocument_ApplicationDocumentBatch] FOREIGN KEY ( [ApplicationDocumentBatchID] ) REFERENCES [dbo].[ApplicationDocumentBatch] ( [ApplicationDocumentBatchID] )
GO
ALTER TABLE [dbo].[ApplicationDocument]
 CHECK CONSTRAINT [FK_ApplicationDocument_ApplicationDocumentBatch]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ApplicationDocument_Document]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ApplicationDocument]') ) 
 ALTER TABLE [dbo].[ApplicationDocument]
 WITH CHECK
 ADD CONSTRAINT [FK_ApplicationDocument_Document] FOREIGN KEY ( [DocumentID] ) REFERENCES [dbo].[Document] ( [DocumentID] )
GO
ALTER TABLE [dbo].[ApplicationDocument]
 CHECK CONSTRAINT [FK_ApplicationDocument_Document]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ApplicationDocument_DocumentType]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ApplicationDocument]') ) 
 ALTER TABLE [dbo].[ApplicationDocument]
 WITH CHECK
 ADD CONSTRAINT [FK_ApplicationDocument_DocumentType] FOREIGN KEY ( [DocumentTypeID] ) REFERENCES [dbo].[DocumentType] ( [DocumentTypeID] )
GO
ALTER TABLE [dbo].[ApplicationDocument]
 CHECK CONSTRAINT [FK_ApplicationDocument_DocumentType]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CommunicationHistory_Application]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CommunicationHistory]') ) 
 ALTER TABLE [dbo].[CommunicationHistory]
 WITH CHECK
 ADD CONSTRAINT [FK_CommunicationHistory_Application] FOREIGN KEY ( [ApplicationID] ) REFERENCES [dbo].[Application] ( [ApplicationID] )
GO
ALTER TABLE [dbo].[CommunicationHistory]
 CHECK CONSTRAINT [FK_CommunicationHistory_Application]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CommunicationHistory_Message]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CommunicationHistory]') ) 
 ALTER TABLE [dbo].[CommunicationHistory]
 WITH CHECK
 ADD CONSTRAINT [FK_CommunicationHistory_Message] FOREIGN KEY ( [MessageID] ) REFERENCES [dbo].[Message] ( [MessageID] )
GO
ALTER TABLE [dbo].[CommunicationHistory]
 CHECK CONSTRAINT [FK_CommunicationHistory_Message]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CommunicationHistory_Status]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CommunicationHistory]') ) 
 ALTER TABLE [dbo].[CommunicationHistory]
 WITH CHECK
 ADD CONSTRAINT [FK_CommunicationHistory_Status] FOREIGN KEY ( [StatusID] ) REFERENCES [dbo].[Status] ( [StatusID] )
GO
ALTER TABLE [dbo].[CommunicationHistory]
 CHECK CONSTRAINT [FK_CommunicationHistory_Status]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_MessageDocument_Document]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[MessageDocument]') ) 
 ALTER TABLE [dbo].[MessageDocument]
 WITH CHECK
 ADD CONSTRAINT [FK_MessageDocument_Document] FOREIGN KEY ( [DocumentID] ) REFERENCES [dbo].[Document] ( [DocumentID] )
GO
ALTER TABLE [dbo].[MessageDocument]
 CHECK CONSTRAINT [FK_MessageDocument_Document]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_MessageDocument_Message]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[MessageDocument]') ) 
 ALTER TABLE [dbo].[MessageDocument]
 WITH CHECK
 ADD CONSTRAINT [FK_MessageDocument_Message] FOREIGN KEY ( [MessageID] ) REFERENCES [dbo].[Message] ( [MessageID] )
GO
ALTER TABLE [dbo].[MessageDocument]
 CHECK CONSTRAINT [FK_MessageDocument_Message]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CustomerFeedbackQuestionResponse_CustomerFeedback]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CustomerFeedbackQuestionResponse]') ) 
 ALTER TABLE [dbo].[CustomerFeedbackQuestionResponse]
 WITH CHECK
 ADD CONSTRAINT [FK_CustomerFeedbackQuestionResponse_CustomerFeedback] FOREIGN KEY ( [CustomerFeedbackID] ) REFERENCES [dbo].[CustomerFeedback] ( [CustomerFeedbackID] )
GO
ALTER TABLE [dbo].[CustomerFeedbackQuestionResponse]
 CHECK CONSTRAINT [FK_CustomerFeedbackQuestionResponse_CustomerFeedback]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CustomerFeedbackQuestionResponse_FeedbackQuestion]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CustomerFeedbackQuestionResponse]') ) 
 ALTER TABLE [dbo].[CustomerFeedbackQuestionResponse]
 WITH CHECK
 ADD CONSTRAINT [FK_CustomerFeedbackQuestionResponse_FeedbackQuestion] FOREIGN KEY ( [FeedbackQuestionID] ) REFERENCES [dbo].[FeedbackQuestion] ( [FeedbackQuestionID] )
GO
ALTER TABLE [dbo].[CustomerFeedbackQuestionResponse]
 CHECK CONSTRAINT [FK_CustomerFeedbackQuestionResponse_FeedbackQuestion]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CustomerQuestions_CustomerFeedback]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CustomerQuestion]') ) 
 ALTER TABLE [dbo].[CustomerQuestion]
 WITH CHECK
 ADD CONSTRAINT [FK_CustomerQuestions_CustomerFeedback] FOREIGN KEY ( [CustomerFeedbackID] ) REFERENCES [dbo].[CustomerFeedback] ( [CustomerFeedbackID] )
GO
ALTER TABLE [dbo].[CustomerQuestion]
 CHECK CONSTRAINT [FK_CustomerQuestions_CustomerFeedback]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CustomerQuestions_FeedbackCategory]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CustomerQuestion]') ) 
 ALTER TABLE [dbo].[CustomerQuestion]
 WITH CHECK
 ADD CONSTRAINT [FK_CustomerQuestions_FeedbackCategory] FOREIGN KEY ( [FeedbackCategoryID] ) REFERENCES [dbo].[FeedbackCategory] ( [FeedbackCategoryID] )
GO
ALTER TABLE [dbo].[CustomerQuestion]
 CHECK CONSTRAINT [FK_CustomerQuestions_FeedbackCategory]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ApplicationHistory_Action]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ApplicationHistory]') ) 
 ALTER TABLE [dbo].[ApplicationHistory]
 WITH CHECK
 ADD CONSTRAINT [FK_ApplicationHistory_Action] FOREIGN KEY ( [ActionID] ) REFERENCES [dbo].[Action] ( [ActionID] )
GO
ALTER TABLE [dbo].[ApplicationHistory]
 CHECK CONSTRAINT [FK_ApplicationHistory_Action]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ApplicationHistory_Application]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ApplicationHistory]') ) 
 ALTER TABLE [dbo].[ApplicationHistory]
 WITH CHECK
 ADD CONSTRAINT [FK_ApplicationHistory_Application] FOREIGN KEY ( [ApplicationID] ) REFERENCES [dbo].[Application] ( [ApplicationID] )
GO
ALTER TABLE [dbo].[ApplicationHistory]
 CHECK CONSTRAINT [FK_ApplicationHistory_Application]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ApplicationHistory_Status]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ApplicationHistory]') ) 
 ALTER TABLE [dbo].[ApplicationHistory]
 WITH CHECK
 ADD CONSTRAINT [FK_ApplicationHistory_Status] FOREIGN KEY ( [StatusID] ) REFERENCES [dbo].[Status] ( [StatusID] )
GO
ALTER TABLE [dbo].[ApplicationHistory]
 CHECK CONSTRAINT [FK_ApplicationHistory_Status]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Phone_Manufacturer]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Phone]') ) 
 ALTER TABLE [dbo].[Phone]
 WITH CHECK
 ADD CONSTRAINT [FK_Phone_Manufacturer] FOREIGN KEY ( [ManufacturerID] ) REFERENCES [dbo].[Manufacturer] ( [ManufacturerID] )
GO
ALTER TABLE [dbo].[Phone]
 CHECK CONSTRAINT [FK_Phone_Manufacturer]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Attribute_AttributeType]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Attribute]') ) 
 ALTER TABLE [dbo].[Attribute]
 WITH CHECK
 ADD CONSTRAINT [FK_Attribute_AttributeType] FOREIGN KEY ( [AttributeTypeID] ) REFERENCES [dbo].[AttributeType] ( [AttributeTypeID] )
GO
ALTER TABLE [dbo].[Attribute]
 CHECK CONSTRAINT [FK_Attribute_AttributeType]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ContractAttribute_Attribute]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ContractAttribute]') ) 
 ALTER TABLE [dbo].[ContractAttribute]
 WITH CHECK
 ADD CONSTRAINT [FK_ContractAttribute_Attribute] FOREIGN KEY ( [AttributeID] ) REFERENCES [dbo].[Attribute] ( [AttributeID] )
GO
ALTER TABLE [dbo].[ContractAttribute]
 CHECK CONSTRAINT [FK_ContractAttribute_Attribute]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_ContractAttribute_Contract]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[ContractAttribute]') ) 
 ALTER TABLE [dbo].[ContractAttribute]
 WITH CHECK
 ADD CONSTRAINT [FK_ContractAttribute_Contract] FOREIGN KEY ( [ContractID] ) REFERENCES [dbo].[Contract] ( [ContractID] )
GO
ALTER TABLE [dbo].[ContractAttribute]
 CHECK CONSTRAINT [FK_ContractAttribute_Contract]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Application_Contract]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Application]') ) 
 ALTER TABLE [dbo].[Application]
 WITH CHECK
 ADD CONSTRAINT [FK_Application_Contract] FOREIGN KEY ( [ExistingContractID] ) REFERENCES [dbo].[Contract] ( [ContractID] )
GO
ALTER TABLE [dbo].[Application]
 CHECK CONSTRAINT [FK_Application_Contract]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Application_Customer]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Application]') ) 
 ALTER TABLE [dbo].[Application]
 WITH CHECK
 ADD CONSTRAINT [FK_Application_Customer] FOREIGN KEY ( [CustomerID] ) REFERENCES [dbo].[Customer] ( [CustomerID] )
GO
ALTER TABLE [dbo].[Application]
 CHECK CONSTRAINT [FK_Application_Customer]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Application_Phone]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Application]') ) 
 ALTER TABLE [dbo].[Application]
 WITH CHECK
 ADD CONSTRAINT [FK_Application_Phone] FOREIGN KEY ( [ExistingPhoneID] ) REFERENCES [dbo].[Phone] ( [PhoneID] )
GO
ALTER TABLE [dbo].[Application]
 CHECK CONSTRAINT [FK_Application_Phone]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Application_Status]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Application]') ) 
 ALTER TABLE [dbo].[Application]
 WITH CHECK
 ADD CONSTRAINT [FK_Application_Status] FOREIGN KEY ( [StatusID] ) REFERENCES [dbo].[Status] ( [StatusID] )
GO
ALTER TABLE [dbo].[Application]
 CHECK CONSTRAINT [FK_Application_Status]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Company_Address]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Company]') ) 
 ALTER TABLE [dbo].[Company]
 WITH CHECK
 ADD CONSTRAINT [FK_Company_Address] FOREIGN KEY ( [AddressID] ) REFERENCES [dbo].[Address] ( [AddressID] )
GO
ALTER TABLE [dbo].[Company]
 CHECK CONSTRAINT [FK_Company_Address]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Customer_AnalysisCode]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Customer]') ) 
 ALTER TABLE [dbo].[Customer]
 WITH CHECK
 ADD CONSTRAINT [FK_Customer_AnalysisCode] FOREIGN KEY ( [AnalysisCodeID] ) REFERENCES [dbo].[AnalysisCode] ( [AnalysisCodeID] )
GO
ALTER TABLE [dbo].[Customer]
 CHECK CONSTRAINT [FK_Customer_AnalysisCode]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Customer_Channel]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Customer]') ) 
 ALTER TABLE [dbo].[Customer]
 WITH CHECK
 ADD CONSTRAINT [FK_Customer_Channel] FOREIGN KEY ( [ChannelID] ) REFERENCES [dbo].[Channel] ( [ChannelID] )
GO
ALTER TABLE [dbo].[Customer]
 CHECK CONSTRAINT [FK_Customer_Channel]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_Customer_Dealer]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[Customer]') ) 
 ALTER TABLE [dbo].[Customer]
 WITH CHECK
 ADD CONSTRAINT [FK_Customer_Dealer] FOREIGN KEY ( [DealerID] ) REFERENCES [dbo].[Dealer] ( [DealerID] )
GO
ALTER TABLE [dbo].[Customer]
 CHECK CONSTRAINT [FK_Customer_Dealer]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CallLog_Customer]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CallLog]') ) 
 ALTER TABLE [dbo].[CallLog]
 WITH CHECK
 ADD CONSTRAINT [FK_CallLog_Customer] FOREIGN KEY ( [CustomerID] ) REFERENCES [dbo].[Customer] ( [CustomerID] )
GO
ALTER TABLE [dbo].[CallLog]
 CHECK CONSTRAINT [FK_CallLog_Customer]
GO
IF NOT EXISTS ( SELECT *
 FROM sys.foreign_keys
 WHERE object_id = OBJECT_ID(N'[dbo].[FK_CustomerFeedback_Customer]')
 AND parent_object_id = OBJECT_ID(N'[dbo].[CustomerFeedback]') ) 
 ALTER TABLE [dbo].[CustomerFeedback]
 WITH CHECK
 ADD CONSTRAINT [FK_CustomerFeedback_Customer] FOREIGN KEY ( [CustomerID] ) REFERENCES [dbo].[Customer] ( [CustomerID] )
GO
ALTER TABLE [dbo].[CustomerFeedback]
 CHECK CONSTRAINT [FK_CustomerFeedback_Customer]

Rate

3.5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

3.5 (2)

You rated this post out of 5. Change rating