UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.8.1';
IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'GENERAL_VIEWER'
          AND Object_ID = Object_ID(N'TBDD_USER'))
BEGIN
    ALTER TABLE TBDD_USER
	ADD GENERAL_VIEWER VARCHAR(30)	NOT NULL	DEFAULT 'NONE';
END

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'WAN_ENVIRONMENT'
          AND Object_ID = Object_ID(N'TBDD_USER'))
BEGIN
    ALTER TABLE TBDD_USER
	ADD WAN_ENVIRONMENT BIT	NOT NULL	DEFAULT 0;
END

CREATE OR ALTER VIEW [dbo].[VWDD_GROUPS_USER] AS
select 
	USR.GUID AS USR_ID
	,USR.USERNAME,
	GRP.GUID AS GRP_ID
	,GRP.NAME AS GRP_NAME 
	,GU.ADDED_WHEN
	,GU.ADDED_WHO
from 
	TBDD_GROUPS GRP INNER JOIN TBDD_GROUPS_USER GU ON GRP.GUID = GU.GROUP_ID
	INNER JOIN TBDD_USER USR ON GU.USER_ID = USR.GUID;
