IF (NOT EXISTS 
	(SELECT * FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_NAME = 'pal_SMSCredit'))
BEGIN
	CREATE TABLE [dbo].[pal_SMSCredit](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[customerCode] [varchar](50) NULL,
		[currentBalance]  AS (isnull([dbo].[fn_GetSMSCreditBalance]([customerCode]),(0))),
		[threshold] [int] NULL,
		[allocatedBalance]  AS (isnull([dbo].[fn_GetSMSCreditAllocatedBalance]([customerCode]),(0))),
		[availableBalance]  AS (isnull([dbo].[fn_GetSMSCreditBalance]([customerCode]),(0))-isnull([dbo].[fn_GetSMSCreditAllocatedBalance]([customerCode]),(0))),
	 CONSTRAINT [PK_pal_SMSCredit] PRIMARY KEY CLUSTERED 
	(
		[recId] 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
ELSE
BEGIN
	IF NOT EXISTS(
		SELECT *
		FROM sys.columns 
		WHERE Name      = N'currentBalance'
		  AND Object_ID = Object_ID(N'pal_SMSCredit'))
	BEGIN
		ALTER TABLE pal_SMSCredit
		ADD currentBalance AS (isnull([dbo].[fn_GetSMSCreditBalance]([customerCode]),(0))) 
	END
	IF NOT EXISTS(
		SELECT *
		FROM sys.columns 
		WHERE Name      = N'allocatedBalance'
		  AND Object_ID = Object_ID(N'pal_SMSCredit'))
	BEGIN
		ALTER TABLE pal_SMSCredit
		ADD allocatedBalance AS (isnull([dbo].[fn_GetSMSCreditAllocatedBalance]([customerCode]),(0)))
	END
	IF NOT EXISTS(
		SELECT *
		FROM sys.columns 
		WHERE Name      = N'availableBalance'
		  AND Object_ID = Object_ID(N'pal_SMSCredit'))
	BEGIN
		ALTER TABLE pal_SMSCredit
		ADD availableBalance AS (isnull([dbo].[fn_GetSMSCreditBalance]([customerCode]),(0))-isnull([dbo].[fn_GetSMSCreditAllocatedBalance]([customerCode]),(0))) 
	END
END
