/*
 * $Id: schema795.sql 100106 2011-06-16 00:30:20Z rlowery $
 *
 * Description:
 *
 * SQL Script to build the ManageSoft database on Microsoft SQL Server.
 * It is the first script that has to be run to setup the database.
 *
 * The steps performed by this script are:
 * 1. Create new database
 * 2. Create database table and initialiase data (skip if the table exists)
 *
 * If running the script manually, you might need to substitute some
 * configuration values:
 * DATABASEFILENAME should be set to the pathname (.mdf) for the database file
 * DEFAULT_NAMING_CONTEXT should be set to the DN of your domain controller
 *
 * Protected by US patent 6,006,034; patents pending.
 * Copyright 2000-2002 ManageSoft Corporation. All rights reserved.
 * Reproduction, adaptation, or translation without prior permission
 * is prohibited.
 */
USE master
GO
set nocount on

DECLARE @mgsDatabaseCreateName nvarchar(256)

-- This line gets replaced in mgsDatabaseCreate with the appropriate database name.
SET @mgsDatabaseCreateName = 'ManageSoft'

EXEC ('exec sp_dboption ''' + @mgsDatabaseCreateName + ''', ''arithabort'', ''true''')
EXEC ('exec sp_dboption ''' + @mgsDatabaseCreateName + ''', ''quoted identifier'', ''true''')
SET ARITHABORT ON
SET QUOTED_IDENTIFIER ON
GO

USE ManageSoft
GO

-- Any existing data in these tables can be safely discarded
IF EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ADUser'
)
	DROP TABLE ADUser
IF EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ADComputer'
)
	DROP TABLE ADComputer
IF EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ADSDOU'
)
	DROP TABLE ADSDOU
GO

PRINT '------------------------';
PRINT 'Creating database tables';
PRINT '------------------------';
-- The DatabaseConfiguration table contains configuration properties for the ManageSoft database tables, which are used for ongoing maintenance of the database.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DatabaseConfiguration'
)
BEGIN
	CREATE TABLE DatabaseConfiguration (
		-- The name of the property.
		Property		varchar(32) NOT NULL,
		-- The value of the property.
		Value			varchar(256) NOT NULL,
		-- The date and time the property was created.
		Created			datetime NOT NULL DEFAULT getdate(),
		-- The date and time the property was last updated.
		LastUpdate		datetime NOT NULL DEFAULT getdate(),
		-- The configuration variable name is the primary key
		CONSTRAINT PK_DatabaseConfiguration
			PRIMARY KEY CLUSTERED(Property)
	);
END
ELSE BEGIN
	PRINT 'Using existing DatabaseConfiguration table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DatabaseConfiguration'
	)
		ALTER TABLE DatabaseConfiguration
		ADD CONSTRAINT PK_DatabaseConfiguration
			PRIMARY KEY CLUSTERED(Property)
END
GO

-- The TargetType table contains a row for each type of object that can be targeted in Deployment Manager.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'TargetType'
)
BEGIN
	CREATE TABLE TargetType (
		-- The ID for the target type Computer, User, etc.
		TargetTypeID		int IDENTITY NOT NULL,
		-- The name of the target type.
		TargetTypeName		nvarchar(256) NOT NULL,
		CONSTRAINT UQ_TargetType
			UNIQUE CLUSTERED(TargetTypeID),
		CONSTRAINT PK_TargetTypeName
			PRIMARY KEY NONCLUSTERED(TargetTypeName)
	);
END
ELSE BEGIN
	PRINT 'Using existing TargetType table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_TargetType'
	)
		ALTER TABLE TargetType
		ADD CONSTRAINT UQ_TargetType
			UNIQUE CLUSTERED(TargetTypeID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_TargetTypeName'
	)
		ALTER TABLE TargetType
		ADD CONSTRAINT PK_TargetTypeName
			PRIMARY KEY NONCLUSTERED(TargetTypeName)
END
GO

-- This table stores the information about different types of OS available on the network devices
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'OperatingSystem'
)
BEGIN
	CREATE TABLE OperatingSystem (
		-- Auto-generated identity number
		OperatingSystemID	int IDENTITY NOT NULL,
		-- Name of operating system
		OperatingSystemName	nvarchar(128) NOT NULL,
		-- Reference to operating system category
		Category		int NULL,
		CONSTRAINT FK_OperatingSystem_Category
			FOREIGN KEY(Category)
			REFERENCES OperatingSystem(OperatingSystemID) ON DELETE NO ACTION,
		CONSTRAINT PK_OperatingSystem
			PRIMARY KEY NONCLUSTERED(OperatingSystemName),
		CONSTRAINT UQ_OperatingSystem
			UNIQUE NONCLUSTERED(OperatingSystemID)
	);
END
ELSE BEGIN
	PRINT 'Using existing OperatingSystem table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_OperatingSystem'
	)
		ALTER TABLE OperatingSystem
		ADD CONSTRAINT PK_OperatingSystem
			PRIMARY KEY NONCLUSTERED(OperatingSystemName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_OperatingSystem'
	)
		ALTER TABLE OperatingSystem
		ADD CONSTRAINT UQ_OperatingSystem
			UNIQUE NONCLUSTERED(OperatingSystemID)
END
GO

-- Stores language information, including their English names, and various forms of
-- language id.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Language'
)
BEGIN
	CREATE TABLE Language (
		-- The three letter language code.
		LangCode3		nchar(3) NOT NULL,
		-- The two letter language code.
		LangCode2		nchar(2) NULL,
		-- The english name of the language.
		EnglishName		nvarchar(128) NOT NULL,
		-- The name of the language, written in the local language.
		LocalName		nvarchar(128) NULL,
		-- The Microsoft language id, as specified in winnt.h in the Platform SDK.
		MSLanguageID		int NULL,
		CONSTRAINT PK_Language
			PRIMARY KEY NONCLUSTERED(LangCode3)
	);

	CREATE NONCLUSTERED INDEX IN_LanguageByName
		ON Language(EnglishName)
END
ELSE BEGIN
	PRINT 'Using existing Language table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Language'
	)
		ALTER TABLE Language
		ADD CONSTRAINT PK_Language
			PRIMARY KEY NONCLUSTERED(LangCode3)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'IN_LanguageByName'
	)
		CREATE NONCLUSTERED INDEX IN_LanguageByName
			ON Language(EnglishName)
END
GO

-- Stores country information, including their ISO country code and English names.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Country'
)
BEGIN
	CREATE TABLE Country (
		-- The two letter country code.
		CountryCode		nchar(2) NOT NULL,
		-- The english name of the country.
		[Name]			nvarchar(128) NOT NULL,
		CONSTRAINT PK_Country
			PRIMARY KEY NONCLUSTERED(CountryCode)
	);

	CREATE NONCLUSTERED INDEX IN_CountryByName
		ON Country([Name])
END
ELSE BEGIN
	PRINT 'Using existing Country table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Country'
	)
		ALTER TABLE Country
		ADD CONSTRAINT PK_Country
			PRIMARY KEY NONCLUSTERED(CountryCode)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'IN_CountryByName'
	)
		CREATE NONCLUSTERED INDEX IN_CountryByName
			ON Country([Name])
END
GO

-- Stores locale information, which consists of country and language combinations.
-- Use the LocaleCode column as the foreign key into this table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Locale'
)
BEGIN
	CREATE TABLE Locale (
		-- A combination of the language code and country code, separated by a hyphen. If there is
		-- no country code, then there will be no hyphen added.
		-- This column MUST have the correct value when inserted, based on the values of the language and country codes.
		LocaleCode		nvarchar(6) NOT NULL,
		-- The three letter language code.
		LangCode3		nchar(3) NOT NULL,
		-- The two letter country code.
		CountryCode		nchar(2) NULL,
		-- The name of the locale. Eg. "English (United States)".
		LocaleName		nvarchar(128) NOT NULL,
		-- The Microsoft identifier for the locale. Eg. 1033 for English (United States).
		MSLocaleID		int NULL,
		CONSTRAINT FK_Locale_Country
			FOREIGN KEY(CountryCode)
			REFERENCES Country(CountryCode) ON DELETE CASCADE,
		CONSTRAINT FK_Locale_Language
			FOREIGN KEY(LangCode3)
			REFERENCES Language(LangCode3) ON DELETE CASCADE,
		CONSTRAINT PK_Locale
			PRIMARY KEY CLUSTERED(LocaleCode),
		CONSTRAINT UQ_Locale
			UNIQUE NONCLUSTERED(LangCode3, CountryCode)
	);

	CREATE NONCLUSTERED INDEX LocaleByCountryCode
		ON Locale(CountryCode)
END
ELSE BEGIN
	PRINT 'Using existing Locale table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Locale'
	)
		ALTER TABLE Locale
		ADD CONSTRAINT PK_Locale
			PRIMARY KEY CLUSTERED(LocaleCode)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Locale'
	)
		ALTER TABLE Locale
		ADD CONSTRAINT UQ_Locale
			UNIQUE NONCLUSTERED(LangCode3, CountryCode)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'LocaleByCountryCode'
	)
		CREATE NONCLUSTERED INDEX LocaleByCountryCode
			ON Locale(CountryCode)
END
GO

-- The Location table contains data about Locations
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'NetworkLocation'
)
BEGIN
	CREATE TABLE NetworkLocation (
		-- The ID for the Location
		NetworkLocationID	int IDENTITY NOT NULL,
		-- The name of the Location
		[Name]			nvarchar(256) NOT NULL,
		-- The Distinguished name of the Location
		DN			nvarchar(1024) NULL,
		-- Specifies whether the row was populated automatically(1)  or manually(0).
		AutoPopulated		bit NOT NULL DEFAULT 0,
		-- Specifies whether the row will be used when mapping domains and devices to
		-- Locations
		Enabled			bit NOT NULL DEFAULT 1,
		-- DomainID of the domain in which the NetworkLocation resides
		DomainID		int NOT NULL,
		CONSTRAINT UQ_NetworkLocation_NetworkLocationID
			UNIQUE NONCLUSTERED(NetworkLocationID),
		CONSTRAINT UQ_NetworkLocation_DN
			UNIQUE NONCLUSTERED(DN),
		CONSTRAINT PK_NetworkLocation
			PRIMARY KEY CLUSTERED([Name])
	);

	CREATE NONCLUSTERED INDEX NetworkLocationByDomainID
		ON NetworkLocation(DomainID)
END
ELSE BEGIN
	PRINT 'Using existing NetworkLocation table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_NetworkLocation'
	)
		ALTER TABLE NetworkLocation
		ADD CONSTRAINT PK_NetworkLocation
			PRIMARY KEY CLUSTERED([Name])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_NetworkLocation_NetworkLocationID'
	)
		ALTER TABLE NetworkLocation
		ADD CONSTRAINT UQ_NetworkLocation_NetworkLocationID
			UNIQUE NONCLUSTERED(NetworkLocationID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_NetworkLocation_DN'
	)
		ALTER TABLE NetworkLocation
		ADD CONSTRAINT UQ_NetworkLocation_DN
			UNIQUE NONCLUSTERED(DN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkLocationByDomainID'
	)
		CREATE NONCLUSTERED INDEX NetworkLocationByDomainID
			ON NetworkLocation(DomainID)
END
GO

-- The Subnet table contains data about subnets in a location.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Subnet'
)
BEGIN
	CREATE TABLE Subnet (
		-- The ID for the Subnet
		SubnetID		int IDENTITY NOT NULL,
		-- The IPSubnet of the Subnet
		IPSubnet		nvarchar(64) NOT NULL,
		-- The IPSubnetMask of the Subnet
		IPSubnetMask		nvarchar(64) NOT NULL,
		-- NetworkLocationID of the NetworkLocation in which the Subnet resides
		NetworkLocationID	int NOT NULL,
		-- Specifies whether the row was populated automatically(1)  or manually(0).
		AutoPopulated		bit NOT NULL DEFAULT 0,
		-- Specifies whether the row will be used when mapping domains and devices to
		-- Locations
		Enabled			bit NOT NULL DEFAULT 1,
		-- Every Subnet belongs to a NetworkLocation.
		CONSTRAINT FK_Subnet_NetworkLocation
			FOREIGN KEY(NetworkLocationID)
			REFERENCES NetworkLocation(NetworkLocationID) ON DELETE CASCADE,
		CONSTRAINT UQ_Subnet_ID
			UNIQUE NONCLUSTERED(SubnetID),
		CONSTRAINT PK_Subnet
			PRIMARY KEY CLUSTERED(IPSubnet, IPSubnetMask, NetworkLocationID)
	);

	CREATE NONCLUSTERED INDEX SubnetByNetworkLocationID
		ON Subnet(NetworkLocationID)
END
ELSE BEGIN
	PRINT 'Using existing Subnet table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Subnet'
	)
		ALTER TABLE Subnet
		ADD CONSTRAINT PK_Subnet
			PRIMARY KEY CLUSTERED(IPSubnet, IPSubnetMask, NetworkLocationID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Subnet_ID'
	)
		ALTER TABLE Subnet
		ADD CONSTRAINT UQ_Subnet_ID
			UNIQUE NONCLUSTERED(SubnetID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SubnetByNetworkLocationID'
	)
		CREATE NONCLUSTERED INDEX SubnetByNetworkLocationID
			ON Subnet(NetworkLocationID)
END
GO

-- The Organization table contains data about organizational units used in a ManageSoft environment.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Organization'
)
BEGIN
	CREATE TABLE Organization (
		-- The ID for the organizational unit. (1 is used for "unknown OU" in
		-- the universal domain).This is automatically generated by SQL Server.
		OrganizationID		int IDENTITY NOT NULL,
		-- The relative distinguished name of this organizational unit.
		RDN			nvarchar(400) NULL,
		-- The objectGUID of the Active Directory object that represents this organizational unit, if known.
		GUID			binary(16) NULL,
		-- OrganizationID of the domain in which the entry resides.
		-- For a domain, must be set to reference self.
		DomainID		int NOT NULL DEFAULT 0,
		-- The relative distinguished name of the computer, reversed for superior performance on sub-organization searching.
		RDNReverse		nvarchar(400) NULL DEFAULT 'computed by trigger',
		-- True (1) if the organizational unit cannot be resolved through Active Directory
		-- (for example, the unknown OU, which has a NULL RDN), false (0) otherwise (if the OU has a non-empty RDN).
		IsUnknown AS CASE WHEN RDN IS NULL THEN 1 ELSE 0 END,
		-- True (1) if the organizational unit is a domain (has an empty - not NULL - RDN), false (0) otherwise.
		IsDomain AS CASE WHEN DomainID = OrganizationID THEN 1 ELSE 0 END,
		-- True (1) if package allocations should not be inherited from
		-- the parent Organization, unless NoOverride is set for the
		-- Policy. NoOverride is set using MemberMode=2 (Always) on the
		-- Organization member in the Policy ACL group.
		BlockInheritance	bit NOT NULL DEFAULT 0,
		CONSTRAINT PK_Organization
			PRIMARY KEY CLUSTERED(OrganizationID),
		CONSTRAINT UQ_Organization
			UNIQUE NONCLUSTERED(DomainID, RDN)
	);

	CREATE NONCLUSTERED INDEX UQ_Organization_ReverseRDN
		ON Organization(DomainID, RDNReverse)

	CREATE NONCLUSTERED INDEX OrganizationByGUID
		ON Organization(GUID)
END
ELSE BEGIN
	PRINT 'Using existing Organization table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Organization'
	)
		ALTER TABLE Organization
		ADD CONSTRAINT PK_Organization
			PRIMARY KEY CLUSTERED(OrganizationID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Organization'
	)
		ALTER TABLE Organization
		ADD CONSTRAINT UQ_Organization
			UNIQUE NONCLUSTERED(DomainID, RDN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Organization_ReverseRDN'
	)
		CREATE NONCLUSTERED INDEX UQ_Organization_ReverseRDN
			ON Organization(DomainID, RDNReverse)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'OrganizationByGUID'
	)
		CREATE NONCLUSTERED INDEX OrganizationByGUID
			ON Organization(GUID)
END
GO

-- The Domain table, in combination with the Organization table,
-- contains data about all of the domains, and organizational units
-- that have ever had users or computers report information in a  ManageSoft environment.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Domain'
)
BEGIN
	CREATE TABLE Domain (
		-- Organizational ID. This is a foreign key into the Organization table.
		OrganizationID		int NOT NULL,
		-- Fully qualified distinguished name.
		DN			nvarchar(100) NULL,
		-- The type of directory service running, for example AD, NT 4.
		DomainType		varchar(4) NULL,
		-- The NT 4 domain name.
		FlatName		nvarchar(32) NULL,
		-- Preferred domain controller to query.
		PreferredDomainController nvarchar(32) NULL,
		-- Whether (0) or not to fail over to alternate server if the preferred domain controller is not contactable.
		PreferredDomainControllerOnly bit NOT NULL DEFAULT 0,
		-- Whether (1) or not (0) to reconcile the  ManageSoft database with Active Directory.
		ADReconcile		bit NOT NULL DEFAULT 0,
		-- If reconciling Active Directory with the  ManageSoft database,
		-- the length of time in minutes before the Active Directory data is refreshed
		-- in the ManageSoft database. The default value is 60 minutes.  A value of 0
		-- means load the Active Directory data into the ManageSoft database at each
		-- reconciliation. Set this to a high value to minimize network traffic for
		-- domains for delayed reconciliation is acceptable.
		ADLoadLatency		int NOT NULL DEFAULT 60,
		-- Whether (1) or not (0) to generate merged policies.
		MergePolicies		bit NOT NULL DEFAULT 0,
		-- The date and time of the last reconciliation of the  ManageSoft database with Active Directory.
		LastADReconcile		smalldatetime NULL,
		-- This field is currently unused.
		LastADReconcileStatus	bit NOT NULL DEFAULT 0,
		-- The date and time of the last Active Directory load.
		-- A value of NULL indicates that Active Directory data should be loaded at the next reconcile operation.
		LastADLoad		smalldatetime NULL,
		-- The date and time of the last generation of merged policy.
		LastPolicyMerge		smalldatetime NULL,
		-- This field is currently unused.
		LastPolicyMergeStatus	bit NOT NULL DEFAULT 0,
		-- Fully qualified distinguished name, in reverse order (to improve sub-domain search performance).
		DNReverse		nvarchar(100) NULL DEFAULT '',
		-- Every Domain is an Organization.
		CONSTRAINT FK_Domain_ID
			FOREIGN KEY(OrganizationID)
			REFERENCES Organization(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT PK_Domain
			PRIMARY KEY CLUSTERED(OrganizationID),
		CONSTRAINT UQ_Domain
			UNIQUE NONCLUSTERED(DN)
	);

	CREATE NONCLUSTERED INDEX UQ_Domain_ReverseDN
		ON Domain(DNReverse)

	CREATE NONCLUSTERED INDEX DomainByDomainType
		ON Domain(DomainType)
END
ELSE BEGIN
	PRINT 'Using existing Domain table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Domain'
	)
		ALTER TABLE Domain
		ADD CONSTRAINT PK_Domain
			PRIMARY KEY CLUSTERED(OrganizationID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Domain'
	)
		ALTER TABLE Domain
		ADD CONSTRAINT UQ_Domain
			UNIQUE NONCLUSTERED(DN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Domain_ReverseDN'
	)
		CREATE NONCLUSTERED INDEX UQ_Domain_ReverseDN
			ON Domain(DNReverse)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DomainByDomainType'
	)
		CREATE NONCLUSTERED INDEX DomainByDomainType
			ON Domain(DomainType)
END
GO

-- The DomainConfiguration table contains configuration properties for the Domain table
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DomainConfiguration'
)
BEGIN
	CREATE TABLE DomainConfiguration (
		-- OrganizationID of the domain in which the entry resides.
		DomainID		int NOT NULL DEFAULT 0,
		-- The name of the property.
		Property		varchar(32) NOT NULL,
		-- The value of the property.
		Value			varchar(256) NULL,
		-- The date and time value of the property.
		DateValue		datetime NULL,
		-- The date and time the property was last updated.
		LastUpdate		datetime NOT NULL DEFAULT getdate(),
		-- Every DomainConfiguration belongs to a Domain.
		CONSTRAINT FK_DomainConfiguration_Domain
			FOREIGN KEY(DomainID)
			REFERENCES Domain(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT DomainConfigurationByDomainID
			PRIMARY KEY CLUSTERED(DomainID, Property)
	);
END
ELSE BEGIN
	PRINT 'Using existing DomainConfiguration table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DomainConfigurationByDomainID'
	)
		ALTER TABLE DomainConfiguration
		ADD CONSTRAINT DomainConfigurationByDomainID
			PRIMARY KEY CLUSTERED(DomainID, Property)
END
GO

-- The User table contains all of the users that have ever reported information
-- in a ManageSoft environment.  A row has been added to cater for installations
-- that occur from computer policy. This row has a UserID set to 1 and a UserCN
-- of SYSTEM, and belongs to Organization 1, which is the universal Domain.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'User'
)
BEGIN
	CREATE TABLE [User] (
		-- The ID for the user. This is automatically generated by SQL Server.
		UserID			int IDENTITY NOT NULL,
		-- The organizational unit of the user in Active Directory. This is a foreign
		-- key into the Organization table. In an SMS environment, this is always set to the unknown OU.
		UserOUID		int NOT NULL,
		-- The user's common name.In an Active Directory environment
		-- this is the common name attribute of the user's distinguished name.
		UserCN			nvarchar(64) NOT NULL,
		-- The objectGUID of the Active Directory object that represents this user, if known.
		GUID			binary(16) NULL,
		-- The SAM account name used to uniquely identify this user in event logs and user inventories.
		SAMAccountName		nvarchar(20) NULL,
		CONSTRAINT FK_User_OU
			FOREIGN KEY(UserOUID)
			REFERENCES Organization(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT UQ_User
			UNIQUE CLUSTERED(UserID),
		CONSTRAINT PK_User
			PRIMARY KEY NONCLUSTERED(UserOUID, UserCN),
		CONSTRAINT UQ_User_OUID_SAM
			UNIQUE NONCLUSTERED(UserOUID, SAMAccountName)
	);

	CREATE NONCLUSTERED INDEX UserByGUID
		ON [User](GUID)
END
ELSE BEGIN
	PRINT 'Using existing User table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_User'
	)
		ALTER TABLE [User]
		ADD CONSTRAINT UQ_User
			UNIQUE CLUSTERED(UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_User'
	)
		ALTER TABLE [User]
		ADD CONSTRAINT PK_User
			PRIMARY KEY NONCLUSTERED(UserOUID, UserCN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_User_OUID_SAM'
	)
		ALTER TABLE [User]
		ADD CONSTRAINT UQ_User_OUID_SAM
			UNIQUE NONCLUSTERED(UserOUID, SAMAccountName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UserByGUID'
	)
		CREATE NONCLUSTERED INDEX UserByGUID
			ON [User](GUID)
END
GO

-- The Computer table contains all computers that have ever reported
-- information or have been targeted by policy in a ManageSoft environment.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Computer'
)
BEGIN
	CREATE TABLE Computer (
		-- The ID for the computer. This is automatically generated by SQL Server.
		ComputerID		int IDENTITY NOT NULL,
		-- The organizational unit of the computer in Active Directory.
		-- In an SMS organization, this is set to the OUID of the unknown OU.
		ComputerOUID		int NOT NULL,
		-- The computer's common name. In an Active Directory environment this is the common
		-- name attribute of the computer's distinguished name. This is the same as the SAM account name.
		ComputerCN		nvarchar(64) NOT NULL,
		-- The operating system of the computer, if known. This allows efficient
		-- determination of the operating system breakdown of computers in an organization.
		OperatingSystemID	int NULL,
		-- The objectGUID of the Active Directory object that represents this computer, if known.
		GUID			binary(16) NULL,
		CONSTRAINT FK_Computer_OU
			FOREIGN KEY(ComputerOUID)
			REFERENCES Organization(OrganizationID) ON DELETE CASCADE,
		CONSTRAINT FK_Computer_OperatingSystem
			FOREIGN KEY(OperatingSystemID)
			REFERENCES OperatingSystem(OperatingSystemID) ON DELETE NO ACTION,
		CONSTRAINT UQ_Computer
			UNIQUE CLUSTERED(ComputerID),
		CONSTRAINT PK_Computer
			PRIMARY KEY NONCLUSTERED(ComputerOUID, ComputerCN)
	);

	CREATE NONCLUSTERED INDEX ComputerByGUID
		ON Computer(GUID)

	CREATE NONCLUSTERED INDEX ComputerByComputerCN
		ON Computer(ComputerCN)
END
ELSE BEGIN
	PRINT 'Using existing Computer table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Computer'
	)
		ALTER TABLE Computer
		ADD CONSTRAINT UQ_Computer
			UNIQUE CLUSTERED(ComputerID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Computer'
	)
		ALTER TABLE Computer
		ADD CONSTRAINT PK_Computer
			PRIMARY KEY NONCLUSTERED(ComputerOUID, ComputerCN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'ComputerByGUID'
	)
		CREATE NONCLUSTERED INDEX ComputerByGUID
			ON Computer(GUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'ComputerByComputerCN'
	)
		CREATE NONCLUSTERED INDEX ComputerByComputerCN
			ON Computer(ComputerCN)
END
GO

-- The ADSDOU table is populated with domain, and organizational unit data from Active
-- Directory in preparation for an Active Directory reconciliation.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ADSDOU'
)
BEGIN
	CREATE TABLE ADSDOU (
		-- The domain in which this object resides.
		DomainID		int NOT NULL,
		-- The relative distinguished name of this object.
		RDN			nvarchar(400) NOT NULL,
		-- The ObjectGUID of this Active Directory object.
		GUID			binary(16) NOT NULL,
		-- True (1) if package allocations should not be inherited from
		-- parent OUs or Domain, unless no-override is set for the
		-- Allocation (in the policy group membership mode).
		BlockInheritance	bit NOT NULL DEFAULT 0,
		CONSTRAINT FK_ADSDOU_Domain
			FOREIGN KEY(DomainID)
			REFERENCES Domain(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT PK_ADSDOU
			PRIMARY KEY CLUSTERED(DomainID, RDN),
		CONSTRAINT UQ_ADSDOU_GUID
			UNIQUE NONCLUSTERED(GUID)
	);
END
ELSE BEGIN
	PRINT 'Using existing ADSDOU table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ADSDOU'
	)
		ALTER TABLE ADSDOU
		ADD CONSTRAINT PK_ADSDOU
			PRIMARY KEY CLUSTERED(DomainID, RDN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ADSDOU_GUID'
	)
		ALTER TABLE ADSDOU
		ADD CONSTRAINT UQ_ADSDOU_GUID
			UNIQUE NONCLUSTERED(GUID)
END
GO

-- The ADComputer table is populated with data from Active Directory in preparation for an Active Directory reconciliation.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ADComputer'
)
BEGIN
	CREATE TABLE ADComputer (
		-- Organization ID of the domain in which the computer resides.
		DomainID		int NOT NULL,
		-- The computer's common name.
		ComputerCN		nvarchar(64) NOT NULL,
		-- The relative distinguished name of the
		-- organizational unit or container holding this
		-- computer.
		ComputerOURDN		nvarchar(384) NOT NULL,
		-- The objectGUID of the Active Directory object that represents this computer, if known.
		GUID			binary(16) NOT NULL,
		CONSTRAINT FK_ADComputer_Domain
			FOREIGN KEY(DomainID)
			REFERENCES Domain(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT PK_ADComputer
			PRIMARY KEY CLUSTERED(DomainID, ComputerCN, ComputerOURDN),
		CONSTRAINT UQ_ADComputer_GUID
			UNIQUE NONCLUSTERED(GUID)
	);
END
ELSE BEGIN
	PRINT 'Using existing ADComputer table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ADComputer'
	)
		ALTER TABLE ADComputer
		ADD CONSTRAINT PK_ADComputer
			PRIMARY KEY CLUSTERED(DomainID, ComputerCN, ComputerOURDN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ADComputer_GUID'
	)
		ALTER TABLE ADComputer
		ADD CONSTRAINT UQ_ADComputer_GUID
			UNIQUE NONCLUSTERED(GUID)
END
GO

-- The ADUser table contains is populated with data from Active Directory in
-- preparation for an Active Directory reconciliation. It is a temporary table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ADUser'
)
BEGIN
	CREATE TABLE ADUser (
		-- The domain in which this user resides.
		DomainID		int NOT NULL,
		-- The user's common name.
		UserCN			nvarchar(64) NOT NULL,
		-- The relative distinguished name of the
		-- organizational unit or container holding this
		-- user.
		UserOURDN		nvarchar(384) NOT NULL,
		-- The Active Directory GUID of this user.
		GUID			binary(16) NULL,
		-- The user's logon name used to support clients
		-- and servers from versions of Windows prior to Windows
		-- 2000.
		SAMAccountName		nvarchar(20) NULL,
		CONSTRAINT FK_ADUser_Domain
			FOREIGN KEY(DomainID)
			REFERENCES Domain(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT PK_ADUser
			PRIMARY KEY CLUSTERED(DomainID, UserCN, UserOURDN),
		CONSTRAINT UQ_ADUser_GUID
			UNIQUE NONCLUSTERED(GUID)
	);
END
ELSE BEGIN
	PRINT 'Using existing ADUser table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ADUser'
	)
		ALTER TABLE ADUser
		ADD CONSTRAINT PK_ADUser
			PRIMARY KEY CLUSTERED(DomainID, UserCN, UserOURDN)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ADUser_GUID'
	)
		ALTER TABLE ADUser
		ADD CONSTRAINT UQ_ADUser_GUID
			UNIQUE NONCLUSTERED(GUID)
END
GO

-- Each Group identifies either a named group or an unnamed Access Control List (ACL).
-- Each Group is associated with rows in the Member table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Group'
)
BEGIN
	CREATE TABLE [Group] (
		-- The ID for the group, automatically generated by SQL Server.
		GroupID			int IDENTITY NOT NULL,
		-- The Globally Unique IDentifier for the group.
		-- In the case where this Group represents an Access Control List for a Policy
		-- or a PackageAllocation, the GUID is that of this object.
		GUID			binary(16) NOT NULL,
		-- The Common Name for the group.
		-- In the case where this Group represents an Access Control List for a Policy
		-- or a PackageAllocation, the GroupCN is NULL.
		GroupCN			nvarchar(64) NULL,
		-- A reference to the Organization to which the group belongs.
		GroupOUID		int NOT NULL,
		-- The bitmask of flags defining the type of this Group.
		GroupType		int NULL,
		CONSTRAINT FK_Group_Organization
			FOREIGN KEY(GroupOUID)
			REFERENCES Organization(OrganizationID) ON DELETE CASCADE,
		CONSTRAINT PK_Group
			PRIMARY KEY NONCLUSTERED(GUID),
		CONSTRAINT UQ_Group
			UNIQUE NONCLUSTERED(GroupID)
	);
	-- This is not unique because the Groups that exist to represent
	-- Access Control Lists for Policy and PackageAllocation have a NULL CN,
	-- and the OrganizationID is that of the Domain. This results in duplicates.
	-- GroupCN, when not NULL, should be unique in the Organization.

	CREATE NONCLUSTERED INDEX UQ_GroupName
		ON [Group](GroupOUID, GroupCN)
END
ELSE BEGIN
	PRINT 'Using existing Group table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Group'
	)
		ALTER TABLE [Group]
		ADD CONSTRAINT PK_Group
			PRIMARY KEY NONCLUSTERED(GUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Group'
	)
		ALTER TABLE [Group]
		ADD CONSTRAINT UQ_Group
			UNIQUE NONCLUSTERED(GroupID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_GroupName'
	)
		CREATE NONCLUSTERED INDEX UQ_GroupName
			ON [Group](GroupOUID, GroupCN)
END
GO

-- The Member table stores the membership lists for every group. Each Member details
-- a User, Computer, Group, or Organization (only Policy ACL groups), and whether the
-- specified item is excluded (only ACL groups), included (the default) or included
-- mandatorily (cannot be excluded - used only for Organizations in Policy ACLs).
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Member'
)
BEGIN
	CREATE TABLE Member (
		-- The Group of which this is a Member.
		GroupID			int NOT NULL,
		-- The TargetType, 1=Computer, 2=User, 3=Group, 8=OrgUnit.
		TargetTypeID		int NOT NULL,
		-- The ComputerID, UserID, GroupID or OrganizationID.
		TargetID		int NOT NULL,
		-- The MemberMode is 0 for Exclude (regardless of any other
		-- memberships, the principals of this Target are excluded from
		-- this group), 1 for Include, and 2 for Always - NoOverride.
		MemberMode		int NOT NULL DEFAULT 1,
		CONSTRAINT FK_Member_TargetType
			FOREIGN KEY(TargetTypeID)
			REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION,
		CONSTRAINT FK_Member_Group
			FOREIGN KEY(GroupID)
			REFERENCES [Group](GroupID) ON DELETE CASCADE,
		CONSTRAINT PK_Member
			PRIMARY KEY CLUSTERED(GroupID, TargetTypeID, TargetID)
	);

	CREATE NONCLUSTERED INDEX MemberByGroupID
		ON Member(GroupID)

	CREATE NONCLUSTERED INDEX MemberByTarget
		ON Member(TargetTypeID, TargetID)
END
ELSE BEGIN
	PRINT 'Using existing Member table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Member'
	)
		ALTER TABLE Member
		ADD CONSTRAINT PK_Member
			PRIMARY KEY CLUSTERED(GroupID, TargetTypeID, TargetID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'MemberByGroupID'
	)
		CREATE NONCLUSTERED INDEX MemberByGroupID
			ON Member(GroupID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'MemberByTarget'
	)
		CREATE NONCLUSTERED INDEX MemberByTarget
			ON Member(TargetTypeID, TargetID)
END
GO

-- The TransitiveMember table stores data similar to the Member table, but is
-- populated only when needed, such as to assist in evaluating the rights of a
-- particular user. The difference is that for each user, it contains the full
-- list of groups in which they are members either directly or indirectly through
-- membership in other groups.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'TransitiveMember'
)
BEGIN
	CREATE TABLE TransitiveMember (
		-- The Group of which this is a Member.
		GroupID			int NOT NULL,
		-- The TargetType, 1=Computer, 2=User, 3=Group, 8=OrgUnit.
		TargetTypeID		int NOT NULL,
		-- The ComputerID, UserID, GroupID or OrganizationID.
		TargetID		int NOT NULL,
		-- The MemberMode is 0 for Exclude (regardless of any other
		-- memberships, the principals of this Target are excluded from
		-- this group), 1 for Include, and 2 for Always - NoOverride.
		MemberMode		int NOT NULL DEFAULT 1,
		CONSTRAINT FK_TransitiveMember_TargetType
			FOREIGN KEY(TargetTypeID)
			REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION,
		CONSTRAINT FK_TransitiveMember_Group
			FOREIGN KEY(GroupID)
			REFERENCES [Group](GroupID) ON DELETE CASCADE,
		CONSTRAINT PK_TransitiveMember
			PRIMARY KEY CLUSTERED(GroupID, TargetTypeID, TargetID)
	);

	CREATE NONCLUSTERED INDEX TransitiveMemberByGroupID
		ON TransitiveMember(GroupID)
END
ELSE BEGIN
	PRINT 'Using existing TransitiveMember table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_TransitiveMember'
	)
		ALTER TABLE TransitiveMember
		ADD CONSTRAINT PK_TransitiveMember
			PRIMARY KEY CLUSTERED(GroupID, TargetTypeID, TargetID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'TransitiveMemberByGroupID'
	)
		CREATE NONCLUSTERED INDEX TransitiveMemberByGroupID
			ON TransitiveMember(GroupID)
END
GO

-- PackageFamily is a short name used by the client to
-- decide where a package to be downloaded to and whether it's
-- an upgrade or downgrade of a previous package. Only one package
-- version of a family may be installed in a given context.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageFamily'
)
BEGIN
	CREATE TABLE PackageFamily (
		-- Auto-generated identity number
		PackageFamilyID		int IDENTITY NOT NULL,
		-- Package family name
		PackageName		nvarchar(64) NOT NULL,
		CONSTRAINT UQ_PackageFamily
			UNIQUE CLUSTERED(PackageFamilyID),
		CONSTRAINT PK_PackageName
			PRIMARY KEY NONCLUSTERED(PackageName)
	);
END
ELSE BEGIN
	PRINT 'Using existing PackageFamily table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_PackageFamily'
	)
		ALTER TABLE PackageFamily
		ADD CONSTRAINT UQ_PackageFamily
			UNIQUE CLUSTERED(PackageFamilyID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageName'
	)
		ALTER TABLE PackageFamily
		ADD CONSTRAINT PK_PackageName
			PRIMARY KEY NONCLUSTERED(PackageName)
END
GO

-- This table contains the package states that may be assigned to
-- a package in the software library. The default set of states are
-- based on ITIL release management processes. The state names are
-- internationalized when displayed on the MMC console.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageState'
)
BEGIN
	CREATE TABLE PackageState (
		-- Auto-generated identity number
		PackageStateID		int IDENTITY NOT NULL,
		-- Package State Name
		[Name]			nvarchar(64) NOT NULL,
		-- Whether a package in this state can be added to policy
		CanAddToPolicy		bit NOT NULL,
		CONSTRAINT PK_PackageStateID
			UNIQUE CLUSTERED(PackageStateID),
		CONSTRAINT UQ_PackageStateName
			PRIMARY KEY NONCLUSTERED([Name])
	);
END
ELSE BEGIN
	PRINT 'Using existing PackageState table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageStateID'
	)
		ALTER TABLE PackageState
		ADD CONSTRAINT PK_PackageStateID
			UNIQUE CLUSTERED(PackageStateID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_PackageStateName'
	)
		ALTER TABLE PackageState
		ADD CONSTRAINT UQ_PackageStateName
			PRIMARY KEY NONCLUSTERED([Name])
END
GO

-- This table contains a history of changes made to the state of a
-- package.  Note that the username is recorded as a nvarchar rather
-- than a foreign key to the user table so that if a user is deleted,
-- there is still a record of the changes that were made.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageVersionInState'
)
BEGIN
	CREATE TABLE PackageVersionInState (
		-- The package that has been changed
		PackageVersionID	int NOT NULL,
		-- The state that was set
		PackageStateID		int NOT NULL,
		-- The user that made the state change
		UserName		nvarchar(64) NOT NULL,
		-- The date/time that the change was made
		Changed			datetime NOT NULL,
		-- A user defined set of comments relating to the state change
		Comments		nvarchar(256) NOT NULL,
		CONSTRAINT FK_PackageVersionInState_PackageState
			FOREIGN KEY(PackageStateID)
			REFERENCES PackageState(PackageStateID) ON DELETE NO ACTION,
		CONSTRAINT PK_PackageVersionInState
			PRIMARY KEY CLUSTERED
			(PackageVersionID, PackageStateID, UserName, Changed)
	);

	CREATE NONCLUSTERED INDEX PackageStateHistoryByPackageVersionID
		ON PackageVersionInState(PackageVersionID, Changed)
END
ELSE BEGIN
	PRINT 'Using existing PackageVersionInState table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageVersionInState'
	)
		ALTER TABLE PackageVersionInState
		ADD CONSTRAINT PK_PackageVersionInState
			PRIMARY KEY CLUSTERED
			(PackageVersionID, PackageStateID, UserName, Changed)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageStateHistoryByPackageVersionID'
	)
		CREATE NONCLUSTERED INDEX PackageStateHistoryByPackageVersionID
			ON PackageVersionInState(PackageVersionID, Changed)
END
GO

-- This table contains the list of different types of packages, which also
-- corresponds to the main areas in the Software Library.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackagePathType'
)
BEGIN
	CREATE TABLE PackagePathType (
		-- Auto-generated identity number
		PackagePathTypeID	int IDENTITY NOT NULL,
		-- This describes the type of the package
		Description		nvarchar(128) NOT NULL,
		CONSTRAINT PK_PackagePathTypeID
			PRIMARY KEY CLUSTERED(PackagePathTypeID)
	);

	CREATE NONCLUSTERED INDEX PackagePathTypeGetByDescription
		ON PackagePathType(Description)
END
ELSE BEGIN
	PRINT 'Using existing PackagePathType table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackagePathTypeID'
	)
		ALTER TABLE PackagePathType
		ADD CONSTRAINT PK_PackagePathTypeID
			PRIMARY KEY CLUSTERED(PackagePathTypeID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackagePathTypeGetByDescription'
	)
		CREATE NONCLUSTERED INDEX PackagePathTypeGetByDescription
			ON PackagePathType(Description)
END
GO

-- Package Path identifies a filesystem path where the package
-- will be stored in the software library and in transit. As such, the
-- administration server and each distribution server may only contain
-- a single package version having a given Path
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackagePath'
)
BEGIN
	CREATE TABLE PackagePath (
		-- Auto-generated identity number
		PackagePathID		int IDENTITY NOT NULL,
		-- Package Path string
		PackageFullName		nvarchar(256) NULL,
		-- When a package is a variant of a parent package,
		-- this contains a reference to the parent package's path.
		ParentPathID		int NULL,
		-- The type of PackagePath that this row represents.
		PackagePathTypeID	int NOT NULL DEFAULT 1,
		CONSTRAINT FK_PackagePath_ParentPath
			FOREIGN KEY(ParentPathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE NO ACTION,
		CONSTRAINT FK_PackagePath_PackagePathType
			FOREIGN KEY(PackagePathTypeID)
			REFERENCES PackagePathType(PackagePathTypeID) ON DELETE NO ACTION,
		CONSTRAINT PK_PackagePath
			PRIMARY KEY CLUSTERED(PackagePathID),
		CONSTRAINT UQ_PackagePathName
			UNIQUE NONCLUSTERED(PackageFullName)
	);

	CREATE NONCLUSTERED INDEX PackagePathByParent
		ON PackagePath(ParentPathID)
END
ELSE BEGIN
	PRINT 'Using existing PackagePath table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackagePath'
	)
		ALTER TABLE PackagePath
		ADD CONSTRAINT PK_PackagePath
			PRIMARY KEY CLUSTERED(PackagePathID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_PackagePathName'
	)
		ALTER TABLE PackagePath
		ADD CONSTRAINT UQ_PackagePathName
			UNIQUE NONCLUSTERED(PackageFullName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackagePathByParent'
	)
		CREATE NONCLUSTERED INDEX PackagePathByParent
			ON PackagePath(ParentPathID)
END
GO

-- Architecture identifies a target CPU (ABI), used to identify
-- on what type of computer a package may be installed.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Architecture'
)
BEGIN
	CREATE TABLE Architecture (
		-- Auto-generated identity number
		ArchitectureID		int IDENTITY NOT NULL,
		-- Name of the computer architecture
		ArchitectureName	nvarchar(64) NOT NULL,
		CONSTRAINT UQ_Architecture
			UNIQUE CLUSTERED(ArchitectureID),
		CONSTRAINT PK_Architecture
			PRIMARY KEY NONCLUSTERED(ArchitectureName)
	);
END
ELSE BEGIN
	PRINT 'Using existing Architecture table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Architecture'
	)
		ALTER TABLE Architecture
		ADD CONSTRAINT UQ_Architecture
			UNIQUE CLUSTERED(ArchitectureID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Architecture'
	)
		ALTER TABLE Architecture
		ADD CONSTRAINT PK_Architecture
			PRIMARY KEY NONCLUSTERED(ArchitectureName)
END
GO

-- The PackageVersion table contains information about all of the packages in the
-- software library. It is primarily used to map between Installation and
-- PackageApplies for the purpose of comparing what users and computers should
-- have versus what they actually have installed. This table only stores the
-- details of one version of each package. This will change in future releases.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageVersion'
)
BEGIN
	CREATE TABLE PackageVersion (
		-- Auto-generated identity number
		PackageVersionID	int IDENTITY NOT NULL,
		-- Reference to Path (Full name) of Package
		PackagePathID		int NOT NULL,
		-- The version number of the package. The Installation
		-- table also has PackageName and Version columns. This value can
		-- be used to find the corresponding PackageFullName so that
		-- Installation can be mapped to PackageApplies.
		Version			nvarchar(32) NOT NULL,
		-- The current update (or patch) number of the package
		[Update]		nvarchar(64) NOT NULL DEFAULT '',
		-- A managed device may only have one PackageVersion in a family.
		PackageFamilyID		int NOT NULL,
		-- The friendly name for the package.
		Title			nvarchar(64) NULL,
		-- The MD5 digest of the project file (.ndp) for the package.
		-- This is updated in the database when the package is packed or distributed.
		MD5			nvarchar(40) NULL,
		-- If set, contains the size in bytes of the distributable form of the package
		[Size]			int NULL,
		-- A category or class used to group packages
		Category		nvarchar(128) NULL,
		CONSTRAINT FK_PackageVersion_PackageFamily
			FOREIGN KEY(PackageFamilyID)
			REFERENCES PackageFamily(PackageFamilyID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageVersion_PackagePath
			FOREIGN KEY(PackagePathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE,
		CONSTRAINT UQ_PackageVersionID
			UNIQUE CLUSTERED(PackageVersionID),
		CONSTRAINT PK_PackageVersion
			PRIMARY KEY NONCLUSTERED(PackagePathID, Version, [Update])
	);

	CREATE NONCLUSTERED INDEX PackageVersionByPackageFamilyID
		ON PackageVersion(PackageFamilyID)
END
ELSE BEGIN
	PRINT 'Using existing PackageVersion table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_PackageVersionID'
	)
		ALTER TABLE PackageVersion
		ADD CONSTRAINT UQ_PackageVersionID
			UNIQUE CLUSTERED(PackageVersionID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageVersion'
	)
		ALTER TABLE PackageVersion
		ADD CONSTRAINT PK_PackageVersion
			PRIMARY KEY NONCLUSTERED(PackagePathID, Version, [Update])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageVersionByPackageFamilyID'
	)
		CREATE NONCLUSTERED INDEX PackageVersionByPackageFamilyID
			ON PackageVersion(PackageFamilyID)
END
GO

-- PackageProvides is used when a package can satisfy a virtual
-- dependency, like "web-browser".
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageProvides'
)
BEGIN
	CREATE TABLE PackageProvides (
		-- Auto-generated identity number
		PackageProvidesID	int IDENTITY NOT NULL,
		-- The package which provides the interface
		PackageVersionID	int NOT NULL,
		-- The (virtual) package which is provided
		PackageFamilyID		int NOT NULL,
		-- The version provided, if necessary
		Version			nvarchar(32) NULL,
		CONSTRAINT FK_PackageProvides_PackageFamily
			FOREIGN KEY(PackageFamilyID)
			REFERENCES PackageFamily(PackageFamilyID) ON DELETE NO ACTION,
		CONSTRAINT FK_PackageProvides_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT PK_PackageProvides
			PRIMARY KEY NONCLUSTERED(PackageProvidesID),
		CONSTRAINT UQ_PackageProvides
			UNIQUE CLUSTERED(PackageVersionID, PackageFamilyID, Version)
	);

	CREATE NONCLUSTERED INDEX PackageProvidesByPackageFamily
		ON PackageProvides(PackageFamilyID, Version, PackageVersionID)
END
ELSE BEGIN
	PRINT 'Using existing PackageProvides table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_PackageProvides'
	)
		ALTER TABLE PackageProvides
		ADD CONSTRAINT UQ_PackageProvides
			UNIQUE CLUSTERED(PackageVersionID, PackageFamilyID, Version)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageProvides'
	)
		ALTER TABLE PackageProvides
		ADD CONSTRAINT PK_PackageProvides
			PRIMARY KEY NONCLUSTERED(PackageProvidesID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageProvidesByPackageFamily'
	)
		CREATE NONCLUSTERED INDEX PackageProvidesByPackageFamily
			ON PackageProvides(PackageFamilyID, Version, PackageVersionID)
END
GO

-- PackageRequires is used when a package requires another package
-- or some other configuration, like a piece of hardware for example.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageRequires'
)
BEGIN
	CREATE TABLE PackageRequires (
		-- The package version which has the requirement.
		PackageVersionID	int NOT NULL,
		-- Requirement type: for example, software, hardware.
		RequiredType		char(8) NOT NULL,
		-- Required object: for example, PackageFamily name.
		RequiredObject		nvarchar(64) NOT NULL,
		-- Strength of the requirement.
		Strength		int NULL,
		-- The required property of the object (for example, package version).
		Property		nvarchar(64) NULL DEFAULT 'Version',
		-- The value of the required property.
		Value			nvarchar(64) NULL,
		-- How to match the required value.
		Match			int NOT NULL,
		CONSTRAINT FK_PackageRequires_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT PK_PackageRequires
			PRIMARY KEY CLUSTERED
			(PackageVersionID, RequiredType, RequiredObject, Match)
	);
END
ELSE BEGIN
	PRINT 'Using existing PackageRequires table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageRequires'
	)
		ALTER TABLE PackageRequires
		ADD CONSTRAINT PK_PackageRequires
			PRIMARY KEY CLUSTERED
			(PackageVersionID, RequiredType, RequiredObject, Match)
END
GO

-- Packages are stored on Media of various types.
-- This table contains a record for each type.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'MediaType'
)
BEGIN
	CREATE TABLE MediaType (
		-- Auto-generated identity number, 1 = Warehouse (administration server).
		MediaTypeID		int IDENTITY NOT NULL,
		-- Media type name  (for example: Warehouse, Filesystem, CD).
		Description		nvarchar(128) NOT NULL,
		CONSTRAINT UQ_MediaType
			UNIQUE NONCLUSTERED(MediaTypeID),
		CONSTRAINT PK_MediaType
			PRIMARY KEY NONCLUSTERED(Description)
	);
END
ELSE BEGIN
	PRINT 'Using existing MediaType table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_MediaType'
	)
		ALTER TABLE MediaType
		ADD CONSTRAINT UQ_MediaType
			UNIQUE NONCLUSTERED(MediaTypeID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_MediaType'
	)
		ALTER TABLE MediaType
		ADD CONSTRAINT PK_MediaType
			PRIMARY KEY NONCLUSTERED(Description)
END
GO

-- Packages are stored on Media identified in this table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Media'
)
BEGIN
	CREATE TABLE Media (
		-- Auto-generated identity number, 1 = local administration server.
		MediaID			int IDENTITY NOT NULL,
		-- Name of media (empty for local administration server).
		Title			nvarchar(128) NOT NULL,
		-- What type of media?
		MediaTypeID		int NOT NULL,
		-- Where on the media?
		Location		nvarchar(256) NULL,
		CONSTRAINT FK_MediaType
			FOREIGN KEY(MediaTypeID)
			REFERENCES MediaType(MediaTypeID) ON DELETE NO ACTION,
		CONSTRAINT UQ_Media
			UNIQUE CLUSTERED(MediaID),
		CONSTRAINT PK_Media
			PRIMARY KEY NONCLUSTERED(Title)
	);

	CREATE NONCLUSTERED INDEX MediaByMediaType
		ON Media(MediaTypeID)
END
ELSE BEGIN
	PRINT 'Using existing Media table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Media'
	)
		ALTER TABLE Media
		ADD CONSTRAINT UQ_Media
			UNIQUE CLUSTERED(MediaID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Media'
	)
		ALTER TABLE Media
		ADD CONSTRAINT PK_Media
			PRIMARY KEY NONCLUSTERED(Title)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'MediaByMediaType'
	)
		CREATE NONCLUSTERED INDEX MediaByMediaType
			ON Media(MediaTypeID)
END
GO

-- This table identifies which Media contains which
-- PackageVersion. A record exists here at least for every
-- PackageVersion currently in the local administration server.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'MediaContainsPackageVersion'
)
BEGIN
	CREATE TABLE MediaContainsPackageVersion (
		-- What Media contains the package?
		MediaID			int NOT NULL,
		-- What PackageVersion?
		PackageVersionID	int NOT NULL,
		CONSTRAINT FK_MCPV_MediaID
			FOREIGN KEY(MediaID)
			REFERENCES Media(MediaID) ON DELETE CASCADE,
		CONSTRAINT FK_MCPV_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT PK_MediaContainsPackageVersion
			PRIMARY KEY CLUSTERED(MediaID, PackageVersionID)
	);

	CREATE NONCLUSTERED INDEX MediaContainsPackageVersionByPackageVersionID
		ON MediaContainsPackageVersion(PackageVersionID)
END
ELSE BEGIN
	PRINT 'Using existing MediaContainsPackageVersion table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_MediaContainsPackageVersion'
	)
		ALTER TABLE MediaContainsPackageVersion
		ADD CONSTRAINT PK_MediaContainsPackageVersion
			PRIMARY KEY CLUSTERED(MediaID, PackageVersionID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'MediaContainsPackageVersionByPackageVersionID'
	)
		CREATE NONCLUSTERED INDEX MediaContainsPackageVersionByPackageVersionID
			ON MediaContainsPackageVersion(PackageVersionID)
END
GO

-- This table identifies which Media contains which
-- PackagePath. A record exists here at least for every
-- PackagePath currently in the local administration server.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'MediaContainsPackagePath'
)
BEGIN
	CREATE TABLE MediaContainsPackagePath (
		-- What Media contains the package?
		MediaID			int NOT NULL,
		-- What PackagePath?
		PackagePathID		int NOT NULL,
		CONSTRAINT FK_MCPP_Media
			FOREIGN KEY(MediaID)
			REFERENCES Media(MediaID) ON DELETE CASCADE,
		CONSTRAINT FK_MCPP_PackagePath
			FOREIGN KEY(PackagePathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE,
		CONSTRAINT PK_MediaContainsPackagePath
			PRIMARY KEY CLUSTERED(MediaID, PackagePathID)
	);

	CREATE NONCLUSTERED INDEX MediaContainsPackagePathByPackagePathID
		ON MediaContainsPackagePath(PackagePathID)
END
ELSE BEGIN
	PRINT 'Using existing MediaContainsPackagePath table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_MediaContainsPackagePath'
	)
		ALTER TABLE MediaContainsPackagePath
		ADD CONSTRAINT PK_MediaContainsPackagePath
			PRIMARY KEY CLUSTERED(MediaID, PackagePathID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'MediaContainsPackagePathByPackagePathID'
	)
		CREATE NONCLUSTERED INDEX MediaContainsPackagePathByPackagePathID
			ON MediaContainsPackagePath(PackagePathID)
END
GO

-- PackageVersionLocale specifies all the locales (language and country combinations) that
-- a particular package version applies to.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageVersionLocale'
)
BEGIN
	CREATE TABLE PackageVersionLocale (
		-- Foreign key into the PackageVersion table.
		PackageVersionID	int NOT NULL,
		-- Foreign key into the Locale table.
		LocaleCode		nvarchar(6) NOT NULL,
		CONSTRAINT FK_PackageVersionLocale_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageVersionLocale_Locale
			FOREIGN KEY(LocaleCode)
			REFERENCES Locale(LocaleCode) ON DELETE CASCADE,
		CONSTRAINT PK_PackageVersionLocale
			PRIMARY KEY CLUSTERED(PackageVersionID, LocaleCode)
	);

	CREATE NONCLUSTERED INDEX PackageVersionLocaleByLocaleCode
		ON PackageVersionLocale(LocaleCode)
END
ELSE BEGIN
	PRINT 'Using existing PackageVersionLocale table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageVersionLocale'
	)
		ALTER TABLE PackageVersionLocale
		ADD CONSTRAINT PK_PackageVersionLocale
			PRIMARY KEY CLUSTERED(PackageVersionID, LocaleCode)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageVersionLocaleByLocaleCode'
	)
		CREATE NONCLUSTERED INDEX PackageVersionLocaleByLocaleCode
			ON PackageVersionLocale(LocaleCode)
END
GO

-- PackageVersionArchitecture specifies all the architectures that a particular
-- package version applies to.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageVersionArchitecture'
)
BEGIN
	CREATE TABLE PackageVersionArchitecture (
		-- Foreign key into the PackageVersion table.
		PackageVersionID	int NOT NULL,
		-- Foreign key into the Architecture table.
		ArchitectureID		int NOT NULL,
		CONSTRAINT FK_PackageVersionArchitecture_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageVersionArchitecture_Architecture
			FOREIGN KEY(ArchitectureID)
			REFERENCES Architecture(ArchitectureID) ON DELETE NO ACTION,
		CONSTRAINT PK_PackageVersionArchitecture
			PRIMARY KEY CLUSTERED(PackageVersionID, ArchitectureID)
	);
END
ELSE BEGIN
	PRINT 'Using existing PackageVersionArchitecture table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageVersionArchitecture'
	)
		ALTER TABLE PackageVersionArchitecture
		ADD CONSTRAINT PK_PackageVersionArchitecture
			PRIMARY KEY CLUSTERED(PackageVersionID, ArchitectureID)
END
GO

-- PackageVersionEnvironment specifies all the environments (operating systems)
-- that a particular package version applies to.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageVersionEnvironment'
)
BEGIN
	CREATE TABLE PackageVersionEnvironment (
		-- Foreign key into the PackageVersion table.
		PackageVersionID	int NOT NULL,
		-- Name of the environment that is used in the package. This refers to the environments used in the Packer.
		Environment		nvarchar(128) NOT NULL,
		CONSTRAINT FK_PackageVersionEnvironment_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT PK_PackageVersionEnvironment
			PRIMARY KEY CLUSTERED(PackageVersionID, Environment)
	);

	CREATE NONCLUSTERED INDEX IN_PackageVersionEnvironmentByPackageVersion
		ON PackageVersionEnvironment(PackageVersionID)
END
ELSE BEGIN
	PRINT 'Using existing PackageVersionEnvironment table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageVersionEnvironment'
	)
		ALTER TABLE PackageVersionEnvironment
		ADD CONSTRAINT PK_PackageVersionEnvironment
			PRIMARY KEY CLUSTERED(PackageVersionID, Environment)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'IN_PackageVersionEnvironmentByPackageVersion'
	)
		CREATE NONCLUSTERED INDEX IN_PackageVersionEnvironmentByPackageVersion
			ON PackageVersionEnvironment(PackageVersionID)
END
GO

-- Stores mappings from a file on disk to a filename that will be used on the managed device.
-- Currently used by the HPUX wizards to rename files that have non-Windows conforming names.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'FileNameMap'
)
BEGIN
	CREATE TABLE FileNameMap (
		-- The Media that the SourceFile exists on.
		MediaID			int NOT NULL,
		-- The file to be renamed.
		SourceFile		nvarchar(256) NOT NULL,
		-- The final file name.
		DestFile		nvarchar(256) NOT NULL,
		-- Boolean field that specifies whether the row refers to a file or a directory.
		IsFile			bit NOT NULL DEFAULT 1,
		CONSTRAINT FK_FileNameMap_Media
			FOREIGN KEY(MediaID)
			REFERENCES Media(MediaID) ON DELETE CASCADE,
		CONSTRAINT PK_FileNameMap
			PRIMARY KEY CLUSTERED(MediaID, SourceFile)
	);
END
ELSE BEGIN
	PRINT 'Using existing FileNameMap table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_FileNameMap'
	)
		ALTER TABLE FileNameMap
		ADD CONSTRAINT PK_FileNameMap
			PRIMARY KEY CLUSTERED(MediaID, SourceFile)
END
GO

-- Stores extended text uploaded from logs to describe operational failures.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Reason'
)
BEGIN
	CREATE TABLE Reason (
		-- The ID for the Reason. This is automatically generated by SQL Server.
		ReasonID		int IDENTITY NOT NULL,
		-- The checksum of the ReasonText, calculated by SQL Server.
		ReasonHash AS isnull(checksum(convert(nvarchar(4000),[ReasonText])),0),
		-- The Reason text.
		ReasonText		ntext NOT NULL,
		CONSTRAINT UQ_Reason
			UNIQUE CLUSTERED(ReasonID),
		CONSTRAINT PK_Reason
			PRIMARY KEY NONCLUSTERED(ReasonHash)
	);
END
ELSE BEGIN
	PRINT 'Using existing Reason table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Reason'
	)
		ALTER TABLE Reason
		ADD CONSTRAINT UQ_Reason
			UNIQUE CLUSTERED(ReasonID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Reason'
	)
		ALTER TABLE Reason
		ADD CONSTRAINT PK_Reason
			PRIMARY KEY NONCLUSTERED(ReasonHash)
END
GO

-- The Installation table contains the latest installation status
-- of each package for each user and computer. Success or failure of
-- installations is recorded. When a package is uninstalled, its installation
-- status record is removed from the table. When an installation is successful,
-- the successful installation status record replaces any earlier failure status
-- records. Once an installation is successful, the Installation table retains
-- the successful installation status record even if there are subsequent failed
-- installation attempts. If there have been any subsequent failed installations,
-- the latest of these failure records is also be retained. For example, if an
-- installation fails, and then succeeds on a subsequent attempt, only the successful
-- status is recorded. If an installation succeeds, but a later installation attempt
-- fails, then both the success and failure status records are stored. All other
-- installations are added to the InstallationHistory table as new status information is generated.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Installation'
)
BEGIN
	CREATE TABLE Installation (
		-- The computer that the installation event occurred on. This is a foreign key into the Computer table.
		ComputerID		int NOT NULL,
		-- The user associated with the installation event. This is a foreign
		-- key into the User table. If the UserID is 1 (system user), then the installation
		-- event occurred as part of computer policy. Otherwise, the installation event
		-- occurred as part of user policy.
		UserID			int NOT NULL,
		-- Package version that was installed
		PackageVersionID	int NOT NULL,
		-- The organizational unit of the user or computer associated with
		-- the installation event. This is a foreign key into the Organization table.
		-- This column is included in the table for clustering purposes. The value of
		-- OUID could be determined by looking up the User or Computer table. If the
		-- UserID is 1 (system user), OUID represents the organizational unit of the
		-- computer that the installation event occurred on. Otherwise, it represents
		-- the organizational unit of the user associated with the installation event.
		OrganizationID		int NOT NULL,
		-- The action performed on the package.
		-- This is currently set to "install".
		-- In future, "upgrade", "update" and "selfheal" may be added.
		Action			varchar(10) NOT NULL,
		-- The date and time that the installation event occurred.
		Reported		datetime NULL,
		-- The date and time that the installation status event was received into the database.
		Received		datetime NULL DEFAULT getdate(),
		-- A reference to the reason for the installation failure.
		-- If the installation succeeded then this value is NULL.
		FailReasonID		int NULL,
		-- The result of the package installation. Possible values are success or failure.
		Result			varchar(16) NULL,
		CONSTRAINT FK_Installation_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_Installation_Organization
			FOREIGN KEY(OrganizationID)
			REFERENCES Organization(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT FK_Installation_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT FK_Installation_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT FK_Installation_Reason
			FOREIGN KEY(FailReasonID)
			REFERENCES Reason(ReasonID) ON DELETE NO ACTION,
		CONSTRAINT PK_Installation
			PRIMARY KEY NONCLUSTERED(ComputerID, UserID, PackageVersionID)
	);

	CREATE CLUSTERED INDEX InstallationByCompPkgVer
		ON Installation
		(OrganizationID, ComputerID, PackageVersionID, UserID)

	CREATE NONCLUSTERED INDEX InstallationByPackage
		ON Installation(PackageVersionID, OrganizationID, ComputerID)

	CREATE NONCLUSTERED INDEX InstallationByUser
		ON Installation(OrganizationID, UserID)

	CREATE NONCLUSTERED INDEX InstallationByUserOnly
		ON Installation(UserID)
END
ELSE BEGIN
	PRINT 'Using existing Installation table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationByCompPkgVer'
	)
		CREATE CLUSTERED INDEX InstallationByCompPkgVer
			ON Installation
			(OrganizationID, ComputerID, PackageVersionID, UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Installation'
	)
		ALTER TABLE Installation
		ADD CONSTRAINT PK_Installation
			PRIMARY KEY NONCLUSTERED(ComputerID, UserID, PackageVersionID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationByPackage'
	)
		CREATE NONCLUSTERED INDEX InstallationByPackage
			ON Installation(PackageVersionID, OrganizationID, ComputerID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationByUser'
	)
		CREATE NONCLUSTERED INDEX InstallationByUser
			ON Installation(OrganizationID, UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationByUserOnly'
	)
		CREATE NONCLUSTERED INDEX InstallationByUserOnly
			ON Installation(UserID)
END
GO

-- The InstallationHistory table contains a history of the installation
-- status of each package for each user and computer.
-- Successful and failed installations are recorded, as well an uninstall events.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'InstallationHistory'
)
BEGIN
	CREATE TABLE InstallationHistory (
		-- The computer that the installation event occurred on. This is a foreign key into the Computer table.
		ComputerID		int NOT NULL,
		-- The user associated with the installation event. This is a foreign
		-- key into the User table. If the UserID is 1 then the installation event
		-- occurred as part of computer policy. Otherwise, the installation event
		-- occurred as part of user policy.
		UserID			int NOT NULL,
		-- The id for the Package Version installed
		PackageVersionID	int NOT NULL,
		-- The date and time that the installation event occurred.
		Reported		datetime NOT NULL,
		-- The action performed on the package. This value can be either, install or uninstall.
		Action			varchar(10) NOT NULL,
		-- The date and time that the installation status event was received into the database.
		Received		datetime NULL DEFAULT getdate(),
		-- A reference to the reason for the installation failure.
		-- If the installation succeeded then this value is NULL.
		FailReasonID		int NULL,
		-- The result of the package installation. Possible values are either success or failure.
		Result			varchar(16) NULL,
		CONSTRAINT FK_InstallationHistory_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_InstallationHistory_PackageVersion
			FOREIGN KEY(PackageVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE,
		CONSTRAINT FK_InstallationHistory_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT FK_InstallationHistory_Reason
			FOREIGN KEY(FailReasonID)
			REFERENCES Reason(ReasonID) ON DELETE NO ACTION,
		CONSTRAINT PK_InstallationHistory
			PRIMARY KEY CLUSTERED
			(ComputerID, UserID, PackageVersionID, Reported)
	);

	CREATE NONCLUSTERED INDEX InstallationHistoryByUser
		ON InstallationHistory(UserID)

	CREATE NONCLUSTERED INDEX InstallationHistoryByPackageVersionID
		ON InstallationHistory(PackageVersionID)
END
ELSE BEGIN
	PRINT 'Using existing InstallationHistory table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_InstallationHistory'
	)
		ALTER TABLE InstallationHistory
		ADD CONSTRAINT PK_InstallationHistory
			PRIMARY KEY CLUSTERED
			(ComputerID, UserID, PackageVersionID, Reported)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationHistoryByUser'
	)
		CREATE NONCLUSTERED INDEX InstallationHistoryByUser
			ON InstallationHistory(UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationHistoryByPackageVersionID'
	)
		CREATE NONCLUSTERED INDEX InstallationHistoryByPackageVersionID
			ON InstallationHistory(PackageVersionID)
END
GO

-- The InventoryReport table contains a record of every user and computer
-- that has reported hardware or software inventory. It details the date and
-- time when the hardware or software tracking was performed.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'InventoryReport'
)
BEGIN
	CREATE TABLE InventoryReport (
		-- The computer that the inventory was tracked on.
		-- This is a foreign key into the Computer table.
		ComputerID		int NOT NULL,
		-- The user for whom inventory was tracked. For computer inventory, the UserID is 1 (system user).
		-- This is a foreign key into the User table.
		UserID			int NOT NULL,
		-- The time software was tracked, or is NULL if no tracking is recorded.
		SWDate			datetime NULL,
		-- The time hardware was tracked, or is NULL if no tracking is recorded.
		HWDate			datetime NULL,
		-- The time files were tracked, or is NULL if no tracking is recorded.
		FilesDate		datetime NULL,
		-- Used when generating a differential inventory.
		SequenceNumber		int NULL,
		CONSTRAINT FK_InventoryReport_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_InventoryReport_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT PK_InventoryReport
			PRIMARY KEY CLUSTERED(ComputerID, UserID)
	);

	CREATE NONCLUSTERED INDEX InventoryReportByUser
		ON InventoryReport(UserID)
END
ELSE BEGIN
	PRINT 'Using existing InventoryReport table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_InventoryReport'
	)
		ALTER TABLE InventoryReport
		ADD CONSTRAINT PK_InventoryReport
			PRIMARY KEY CLUSTERED(ComputerID, UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InventoryReportByUser'
	)
		CREATE NONCLUSTERED INDEX InventoryReportByUser
			ON InventoryReport(UserID)
END
GO

-- The SoftwareVersion table contains a record for each software name/version
-- combination returned through inventory. The software names and versions are
-- gathered from places such as Add/Remove Programs on managed devices.
-- They do not represent package names and versions from the software library,
-- although correlation is likely.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareVersion'
)
BEGIN
	CREATE TABLE SoftwareVersion (
		-- The id for the software version. This is automatically generated by SQL Server.
		SoftwareID		int IDENTITY NOT NULL,
		-- The name of the software defined by the vendor.
		SoftwareName		nvarchar(128) NOT NULL,
		-- The version of the software defined by the vendor.
		Version			nvarchar(32) NOT NULL,
		-- The name of the software defined by the vendor unprocessed by Managesoft.
		RawSoftwareName		nvarchar(128) NULL,
		-- The version of the software defined by the vendor unprocessed by Managesoft.
		RawVersion		nvarchar(32) NULL,
		-- The publisher of the software defined by the vendor.
		Publisher		nvarchar(256) NULL,
		-- The MSI product ID of the software defined by the vendor.
		ProductID		nvarchar(256) NULL,
		CONSTRAINT UQ_SoftwareVersion
			UNIQUE CLUSTERED(SoftwareID),
		CONSTRAINT PK_SoftwareVersion
			PRIMARY KEY NONCLUSTERED(SoftwareName, Version)
	);
END
ELSE BEGIN
	PRINT 'Using existing SoftwareVersion table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareVersion'
	)
		ALTER TABLE SoftwareVersion
		ADD CONSTRAINT UQ_SoftwareVersion
			UNIQUE CLUSTERED(SoftwareID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareVersion'
	)
		ALTER TABLE SoftwareVersion
		ADD CONSTRAINT PK_SoftwareVersion
			PRIMARY KEY NONCLUSTERED(SoftwareName, Version)
END
GO

-- The SoftwareOccurrence table contains the list (by computer and user) of
-- applications that are installed. The applications may not have been installed
-- through Deployment Manager. The information is obtained from managed devices from:
-- ManageSoft packages cache, Add/Remove Programs registry entries, Microsoft Installer,
-- ProductVersion resource strings in program files, if files are tracked.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareOccurrence'
)
BEGIN
	CREATE TABLE SoftwareOccurrence (
		-- The computer on which the software was tracked. For user inventory,
		-- this is the computer that the user was logged on to at the time of the
		-- Generate Inventory event. This is a foreign key into the Computer table.
		ComputerID		int NOT NULL,
		-- User for whom the SoftwareVersion was installed. This is a foreign key to the User table.
		UserID			int NOT NULL,
		-- The software that has been tracked. This is a foreign key to the SoftwareVersion table.
		SoftwareID		int NOT NULL,
		-- The organizational unit of the computer that the software was tracked on.
		-- This is a foreign key into the Organization table. This column is included in
		-- the table for clustering purposes. The value of ComputerOUID could be determined
		-- by looking up the Computer table.
		ComputerOUID		int NOT NULL,
		-- Foreign key to OU of User. This is denormalised, since it's
		-- dependent on the UserID, but is useful for clustering!
		UserOUID		int NOT NULL,
		-- An indication of how the software was determined to be on the managed device.
		-- The valid entries are msi, managesoft, uninstall, exehdr (for file tracking only),
		-- dllhdr (for file tracking only).
		Evidence		varchar(32) NULL,
		-- ManageSoft PackageFullName if known (not always!).
		PackagePathID		int NULL,
		-- ManageSoft Policy GUID if known.
		PolicyGUID		binary(16) NULL,
		-- The date and time that the software was installed.
		InstallationDate	datetime NULL,
		-- The product family
		UpgradeCode		nvarchar(100) NULL,
		-- Unique ID of product as purchased
		ProductCode		nvarchar(100) NULL,
		-- Entered by user to activate the install
		ActivationKey		nvarchar(100) NULL,
		-- Distinguishes activation on this computer
		InstalledKey		nvarchar(100) NULL,
		CONSTRAINT FK_SoftwareOccurrence_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareOccurrence_ComputerOU
			FOREIGN KEY(ComputerOUID)
			REFERENCES Organization(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT FK_SoftwareOccurrence_Package
			FOREIGN KEY(PackagePathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareOccurrence_Software
			FOREIGN KEY(SoftwareID)
			REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareOccurrence_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareOccurrence_UserOU
			FOREIGN KEY(UserOUID)
			REFERENCES Organization(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT SoftwareOccurrenceBySWComputer
			UNIQUE CLUSTERED
			(SoftwareID, ComputerOUID, ComputerID, UserID),
		CONSTRAINT PK_SoftwareOccurrence
			PRIMARY KEY NONCLUSTERED(ComputerID, UserID, SoftwareID)
	);

	CREATE NONCLUSTERED INDEX SoftwareByOrganizationComputer
		ON SoftwareOccurrence
		(ComputerOUID, ComputerID, UserID, SoftwareID)

	CREATE NONCLUSTERED INDEX SoftwareByUser
		ON SoftwareOccurrence(UserID)

	CREATE NONCLUSTERED INDEX SoftwareByOrganizationUser
		ON SoftwareOccurrence(UserOUID)

	CREATE NONCLUSTERED INDEX SoftwareOccurrenceByPackagePathID
		ON SoftwareOccurrence(PackagePathID)
END
ELSE BEGIN
	PRINT 'Using existing SoftwareOccurrence table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareOccurrenceBySWComputer'
	)
		ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT SoftwareOccurrenceBySWComputer
			UNIQUE CLUSTERED
			(SoftwareID, ComputerOUID, ComputerID, UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareOccurrence'
	)
		ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT PK_SoftwareOccurrence
			PRIMARY KEY NONCLUSTERED(ComputerID, UserID, SoftwareID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareByOrganizationComputer'
	)
		CREATE NONCLUSTERED INDEX SoftwareByOrganizationComputer
			ON SoftwareOccurrence
			(ComputerOUID, ComputerID, UserID, SoftwareID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareByUser'
	)
		CREATE NONCLUSTERED INDEX SoftwareByUser
			ON SoftwareOccurrence(UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareByOrganizationUser'
	)
		CREATE NONCLUSTERED INDEX SoftwareByOrganizationUser
			ON SoftwareOccurrence(UserOUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareOccurrenceByPackagePathID'
	)
		CREATE NONCLUSTERED INDEX SoftwareOccurrenceByPackagePathID
			ON SoftwareOccurrence(PackagePathID)
END
GO

-- The SoftwareFilePath table contains a record for each unique
-- file path for files captured in inventory.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareFilePath'
)
BEGIN
	CREATE TABLE SoftwareFilePath (
		-- The id for the software file path. This is automatically generated by SQL Server.
		SoftwareFilePathID	int IDENTITY NOT NULL,
		-- The full path to a file captured in inventory, minus the filename.
		Path			nvarchar(400) NOT NULL,
		CONSTRAINT UQ_SoftwareFilePath
			UNIQUE NONCLUSTERED(Path),
		CONSTRAINT PK_SoftwareFilePath
			PRIMARY KEY NONCLUSTERED(SoftwareFilePathID)
	);
END
ELSE BEGIN
	PRINT 'Using existing SoftwareFilePath table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareFilePath'
	)
		ALTER TABLE SoftwareFilePath
		ADD CONSTRAINT UQ_SoftwareFilePath
			UNIQUE NONCLUSTERED(Path)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareFilePath'
	)
		ALTER TABLE SoftwareFilePath
		ADD CONSTRAINT PK_SoftwareFilePath
			PRIMARY KEY NONCLUSTERED(SoftwareFilePathID)
END
GO

-- The SoftwareFileName table contains a record for each unique
-- file name for files captured in inventory.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareFileName'
)
BEGIN
	CREATE TABLE SoftwareFileName (
		-- The id for the software file name. This is automatically generated by SQL Server.
		SoftwareFileNameID	int IDENTITY NOT NULL,
		-- The name of a file captured in inventory, minus the path.
		[Name]			nvarchar(400) NOT NULL,
		CONSTRAINT UQ_SoftwareFileName
			UNIQUE NONCLUSTERED([Name]),
		CONSTRAINT PK_SoftwareFileName
			PRIMARY KEY NONCLUSTERED(SoftwareFileNameID)
	);
END
ELSE BEGIN
	PRINT 'Using existing SoftwareFileName table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareFileName'
	)
		ALTER TABLE SoftwareFileName
		ADD CONSTRAINT UQ_SoftwareFileName
			UNIQUE NONCLUSTERED([Name])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareFileName'
	)
		ALTER TABLE SoftwareFileName
		ADD CONSTRAINT PK_SoftwareFileName
			PRIMARY KEY NONCLUSTERED(SoftwareFileNameID)
END
GO

-- The SoftwareFile table contains a record for each file associated with an application
-- on each computer. File tracking is not enabled by default. For more information on
-- configuring which files to track, see the section about the Inventory Agent.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareFile'
)
BEGIN
	CREATE TABLE SoftwareFile (
		-- The id for the software file. This is automatically generated by SQL Server.
		SoftwareFileID		int IDENTITY NOT NULL,
		-- The computer on which the file was tracked. This is a foreign key into the Computer table.
		ComputerID		int NOT NULL,
		-- The software containing the file that was tracked.This is a foreign key into the SoftwareVersion table.
		SoftwareID		int NOT NULL,
		-- The full path where the file was found on the computer.
		[Filename]		varchar(800) NOT NULL,
		-- The organizational unit of the computer on which the file was tracked.
		-- This is a foreign key into the Organization table. This column is included
		-- in the table for clustering purposes. The value of ComputerOUID could be
		-- determined by looking up the Computer table.
		ComputerOUID		int NOT NULL,
		-- The version of the software file defined by the vendor.
		Version			nvarchar(32) NULL,
		-- The file's MD5 digest.
		MD5			varchar(32) NOT NULL,
		-- The file's size in bytes.
		[Size]			int NOT NULL,
		-- The last date and time the file was modified on the computer.
		[DateTime]		datetime NULL,
		-- The file version of the software file defined by the vendor.
		FileVersion		nvarchar(256) NULL,
		-- The file description of the software file defined by the vendor.
		FileDescription		nvarchar(256) NULL,
		-- The language of the software file defined by the vendor.
		Language		nvarchar(256) NULL,
		-- The company name of the software file defined by the vendor.
		CompanyName		nvarchar(256) NULL,
		-- The full path to the file that was tracked, minus the filename.
		-- This is a foreign key into the SoftwareFilePath table.
		SoftwareFilePathID	int NULL,
		-- The name of the file that was tracked, minus the path.
		-- This is a foreign key into the SoftwareFileName table.
		SoftwareFileNameID	int NULL,
		CONSTRAINT FK_SoftwareFile_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareFile_ComputerOU
			FOREIGN KEY(ComputerOUID)
			REFERENCES Organization(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT FK_SoftwareFile_Software
			FOREIGN KEY(SoftwareID)
			REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareFile_SoftwareFilePath
			FOREIGN KEY(SoftwareFilePathID)
			REFERENCES SoftwareFilePath(SoftwareFilePathID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareFile_SoftwareFileName
			FOREIGN KEY(SoftwareFileNameID)
			REFERENCES SoftwareFileName(SoftwareFileNameID) ON DELETE CASCADE,
		CONSTRAINT UQ_SoftwareFile
			UNIQUE NONCLUSTERED(SoftwareFileID),
		CONSTRAINT PK_SoftwareFile
			PRIMARY KEY NONCLUSTERED(ComputerID, SoftwareID, [Filename])
	);

	CREATE CLUSTERED INDEX SoftwareFileByComputer
		ON SoftwareFile(ComputerOUID, ComputerID, SoftwareID)

	CREATE NONCLUSTERED INDEX SoftwareFileBySoftwareID
		ON SoftwareFile(SoftwareID)

	CREATE NONCLUSTERED INDEX SoftwareFileBySoftwareFilePathID
		ON SoftwareFile(SoftwareFilePathID)

	CREATE NONCLUSTERED INDEX SoftwareFileBySoftwareFileNameID
		ON SoftwareFile(SoftwareFileNameID)
END
ELSE BEGIN
	PRINT 'Using existing SoftwareFile table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareFileByComputer'
	)
		CREATE CLUSTERED INDEX SoftwareFileByComputer
			ON SoftwareFile(ComputerOUID, ComputerID, SoftwareID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareFile'
	)
		ALTER TABLE SoftwareFile
		ADD CONSTRAINT UQ_SoftwareFile
			UNIQUE NONCLUSTERED(SoftwareFileID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareFile'
	)
		ALTER TABLE SoftwareFile
		ADD CONSTRAINT PK_SoftwareFile
			PRIMARY KEY NONCLUSTERED(ComputerID, SoftwareID, [Filename])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareFileBySoftwareID'
	)
		CREATE NONCLUSTERED INDEX SoftwareFileBySoftwareID
			ON SoftwareFile(SoftwareID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareFileBySoftwareFilePathID'
	)
		CREATE NONCLUSTERED INDEX SoftwareFileBySoftwareFilePathID
			ON SoftwareFile(SoftwareFilePathID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareFileBySoftwareFileNameID'
	)
		CREATE NONCLUSTERED INDEX SoftwareFileBySoftwareFileNameID
			ON SoftwareFile(SoftwareFileNameID)
END
GO

-- The SoftwareFileProperty table provides property names and values for each
-- software file object.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareFileProperty'
)
BEGIN
	CREATE TABLE SoftwareFileProperty (
		-- The SoftwareFile that this property belongs to
		SoftwareFileID		int NOT NULL,
		-- The software file property name.
		[Name]			nvarchar(256) NOT NULL,
		-- The software file property value.
		Value			nvarchar(256) NOT NULL,
		CONSTRAINT FK_SoftwareFileProperty_SoftwareFile
			FOREIGN KEY(SoftwareFileID)
			REFERENCES SoftwareFile(SoftwareFileID) ON DELETE CASCADE,
		CONSTRAINT PK_SoftwareFileProperty
			PRIMARY KEY NONCLUSTERED(SoftwareFileID, [Name])
	);
END
ELSE BEGIN
	PRINT 'Using existing SoftwareFileProperty table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareFileProperty'
	)
		ALTER TABLE SoftwareFileProperty
		ADD CONSTRAINT PK_SoftwareFileProperty
			PRIMARY KEY NONCLUSTERED(SoftwareFileID, [Name])
END
GO

-- The UnknownFile table contains a record for each file not known
-- to be associated with a software application, on each computer.
-- For more information on configuring which files to track, refer
-- to the command-line tools section of the	ManageSoft Reference: System Reference.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'UnknownFile'
)
BEGIN
	CREATE TABLE UnknownFile (
		-- Computer where the UnknownFile was found. This is a foreign key to the Computer table.
		ComputerID		int NOT NULL,
		-- The full path of the file on the computer.
		[Filename]		varchar(800) NOT NULL,
		-- The file's MD5 digest.
		MD5			varchar(32) NOT NULL,
		-- The file's size in bytes.
		[Size]			int NOT NULL,
		-- The last date and time the file was modified on the computer.
		[DateTime]		datetime NULL,
		CONSTRAINT FK_UnknownFile_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT PK_UnknownFile
			PRIMARY KEY NONCLUSTERED(ComputerID, [Filename])
	);

	CREATE CLUSTERED INDEX UnknownFileByComputer
		ON UnknownFile(ComputerID, MD5)
END
ELSE BEGIN
	PRINT 'Using existing UnknownFile table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UnknownFileByComputer'
	)
		CREATE CLUSTERED INDEX UnknownFileByComputer
			ON UnknownFile(ComputerID, MD5)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_UnknownFile'
	)
		ALTER TABLE UnknownFile
		ADD CONSTRAINT PK_UnknownFile
			PRIMARY KEY NONCLUSTERED(ComputerID, [Filename])
END
GO

-- HardwareClass contains a record for every class of
-- hardware object found during hardware inventories, including
-- mainly the WMI classes
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'HardwareClass'
)
BEGIN
	CREATE TABLE HardwareClass (
		-- Auto-generated identity number
		HardwareClassID		int IDENTITY NOT NULL,
		-- Hardware Class name
		Class			nvarchar(256) NOT NULL,
		-- Reference to superclass, if any (and known)
		SuperClassID		int NULL,
		CONSTRAINT FK_HardwareClass_Superclass
			FOREIGN KEY(SuperClassID)
			REFERENCES HardwareClass(HardwareClassID) ON DELETE NO ACTION,
		CONSTRAINT UQ_HardwareClass
			UNIQUE CLUSTERED(HardwareClassID),
		CONSTRAINT PK_HardwareClass
			PRIMARY KEY NONCLUSTERED(Class)
	);

	CREATE NONCLUSTERED INDEX HardwareClassBySuperClass
		ON HardwareClass(SuperClassID, HardwareClassID)
END
ELSE BEGIN
	PRINT 'Using existing HardwareClass table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_HardwareClass'
	)
		ALTER TABLE HardwareClass
		ADD CONSTRAINT UQ_HardwareClass
			UNIQUE CLUSTERED(HardwareClassID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_HardwareClass'
	)
		ALTER TABLE HardwareClass
		ADD CONSTRAINT PK_HardwareClass
			PRIMARY KEY NONCLUSTERED(Class)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'HardwareClassBySuperClass'
	)
		CREATE NONCLUSTERED INDEX HardwareClassBySuperClass
			ON HardwareClass(SuperClassID, HardwareClassID)
END
GO

-- The HardwareObject table entries describe a specific configuration item
-- (usually a piece of physical hardware) associated with a computer. The information
-- is represented in the database as Windows Management Instrumentation (WMI) classes.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'HardwareObject'
)
BEGIN
	CREATE TABLE HardwareObject (
		-- Auto-generated identity number
		HardwareObjectID	int IDENTITY NOT NULL,
		-- The computer on which the hardware was found. It is a foreign key into the Computer table.
		ComputerID		int NOT NULL,
		-- The hardware name as reported by the system.
		HardwareName		nvarchar(256) NOT NULL,
		-- The distinguishing identifier for the hardware. For example,
		-- if a computer has more than one memory card with the same Class
		-- and HardwareName, each memory card is assigned an Occurrence value (0, 1, 2...).
		Occurrence		int NOT NULL,
		-- The organizational unit of the computer on which the hardware was found.
		-- This is a foreign key into the Organization table. This column is included
		-- in the table for clustering purposes. The value of ComputerOUID could be
		-- determined by looking up the Computer table.
		ComputerOUID		int NOT NULL,
		-- The id for the HardwareClass of the object.
		HardwareClassID		int NOT NULL,
		CONSTRAINT FK_HardwareObject_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_HardwareObject_ComputerOU
			FOREIGN KEY(ComputerOUID)
			REFERENCES Organization(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT FK_HardwareObject_Class
			FOREIGN KEY(HardwareClassID)
			REFERENCES HardwareClass(HardwareClassID) ON DELETE CASCADE,
		CONSTRAINT PK_HardwareObject
			PRIMARY KEY NONCLUSTERED(ComputerID, HardwareName, Occurrence),
		CONSTRAINT UQ_HardwareObject
			UNIQUE CLUSTERED(HardwareObjectID)
	);

	CREATE NONCLUSTERED INDEX HardwareObjectByClass
		ON HardwareObject(HardwareClassID)

	CREATE NONCLUSTERED INDEX HardwareObjectByOrganization
		ON HardwareObject(ComputerOUID, HardwareClassID)
END
ELSE BEGIN
	PRINT 'Using existing HardwareObject table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_HardwareObject'
	)
		ALTER TABLE HardwareObject
		ADD CONSTRAINT UQ_HardwareObject
			UNIQUE CLUSTERED(HardwareObjectID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_HardwareObject'
	)
		ALTER TABLE HardwareObject
		ADD CONSTRAINT PK_HardwareObject
			PRIMARY KEY NONCLUSTERED(ComputerID, HardwareName, Occurrence)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'HardwareObjectByClass'
	)
		CREATE NONCLUSTERED INDEX HardwareObjectByClass
			ON HardwareObject(HardwareClassID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'HardwareObjectByOrganization'
	)
		CREATE NONCLUSTERED INDEX HardwareObjectByOrganization
			ON HardwareObject(ComputerOUID, HardwareClassID)
END
GO

-- The HardwareProperty table provides property names and values for each hardware
-- object.  The information is represented in the database as Windows Management
-- Instrumentation (WMI) properties.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'HardwareProperty'
)
BEGIN
	CREATE TABLE HardwareProperty (
		-- Auto-generated identity number
		HardwarePropertyID	int IDENTITY NOT NULL,
		-- The hardware property. A single hardware object can have many properties.
		Property		nvarchar(256) NOT NULL,
		CONSTRAINT PK_HardwareProperty
			PRIMARY KEY CLUSTERED(Property),
		CONSTRAINT UQ_HardwareProperty
			UNIQUE NONCLUSTERED(HardwarePropertyID)
	);
END
ELSE BEGIN
	PRINT 'Using existing HardwareProperty table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_HardwareProperty'
	)
		ALTER TABLE HardwareProperty
		ADD CONSTRAINT PK_HardwareProperty
			PRIMARY KEY CLUSTERED(Property)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_HardwareProperty'
	)
		ALTER TABLE HardwareProperty
		ADD CONSTRAINT UQ_HardwareProperty
			UNIQUE NONCLUSTERED(HardwarePropertyID)
END
GO

-- The value of a specified HardwareProperty of the specified
-- HardwareObject.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'HardwareValue'
)
BEGIN
	CREATE TABLE HardwareValue (
		-- Object.
		HardwareObjectID	int NOT NULL,
		-- Property.
		HardwarePropertyID	int NOT NULL,
		-- Property value.
		Value			nvarchar(256) NULL,
		CONSTRAINT FK_HardwareValue_HardwareObject
			FOREIGN KEY(HardwareObjectID)
			REFERENCES HardwareObject(HardwareObjectID) ON DELETE CASCADE,
		CONSTRAINT FK_HardwareValue_HardwareProperty
			FOREIGN KEY(HardwarePropertyID)
			REFERENCES HardwareProperty(HardwarePropertyID) ON DELETE CASCADE,
		CONSTRAINT PK_HardwareValue
			PRIMARY KEY CLUSTERED(HardwareObjectID, HardwarePropertyID)
	);

	CREATE NONCLUSTERED INDEX HardwareValueByHardwarePropertyID
		ON HardwareValue(HardwarePropertyID)
END
ELSE BEGIN
	PRINT 'Using existing HardwareValue table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_HardwareValue'
	)
		ALTER TABLE HardwareValue
		ADD CONSTRAINT PK_HardwareValue
			PRIMARY KEY CLUSTERED(HardwareObjectID, HardwarePropertyID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'HardwareValueByHardwarePropertyID'
	)
		CREATE NONCLUSTERED INDEX HardwareValueByHardwarePropertyID
			ON HardwareValue(HardwarePropertyID)
END
GO

-- The available distribution server types are defined in the DistributionServerType table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DistributionServerType'
)
BEGIN
	CREATE TABLE DistributionServerType (
		-- An identifier for this distribution server type.
		DistributionServerTypeID int NOT NULL,
		-- The descriptive name assigned to this distribution server type.
		DistributionServerTypeName nvarchar(256) NOT NULL,
		CONSTRAINT PK_DistributionServerType
			PRIMARY KEY CLUSTERED(DistributionServerTypeID),
		CONSTRAINT UN_DistributionServerTypeName
			UNIQUE NONCLUSTERED(DistributionServerTypeName)
	);
END
ELSE BEGIN
	PRINT 'Using existing DistributionServerType table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DistributionServerType'
	)
		ALTER TABLE DistributionServerType
		ADD CONSTRAINT PK_DistributionServerType
			PRIMARY KEY CLUSTERED(DistributionServerTypeID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UN_DistributionServerTypeName'
	)
		ALTER TABLE DistributionServerType
		ADD CONSTRAINT UN_DistributionServerTypeName
			UNIQUE NONCLUSTERED(DistributionServerTypeName)
END
GO

-- The DistributionServer table stores all of the distribution servers and
-- distribution locations in the ManageSoft distribution hierarchy.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DistributionServer'
)
BEGIN
	CREATE TABLE DistributionServer (
		-- A unique identifier for the distribution server or distribution
		-- location. The core distribution server has a value of all
		-- zeroes.
		ServerUID		binary(16) NOT NULL,
		-- DNS name of the server
		DNSName			nvarchar(128) NULL,
		-- The server type. The possible values are 0 for distribution location,
		-- 1 for distribution server.
		ServerType		smallint NOT NULL DEFAULT 0,
		-- The name of the distribution server or distribution location.
		ServerName		nvarchar(64) NOT NULL,
		-- The parent of the distribution server or distribution location.For
		-- the core distribution server, the PrimaryParentUID is NULL.
		PrimaryParentUID	binary(16) NULL,
		-- The state of configuration of the distribution server. This is only
		-- set for distribution servers (ServerType is 1).This can be one of
		-- the following values : configure, failed, pending, NULL.
		ConfigState		nvarchar(20) NULL,
		-- The date and time of the last configuration message sent to the
		-- distribution server. This is only set for distribution servers (ServerType is 1).
		LastConfigStart		datetime NULL,
		-- The job identifier for the last configuration message sent to the
		-- distribution server. This is only set for distribution servers (ServerType is 1).
		LastConfigJobId		nvarchar(40) NULL,
		-- The reason for a configuration failure for the distribution server.
		ConfigFailReason	ntext NULL,
		-- Boolean value indicating whether this distribution location is quarantined from receiving policy distributions.
		PolicyQuarantined	bit NOT NULL DEFAULT 0,
		CONSTRAINT FK_DistributionServer_PrimaryParent
			FOREIGN KEY(PrimaryParentUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE NO ACTION,
		CONSTRAINT PK_DistributionServer
			PRIMARY KEY CLUSTERED(ServerUID)
	);

	CREATE NONCLUSTERED INDEX DistributionServerByPrimaryParentUID
		ON DistributionServer(PrimaryParentUID)
END
ELSE BEGIN
	PRINT 'Using existing DistributionServer table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DistributionServer'
	)
		ALTER TABLE DistributionServer
		ADD CONSTRAINT PK_DistributionServer
			PRIMARY KEY CLUSTERED(ServerUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DistributionServerByPrimaryParentUID'
	)
		CREATE NONCLUSTERED INDEX DistributionServerByPrimaryParentUID
			ON DistributionServer(PrimaryParentUID)
END
GO

-- The DistributionServerStatus table stores status information for the distribution servers
-- in the ManageSoft distribution hierarchy.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DistributionServerStatus'
)
BEGIN
	CREATE TABLE DistributionServerStatus (
		-- The distribution server related to the status record. This is a
		-- foreign key into the DistributionServer table.
		ServerUID		binary(16) NOT NULL,
		-- The date and time at which the distribution server last
		-- reported status information for this parameter.
		ReportedDate		datetime NOT NULL,
		-- The type of the status parameter reported. Currently supported
		-- types are 'job' for jobs on the distribution server and 'logs'
		-- for log files awaiting upload from the distribution server.
		Type			varchar(32) NOT NULL,
		-- The name of the status parameter reported. This is an internal
		-- name for the parameter and is not intended for display.
		[Name]			varchar(64) NOT NULL,
		-- The count of items for this status parameter currently awaiting
		-- processing by this distribution server.
		Count			int NOT NULL,
		-- The count of items for this status parameter that are older
		-- than a configurable time period that are currently awaiting
		-- processing by this distribution server. This will not
		-- necessarily have meaning for each status parameter.
		DelayedCount		int NOT NULL,
		CONSTRAINT FK_DistributionServerStatus_ServerUID
			FOREIGN KEY(ServerUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE,
		CONSTRAINT PK_DistributionServerStatus
			PRIMARY KEY CLUSTERED(ServerUID, Type, [Name])
	);
END
ELSE BEGIN
	PRINT 'Using existing DistributionServerStatus table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DistributionServerStatus'
	)
		ALTER TABLE DistributionServerStatus
		ADD CONSTRAINT PK_DistributionServerStatus
			PRIMARY KEY CLUSTERED(ServerUID, Type, [Name])
END
GO

-- The DistributedPackage table stores the status (both of current
-- and pending distributions) of package distributions to distribution servers
-- and distribution locations.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DistributedPackage'
)
BEGIN
	CREATE TABLE DistributedPackage (
		-- Auto-generated identity number
		DistributedPackageID	int IDENTITY NOT NULL,
		-- The distribution server or distribution location related to the
		-- status record. This is a foreign key into the DistributionServer table.
		ServerUID		binary(16) NOT NULL,
		-- The id for the Requested PackageVersion.
		RequestedVersionID	int NULL,
		-- The state of a package that is pending distribution.
		-- The possible values are empty (literal string), pending, removing.
		-- If the RequestState field contains a value other than the literal
		-- string empty, the RequestState overrides the ConfirmedState of the package.
		RequestState		nvarchar(16) NULL,
		-- The date and time at which the package distribution began. Only used for distributions currently in progress.
		RequestDate		datetime NULL,
		-- The id for the Existing PackageVersion
		ConfirmedVersionID	int NULL,
		-- The state of the package currently on the distribution server or distribution
		-- location.The possible values are available, unavailable.
		-- If the RequestState field contains a value other than the literal string empty,
		-- then the RequestState overrides the ConfirmedState of the package.
		ConfirmedState		nvarchar(16) NULL,
		-- The date and time that the current distribution status of a package was recorded.
		ConfirmedDate		datetime NULL,
		-- The reason that package distribution failed.This is only specified in the case of a failure.
		ConfirmedReason		ntext NULL,
		CONSTRAINT FK_DistributedPackage_Confirmed
			FOREIGN KEY(ConfirmedVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE NO ACTION,
		CONSTRAINT FK_DistributedPackage_Requested
			FOREIGN KEY(RequestedVersionID)
			REFERENCES PackageVersion(PackageVersionID) ON DELETE NO ACTION,
		CONSTRAINT FK_DistributedPackage_ServerUID
			FOREIGN KEY(ServerUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE,
		CONSTRAINT PK_DistributedPackage
			PRIMARY KEY NONCLUSTERED(DistributedPackageID),
		CONSTRAINT DistributedPackageByServer
			UNIQUE NONCLUSTERED(ServerUID, RequestedVersionID)
	);

	CREATE NONCLUSTERED INDEX DistributedPackageByConfirmedVersion
		ON DistributedPackage(ConfirmedVersionID)

	CREATE NONCLUSTERED INDEX DistributedPackageByRequestedVersion
		ON DistributedPackage(RequestedVersionID)
END
ELSE BEGIN
	PRINT 'Using existing DistributedPackage table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DistributedPackage'
	)
		ALTER TABLE DistributedPackage
		ADD CONSTRAINT PK_DistributedPackage
			PRIMARY KEY NONCLUSTERED(DistributedPackageID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DistributedPackageByServer'
	)
		ALTER TABLE DistributedPackage
		ADD CONSTRAINT DistributedPackageByServer
			UNIQUE NONCLUSTERED(ServerUID, RequestedVersionID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DistributedPackageByConfirmedVersion'
	)
		CREATE NONCLUSTERED INDEX DistributedPackageByConfirmedVersion
			ON DistributedPackage(ConfirmedVersionID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DistributedPackageByRequestedVersion'
	)
		CREATE NONCLUSTERED INDEX DistributedPackageByRequestedVersion
			ON DistributedPackage(RequestedVersionID)
END
GO

-- All defined distribution groups are stored in the DistributionGroup table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DistributionGroup'
)
BEGIN
	CREATE TABLE DistributionGroup (
		-- A unique identifier for this distribution group.
		GroupUID		binary(16) NOT NULL,
		-- The descriptive name assigned to this distribution group.
		GroupName		nvarchar(128) NOT NULL,
		CONSTRAINT PK_DistributionGroup
			PRIMARY KEY NONCLUSTERED(GroupName),
		CONSTRAINT UQ_DistributionGroupID
			UNIQUE CLUSTERED(GroupUID)
	);
END
ELSE BEGIN
	PRINT 'Using existing DistributionGroup table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_DistributionGroupID'
	)
		ALTER TABLE DistributionGroup
		ADD CONSTRAINT UQ_DistributionGroupID
			UNIQUE CLUSTERED(GroupUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DistributionGroup'
	)
		ALTER TABLE DistributionGroup
		ADD CONSTRAINT PK_DistributionGroup
			PRIMARY KEY NONCLUSTERED(GroupName)
END
GO

-- Any distribution servers and distribution locations assigned to distribution
-- groups are stored in the DistributionGroupMember table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DistributionGroupMember'
)
BEGIN
	CREATE TABLE DistributionGroupMember (
		-- A unique identifier for this distribution group. This UID is a foreign
		-- key to the GroupUID in the DistributionGroup table.
		GroupUID		binary(16) NOT NULL,
		-- A unique identifier for the distribution server or distribution location
		-- that is a member of this group. This UID is a foreign key to the ServerUID
		-- in the DistributionServer table.
		MemberID		binary(16) NOT NULL,
		-- An identifier for the type of this distribution group member. This
		-- identifier is a foreign key to the TargetTypeID in the DistributionServerType table.
		MemberType		int NOT NULL,
		CONSTRAINT FK_DistributionGroupMember_GroupUID
			FOREIGN KEY(GroupUID)
			REFERENCES DistributionGroup(GroupUID) ON DELETE CASCADE,
		CONSTRAINT FK_DistributionGroupMember_ServerType
			FOREIGN KEY(MemberType)
			REFERENCES DistributionServerType(DistributionServerTypeID) ON DELETE NO ACTION,
		CONSTRAINT PK_DistributionGroupMember
			PRIMARY KEY CLUSTERED(GroupUID, MemberID)
	);

	CREATE NONCLUSTERED INDEX RemoveByMemberID
		ON DistributionGroupMember(MemberID)
END
ELSE BEGIN
	PRINT 'Using existing DistributionGroupMember table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DistributionGroupMember'
	)
		ALTER TABLE DistributionGroupMember
		ADD CONSTRAINT PK_DistributionGroupMember
			PRIMARY KEY CLUSTERED(GroupUID, MemberID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'RemoveByMemberID'
	)
		CREATE NONCLUSTERED INDEX RemoveByMemberID
			ON DistributionGroupMember(MemberID)
END
GO

-- The LicenseModel table defines the license models available
-- (for example, Site license). Each licensable product (listed
-- in SoftwareProduct) is assigned a license model. A license
-- model may apply to multiple licensable products.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'LicenseModel'
)
BEGIN
	CREATE TABLE LicenseModel (
		-- The unique identifier for a license model.
		ModelID			int IDENTITY NOT NULL,
		-- The name of the license model.
		[Name]			nvarchar(256) NOT NULL,
		CONSTRAINT PK_LicenseModel
			PRIMARY KEY CLUSTERED([Name]),
		CONSTRAINT UQ_LicenseModel
			UNIQUE NONCLUSTERED(ModelID)
	);
END
ELSE BEGIN
	PRINT 'Using existing LicenseModel table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_LicenseModel'
	)
		ALTER TABLE LicenseModel
		ADD CONSTRAINT PK_LicenseModel
			PRIMARY KEY CLUSTERED([Name])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_LicenseModel'
	)
		ALTER TABLE LicenseModel
		ADD CONSTRAINT UQ_LicenseModel
			UNIQUE NONCLUSTERED(ModelID)
END
GO

-- The SoftwarePublisher table lists application publishers (for example, Microsoft).
-- Each licensable product (listed in SoftwareProduct) is assigned a publisher.
-- A publisher may be assigned to multiple licensable products.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwarePublisher'
)
BEGIN
	CREATE TABLE SoftwarePublisher (
		-- The unique identifier for a publisher.
		PublisherID		int IDENTITY NOT NULL,
		-- The name of the publisher.
		[Name]			nvarchar(256) NOT NULL,
		-- The support URL.
		SupportURL		nvarchar(256) NOT NULL DEFAULT '',
		-- The support phone number.
		SupportPhone		nvarchar(256) NOT NULL DEFAULT '',
		-- The name of the contact.
		ContactName		nvarchar(256) NOT NULL DEFAULT '',
		-- An arbitrary comment about the publisher.
		Comments		nvarchar(512) NOT NULL DEFAULT '',
		CONSTRAINT PK_SoftwarePublisher
			PRIMARY KEY CLUSTERED([Name]),
		CONSTRAINT UQ_SoftwarePublisher
			UNIQUE NONCLUSTERED(PublisherID)
	);
END
ELSE BEGIN
	PRINT 'Using existing SoftwarePublisher table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwarePublisher'
	)
		ALTER TABLE SoftwarePublisher
		ADD CONSTRAINT PK_SoftwarePublisher
			PRIMARY KEY CLUSTERED([Name])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwarePublisher'
	)
		ALTER TABLE SoftwarePublisher
		ADD CONSTRAINT UQ_SoftwarePublisher
			UNIQUE NONCLUSTERED(PublisherID)
END
GO

-- The SoftwareReseller table lists application resellers
-- (usually the organization listed on the purchase order for the product).
-- Each licensable product (listed in SoftwareProduct) is assigned an
-- application reseller. A reseller may be assigned to multiple licensable products.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareReseller'
)
BEGIN
	CREATE TABLE SoftwareReseller (
		-- Auto-generated identifier of Reseller
		ResellerID		int IDENTITY NOT NULL,
		-- The name of the reseller.
		[Name]			nvarchar(256) NOT NULL,
		-- The name of the sales contact.
		ContactName		nvarchar(256) NOT NULL DEFAULT '',
		-- The contact phone number.
		ContactPhone		nvarchar(256) NOT NULL DEFAULT '',
		-- An arbitrary comment about the reseller.
		Comments		nvarchar(512) NOT NULL DEFAULT '',
		CONSTRAINT PK_SoftwareReseller
			PRIMARY KEY CLUSTERED([Name]),
		CONSTRAINT UQ_SoftwareReseller
			UNIQUE NONCLUSTERED(ResellerID)
	);
END
ELSE BEGIN
	PRINT 'Using existing SoftwareReseller table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareReseller'
	)
		ALTER TABLE SoftwareReseller
		ADD CONSTRAINT PK_SoftwareReseller
			PRIMARY KEY CLUSTERED([Name])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareReseller'
	)
		ALTER TABLE SoftwareReseller
		ADD CONSTRAINT UQ_SoftwareReseller
			UNIQUE NONCLUSTERED(ResellerID)
END
GO

-- The SoftwareProduct table contains all of the licensable products
-- (license definitions) for an organization. It represents all of
-- the license agreements available for monitoring.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareProduct'
)
BEGIN
	CREATE TABLE SoftwareProduct (
		-- This is a unique identifier for the software product.
		SoftwareProductID	int IDENTITY NOT NULL,
		-- The name of the license. This normally corresponds to the name of the software product as defined by the vendor.
		ProductName		nvarchar(256) NOT NULL,
		-- Reference to the Licensing model for Deployment Manager
		ModelID			int NOT NULL,
		-- In what units are Licences counted?
		TrackedByID		int NOT NULL,
		-- Reference to publisher
		PublisherID		int NOT NULL,
		-- A URL to the license agreement for the product.[Comments]
		Agreement		nvarchar(256) NOT NULL,
		-- Additional comments
		Comments		ntext NULL,
		CONSTRAINT FK_SoftwareProduct_Model
			FOREIGN KEY(ModelID)
			REFERENCES LicenseModel(ModelID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareProduct_Publisher
			FOREIGN KEY(PublisherID)
			REFERENCES SoftwarePublisher(PublisherID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareProduct_TrackedBy
			FOREIGN KEY(TrackedByID)
			REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION,
		CONSTRAINT PK_SoftwareProduct
			PRIMARY KEY CLUSTERED(ProductName),
		CONSTRAINT UQ_SoftwareProduct
			UNIQUE NONCLUSTERED(SoftwareProductID)
	);

	CREATE NONCLUSTERED INDEX SoftwareProductByModel
		ON SoftwareProduct(ModelID)

	CREATE NONCLUSTERED INDEX SoftwareProductByPublisher
		ON SoftwareProduct(PublisherID)
END
ELSE BEGIN
	PRINT 'Using existing SoftwareProduct table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareProduct'
	)
		ALTER TABLE SoftwareProduct
		ADD CONSTRAINT PK_SoftwareProduct
			PRIMARY KEY CLUSTERED(ProductName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareProduct'
	)
		ALTER TABLE SoftwareProduct
		ADD CONSTRAINT UQ_SoftwareProduct
			UNIQUE NONCLUSTERED(SoftwareProductID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareProductByModel'
	)
		CREATE NONCLUSTERED INDEX SoftwareProductByModel
			ON SoftwareProduct(ModelID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareProductByPublisher'
	)
		CREATE NONCLUSTERED INDEX SoftwareProductByPublisher
			ON SoftwareProduct(PublisherID)
END
GO

-- The LicenseAllocation table specifies the allocation of licenses for each
-- organizational unit. The same licensable product definition (from SoftwareProduct)
-- may have license allocations for more than one organizational unit.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'LicenseAllocation'
)
BEGIN
	CREATE TABLE LicenseAllocation (
		-- Unique identifier for the license allocation record. This is automatically generated by SQL Server.
		AllocationID		int IDENTITY NOT NULL,
		-- The license that maps to an application.
		-- This is a foreign key into the SoftwareProduct table.
		SoftwareProductID	int NOT NULL,
		-- Id of the organisational unit to which the software is allocated.
		OrganizationID		int NOT NULL,
		-- The number of units allocated for the application.
		UnitsAllocated		int NULL,
		-- The date and time that the license allocation expires.
		Expiry			datetime NULL,
		CONSTRAINT FK_LicenseAllocation_OrganizationID
			FOREIGN KEY(OrganizationID)
			REFERENCES Organization(OrganizationID) ON DELETE CASCADE,
		CONSTRAINT FK_LicenseAllocation_SoftwareProductID
			FOREIGN KEY(SoftwareProductID)
			REFERENCES SoftwareProduct(SoftwareProductID) ON DELETE CASCADE,
		CONSTRAINT PK_LicenseAllocation
			PRIMARY KEY NONCLUSTERED(AllocationID)
	);

	CREATE NONCLUSTERED INDEX LicenseAllocationByOU
		ON LicenseAllocation(OrganizationID, SoftwareProductID)

	CREATE NONCLUSTERED INDEX LicenseAllocationBySoftwareProduct
		ON LicenseAllocation(SoftwareProductID, OrganizationID)
END
ELSE BEGIN
	PRINT 'Using existing LicenseAllocation table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_LicenseAllocation'
	)
		ALTER TABLE LicenseAllocation
		ADD CONSTRAINT PK_LicenseAllocation
			PRIMARY KEY NONCLUSTERED(AllocationID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'LicenseAllocationByOU'
	)
		CREATE NONCLUSTERED INDEX LicenseAllocationByOU
			ON LicenseAllocation(OrganizationID, SoftwareProductID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'LicenseAllocationBySoftwareProduct'
	)
		CREATE NONCLUSTERED INDEX LicenseAllocationBySoftwareProduct
			ON LicenseAllocation(SoftwareProductID, OrganizationID)
END
GO

-- LicensePurchase records details of purchases of licenses for
-- a specified SoftwareProduct.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'LicensePurchase'
)
BEGIN
	CREATE TABLE LicensePurchase (
		-- The SoftwareProduct purchased.
		SoftwareProductID	int NOT NULL,
		-- The Reseller from which the software product was purchased.
		ResellerID		int NOT NULL,
		-- The organizational unit that owns the license for the product.
		OrganizationID		int NOT NULL,
		-- When the purchase was made.
		Purchased		datetime NOT NULL,
		-- When the license expires.
		Expires			datetime NULL,
		-- The price paid for the license.
		Price			int NULL,
		-- Number of units licensed.
		Quantity		int NOT NULL DEFAULT 1,
		-- Cross-reference to customer's purchase order number.
		OrderNumber		nvarchar(32) NOT NULL,
		CONSTRAINT FK_LicensePurchase_SoftwareReseller
			FOREIGN KEY(ResellerID)
			REFERENCES SoftwareReseller(ResellerID) ON DELETE CASCADE,
		CONSTRAINT FK_LicensePurchase_Organization
			FOREIGN KEY(OrganizationID)
			REFERENCES Organization(OrganizationID) ON DELETE CASCADE,
		CONSTRAINT FK_LicensePurchase
			FOREIGN KEY(SoftwareProductID)
			REFERENCES SoftwareProduct(SoftwareProductID) ON DELETE CASCADE,
		CONSTRAINT PK_LicensePurchase
			PRIMARY KEY CLUSTERED
			(SoftwareProductID, OrganizationID, ResellerID, Purchased, OrderNumber)
	);

	CREATE NONCLUSTERED INDEX LicensePurchaseByOrganizationID
		ON LicensePurchase(OrganizationID)

	CREATE NONCLUSTERED INDEX LicensePurchaseByResellerID
		ON LicensePurchase(ResellerID)
END
ELSE BEGIN
	PRINT 'Using existing LicensePurchase table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_LicensePurchase'
	)
		ALTER TABLE LicensePurchase
		ADD CONSTRAINT PK_LicensePurchase
			PRIMARY KEY CLUSTERED
			(SoftwareProductID, OrganizationID, ResellerID, Purchased, OrderNumber)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'LicensePurchaseByOrganizationID'
	)
		CREATE NONCLUSTERED INDEX LicensePurchaseByOrganizationID
			ON LicensePurchase(OrganizationID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'LicensePurchaseByResellerID'
	)
		CREATE NONCLUSTERED INDEX LicensePurchaseByResellerID
			ON LicensePurchase(ResellerID)
END
GO

-- The ProductContainsSoftware table lists the applications returned by
-- inventory (in the SoftwareVersion table) that are covered by licensable
-- products (listed in SoftwareProduct). A license can map to multiple
-- applications: if any of these applications is installed, a
-- license is required.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ProductContainsSoftware'
)
BEGIN
	CREATE TABLE ProductContainsSoftware (
		-- The license that maps to an application. This is a foreign key into the SoftwareProduct table.
		SoftwareProductID	int NOT NULL,
		-- The application maps to the license. This is a foreign key into the SoftwareVersion table.
		SoftwareVersionID	int NOT NULL,
		CONSTRAINT FK_ProductContains_SoftwareProductID
			FOREIGN KEY(SoftwareProductID)
			REFERENCES SoftwareProduct(SoftwareProductID) ON DELETE CASCADE,
		CONSTRAINT FK_ProductContains_SoftwareVersionID
			FOREIGN KEY(SoftwareVersionID)
			REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE,
		CONSTRAINT PK_ProductContainsSoftware
			PRIMARY KEY CLUSTERED(SoftwareVersionID, SoftwareProductID)
	);

	CREATE NONCLUSTERED INDEX SoftwareVersionByProduct
		ON ProductContainsSoftware(SoftwareProductID)
END
ELSE BEGIN
	PRINT 'Using existing ProductContainsSoftware table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ProductContainsSoftware'
	)
		ALTER TABLE ProductContainsSoftware
		ADD CONSTRAINT PK_ProductContainsSoftware
			PRIMARY KEY CLUSTERED(SoftwareVersionID, SoftwareProductID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareVersionByProduct'
	)
		CREATE NONCLUSTERED INDEX SoftwareVersionByProduct
			ON ProductContainsSoftware(SoftwareProductID)
END
GO

-- The Policy table correlates the GUID of an Active Directory
-- policy with its display name. This is used in reporting and
-- in .npl files.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Policy'
)
BEGIN
	CREATE TABLE Policy (
		-- The GUID of the policy in Active Directory.
		GUID			binary(16) NOT NULL,
		-- Organization id of the domain in which the policy resides.
		DomainID		int NULL,
		-- The display name for the policy, for use in .npl files.
		DisplayName		nvarchar(512) NULL,
		-- The Access Control List for the policy, represented as an anonymous Group.
		AccessGroupID		int NULL,
		-- This policy has been enabled for software management for users.
		EnabledForUsers		bit NOT NULL DEFAULT 1,
		-- This policy has been enabled for software management for computers.
		EnabledForComputers	bit NOT NULL DEFAULT 1,
		CONSTRAINT FK_Policy_Domain
			FOREIGN KEY(DomainID)
			REFERENCES Domain(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT FK_Policy_AccessGroup
			FOREIGN KEY(AccessGroupID)
			REFERENCES [Group](GroupID) ON DELETE NO ACTION,
		CONSTRAINT PK_Policy
			PRIMARY KEY CLUSTERED(GUID)
	);

	CREATE NONCLUSTERED INDEX PolicyByDomain
		ON Policy(DomainID)

	CREATE NONCLUSTERED INDEX PolicyByAccessGroupID
		ON Policy(AccessGroupID)

	CREATE NONCLUSTERED INDEX PolicyByDomainAndDisplayName
		ON Policy(DomainID, DisplayName)
END
ELSE BEGIN
	PRINT 'Using existing Policy table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Policy'
	)
		ALTER TABLE Policy
		ADD CONSTRAINT PK_Policy
			PRIMARY KEY CLUSTERED(GUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PolicyByDomain'
	)
		CREATE NONCLUSTERED INDEX PolicyByDomain
			ON Policy(DomainID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PolicyByAccessGroupID'
	)
		CREATE NONCLUSTERED INDEX PolicyByAccessGroupID
			ON Policy(AccessGroupID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PolicyByDomainAndDisplayName'
	)
		CREATE NONCLUSTERED INDEX PolicyByDomainAndDisplayName
			ON Policy(DomainID, DisplayName)
END
GO

-- The AllocationDetails table contains various details that, when taken together,
-- describe the rules under which a particular software policy (allocation) should
-- be applied. One row is created in this table for each distinct set of values,
-- and these collected details may apply to many packages and to many users or
-- computers.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'AllocationDetails'
)
BEGIN
	CREATE TABLE AllocationDetails (
		-- Auto-generated unique identifier
		AllocationDetailsID	int IDENTITY NOT NULL,
		-- An action that indicates whether the package is mandatory or
		-- optional for the user or computer. Possible values are
		-- "install" (for mandatory installations), "choose" (for
		-- optional installations) and "alwaysupdate" (for installations
		-- that are optional, but with updates to existing installations
		-- of the same package being mandatory).
		Action			varchar(16) NULL,
		-- Date and time at which the policy becomes effective. Prior to
		-- this date and time, the package will not be installed.
		EffectiveFrom		datetime NULL,
		-- Indicates whether the date and time in the EffectiveFrom
		-- field is local time on the managed device, or UTC time.	 The
		-- possible values are zero for UTC time and one for local time.
		EffectiveFromIsLocal	bit NULL DEFAULT 1,
		-- Date and time at which the policy ceases to be effective. After
		-- this date and time, the package will not be installed. The time
		-- zone used is the same as for EffectiveFrom.
		EffectiveUntil		datetime NULL,
		-- Indicates whether ManageSoft should use the Wake on LAN feature
		-- to wake a managed device when the package is to be installed.
		-- For more information on this feature, see the ManageSoft Software Deployment Guide.
		Wake			bit NULL DEFAULT 0,
		-- Latest absolute time until which an end-user may postpone
		-- installation of this package. Only mandatory package
		-- installations can be postponed (that is, packages for which
		-- Action is either install or alwaysupdate).
		PostponeNoLaterThan	datetime NULL,
		-- Indicates whether PostponeNoLaterThan should be interpreted
		-- as a local time on each managed device (1), or as a UTC time
		-- (0). The value of this field should be ignored if
		-- PostponeNoLaterThan is NULL.
		PostponeNoLaterThanIsLocal bit NOT NULL DEFAULT 0,
		-- Number of seconds for which package installations may be
		-- postponed by an end-user after this policy first applies.
		-- Only mandatory package installations can be postponed (that
		-- is, packages for which Action is either install or
		-- alwaysupdate).
		PostponePeriod		int NULL,
		-- Indicates whether package installation may be postponed to the
		-- latest (1) or earliest (0) date indicated by the
		-- PostponeNoLaterThan and PostponePeriod fields if both of
		-- those fields are set. The value of this field should be
		-- ignored if either PostponeNoLaterThan or PostponePeriod
		-- are NULL.
		PostponeLatest		bit NOT NULL DEFAULT 1,
		-- Indicates whether a valid reason for postponing installation
		-- of this package is because the managed device is connected to
		-- a distribution location via a "slow" network connection (as
		-- determined by the NetworkHighSpeed preference on the managed
		-- device).
		PostponeOKForLowBandwidth bit NOT NULL DEFAULT 1,
		-- Indicates whether installation of this package can be
		-- postponed for any reason at the discretion of the end-user on
		-- the managed device on which this package is to be installed.
		PostponeOKForAnyReason	bit NOT NULL DEFAULT 1,
		-- Whether (1) or not (0, default) to uninstall the package when
		-- it is removed from policy. This value is retrieved from Active
		-- Directory.
		Exclusive		bit NOT NULL DEFAULT 0,
		CONSTRAINT PK_AllocationDetails
			PRIMARY KEY NONCLUSTERED(AllocationDetailsID),
		CONSTRAINT UQ_AllocationDetails
			UNIQUE NONCLUSTERED
			(Action, EffectiveFrom, EffectiveFromIsLocal, EffectiveUntil, Wake, PostponeNoLaterThan, PostponeNoLaterThanIsLocal, PostponePeriod, PostponeLatest, PostponeOKForLowBandwidth, PostponeOKForAnyReason, Exclusive)
	);
END
ELSE BEGIN
	PRINT 'Using existing AllocationDetails table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_AllocationDetails'
	)
		ALTER TABLE AllocationDetails
		ADD CONSTRAINT PK_AllocationDetails
			PRIMARY KEY NONCLUSTERED(AllocationDetailsID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_AllocationDetails'
	)
		ALTER TABLE AllocationDetails
		ADD CONSTRAINT UQ_AllocationDetails
			UNIQUE NONCLUSTERED
			(Action, EffectiveFrom, EffectiveFromIsLocal, EffectiveUntil, Wake, PostponeNoLaterThan, PostponeNoLaterThanIsLocal, PostponePeriod, PostponeLatest, PostponeOKForLowBandwidth, PostponeOKForAnyReason, Exclusive)
END
GO

-- A PackageAllocation row exists for every PackagePath which has been approved
-- to a Policy.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageAllocation'
)
BEGIN
	CREATE TABLE PackageAllocation (
		-- The Policy to which the PackagePath has been approved.
		-- This is a foreign key into the Policy table.
		PolicyGUID		binary(16) NOT NULL,
		-- The PackagePath which has been approved.
		-- This is a foreign key into the PackagePath table.
		PackagePathID		int NOT NULL,
		-- The Group to which the package applies.
		-- This group will have a NULL GroupCN; it's an Access Control List (ACL) group.
		-- This is a foreign key into the Group table.
		AccessGroupID		int NULL,
		-- The target type of the package. Possible values are 1 (computer
		-- policy), 2 (user policy).
		-- This is a foreign key into the TargetType table.
		TargetTypeID		int NOT NULL,
		-- The order of application of this package within this policy.
		-- The default value is 0.
		Precedence		int NOT NULL DEFAULT 0,
		-- The details indicating how and when this package should be installed.
		-- This is a foreign key into the AllocationDetails table.
		AllocationDetailsID	int NOT NULL,
		CONSTRAINT FK_Allocation_AccessGroup
			FOREIGN KEY(AccessGroupID)
			REFERENCES [Group](GroupID) ON DELETE CASCADE,
		CONSTRAINT FK_Allocation_Policy
			FOREIGN KEY(PolicyGUID)
			REFERENCES Policy(GUID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageAllocation_TargetType
			FOREIGN KEY(TargetTypeID)
			REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION,
		CONSTRAINT FK_Allocation_PackagePath
			FOREIGN KEY(PackagePathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE,
		CONSTRAINT FK_Allocation_Details
			FOREIGN KEY(AllocationDetailsID)
			REFERENCES AllocationDetails(AllocationDetailsID) ON DELETE CASCADE,
		CONSTRAINT PK_PackageAllocation
			PRIMARY KEY NONCLUSTERED
			(PolicyGUID, TargetTypeID, PackagePathID, AllocationDetailsID)
	);

	CREATE NONCLUSTERED INDEX PackageAllocationByPackagePathID
		ON PackageAllocation(PackagePathID)

	CREATE NONCLUSTERED INDEX PackageAllocationByAllocationDetailsID
		ON PackageAllocation(AllocationDetailsID)

	CREATE NONCLUSTERED INDEX PackageAllocationByAccessGroupID
		ON PackageAllocation(AccessGroupID)
END
ELSE BEGIN
	PRINT 'Using existing PackageAllocation table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageAllocation'
	)
		ALTER TABLE PackageAllocation
		ADD CONSTRAINT PK_PackageAllocation
			PRIMARY KEY NONCLUSTERED
			(PolicyGUID, TargetTypeID, PackagePathID, AllocationDetailsID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageAllocationByPackagePathID'
	)
		CREATE NONCLUSTERED INDEX PackageAllocationByPackagePathID
			ON PackageAllocation(PackagePathID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageAllocationByAllocationDetailsID'
	)
		CREATE NONCLUSTERED INDEX PackageAllocationByAllocationDetailsID
			ON PackageAllocation(AllocationDetailsID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageAllocationByAccessGroupID'
	)
		CREATE NONCLUSTERED INDEX PackageAllocationByAccessGroupID
			ON PackageAllocation(AccessGroupID)
END
GO

-- The PolicyApplies table stores the identities of
-- the principals to whom each policy applies, whether or not
-- any packages or schedules apply.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PolicyApplies'
)
BEGIN
	CREATE TABLE PolicyApplies (
		-- Organizational id of the domain in which the policy resides.
		DomainID		int NOT NULL,
		-- The OUID of the user or computer to whom the policy applies.
		-- Foreign key (unchecked) into the Organization table.
		TargetOUID		int NOT NULL,
		-- The target type of the package.
		-- Possible values are 1 (computer policy), 2 (user policy).
		-- This is a foreign key into the TargetType table.
		TargetTypeID		int NOT NULL,
		-- The user or computer to whom the policy applies.
		-- Foreign key (unchecked) into the User or Computer table.
		TargetID		int NOT NULL,
		-- The GUID of the policy in Active Directory.
		GUID			binary(16) NOT NULL,
		CONSTRAINT FK_PolicyApplies_Domain
			FOREIGN KEY(DomainID)
			REFERENCES Domain(OrganizationID) ON DELETE NO ACTION,
		CONSTRAINT FK_PolicyApplies_OU
			FOREIGN KEY(TargetOUID)
			REFERENCES Organization(OrganizationID) ON DELETE CASCADE,
		CONSTRAINT FK_PolicyApplies_TargetType
			FOREIGN KEY(TargetTypeID)
			REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION,
		CONSTRAINT FK_PolicyApplies_Policy
			FOREIGN KEY(GUID)
			REFERENCES Policy(GUID) ON DELETE CASCADE,
		CONSTRAINT PK_PolicyApplies
			PRIMARY KEY CLUSTERED
			(TargetOUID, TargetTypeID, TargetID, GUID)
	);

	CREATE NONCLUSTERED INDEX PolicyAppliesByPolicy
		ON PolicyApplies(GUID, TargetOUID)

	CREATE NONCLUSTERED INDEX PolicyAppliesByDomain
		ON PolicyApplies(DomainID)
END
ELSE BEGIN
	PRINT 'Using existing PolicyApplies table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PolicyApplies'
	)
		ALTER TABLE PolicyApplies
		ADD CONSTRAINT PK_PolicyApplies
			PRIMARY KEY CLUSTERED
			(TargetOUID, TargetTypeID, TargetID, GUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PolicyAppliesByPolicy'
	)
		CREATE NONCLUSTERED INDEX PolicyAppliesByPolicy
			ON PolicyApplies(GUID, TargetOUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PolicyAppliesByDomain'
	)
		CREATE NONCLUSTERED INDEX PolicyAppliesByDomain
			ON PolicyApplies(DomainID)
END
GO

-- The PackageApplies table stores the resultant set of policy
-- (RSoP) for all users and computers. It represents what
-- packages each user and computer should have installed,
-- whereas Installation represents what they actually have
-- installed.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PackageApplies'
)
BEGIN
	CREATE TABLE PackageApplies (
		-- The target type of the package. Possible values are 1 (computer policy),
		-- 2 (user policy). This is a foreign key into the TargetType table.
		TargetTypeID		int NOT NULL,
		-- The organizational unit of the user or computer targeted
		-- by the package.  This is a foreign key into the
		-- Organization table. This column is included in the table
		-- for clustering purposes. The value of TargetOUID could be
		-- determined by looking up the User or Computer table.
		TargetOUID		int NOT NULL,
		-- The user or computer targeted by the package. This is a foreign key into the User or Computer table.
		TargetID		int NOT NULL,
		-- The package applied by the Policy.
		PackagePathID		int NOT NULL,
		-- The GUID of the group policy in Active Directory that records whether the package is targeted
		-- to the user or computer.
		PolicyGUID		binary(16) NOT NULL,
		-- The details indicating how and when this package should be installed.
		-- This is a foreign key into the AllocationDetails table.
		AllocationDetailsID	int NOT NULL,
		-- The order of application of this policy for this target device, defaults to 0.
		Precedence		int NOT NULL DEFAULT 0,
		-- Policy version number from the underlying directory service.
		PolicyVersion		int NOT NULL DEFAULT 0,
		CONSTRAINT FK_PackageApplies_PackagePath
			FOREIGN KEY(PackagePathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageApplies_Policy
			FOREIGN KEY(PolicyGUID)
			REFERENCES Policy(GUID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageApplies_AllocationDetails
			FOREIGN KEY(AllocationDetailsID)
			REFERENCES AllocationDetails(AllocationDetailsID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageApplies_OU
			FOREIGN KEY(TargetOUID)
			REFERENCES Organization(OrganizationID) ON DELETE CASCADE,
		CONSTRAINT FK_PackageApplies_TargetType
			FOREIGN KEY(TargetTypeID)
			REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION,
		CONSTRAINT PK_PackageApplies
			PRIMARY KEY CLUSTERED
			(TargetOUID, TargetTypeID, TargetID, PackagePathID, PolicyGUID)
	);

	CREATE NONCLUSTERED INDEX PackageAppliesByAllocationDetailsID
		ON PackageApplies(AllocationDetailsID)

	CREATE NONCLUSTERED INDEX PackageAppliesByPackagePathID
		ON PackageApplies(PackagePathID)

	CREATE NONCLUSTERED INDEX PackageAppliesByPolicyGUID
		ON PackageApplies(PolicyGUID)
END
ELSE BEGIN
	PRINT 'Using existing PackageApplies table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_PackageApplies'
	)
		ALTER TABLE PackageApplies
		ADD CONSTRAINT PK_PackageApplies
			PRIMARY KEY CLUSTERED
			(TargetOUID, TargetTypeID, TargetID, PackagePathID, PolicyGUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageAppliesByAllocationDetailsID'
	)
		CREATE NONCLUSTERED INDEX PackageAppliesByAllocationDetailsID
			ON PackageApplies(AllocationDetailsID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageAppliesByPackagePathID'
	)
		CREATE NONCLUSTERED INDEX PackageAppliesByPackagePathID
			ON PackageApplies(PackagePathID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PackageAppliesByPolicyGUID'
	)
		CREATE NONCLUSTERED INDEX PackageAppliesByPolicyGUID
			ON PackageApplies(PolicyGUID)
END
GO

-- The InstallationPostponement table stores the resultant set of policy
-- (RSoP) for all users and computers. It represents what
-- packages each user and computer should have installed,
-- whereas Installation represents what they actually have
-- installed.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'InstallationPostponement'
)
BEGIN
	CREATE TABLE InstallationPostponement (
		-- The target type of the package. Possible values are 1 (computer policy),
		-- 2 (user policy). This is a foreign key into the TargetType table.
		TargetTypeID		int NOT NULL,
		-- The user or computer targeted by the package. This is a foreign key into the User or Computer table.
		TargetID		int NOT NULL,
		-- The package applied by the Policy.
		PackagePathID		int NOT NULL,
		-- The GUID of the group policy in Active Directory that records whether the package is targeted
		-- to the user or computer.
		PolicyGUID		binary(16) NOT NULL,
		-- The details indicating how and when this package should be installed.
		-- This is a foreign key into the AllocationDetails table.
		AllocationDetailsID	int NOT NULL,
		-- UTC time at which any postponement period for this
		-- policy started. The postponement period for
		-- installation of this package (that is, the period
		-- during which end-users may postpone installation
		-- of this package) will end at this time plus any
		-- period specified by PostponePeriod. After the
		-- postponement period ends, the installation agent
		-- will attempt to force the package to be installed.
		-- This field is NULL until and unless an
		-- installation event log is received from the
		-- managed device indicating that the installation of
		-- the package has in fact been postponed. If
		-- PostponePeriod is NULL, the value of
		-- PostponePeriodStart represents the time at which
		-- the installation of this package was first
		-- postponed.
		-- 
		-- An example SQL query to determine the expected
		-- (UTC) time at which the postponement period for
		-- this package will expire can be written as shown
		-- below. Note that this query will convert any local
		-- time PostponeNoLaterThan value to UTC using the
		-- timezone configured on the SQL Server.
		-- 
		-- SELECT
		-- *
		-- , CASE
		-- WHEN
		-- PostponePeriod IS NULL
		-- OR PostponePeriodStart IS NULL
		-- OR (PostponeLatest = 1 AND PostponePeriodEndUTC <= PostponeNoLaterThanUTC)
		-- OR (PostponeLatest = 0 AND PostponePeriodEndUTC >= PostponeNoLaterThanUTC)
		-- THEN PostponeNoLaterThanUTC
		-- ELSE PostponePeriodEndUTC
		-- END AS PostponeLatestUTC
		-- FROM (
		-- SELECT
		-- *
		-- , CASE PostponeNoLaterThanIsLocal
		-- WHEN 1 THEN DATEADD(s, DATEDIFF(s, GETDATE(), GETUTCDATE()), PostponeNoLaterThan)
		-- ELSE PostponeNoLaterThan
		-- END
		-- AS PostponeNoLaterThanUTC
		-- , DATEADD(s, PostponePeriod, PostponePeriodStart)
		-- AS PostponePeriodEndUTC
		-- FROM Targetedpackage
		-- ) tp
		PostponePeriodStart	datetime NULL,
		CONSTRAINT FK_InstallationPostponement_TargetType
			FOREIGN KEY(TargetTypeID)
			REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION,
		CONSTRAINT FK_InstallationPostponement_PackagePath
			FOREIGN KEY(PackagePathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE,
		CONSTRAINT FK_InstallationPostponement_Policy
			FOREIGN KEY(PolicyGUID)
			REFERENCES Policy(GUID) ON DELETE CASCADE,
		CONSTRAINT FK_InstallationPostponement_AllocationDetails
			FOREIGN KEY(AllocationDetailsID)
			REFERENCES AllocationDetails(AllocationDetailsID) ON DELETE CASCADE,
		CONSTRAINT PK_InstallationPostponement
			PRIMARY KEY CLUSTERED
			(TargetTypeID, TargetID, PackagePathID, PolicyGUID)
	);

	CREATE NONCLUSTERED INDEX InstallationPostponementByAllocationDetailsID
		ON InstallationPostponement(AllocationDetailsID)

	CREATE NONCLUSTERED INDEX InstallationPostponementByPackagePathID
		ON InstallationPostponement(PackagePathID)

	CREATE NONCLUSTERED INDEX InstallationPostponementByPolicyGUID
		ON InstallationPostponement(PolicyGUID)
END
ELSE BEGIN
	PRINT 'Using existing InstallationPostponement table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_InstallationPostponement'
	)
		ALTER TABLE InstallationPostponement
		ADD CONSTRAINT PK_InstallationPostponement
			PRIMARY KEY CLUSTERED
			(TargetTypeID, TargetID, PackagePathID, PolicyGUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationPostponementByAllocationDetailsID'
	)
		CREATE NONCLUSTERED INDEX InstallationPostponementByAllocationDetailsID
			ON InstallationPostponement(AllocationDetailsID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationPostponementByPackagePathID'
	)
		CREATE NONCLUSTERED INDEX InstallationPostponementByPackagePathID
			ON InstallationPostponement(PackagePathID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'InstallationPostponementByPolicyGUID'
	)
		CREATE NONCLUSTERED INDEX InstallationPostponementByPolicyGUID
			ON InstallationPostponement(PolicyGUID)
END
GO

-- The role of a network device.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DeviceRole'
)
BEGIN
	CREATE TABLE DeviceRole (
		-- The id of the device role.
		DeviceRoleID		int IDENTITY NOT NULL,
		-- The name of the device role.
		Description		nvarchar(64) NOT NULL,
		CONSTRAINT UQ_DeviceRole
			UNIQUE NONCLUSTERED(DeviceRoleID),
		CONSTRAINT PK_DeviceRole
			PRIMARY KEY NONCLUSTERED(Description)
	);
END
ELSE BEGIN
	PRINT 'Using existing DeviceRole table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_DeviceRole'
	)
		ALTER TABLE DeviceRole
		ADD CONSTRAINT UQ_DeviceRole
			UNIQUE NONCLUSTERED(DeviceRoleID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DeviceRole'
	)
		ALTER TABLE DeviceRole
		ADD CONSTRAINT PK_DeviceRole
			PRIMARY KEY NONCLUSTERED(Description)
END
GO

-- Stores a mapping from a ASN ObjectID (OID) to a type of device.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ASN1Object'
)
BEGIN
	CREATE TABLE ASN1Object (
		-- ASN object identifier.
		OID			nvarchar(128) NOT NULL,
		-- The fully expanded text version of the object identifier.
		Description		nvarchar(512) NOT NULL,
		-- What role does the device perform?
		ObjectRole		int NULL,
		CONSTRAINT FK_ASN1Object_DeviceRole
			FOREIGN KEY(ObjectRole)
			REFERENCES DeviceRole(DeviceRoleID) ON DELETE NO ACTION,
		CONSTRAINT PK_ASN1Object
			PRIMARY KEY NONCLUSTERED(OID)
	);
END
ELSE BEGIN
	PRINT 'Using existing ASN1Object table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ASN1Object'
	)
		ALTER TABLE ASN1Object
		ADD CONSTRAINT PK_ASN1Object
			PRIMARY KEY NONCLUSTERED(OID)
END
GO

-- A NetworkDevice is a loose record of the discovery of a
-- device on a network, using any of a number of discovery
-- methods. As such, the same device may be found in more than
-- one way (see DuplicateID which may be able to track this fact
-- if known), or by more than one distinguishing feature.
-- Accordingly this table has a somewhat unsatisfactory primary
-- key!
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'NetworkDevice'
)
BEGIN
	CREATE TABLE NetworkDevice (
		-- Auto-generated identity number.
		DeviceID		int IDENTITY NOT NULL,
		-- Reference to another discovery record for this device,
		-- if known.
		DuplicateID		int NULL,
		-- ManageSoft computer id, if known
		ComputerID		int NULL,
		-- NetBIOS name for computers or any name for other devices,
		-- if known.
		DeviceName		nvarchar(64) NULL,
		-- Fully qualified DNS name, if known.
		DNSFullName		nvarchar(256) NULL,
		-- NT domain name, if known.
		NTDomainName		nvarchar(256) NULL,
		-- IP address of the device.
		IPAddress		varchar(64) NULL,
		-- IP subnet that contains the node.
		IPSubnet		varchar(64) NULL,
		-- IP subnet mask for the subnet contains the device.
		IPSubnetMask		varchar(64) NULL,
		-- Network adapter physical address of the node.
		-- Can be a MAC address or token ring address.
		PhysicalAddress		varchar(64) NULL,
		-- What role does the device perform?
		-- NULL = unknown
		-- 0 = Computer (don't know if server or workstation)
		-- 1 = Workstation
		-- 2 = Server
		-- 3 = Printer
		-- 4 = Switch
		-- 5 = Router
		-- 6 = Hub
		DeviceRole		int NULL,
		-- Operating system of the node, if it is a computer.
		OperatingSystem		nvarchar(128) NULL,
		-- Operating system of the node, if it is a computer.
		OperatingSystemID	int NULL,
		-- Is the device to be managed by ManageSoft?
		-- 0 = no, 1 = yes, NULL = unknown.
		IsManaged		int NULL,
		-- Customer-entered description of the device.
		Description		nvarchar(256) NULL,
		-- This field is currently unused.
		SystemDescription	nvarchar(256) NULL,
		-- This field is currently unused.
		SystemLocation		nvarchar(256) NULL,
		-- This field is currently unused.
		SystemContact		nvarchar(256) NULL,
		-- The date and time that the node was first discovered.
		FirstDiscovered		datetime NOT NULL DEFAULT getdate(),
		-- The last time the node was checked or updated.
		LastUpdate		datetime NOT NULL DEFAULT getdate(),
		-- A name that identifies where the discovery information came
		-- from (for example: physical location, server, and so on).
		LastDataSourceName	nvarchar(128) NULL,
		-- The type of data source (for example: Excel, Fluke, NM, Text).
		LastDataSourceType	nvarchar(32) NULL,
		-- The comma-delimited list of TCP ports which were found to be open on scan.
		OpenPortsTCP		varchar(512) NULL,
		-- The comma-delimited list of UDP ports which were found to be open on scan.
		OpenPortsUDP		varchar(512) NULL,
		-- The IP scan tool's best guess at the operating system.
		-- This is based on corner cases in the behavior of the network protocol stack.
		ScannedOperatingSystem	nvarchar(512) NULL,
		-- OS Type, as reported by scan tool.
		ScannedOsType		nvarchar(512) NULL,
		-- OS Vendor, as reported by scan tool.
		ScannedOsVendor		nvarchar(512) NULL,
		-- OS family, as reported by scan tool.
		ScannedOsFamily		nvarchar(512) NULL,
		-- OS Generation(Versions), as reported by scan tool.
		ScannedOsGen		nvarchar(512) NULL,
		-- MAC Address, as reported by scan tool.
		ScannedMacAddress	varchar(64) NULL,
		-- MAC Vendor, as reported by scan tool.
		ScannedMacVendor	nvarchar(512) NULL,
		CONSTRAINT FK_NetworkDevice_OperatingSystem
			FOREIGN KEY(OperatingSystemID)
			REFERENCES OperatingSystem(OperatingSystemID) ON DELETE NO ACTION,
		CONSTRAINT FK_NetworkDevice_Duplicate
			FOREIGN KEY(DuplicateID)
			REFERENCES NetworkDevice(DeviceID) ON DELETE NO ACTION,
		CONSTRAINT FK_NetworkDevice_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_NetworkDevice_Role
			FOREIGN KEY(DeviceRole)
			REFERENCES DeviceRole(DeviceRoleID) ON DELETE NO ACTION,
		CONSTRAINT PK_NetworkDevice
			PRIMARY KEY NONCLUSTERED(DeviceID)
	);

	CREATE NONCLUSTERED INDEX NetworkDeviceByComputerID
		ON NetworkDevice(ComputerID)

	CREATE NONCLUSTERED INDEX NetworkDeviceByIsManagedComputerID
		ON NetworkDevice(IsManaged, ComputerID)

	CREATE NONCLUSTERED INDEX NetworkDeviceByDeviceName
		ON NetworkDevice(DeviceName)

	CREATE NONCLUSTERED INDEX NetworkDeviceByDuplicateID
		ON NetworkDevice(DuplicateID)

	CREATE NONCLUSTERED INDEX NetworkDeviceByIPAddress
		ON NetworkDevice(IPAddress)

	CREATE NONCLUSTERED INDEX NetworkDeviceByNTDomainName
		ON NetworkDevice(NTDomainName)

	CREATE NONCLUSTERED INDEX NetworkDeviceByPhysicalAddress
		ON NetworkDevice(PhysicalAddress)

	CREATE NONCLUSTERED INDEX NetworkDeviceByLastDataSourceTypeLastDataSourceName
		ON NetworkDevice(LastDataSourceType, LastDataSourceName)
END
ELSE BEGIN
	PRINT 'Using existing NetworkDevice table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_NetworkDevice'
	)
		ALTER TABLE NetworkDevice
		ADD CONSTRAINT PK_NetworkDevice
			PRIMARY KEY NONCLUSTERED(DeviceID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByComputerID'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByComputerID
			ON NetworkDevice(ComputerID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByIsManagedComputerID'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByIsManagedComputerID
			ON NetworkDevice(IsManaged, ComputerID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByDeviceName'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByDeviceName
			ON NetworkDevice(DeviceName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByDuplicateID'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByDuplicateID
			ON NetworkDevice(DuplicateID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByIPAddress'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByIPAddress
			ON NetworkDevice(IPAddress)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByNTDomainName'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByNTDomainName
			ON NetworkDevice(NTDomainName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByPhysicalAddress'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByPhysicalAddress
			ON NetworkDevice(PhysicalAddress)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceByLastDataSourceTypeLastDataSourceName'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceByLastDataSourceTypeLastDataSourceName
			ON NetworkDevice(LastDataSourceType, LastDataSourceName)
END
GO

-- Records any SNMP information discovered for a NetworkDevice.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'NetworkDeviceSNMPInfo'
)
BEGIN
	CREATE TABLE NetworkDeviceSNMPInfo (
		-- Device identity number.
		DeviceID		int NOT NULL,
		-- The SNMP Community String that was used for obtaining SNMP MIBs.
		SNMP_snmpCommunityString nvarchar(256) NULL,
		-- A textual description of the device.  This value should
		-- include the full name and version identification of the
		-- system's hardware type, software operating-system, and
		-- networking software.
		SNMP_sysDescr		nvarchar(256) NULL,
		-- The vendor's authoritative identification of the network
		-- management subsystem contained in the entity.  This value is
		-- allocated within the SMI enterprises subtree (1.3.6.1.4.1)
		-- and provides an easy and unambiguous means for determining
		-- 'what kind of device' is being managed.  For example, if vendor
		-- 'Flintstones, Inc.' was assigned the subtree
		-- 1.3.6.1.4.1.4242, it could assign the identifier
		-- 1.3.6.1.4.1.4242.1.1 to its 'Fred Router'.
		SNMP_sysObjectID	nvarchar(256) NULL,
		-- The symbolic representation of the same value as sysObjectID.
		SNMP_sysObjectIDSymbolic nvarchar(256) NULL,
		-- The time (in hundredths of a second) since the network
		-- management portion of the system was last re-initialized.
		SNMP_sysUpTime		bigint NULL,
		-- The textual identification of the contact person for this managed node,
		-- together with information on how to contact this person.
		SNMP_sysContact		nvarchar(256) NULL,
		-- An administratively-assigned name for this managed node.
		-- By convention, this is the node's fully-qualified domain name.
		SNMP_sysName		nvarchar(256) NULL,
		-- The physical location of this node (for example, 'telephone closet, 3rd floor').
		SNMP_sysLocation	nvarchar(256) NULL,
		-- a bitmask indicating at which of the seven OSI protocol levels the
		-- system provides services (physical=1, TCP = 8, applications = 64, etc)
		SNMP_sysServices	int NULL,
		-- Set to 1 if the device forwards IP packets, 2 otherwise.
		SNMP_ipForwarding	int NULL,
		CONSTRAINT FK_NetworkDeviceSNMPInfo_NetworkDevice
			FOREIGN KEY(DeviceID)
			REFERENCES NetworkDevice(DeviceID) ON DELETE CASCADE,
		CONSTRAINT PK_NetworkDeviceSNMPInfo
			PRIMARY KEY NONCLUSTERED(DeviceID)
	);
END
ELSE BEGIN
	PRINT 'Using existing NetworkDeviceSNMPInfo table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_NetworkDeviceSNMPInfo'
	)
		ALTER TABLE NetworkDeviceSNMPInfo
		ADD CONSTRAINT PK_NetworkDeviceSNMPInfo
			PRIMARY KEY NONCLUSTERED(DeviceID)
END
GO

-- Records any Intel AMT information discovered for a NetworkDevice.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'NetworkDeviceAMTInfo'
)
BEGIN
	CREATE TABLE NetworkDeviceAMTInfo (
		-- Device identity number.
		DeviceID		int NOT NULL,
		-- Whether the Intel AMT is configured in secure mode on the device.
		Secure			int NULL,
		-- A textual description of the Intel AMT supported on the device.
		Description		nvarchar(256) NULL,
		CONSTRAINT FK_NetworkDeviceAMTInfo_NetworkDevice
			FOREIGN KEY(DeviceID)
			REFERENCES NetworkDevice(DeviceID) ON DELETE CASCADE,
		CONSTRAINT PK_NetworkDeviceAMTInfo
			PRIMARY KEY NONCLUSTERED(DeviceID)
	);
END
ELSE BEGIN
	PRINT 'Using existing NetworkDeviceAMTInfo table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_NetworkDeviceAMTInfo'
	)
		ALTER TABLE NetworkDeviceAMTInfo
		ADD CONSTRAINT PK_NetworkDeviceAMTInfo
			PRIMARY KEY NONCLUSTERED(DeviceID)
END
GO

-- Records the complete set of administration accounts configured on distribution servers.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'AdministrationAccount'
)
BEGIN
	CREATE TABLE AdministrationAccount (
		-- Unique id for the account.
		AccountID		int IDENTITY NOT NULL,
		-- The logical name of the account.
		AccountName		nvarchar(256) NOT NULL,
		CONSTRAINT PK_AccountID
			UNIQUE NONCLUSTERED(AccountID),
		CONSTRAINT UQ_AdministrationAccountByAccountName
			PRIMARY KEY NONCLUSTERED(AccountName)
	);
END
ELSE BEGIN
	PRINT 'Using existing AdministrationAccount table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_AccountID'
	)
		ALTER TABLE AdministrationAccount
		ADD CONSTRAINT PK_AccountID
			UNIQUE NONCLUSTERED(AccountID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_AdministrationAccountByAccountName'
	)
		ALTER TABLE AdministrationAccount
		ADD CONSTRAINT UQ_AdministrationAccountByAccountName
			PRIMARY KEY NONCLUSTERED(AccountName)
END
GO

-- Records an administration account discovered on a distribution server.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DistributionServerAdministrationAccount'
)
BEGIN
	CREATE TABLE DistributionServerAdministrationAccount (
		-- Unique id for the account.
		AccountID		int NOT NULL,
		-- Identifies the distribution server which discovered the account.
		ServerUID		binary(16) NOT NULL,
		CONSTRAINT FK_DSAdministrationAccount_Server
			FOREIGN KEY(ServerUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE,
		CONSTRAINT FK_DSAdministrationAccount_AdministrationAccount
			FOREIGN KEY(AccountID)
			REFERENCES AdministrationAccount(AccountID) ON DELETE CASCADE,
		CONSTRAINT PK_DistributionServerAdministrationAccount
			PRIMARY KEY NONCLUSTERED(ServerUID, AccountID)
	);

	CREATE NONCLUSTERED INDEX DSAdministrationAccountByAccountID
		ON DistributionServerAdministrationAccount(AccountID)

	CREATE NONCLUSTERED INDEX DSAdministrationAccountByServerUID
		ON DistributionServerAdministrationAccount(ServerUID)
END
ELSE BEGIN
	PRINT 'Using existing DistributionServerAdministrationAccount table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DSAdministrationAccountByAccountID'
	)
		CREATE NONCLUSTERED INDEX DSAdministrationAccountByAccountID
			ON DistributionServerAdministrationAccount(AccountID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DSAdministrationAccountByServerUID'
	)
		CREATE NONCLUSTERED INDEX DSAdministrationAccountByServerUID
			ON DistributionServerAdministrationAccount(ServerUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DistributionServerAdministrationAccount'
	)
		ALTER TABLE DistributionServerAdministrationAccount
		ADD CONSTRAINT PK_DistributionServerAdministrationAccount
			PRIMARY KEY NONCLUSTERED(ServerUID, AccountID)
END
GO

-- By which distribution server was this device discovered?
-- Sometimes useful when other identifying features are duplicated,
-- and when the distribution server should do something to the device.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'NetworkDeviceDiscoveredBy'
)
BEGIN
	CREATE TABLE NetworkDeviceDiscoveredBy (
		-- The id of the device discovered.
		DeviceID		int NOT NULL,
		-- The distribution server that discovered it.
		ServerUID		binary(16) NOT NULL,
		-- Does the distribution server have administrative privileges for the device?
		CanAdminister		bit NULL DEFAULT 0,
		-- The date and time that the distribution server last reported its discovery of this device.
		LastUpdate		datetime NOT NULL DEFAULT getdate(),
		-- Account that can administer the device.
		AccountID		int NULL,
		-- Account that can administer the device, overridden by the user.
		AccountIDOverride	int NULL,
		CONSTRAINT FK_NetworkDeviceDiscoveredBy_Account
			FOREIGN KEY(AccountID)
			REFERENCES AdministrationAccount(AccountID) ON DELETE CASCADE,
		CONSTRAINT FK_NetworkDeviceDiscoveredBy_AccountOverride
			FOREIGN KEY(AccountIDOverride)
			REFERENCES AdministrationAccount(AccountID) ON DELETE NO ACTION,
		CONSTRAINT FK_NetworkDeviceDiscoveredBy_Device
			FOREIGN KEY(DeviceID)
			REFERENCES NetworkDevice(DeviceID) ON DELETE CASCADE,
		CONSTRAINT FK_NetworkDeviceDiscoveredBy_Server
			FOREIGN KEY(ServerUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE,
		CONSTRAINT PK_NetworkDeviceDiscoveredBy
			PRIMARY KEY CLUSTERED(DeviceID, ServerUID)
	);

	CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByServerUID
		ON NetworkDeviceDiscoveredBy(ServerUID)

	CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByDeviceID
		ON NetworkDeviceDiscoveredBy(DeviceID)

	CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByAccountID
		ON NetworkDeviceDiscoveredBy(AccountID)

	CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByAccountIDOverride
		ON NetworkDeviceDiscoveredBy(AccountIDOverride)
END
ELSE BEGIN
	PRINT 'Using existing NetworkDeviceDiscoveredBy table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_NetworkDeviceDiscoveredBy'
	)
		ALTER TABLE NetworkDeviceDiscoveredBy
		ADD CONSTRAINT PK_NetworkDeviceDiscoveredBy
			PRIMARY KEY CLUSTERED(DeviceID, ServerUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceDiscoveredByByServerUID'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByServerUID
			ON NetworkDeviceDiscoveredBy(ServerUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceDiscoveredByByDeviceID'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByDeviceID
			ON NetworkDeviceDiscoveredBy(DeviceID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceDiscoveredByByAccountID'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByAccountID
			ON NetworkDeviceDiscoveredBy(AccountID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'NetworkDeviceDiscoveredByByAccountIDOverride'
	)
		CREATE NONCLUSTERED INDEX NetworkDeviceDiscoveredByByAccountIDOverride
			ON NetworkDeviceDiscoveredBy(AccountIDOverride)
END
GO

-- This table stores the information about different types of tasks and their associated IDs.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'TaskType'
)
BEGIN
	CREATE TABLE TaskType (
		-- The id for the task.
		TaskTypeID		int IDENTITY NOT NULL,
		-- The name of the task.
		TaskTypeName		nvarchar(32) NOT NULL,
		CONSTRAINT UQ_TaskTypeID
			UNIQUE CLUSTERED(TaskTypeID),
		CONSTRAINT PK_TaskType
			PRIMARY KEY NONCLUSTERED(TaskTypeName)
	);
END
ELSE BEGIN
	PRINT 'Using existing TaskType table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_TaskTypeID'
	)
		ALTER TABLE TaskType
		ADD CONSTRAINT UQ_TaskTypeID
			UNIQUE CLUSTERED(TaskTypeID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_TaskType'
	)
		ALTER TABLE TaskType
		ADD CONSTRAINT PK_TaskType
			PRIMARY KEY NONCLUSTERED(TaskTypeName)
END
GO

-- This table stores the required information about the task schedule, such as the start and finish times
-- number of retries, delays and other related information.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'TaskSchedule'
)
BEGIN
	CREATE TABLE TaskSchedule (
		-- The id for the task schedule.
		TaskScheduleID		int IDENTITY NOT NULL,
		-- The time that the scheduled task must start.
		StartTime		datetime NULL,
		-- The time that the scheduled task must end.
		EndTime			datetime NULL,
		-- Number of times for task retries.
		RetryCount		int NULL DEFAULT 1,
		-- Number of seconds before a retry occurs in case of a failure.
		MinRetryDelay		int NULL DEFAULT 60,
		-- Number of seconds before the task is repeated.
		RepeatDelay		int NULL DEFAULT 24 * 3600,
		-- Number of tasks that can be run in parallel.
		NumParallelTasks	int NULL DEFAULT 1,
		-- Amount of time before the next task can start.
		SleepBetweenTasks	int NULL DEFAULT 0,
		CONSTRAINT PK_TaskSchedule
			PRIMARY KEY CLUSTERED(TaskScheduleID)
	);
END
ELSE BEGIN
	PRINT 'Using existing TaskSchedule table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_TaskSchedule'
	)
		ALTER TABLE TaskSchedule
		ADD CONSTRAINT PK_TaskSchedule
			PRIMARY KEY CLUSTERED(TaskScheduleID)
END
GO

-- This table stores the information about the tasks.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Task'
)
BEGIN
	CREATE TABLE Task (
		-- The id of the task.
		TaskID			int IDENTITY NOT NULL,
		-- The id for the task type.
		TaskTypeID		int NOT NULL,
		-- The name for the task.
		TaskName		nvarchar(128) NOT NULL,
		-- For a distribution task, which package.
		PackagePathID		int NULL,
		-- The id for the task schedule.
		TaskScheduleID		int NOT NULL,
		CONSTRAINT FK_Task_PackagePath
			FOREIGN KEY(PackagePathID)
			REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE,
		CONSTRAINT FK_Task_TaskType
			FOREIGN KEY(TaskTypeID)
			REFERENCES TaskType(TaskTypeID) ON DELETE NO ACTION,
		CONSTRAINT UQ_TaskID
			UNIQUE CLUSTERED(TaskID),
		CONSTRAINT PK_Task
			PRIMARY KEY NONCLUSTERED(TaskName)
	);

	CREATE NONCLUSTERED INDEX TaskByPackagePathID
		ON Task(PackagePathID)
END
ELSE BEGIN
	PRINT 'Using existing Task table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_TaskID'
	)
		ALTER TABLE Task
		ADD CONSTRAINT UQ_TaskID
			UNIQUE CLUSTERED(TaskID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Task'
	)
		ALTER TABLE Task
		ADD CONSTRAINT PK_Task
			PRIMARY KEY NONCLUSTERED(TaskName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'TaskByPackagePathID'
	)
		CREATE NONCLUSTERED INDEX TaskByPackagePathID
			ON Task(PackagePathID)
END
GO

-- This table stores the information about the jobs.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Job'
)
BEGIN
	CREATE TABLE Job (
		-- The unique id for the job.
		JobUID			binary(16) NOT NULL,
		-- The id for the task.
		TaskID			int NOT NULL,
		-- The unique id for the server.
		ServerUID		binary(16) NOT NULL,
		CONSTRAINT FK_Job_Server
			FOREIGN KEY(ServerUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE,
		CONSTRAINT FK_Job_Task
			FOREIGN KEY(TaskID)
			REFERENCES Task(TaskID) ON DELETE CASCADE,
		CONSTRAINT PK_Job
			PRIMARY KEY NONCLUSTERED(TaskID, ServerUID),
		CONSTRAINT UQ_Job
			UNIQUE NONCLUSTERED(JobUID)
	);

	CREATE NONCLUSTERED INDEX JobByServerUID
		ON Job(ServerUID)
END
ELSE BEGIN
	PRINT 'Using existing Job table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Job'
	)
		ALTER TABLE Job
		ADD CONSTRAINT PK_Job
			PRIMARY KEY NONCLUSTERED(TaskID, ServerUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Job'
	)
		ALTER TABLE Job
		ADD CONSTRAINT UQ_Job
			UNIQUE NONCLUSTERED(JobUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'JobByServerUID'
	)
		CREATE NONCLUSTERED INDEX JobByServerUID
			ON Job(ServerUID)
END
GO

-- All possible states for an action are reflected in a record here.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ActionState'
)
BEGIN
	CREATE TABLE ActionState (
		-- The id for the action state.
		ActionStateID		int IDENTITY NOT NULL,
		-- The name for the action state.
		ActionStateName		nvarchar(32) NOT NULL,
		CONSTRAINT UQ_ActionStateID
			UNIQUE CLUSTERED(ActionStateID),
		CONSTRAINT PK_ActionState
			PRIMARY KEY NONCLUSTERED(ActionStateName)
	);
END
ELSE BEGIN
	PRINT 'Using existing ActionState table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ActionStateID'
	)
		ALTER TABLE ActionState
		ADD CONSTRAINT UQ_ActionStateID
			UNIQUE CLUSTERED(ActionStateID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ActionState'
	)
		ALTER TABLE ActionState
		ADD CONSTRAINT PK_ActionState
			PRIMARY KEY NONCLUSTERED(ActionStateName)
END
GO

-- An Action arising from a Task, to be applied (possibly repeatedly)
-- by an actor (often a distribution server) to a set of target devices.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Action'
)
BEGIN
	CREATE TABLE Action (
		-- The unique identifier for the Action.
		ActionUID		binary(16) NOT NULL,
		-- The Task which gave rise to this Action.
		TaskID			int NOT NULL,
		-- True if this Action has been delegated to a distribution server.
		ServerUID		binary(16) NULL,
		-- The Job which instructed the DS to perform the Action,
		-- if the Job still exists.
		JobUID			binary(16) NULL,
		-- .One of the action states defined in the ActionState table.
		ActionStateID		int NOT NULL,
		-- If Task is of type Distribution, a PackageVersion applies.
		PackageVersionID	int NULL,
		-- If not empty, text describing the reason the Action failed.
		FailureReason		ntext NULL,
		-- The last time that the ActionState was updated. This value
		-- is the UTC date time of the event.
		LastUpdate		datetime NOT NULL DEFAULT getdate(),
		CONSTRAINT FK_Action_ActionState
			FOREIGN KEY(ActionStateID)
			REFERENCES ActionState(ActionStateID) ON DELETE NO ACTION,
		CONSTRAINT FK_Action_Job
			FOREIGN KEY(JobUID)
			REFERENCES Job(JobUID) ON DELETE CASCADE,
		CONSTRAINT FK_Action_Server
			FOREIGN KEY(ServerUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE NO ACTION,
		CONSTRAINT FK_Action_Task
			FOREIGN KEY(TaskID)
			REFERENCES Task(TaskID) ON DELETE NO ACTION,
		CONSTRAINT PK_Action
			PRIMARY KEY CLUSTERED(ActionUID),
		CONSTRAINT UQ_Action
			UNIQUE NONCLUSTERED(TaskID, ServerUID)
	);

	CREATE NONCLUSTERED INDEX ActionByJobID
		ON Action(JobUID)

	CREATE NONCLUSTERED INDEX ActionByServerUID
		ON Action(ServerUID)
END
ELSE BEGIN
	PRINT 'Using existing Action table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Action'
	)
		ALTER TABLE Action
		ADD CONSTRAINT PK_Action
			PRIMARY KEY CLUSTERED(ActionUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Action'
	)
		ALTER TABLE Action
		ADD CONSTRAINT UQ_Action
			UNIQUE NONCLUSTERED(TaskID, ServerUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'ActionByJobID'
	)
		CREATE NONCLUSTERED INDEX ActionByJobID
			ON Action(JobUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'ActionByServerUID'
	)
		CREATE NONCLUSTERED INDEX ActionByServerUID
			ON Action(ServerUID)
END
GO

-- An action applies/applied to this computer, which can be identified
-- by its computer id, device id, DNS, IP or MAC address. One of the five related
-- cross-references must be non-null. If more than one is non-null, precedence is
-- applied top to bottom in the order documented below.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ActionApplies'
)
BEGIN
	CREATE TABLE ActionApplies (
		-- Auto-generated identity number
		ActionAppliesID		int IDENTITY NOT NULL,
		-- The Action which applies.
		ActionUID		binary(16) NOT NULL,
		-- The computer id of the device to which the Action applies. Index into the
		-- Computer table.
		ComputerID		int NULL,
		-- Index into the NetworkDevice table for this device.
		DeviceID		int NULL,
		-- The network hardware address of the device.
		MACAddress		char(18) NULL,
		-- The DNS name of the device.
		DNSName			nvarchar(128) NULL,
		-- The IP Address of the device.
		IPAddress		varchar(64) NULL,
		-- One of the action states defined in the $ActionState; table.
		ActionStateID		int NOT NULL,
		-- If not empty, text describing the reason the action failed.
		FailureReason		ntext NULL,
		-- The last time that the state of this action was updated. This value
		-- is the UTC date-time of the event.
		LastUpdate		datetime NOT NULL DEFAULT getdate(),
		CONSTRAINT FK_ActionApplies_Action
			FOREIGN KEY(ActionUID)
			REFERENCES Action(ActionUID) ON DELETE CASCADE,
		CONSTRAINT FK_ActionApplies_ActionState
			FOREIGN KEY(ActionStateID)
			REFERENCES ActionState(ActionStateID) ON DELETE NO ACTION,
		CONSTRAINT FK_ActionApplies_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_ActionApplies_DeviceID
			FOREIGN KEY(DeviceID)
			REFERENCES NetworkDevice(DeviceID) ON DELETE NO ACTION,
		CONSTRAINT PK_ActionApplies
			PRIMARY KEY NONCLUSTERED(ActionAppliesID)
	);

	CREATE NONCLUSTERED INDEX UQ_ActionApplies_Device
		ON ActionApplies(ActionUID, DeviceID)

	CREATE NONCLUSTERED INDEX UQ_ActionApplies_Computer
		ON ActionApplies(ActionUID, ComputerID)

	CREATE NONCLUSTERED INDEX UQ_ActionApplies_DNSName
		ON ActionApplies(ActionUID, DNSName)

	CREATE NONCLUSTERED INDEX UQ_ActionApplies_IPAddress
		ON ActionApplies(ActionUID, IPAddress)

	CREATE NONCLUSTERED INDEX UQ_ActionApplies_MACAddress
		ON ActionApplies(ActionUID, MACAddress)

	CREATE NONCLUSTERED INDEX ActionAppliesByComputer
		ON ActionApplies(ComputerID)

	CREATE NONCLUSTERED INDEX ActionAppliesByDevice
		ON ActionApplies(DeviceID)
END
ELSE BEGIN
	PRINT 'Using existing ActionApplies table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ActionApplies'
	)
		ALTER TABLE ActionApplies
		ADD CONSTRAINT PK_ActionApplies
			PRIMARY KEY NONCLUSTERED(ActionAppliesID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ActionApplies_Device'
	)
		CREATE NONCLUSTERED INDEX UQ_ActionApplies_Device
			ON ActionApplies(ActionUID, DeviceID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ActionApplies_Computer'
	)
		CREATE NONCLUSTERED INDEX UQ_ActionApplies_Computer
			ON ActionApplies(ActionUID, ComputerID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ActionApplies_DNSName'
	)
		CREATE NONCLUSTERED INDEX UQ_ActionApplies_DNSName
			ON ActionApplies(ActionUID, DNSName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ActionApplies_IPAddress'
	)
		CREATE NONCLUSTERED INDEX UQ_ActionApplies_IPAddress
			ON ActionApplies(ActionUID, IPAddress)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ActionApplies_MACAddress'
	)
		CREATE NONCLUSTERED INDEX UQ_ActionApplies_MACAddress
			ON ActionApplies(ActionUID, MACAddress)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'ActionAppliesByComputer'
	)
		CREATE NONCLUSTERED INDEX ActionAppliesByComputer
			ON ActionApplies(ComputerID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'ActionAppliesByDevice'
	)
		CREATE NONCLUSTERED INDEX ActionAppliesByDevice
			ON ActionApplies(DeviceID)
END
GO

-- Software usage information is stored in weekly batches. Information received
-- by the server is stored in the SoftwareUsagePerWeek table. Each row in the table
-- represents usage information received from a specified user, on a specified
-- managed device, regarding usage of specified software, during the week where
-- the Monday is the specified date.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareUsagePerWeek'
)
BEGIN
	CREATE TABLE SoftwareUsagePerWeek (
		-- Auto-generated identity number
		SoftwareUsagePerWeekID	int IDENTITY NOT NULL,
		-- The id of the computer this information applies to. This id is a foreign
		-- key to the Computer table. It forms part of the unique-clustered-index
		-- that identifies each row of data.
		ComputerID		int NOT NULL,
		-- The id of the user context in which the application was detected.
		-- This id is a foreign key to the User table. It forms part of the
		-- unique-clustered-index that identifies each row of data.
		UserID			int NOT NULL,
		-- The id of the software that was used. This is a foreign key to the
		-- SoftwareVersion table. It forms part of the unique-clustered-index
		-- that identifies each row of data.
		SoftwareID		int NOT NULL,
		-- The id of the software file usage that was used. This is a foreign key to the
		-- SoftwareFileUsage table. It forms part of the unique-clustered-index
		-- that identifies each row of data.
		SoftwareFileUsageID	int NULL,
		-- The first day for the week. This date identifies the week that usage data applies to.
		StartOfWeek		datetime NOT NULL,
		-- The total duration, in seconds, that the application was run. It represents the total spanning
		-- across many sessions.
		Duration		int NULL,
		-- The total active time, in seconds, that the application was in the foreground.
		-- It represents the total spanning across many sessions.
		ActiveTime		int NULL,
		-- The number of sessions the in which the application was used within the week.
		Sessions		int NULL,
		-- The number of distinct days the application was used within the week.
		Days			int NULL,
		CONSTRAINT FK_SoftwareUsagePerWeek_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareUsagePerWeek_Software
			FOREIGN KEY(SoftwareID)
			REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE,
		CONSTRAINT FK_SoftwareUsagePerWeek_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT PK_SoftwareUsagePerWeek
			PRIMARY KEY NONCLUSTERED(SoftwareUsagePerWeekID),
		CONSTRAINT UQ_SoftwareUsagePerWeek
			UNIQUE CLUSTERED
			(ComputerID, UserID, SoftwareID, SoftwareFileUsageID, StartOfWeek)
	);

	CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekByUser
		ON SoftwareUsagePerWeek(UserID)

	CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekBySoftwareFileUsageID
		ON SoftwareUsagePerWeek(SoftwareFileUsageID)

	CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekBySoftwareID
		ON SoftwareUsagePerWeek(SoftwareID)

	CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekByStartOfWeek
		ON SoftwareUsagePerWeek(StartOfWeek)
END
ELSE BEGIN
	PRINT 'Using existing SoftwareUsagePerWeek table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareUsagePerWeek'
	)
		ALTER TABLE SoftwareUsagePerWeek
		ADD CONSTRAINT UQ_SoftwareUsagePerWeek
			UNIQUE CLUSTERED
			(ComputerID, UserID, SoftwareID, SoftwareFileUsageID, StartOfWeek)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareUsagePerWeek'
	)
		ALTER TABLE SoftwareUsagePerWeek
		ADD CONSTRAINT PK_SoftwareUsagePerWeek
			PRIMARY KEY NONCLUSTERED(SoftwareUsagePerWeekID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareUsagePerWeekByUser'
	)
		CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekByUser
			ON SoftwareUsagePerWeek(UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareUsagePerWeekBySoftwareFileUsageID'
	)
		CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekBySoftwareFileUsageID
			ON SoftwareUsagePerWeek(SoftwareFileUsageID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareUsagePerWeekBySoftwareID'
	)
		CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekBySoftwareID
			ON SoftwareUsagePerWeek(SoftwareID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareUsagePerWeekByStartOfWeek'
	)
		CREATE NONCLUSTERED INDEX SoftwareUsagePerWeekByStartOfWeek
			ON SoftwareUsagePerWeek(StartOfWeek)
END
GO

-- Each time usage information is received, the ComputerUsage table is updated with the
-- current day's time-stamp.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ComputerUsage'
)
BEGIN
	CREATE TABLE ComputerUsage (
		-- The id of the computer this information applies to. This id is a foreign
		-- key to the Computer table. It forms part of the unique index
		-- that identifies each row of data.
		ComputerID		int NOT NULL,
		-- The id of the user context in which the application was detected.
		-- This is a foreign key to the User table. It forms part of the unique index
		-- that identifies each row of data.
		UserID			int NOT NULL,
		-- The date that the user last reported usage information from the specified computer.
		LastReported		datetime NULL,
		CONSTRAINT FK_ComputerUsage_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_ComputerUsage_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT PK_ComputerUsage
			PRIMARY KEY NONCLUSTERED(ComputerID, UserID)
	);

	CREATE NONCLUSTERED INDEX ComputerUsageByUser
		ON ComputerUsage(UserID)
END
ELSE BEGIN
	PRINT 'Using existing ComputerUsage table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ComputerUsage'
	)
		ALTER TABLE ComputerUsage
		ADD CONSTRAINT PK_ComputerUsage
			PRIMARY KEY NONCLUSTERED(ComputerID, UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'ComputerUsageByUser'
	)
		CREATE NONCLUSTERED INDEX ComputerUsageByUser
			ON ComputerUsage(UserID)
END
GO

-- This table contains information about each file relevant to reporting
-- software usage information on each computer.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SoftwareFileUsage'
)
BEGIN
	CREATE TABLE SoftwareFileUsage (
		-- Auto-generated identity number
		SoftwareFileUsageID	int IDENTITY NOT NULL,
		-- The id of the computer this information applies to. This id is a foreign
		-- key to the Computer table. It forms part of the unique index
		-- that identifies each row of data.
		ComputerID		int NOT NULL,
		-- The id of the user context in which the application was detected.
		-- This is a foreign key to the User table. It forms part of the unique index
		-- that identifies each row of data.
		UserID			int NOT NULL,
		-- The version of the software file defined by the vendor.
		Version			nvarchar(32) NOT NULL,
		-- The name of the file that was tracked, minus the path.
		-- This is a foreign key into the SoftwareFileName table.
		SoftwareFileNameID	int NOT NULL,
		-- The full path and file that was tracked.
		SoftwareLongFileName	nvarchar(4000) NULL,
		-- The company name of the software.
		SoftwareCompanyName	nvarchar(50) NULL,
		-- The file description of the software.
		SoftwareFileDescription	nvarchar(1024) NULL,
		CONSTRAINT FK_SoftwareFileUsage_SoftwareFileName
			FOREIGN KEY(SoftwareFileNameID)
			REFERENCES SoftwareFileName(SoftwareFileNameID) ON DELETE CASCADE,
		CONSTRAINT PK_SoftwareFileUsage
			PRIMARY KEY CLUSTERED
			(ComputerID, UserID, SoftwareFileNameID, Version),
		CONSTRAINT UQ_SoftwareFileUsage
			UNIQUE NONCLUSTERED(SoftwareFileUsageID)
	);

	CREATE NONCLUSTERED INDEX SoftwareFileUsageBySoftwareFileNameID
		ON SoftwareFileUsage(SoftwareFileNameID)
END
ELSE BEGIN
	PRINT 'Using existing SoftwareFileUsage table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SoftwareFileUsage'
	)
		ALTER TABLE SoftwareFileUsage
		ADD CONSTRAINT PK_SoftwareFileUsage
			PRIMARY KEY CLUSTERED
			(ComputerID, UserID, SoftwareFileNameID, Version)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SoftwareFileUsage'
	)
		ALTER TABLE SoftwareFileUsage
		ADD CONSTRAINT UQ_SoftwareFileUsage
			UNIQUE NONCLUSTERED(SoftwareFileUsageID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SoftwareFileUsageBySoftwareFileNameID'
	)
		CREATE NONCLUSTERED INDEX SoftwareFileUsageBySoftwareFileNameID
			ON SoftwareFileUsage(SoftwareFileNameID)
END
GO

-- Classes of events reported from the device blocking component
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'EventClass'
)
BEGIN
	CREATE TABLE EventClass (
		-- Unique identifier for an event class
		EventClassID		int IDENTITY NOT NULL,
		-- Name of the event class
		[Name]			nvarchar(50) NOT NULL,
		CONSTRAINT PK_EventClass
			PRIMARY KEY NONCLUSTERED(EventClassID)
	);
END
ELSE BEGIN
	PRINT 'Using existing EventClass table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_EventClass'
	)
		ALTER TABLE EventClass
		ADD CONSTRAINT PK_EventClass
			PRIMARY KEY NONCLUSTERED(EventClassID)
END
GO

-- Events happening to the devices affected by the device blocking component
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'DeviceEvent'
)
BEGIN
	CREATE TABLE DeviceEvent (
		-- The unique identifier for a device event record.
		DeviceEventID		int IDENTITY NOT NULL,
		-- The unique identifier of the computer where the event occurred.
		ComputerID		int NOT NULL,
		-- The unique identifier of the user associated with the event, if such a name can be pinpointed exactly, otherwise it will be null.
		UserID			int NULL,
		-- The date and time when the event occurred. Datetime format used: "yyyymmddThhmmss[Z]"
		[DateTime]		datetime NOT NULL,
		-- A readable name for the device
		DeviceName		nvarchar(256) NOT NULL,
		-- The setup class of the device, could be an empty string
		DeviceClass		nvarchar(256) NOT NULL,
		-- The unique identifier for the device
		DeviceID		nvarchar(128) NOT NULL,
		-- The state of the device after the event, either "True" or "False" but can also be null if the device is not claimed/managed
		Enabled			bit NULL,
		-- The unique identifier for the event
		EventID			int NOT NULL,
		-- The description of the event
		EventDescription	nvarchar(256) NOT NULL,
		CONSTRAINT FK_DeviceEvent_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_DeviceEvent_EventClass
			FOREIGN KEY(EventID)
			REFERENCES EventClass(EventClassID) ON DELETE CASCADE,
		CONSTRAINT PK_DeviceEvent
			PRIMARY KEY NONCLUSTERED(DeviceEventID),
		CONSTRAINT UQ_DeviceEvent
			UNIQUE CLUSTERED
			(ComputerID, UserID, [DateTime], DeviceID, EventID)
	);

	CREATE NONCLUSTERED INDEX DeviceEventByDateTime
		ON DeviceEvent([DateTime])

	CREATE NONCLUSTERED INDEX DeviceEventByEventID
		ON DeviceEvent(EventID)
END
ELSE BEGIN
	PRINT 'Using existing DeviceEvent table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_DeviceEvent'
	)
		ALTER TABLE DeviceEvent
		ADD CONSTRAINT UQ_DeviceEvent
			UNIQUE CLUSTERED
			(ComputerID, UserID, [DateTime], DeviceID, EventID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_DeviceEvent'
	)
		ALTER TABLE DeviceEvent
		ADD CONSTRAINT PK_DeviceEvent
			PRIMARY KEY NONCLUSTERED(DeviceEventID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DeviceEventByDateTime'
	)
		CREATE NONCLUSTERED INDEX DeviceEventByDateTime
			ON DeviceEvent([DateTime])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'DeviceEventByEventID'
	)
		CREATE NONCLUSTERED INDEX DeviceEventByEventID
			ON DeviceEvent(EventID)
END
GO

-- Session state events such as log on, log off, etc.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'SessionStateEvent'
)
BEGIN
	CREATE TABLE SessionStateEvent (
		-- The unique identifier for a session state event record.
		SessionStateEventID	int IDENTITY NOT NULL,
		-- The unique identifier of the machine where the event occurred
		ComputerID		int NOT NULL,
		-- The unique identifier of the user associated with the event
		UserID			int NOT NULL,
		-- The date and time when the event occurred. Datetime format used: "yyyymmddThhmmss[Z]"
		[DateTime]		datetime NOT NULL,
		-- The unique identifier for the event
		EventID			int NOT NULL,
		-- The name of the client software involved in the event. Can contain some string value or "Console" or "NotApplicable".
		Client			nvarchar(56) NOT NULL,
		-- The description of the event
		EventDescription	nvarchar(256) NOT NULL,
		CONSTRAINT FK_SessionStateEvent_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_SessionStateEvent_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT FK_SessionStateEvent_EventClass
			FOREIGN KEY(EventID)
			REFERENCES EventClass(EventClassID) ON DELETE CASCADE,
		CONSTRAINT PK_SessionStateEvent
			PRIMARY KEY NONCLUSTERED(SessionStateEventID),
		CONSTRAINT UQ_SessionStateEvent
			UNIQUE CLUSTERED
			(ComputerID, UserID, [DateTime], EventID)
	);

	CREATE NONCLUSTERED INDEX SessionStateEventByUserID
		ON SessionStateEvent(UserID)

	CREATE NONCLUSTERED INDEX SessionStateEventByDateTime
		ON SessionStateEvent([DateTime])

	CREATE NONCLUSTERED INDEX SessionStateEventByEventID
		ON SessionStateEvent(EventID)
END
ELSE BEGIN
	PRINT 'Using existing SessionStateEvent table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_SessionStateEvent'
	)
		ALTER TABLE SessionStateEvent
		ADD CONSTRAINT UQ_SessionStateEvent
			UNIQUE CLUSTERED
			(ComputerID, UserID, [DateTime], EventID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_SessionStateEvent'
	)
		ALTER TABLE SessionStateEvent
		ADD CONSTRAINT PK_SessionStateEvent
			PRIMARY KEY NONCLUSTERED(SessionStateEventID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SessionStateEventByUserID'
	)
		CREATE NONCLUSTERED INDEX SessionStateEventByUserID
			ON SessionStateEvent(UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SessionStateEventByDateTime'
	)
		CREATE NONCLUSTERED INDEX SessionStateEventByDateTime
			ON SessionStateEvent([DateTime])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'SessionStateEventByEventID'
	)
		CREATE NONCLUSTERED INDEX SessionStateEventByEventID
			ON SessionStateEvent(EventID)
END
GO

-- General security events such as service start, service stop, user request for elevated rights, etc.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'GeneralSecurityEvent'
)
BEGIN
	CREATE TABLE GeneralSecurityEvent (
		-- The unique identifier for a general security state event record.
		GeneralSecurityEventID	int IDENTITY NOT NULL,
		-- The unique identifier of the machine where the event occurred
		ComputerID		int NOT NULL,
		-- The unique identifier of the user associated with the event
		UserID			int NOT NULL,
		-- The date and time when the event occurred. Datetime format used: "yyyymmddThhmmss[Z]"
		[DateTime]		datetime NOT NULL,
		-- Only applicable to request events, indicating whether the request has been granted or not, otherwise, it is null.
		Allowed			bit NULL,
		-- Only applicable to events that involve a setting, indicating the name of the setting, otherwise, it is "NotApplicable".
		Setting			nvarchar(128) NOT NULL,
		-- The unique identifier for the event
		EventID			int NOT NULL,
		-- The description of the event
		EventDescription	nvarchar(256) NOT NULL,
		CONSTRAINT FK_GeneralSecurityEvent_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_GeneralSecurityEvent_User
			FOREIGN KEY(UserID)
			REFERENCES [User](UserID) ON DELETE CASCADE,
		CONSTRAINT FK_GeneralSecurityEvent_EventClass
			FOREIGN KEY(EventID)
			REFERENCES EventClass(EventClassID) ON DELETE CASCADE,
		CONSTRAINT PK_GeneralSecurityEvent
			PRIMARY KEY NONCLUSTERED(GeneralSecurityEventID),
		CONSTRAINT UQ_GeneralSecurityEvent
			UNIQUE CLUSTERED
			(ComputerID, UserID, [DateTime], EventID)
	);

	CREATE NONCLUSTERED INDEX GeneralSecurityEventByUserID
		ON GeneralSecurityEvent(UserID)

	CREATE NONCLUSTERED INDEX GeneralSecurityEventByDateTime
		ON GeneralSecurityEvent([DateTime])

	CREATE NONCLUSTERED INDEX GeneralSecurityEventByEventID
		ON GeneralSecurityEvent(EventID)
END
ELSE BEGIN
	PRINT 'Using existing GeneralSecurityEvent table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_GeneralSecurityEvent'
	)
		ALTER TABLE GeneralSecurityEvent
		ADD CONSTRAINT UQ_GeneralSecurityEvent
			UNIQUE CLUSTERED
			(ComputerID, UserID, [DateTime], EventID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_GeneralSecurityEvent'
	)
		ALTER TABLE GeneralSecurityEvent
		ADD CONSTRAINT PK_GeneralSecurityEvent
			PRIMARY KEY NONCLUSTERED(GeneralSecurityEventID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'GeneralSecurityEventByUserID'
	)
		CREATE NONCLUSTERED INDEX GeneralSecurityEventByUserID
			ON GeneralSecurityEvent(UserID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'GeneralSecurityEventByDateTime'
	)
		CREATE NONCLUSTERED INDEX GeneralSecurityEventByDateTime
			ON GeneralSecurityEvent([DateTime])

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'GeneralSecurityEventByEventID'
	)
		CREATE NONCLUSTERED INDEX GeneralSecurityEventByEventID
			ON GeneralSecurityEvent(EventID)
END
GO

-- Wake on LAN tasks control any targeted managed devices. The details of these
-- tasks are stored in the WakeOnLANTask table. Each row in the table represents
-- a Wake on LAN task.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'WakeOnLANTask'
)
BEGIN
	CREATE TABLE WakeOnLANTask (
		-- A unique identifier for the task that created a Wake on LAN job.
		TaskUID			binary(16) NOT NULL,
		-- The descriptive name assigned to the Wake on LAN task.
		FriendlyName		nvarchar(400) NOT NULL,
		-- The time at which the managed devices will be woken.
		StartTime		datetime NULL,
		CONSTRAINT PK_WakeOnLANTask
			PRIMARY KEY CLUSTERED(TaskUID)
	);
END
ELSE BEGIN
	PRINT 'Using existing WakeOnLANTask table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_WakeOnLANTask'
	)
		ALTER TABLE WakeOnLANTask
		ADD CONSTRAINT PK_WakeOnLANTask
			PRIMARY KEY CLUSTERED(TaskUID)
END
GO

-- Wake on LAN distribution jobs control the distribution of a Wake on LAN
-- task to the nearest distribution server for the targeted managed devices.
-- The status of these distribution jobs is stored in the WakeOnLANDistributionJob table.
-- Each row in the table represents a Wake on LAN job, which is any Wake on LAN task (or a subset
-- of a Wake on LAN task), that has been distributed to a distribution server.
-- Be aware: There can be multiple distribution jobs for a given Wake on LAN task.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'WakeOnLANDistributionJob'
)
BEGIN
	CREATE TABLE WakeOnLANDistributionJob (
		-- A unique identifier for this distribution job.
		DistJobUID		binary(16) NOT NULL,
		-- A unique identifier for the task that created this distribution job.
		-- This is a foreign key linked to the TaskUID in the WakeOnLANTask table.
		TaskUID			binary(16) NOT NULL,
		-- A unique identifier for the distribution server that this distribution job targets.
		-- This foreign key links to the ServerUID in the DistributionServer table.
		ServerUID		binary(16) NOT NULL,
		-- The state of this distribution job. This can be one of the following values : Pending, Failed, Success.
		State			varchar(16) NOT NULL,
		CONSTRAINT FK_WOLDistJob_Server
			FOREIGN KEY(ServerUID)
			REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE,
		CONSTRAINT FK_WOLDistJob_Task
			FOREIGN KEY(TaskUID)
			REFERENCES WakeOnLANTask(TaskUID) ON DELETE CASCADE,
		CONSTRAINT PK_WakeOnLANDistributionJob
			PRIMARY KEY CLUSTERED(DistJobUID)
	);

	CREATE NONCLUSTERED INDEX WakeOnLANDistributionJobByServerUID
		ON WakeOnLANDistributionJob(ServerUID)

	CREATE NONCLUSTERED INDEX WakeOnLANDistributionJobByTaskUID
		ON WakeOnLANDistributionJob(TaskUID)
END
ELSE BEGIN
	PRINT 'Using existing WakeOnLANDistributionJob table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_WakeOnLANDistributionJob'
	)
		ALTER TABLE WakeOnLANDistributionJob
		ADD CONSTRAINT PK_WakeOnLANDistributionJob
			PRIMARY KEY CLUSTERED(DistJobUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'WakeOnLANDistributionJobByServerUID'
	)
		CREATE NONCLUSTERED INDEX WakeOnLANDistributionJobByServerUID
			ON WakeOnLANDistributionJob(ServerUID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'WakeOnLANDistributionJobByTaskUID'
	)
		CREATE NONCLUSTERED INDEX WakeOnLANDistributionJobByTaskUID
			ON WakeOnLANDistributionJob(TaskUID)
END
GO

-- All managed devices targeted by a Wake on LAN task have a status associated
-- with them. The status of the managed devices is stored in the WakeOnLANStatus table.
-- Each row in the table represents a managed device to be woken by a Wake on LAN task from a distribution job.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'WakeOnLANStatus'
)
BEGIN
	CREATE TABLE WakeOnLANStatus (
		-- A unique identifier for a distribution job. This foreign key links to
		-- the DistJobUID in the WakeOnLANDistributionJob table. It forms part of the
		-- unique index that identifies each row of data.
		DistJobUID		binary(16) NOT NULL,
		-- The id for the managed device. It forms part of the unique index that identifies each row of data.
		ComputerID		int NOT NULL,
		-- The state of this managed device. This can be one of the following values: Pending, Failed, Woken, Awake.
		State			varchar(16) NOT NULL,
		CONSTRAINT FK_WOLStatus_Computer
			FOREIGN KEY(ComputerID)
			REFERENCES Computer(ComputerID) ON DELETE CASCADE,
		CONSTRAINT FK_WOLStatus_DistJob
			FOREIGN KEY(DistJobUID)
			REFERENCES WakeOnLANDistributionJob(DistJobUID) ON DELETE CASCADE,
		CONSTRAINT PK_WakeOnLANStatus
			PRIMARY KEY NONCLUSTERED(DistJobUID, ComputerID)
	);

	CREATE NONCLUSTERED INDEX WakeOnLANStatusByComputer
		ON WakeOnLANStatus(ComputerID)
END
ELSE BEGIN
	PRINT 'Using existing WakeOnLANStatus table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_WakeOnLANStatus'
	)
		ALTER TABLE WakeOnLANStatus
		ADD CONSTRAINT PK_WakeOnLANStatus
			PRIMARY KEY NONCLUSTERED(DistJobUID, ComputerID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'WakeOnLANStatusByComputer'
	)
		CREATE NONCLUSTERED INDEX WakeOnLANStatusByComputer
			ON WakeOnLANStatus(ComputerID)
END
GO

-- Some secured Resources may be partitioned.
-- Partitions are used to grant rights to one part of a Resource
-- excluding other parts, for example limiting rights so that the
-- operator can access only certain distribution servers,
-- organizational units, or areas in the software library.
-- There are three types of partitioning, defined by entries in this table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'PartitionType'
)
BEGIN
	CREATE TABLE PartitionType (
		-- Auto-generated identity number.
		PartitionTypeID		int IDENTITY NOT NULL,
		-- Name of the PartitionType.
		PartitionTypeName	nvarchar(32) NOT NULL,
		CONSTRAINT UQ_PartitionType
			UNIQUE NONCLUSTERED(PartitionTypeName),
		CONSTRAINT PK_Partition
			PRIMARY KEY NONCLUSTERED(PartitionTypeID)
	);
END
ELSE BEGIN
	PRINT 'Using existing PartitionType table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_PartitionType'
	)
		ALTER TABLE PartitionType
		ADD CONSTRAINT UQ_PartitionType
			UNIQUE NONCLUSTERED(PartitionTypeName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Partition'
	)
		ALTER TABLE PartitionType
		ADD CONSTRAINT PK_Partition
			PRIMARY KEY NONCLUSTERED(PartitionTypeID)
END
GO

-- Access rights are granted to the Resources defined in this table.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Resource'
)
BEGIN
	CREATE TABLE Resource (
		-- Auto-generated identity number.
		ResourceID		int IDENTITY NOT NULL,
		-- Name of the Resource.
		ResourceName		nvarchar(16) NOT NULL,
		-- If not NULL, the type of partitioning used with this Resource.
		PartitionTypeID		int NULL,
		CONSTRAINT FK_Resource_PartitionType
			FOREIGN KEY(PartitionTypeID)
			REFERENCES PartitionType(PartitionTypeID) ON DELETE NO ACTION,
		CONSTRAINT UQ_Resource
			UNIQUE NONCLUSTERED(ResourceName),
		CONSTRAINT PK_Resource
			PRIMARY KEY NONCLUSTERED(ResourceID)
	);
END
ELSE BEGIN
	PRINT 'Using existing Resource table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Resource'
	)
		ALTER TABLE Resource
		ADD CONSTRAINT UQ_Resource
			UNIQUE NONCLUSTERED(ResourceName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Resource'
	)
		ALTER TABLE Resource
		ADD CONSTRAINT PK_Resource
			PRIMARY KEY NONCLUSTERED(ResourceID)
END
GO

-- The types of action on a Resource for which rights may be granted or denied.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'ActionClass'
)
BEGIN
	CREATE TABLE ActionClass (
		-- Auto-generated identity number.
		ActionClassID		int IDENTITY NOT NULL,
		-- The name of the ActionClass.
		ActionClassName		nvarchar(16) NOT NULL,
		CONSTRAINT UQ_ActionClass
			UNIQUE NONCLUSTERED(ActionClassName),
		CONSTRAINT PK_ActionClass
			PRIMARY KEY NONCLUSTERED(ActionClassID)
	);
END
ELSE BEGIN
	PRINT 'Using existing ActionClass table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_ActionClass'
	)
		ALTER TABLE ActionClass
		ADD CONSTRAINT UQ_ActionClass
			UNIQUE NONCLUSTERED(ActionClassName)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_ActionClass'
	)
		ALTER TABLE ActionClass
		ADD CONSTRAINT PK_ActionClass
			PRIMARY KEY NONCLUSTERED(ActionClassID)
END
GO

-- Each action by ManageSoft requires one or more Rights to perform an ActionClass
-- over a given Resource.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'Right'
)
BEGIN
	CREATE TABLE [Right] (
		-- Auto-generated identity number.
		RightID			int IDENTITY NOT NULL,
		-- The group to whom the Right is granted or denied (deny always takes precedence!).
		GroupID			int NOT NULL,
		-- The Resource to which the Right applies.
		ResourceID		int NOT NULL,
		-- The action class which applies (read or modify).
		ActionClassID		int NOT NULL,
		-- When TRUE (1), indicates that the specified right is denied.
		Denied			bit NOT NULL DEFAULT 0,
		-- The integer id of an item which depends on the PartitionType of the
		-- associated resource. In ManageSoft 7.5, only Organization partitioning
		-- (PartitionTypeID = 1) is used, so the Value field contains an OrganizationID.
		-- The Right applies to this organization and all child organizations, unless
		-- denied by another Right.
		Value			int NULL,
		CONSTRAINT FK_Right_Resource
			FOREIGN KEY(ResourceID)
			REFERENCES Resource(ResourceID) ON DELETE NO ACTION,
		CONSTRAINT FK_Right_ActionClass
			FOREIGN KEY(ActionClassID)
			REFERENCES ActionClass(ActionClassID) ON DELETE NO ACTION,
		CONSTRAINT FK_Right_Group
			FOREIGN KEY(GroupID)
			REFERENCES [Group](GroupID) ON DELETE CASCADE,
		CONSTRAINT PK_Right
			PRIMARY KEY NONCLUSTERED(RightID),
		CONSTRAINT UQ_Right
			UNIQUE NONCLUSTERED
			(GroupID, ResourceID, ActionClassID, Value)
	);
END
ELSE BEGIN
	PRINT 'Using existing Right table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_Right'
	)
		ALTER TABLE [Right]
		ADD CONSTRAINT PK_Right
			PRIMARY KEY NONCLUSTERED(RightID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'UQ_Right'
	)
		ALTER TABLE [Right]
		ADD CONSTRAINT UQ_Right
			UNIQUE NONCLUSTERED
			(GroupID, ResourceID, ActionClassID, Value)
END
GO

-- Records the status of various agents upon a NetworkDevice.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'AgentStatus'
)
BEGIN
	CREATE TABLE AgentStatus (
		-- NetworkDevice identity number.
		DeviceID		int NOT NULL,
		-- Name of the agent.
		Agent			nvarchar(32) NOT NULL,
		-- Version of the agent.
		Version			nvarchar(32) NOT NULL,
		-- Date and time the status of the agent was reported at.
		Reported		datetime NOT NULL,
		-- The result code returned by the agent.
		ResultCode		bigint NOT NULL,
		CONSTRAINT FK_AgentStatus_NetworkDevice
			FOREIGN KEY(DeviceID)
			REFERENCES NetworkDevice(DeviceID) ON DELETE CASCADE,
		CONSTRAINT PK_AgentStatus
			PRIMARY KEY NONCLUSTERED
			(DeviceID, Agent, Version, Reported)
	);

	CREATE NONCLUSTERED INDEX AgentStatusByDeviceID
		ON AgentStatus(DeviceID)
END
ELSE BEGIN
	PRINT 'Using existing AgentStatus table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_AgentStatus'
	)
		ALTER TABLE AgentStatus
		ADD CONSTRAINT PK_AgentStatus
			PRIMARY KEY NONCLUSTERED
			(DeviceID, Agent, Version, Reported)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'AgentStatusByDeviceID'
	)
		CREATE NONCLUSTERED INDEX AgentStatusByDeviceID
			ON AgentStatus(DeviceID)
END
GO

-- Records the entries in the AMT event log for a NetworkDevice.
IF NOT EXISTS (
	SELECT * FROM INFORMATION_SCHEMA.TABLES
	WHERE TABLE_NAME = 'AMTEventLog'
)
BEGIN
	CREATE TABLE AMTEventLog (
		-- Auto-generated identity number.
		AMTEventLogID		int IDENTITY NOT NULL,
		-- NetworkDevice identity number.
		DeviceID		int NOT NULL,
		-- Date and time the event log entry was reported at.
		Reported		datetime NOT NULL,
		-- The device address from the PET message format.
		PETDeviceAddress	smallint NOT NULL,
		-- The event sensor type from the PET message format.
		PETEventSensorType	smallint NOT NULL,
		-- The event type from the PET message format.
		PETEventType		smallint NOT NULL,
		-- The event offset from the PET message format.
		PETEventOffset		smallint NOT NULL,
		-- The event source type from the PET message format.
		PETEventSourceType	smallint NOT NULL,
		-- The event severity from the PET message format.
		PETEventSeverity	smallint NOT NULL,
		-- The sensor number from the PET message format.
		PETSensorNumber		smallint NOT NULL,
		-- The entity from the PET message format.
		PETEntity		smallint NOT NULL,
		-- The entity instance address from the PET message format.
		PETEntityInstance	smallint NOT NULL,
		-- The event data from the PET message format.
		PETEventData		nvarchar(32) NOT NULL,
		CONSTRAINT FK_AMTEventLog_NetworkDevice
			FOREIGN KEY(DeviceID)
			REFERENCES NetworkDevice(DeviceID) ON DELETE CASCADE,
		CONSTRAINT PK_AMTEventLog
			PRIMARY KEY NONCLUSTERED(AMTEventLogID)
	);

	CREATE NONCLUSTERED INDEX AMTEventLogByDeviceID
		ON AMTEventLog(DeviceID)
END
ELSE BEGIN
	PRINT 'Using existing AMTEventLog table'
	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'PK_AMTEventLog'
	)
		ALTER TABLE AMTEventLog
		ADD CONSTRAINT PK_AMTEventLog
			PRIMARY KEY NONCLUSTERED(AMTEventLogID)

	IF NOT EXISTS (
		SELECT * FROM sysindexes
		WHERE name = 'AMTEventLogByDeviceID'
	)
		CREATE NONCLUSTERED INDEX AMTEventLogByDeviceID
			ON AMTEventLog(DeviceID)
END
GO


IF NOT EXISTS(SELECT * FROM TargetType)
BEGIN
	SET IDENTITY_INSERT TargetType ON
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (1, 'Computer')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (2, 'User')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (3, 'Group')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (4, 'DistributionLocation')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (5, 'DistributionServer')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (8, 'Organization')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (9, 'Asset')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (10, 'Contract')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (11, 'PurchaseOrder')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (12, 'SoftwareLicense')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (13, 'SoftwareTitle')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (14, 'ComplianceComputer')
	INSERT INTO TargetType (TargetTypeID, TargetTypeName)
		VALUES (15, 'ComplianceUser')
	SET IDENTITY_INSERT TargetType OFF
END
GO

IF NOT EXISTS(SELECT * FROM OperatingSystem)
BEGIN
	SET IDENTITY_INSERT OperatingSystem ON
	-- OperatingSystemIDs from 1000 up are allocated by Deployment Manager,
	-- others are defined by the DMTF for use with WBEM.
	INSERT INTO OperatingSystem (OperatingSystemID, OperatingSystemName)
		VALUES (0, 'All')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 1, 'Other')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 2, 'MACOS')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 5, 'DECNT')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 7, 'OpenVMS')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 10, 'MVS')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 11, 'OS400')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 12, 'OS/2')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 13, 'JavaVM')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 14, 'MSDOS')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 1000, 'Microsoft Windows')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1000, 15, 'Microsoft Windows 3 editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (15, 1001, 'Microsoft Windows 3.0')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (15, 1002, 'Microsoft Windows for Workgroups 3.11')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1000, 1029, 'Microsoft Windows 9x editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1029, 16, 'Microsoft Windows 95')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1029, 17, 'Microsoft Windows 98')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1029, 1003, 'Microsoft Windows ME')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1000, 18, 'Microsoft Windows NT 3 and above')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (18, 1004, 'Microsoft Windows NT 3 editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1004, 1005, 'Microsoft Windows NT Advanced Server 3.1')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1004, 1006, 'Microsoft Windows NT Server 3.5')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1004, 1007, 'Microsoft Windows NT Workstation 3.5')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1004, 1008, 'Microsoft Windows NT Server 3.51')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (18, 1009, 'Microsoft Windows NT 4 and above')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1009, 1030, 'Microsoft Windows NT 4 editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1030, 1010, 'Microsoft Windows NT Server')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1030, 1011, 'Microsoft Windows NT Workstation')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1030, 1031, 'Microsoft Windows NT Enterprise Server')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1009, 1012, 'Microsoft Windows NT 5 and above')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1012, 1013, 'Microsoft Windows 2000 editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1013, 1014, 'Microsoft Windows 2000 Professional')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1013, 1015, 'Microsoft Windows 2000 Server')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1013, 1016, 'Microsoft Windows 2000 Advanced Server')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1013, 1017, 'Microsoft Windows 2000 Datacenter Server')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1012, 1018, 'Microsoft Windows XP editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1018, 1019, 'Microsoft Windows XP Home')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1018, 1020, 'Microsoft Windows XP Professional')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1018, 1021, 'Microsoft Windows XP 64-bit')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1018, 1022, 'Microsoft Windows XP Media Center')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1018, 1023, 'Microsoft Windows XP Tablet PC')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1012, 1024, 'Microsoft Windows Server 2003 editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1024, 1025, 'Microsoft Windows Server 2003 Standard Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1024, 1026, 'Microsoft Windows Server 2003 Enterprise Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1024, 1027, 'Microsoft Windows Server 2003 Datacenter Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1024, 1028, 'Microsoft Windows Server 2003 Web Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1000, 19, 'Microsoft Windows CE')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1009, 1032, 'Microsoft Windows NT 6 and above')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1032, 1033, 'Microsoft Windows Vista editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1033, 1034, 'Microsoft Windows Vista Business')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1033, 1035, 'Microsoft Windows Vista Enterprise')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1033, 1036, 'Microsoft Windows Vista Home Basic')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1033, 1037, 'Microsoft Windows Vista Home Premium')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1033, 1038, 'Microsoft Windows Vista Ultimate')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1012, 1039, 'Microsoft Windows Server 2003 R2 editions')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1039, 1040, 'Microsoft Windows Server 2003 R2 Standard Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1039, 1041, 'Microsoft Windows Server 2003 R2 Enterprise Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1039, 1042, 'Microsoft Windows Server 2003 R2 Datacenter Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (1039, 1043, 'Microsoft Windows Server 2003 R2 Web Edition')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 2000, 'UNIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 3, 'ATTUNIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 4, 'DGUX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 6, 'Digital Unix')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 8, 'HP-UX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 9, 'AIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 24, 'Reliant UNIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 25, 'SCO UnixWare')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 26, 'SCO OpenServer')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 28, 'IRIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 29, 'Solaris')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 30, 'SunOS')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 36, 'LINUX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (36, 2100, 'Red Hat Linux')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (36, 2200, 'SuSE Linux')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (36, 2400, 'Fedora Core Linux')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 40, 'Interactive UNIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 41, 'BSDUNIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (41, 42, 'FreeBSD')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (41, 43, 'NetBSD')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 46, 'MACH Kernel')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (46, 2300, 'Mac OS X')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (46, 55, 'NextStep')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (46, 44, 'GNU Hurd')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 20, 'NCR3000')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 21, 'NetWare')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 22, 'OSF')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 23, 'DC/OS')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 27, 'Sequent')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 31, 'U6000')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 32, 'ASERIES')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 33, 'TandemNSK')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 34, 'TandemNT')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 35, 'BS2000')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 37, 'Lynx')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (2000, 38, 'XENIX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 39, 'VM/ESA')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 45, 'OS9')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 47, 'Inferno')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 48, 'QNX')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 49, 'EPOC')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 50, 'IxWorks')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 51, 'VxWorks')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 52, 'MiNT')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 53, 'BeOS')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 54, 'HP MPE')
	INSERT INTO OperatingSystem (Category, OperatingSystemID, OperatingSystemName)
		VALUES (0, 56, 'PalmPilot')
	SET IDENTITY_INSERT OperatingSystem OFF
END
GO

IF NOT EXISTS(SELECT * FROM Language)
BEGIN
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Afar', 'aa', 'aar')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Abkhazian', 'ab', 'abk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Achinese', NULL, 'ace')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Acoli', NULL, 'ach')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Adangme', NULL, 'ada')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Adyghe; Adygei', NULL, 'ady')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Afro-Asiatic (Other)', NULL, 'afa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Afrihili', NULL, 'afh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Afrikaans', 'af', 'afr', 54)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Akan', 'ak', 'aka')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Akkadian', NULL, 'akk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Aleut', NULL, 'ale')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Algonquian languages', NULL, 'alg')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Amharic', 'am', 'amh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('English, Old (ca.450-1100)', NULL, 'ang')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Apache languages', NULL, 'apa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Arabic', 'ar', 'ara', 1)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Aramaic', NULL, 'arc')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Aragonese', 'an', 'arg')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Araucanian', NULL, 'arn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Arapaho', NULL, 'arp')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Artificial (Other)', NULL, 'art')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Arawak', NULL, 'arw')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Assamese', 'as', 'asm', 77)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Asturian; Bable', NULL, 'ast')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Athapascan languages', NULL, 'ath')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Australian languages', NULL, 'aus')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Avaric', 'av', 'ava')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Avestan', 'ae', 'ave')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Awadhi', NULL, 'awa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Aymara', 'ay', 'aym')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Azerbaijani', 'az', 'aze')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Banda', NULL, 'bad')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bamileke languages', NULL, 'bai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bashkir', 'ba', 'bak')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Baluchi', NULL, 'bal')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bambara', 'bm', 'bam')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Balinese', NULL, 'ban')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Basa', NULL, 'bas')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Baltic (Other)', NULL, 'bat')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Beja', NULL, 'bej')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Belarusian', 'be', 'bel', 35)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bemba', NULL, 'bem')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Bengali', 'bn', 'ben', 69)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Berber (Other)', NULL, 'ber')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bhojpuri', NULL, 'bho')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bihari', 'bh', 'bih')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bikol', NULL, 'bik')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bini', NULL, 'bin')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bislama', 'bi', 'bis')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Siksika', NULL, 'bla')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bantu (Other)', NULL, 'bnt')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tibetan', 'bo', 'bod')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Bosnian', 'bs', 'bos')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Braj', NULL, 'bra')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Breton', 'br', 'bre')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Batak (Indonesia)', NULL, 'btk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Buriat', NULL, 'bua')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Buginese', NULL, 'bug')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Bulgarian', 'bg', 'bul', 2)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Blin; Bilin', NULL, 'byn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Caddo', NULL, 'cad')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Central American Indian (Other)', NULL, 'cai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Carib', NULL, 'car')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Catalan; Valencian', 'ca', 'cat', 3)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Caucasian (Other)', NULL, 'cau')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Cebuano', NULL, 'ceb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Celtic (Other)', NULL, 'cel')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Czech', 'cs', 'ces', 5)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chamorro', 'ch', 'cha')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chibcha', NULL, 'chb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chechen', 'ce', 'che')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chagatai', NULL, 'chg')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chuukese', NULL, 'chk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mari', NULL, 'chm')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chinook jargon', NULL, 'chn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Choctaw', NULL, 'cho')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chipewyan', NULL, 'chp')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Cherokee', NULL, 'chr')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic', 'cu', 'chu')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chuvash', 'cv', 'chv')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Cheyenne', NULL, 'chy')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chamic languages', NULL, 'cmc')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Coptic', NULL, 'cop')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Cornish', 'kw', 'cor')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Corsican', 'co', 'cos')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Creoles and pidgins, English based (Other)', NULL, 'cpe')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Creoles and pidgins, French-based (Other)', NULL, 'cpf')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Creoles and pidgins, Portuguese-based (Other)', NULL, 'cpp')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Cree', 'cr', 'cre')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Crimean Tatar; Crimean Turkish', NULL, 'crh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Creoles and pidgins (Other)', NULL, 'crp')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kashubian', NULL, 'csb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Cushitic (Other)', NULL, 'cus')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Welsh', 'cy', 'cym')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dakota', NULL, 'dak')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Danish', 'da', 'dan', 6)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dargwa', NULL, 'dar')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dayak', NULL, 'day')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Delaware', NULL, 'del')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Slave (Athapascan)', NULL, 'den')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('German', 'de', 'deu', 7)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dogrib', NULL, 'dgr')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dinka', NULL, 'din')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Divehi', 'dv', 'div', 101)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dogri', NULL, 'doi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dravidian (Other)', NULL, 'dra')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lower Sorbian', NULL, 'dsb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Duala', NULL, 'dua')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dutch, Middle (ca.1050-1350)', NULL, 'dum')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dyula', NULL, 'dyu')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Dzongkha; [Bhutani]', 'dz', 'dzo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Efik', NULL, 'efi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Egyptian (Ancient)', NULL, 'egy')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ekajuk', NULL, 'eka')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Greek, Modern (1453-)', 'el', 'ell', 8)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Elamite', NULL, 'elx')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('English', 'en', 'eng', 9)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('English, Middle (1100-1500)', NULL, 'enm')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Esperanto', 'eo', 'epo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Estonian', 'et', 'est', 37)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Basque', 'eu', 'eus', 45)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ewe', 'ee', 'ewe')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ewondo', NULL, 'ewo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Fang', NULL, 'fan')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Faroese', 'fo', 'fao')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Persian', 'fa', 'fas')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Fanti', NULL, 'fat')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Fijian', 'fj', 'fij')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Finnish', 'fi', 'fin', 11)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Finno-Ugrian (Other)', NULL, 'fiu')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Fon', NULL, 'fon')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('French', 'fr', 'fra', 12)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('French, Middle (ca.1400-1800)', NULL, 'frm')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('French, Old (842-ca.1400)', NULL, 'fro')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Frisian', 'fy', 'fry')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Fulah', 'ff', 'ful')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Friulian', NULL, 'fur')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ga', NULL, 'gaa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gayo', NULL, 'gay')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gbaya', NULL, 'gba')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Germanic (Other)', NULL, 'gem')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Geez', NULL, 'gez')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gilbertese', NULL, 'gil')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gaelic; Scottish Gaelic', 'gd', 'gla')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Irish', 'ga', 'gle')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gallegan', 'gl', 'glg')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Manx', 'gv', 'glv')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('German, Middle High (ca.1050-1500)', NULL, 'gmh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('German, Old High (ca.750-1050)', NULL, 'goh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gondi', NULL, 'gon')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gorontalo', NULL, 'gor')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gothic', NULL, 'got')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Grebo', NULL, 'grb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Greek, Ancient (to 1453)', NULL, 'grc')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Guarani', 'gn', 'grn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Gujarati', 'gu', 'guj', 71)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Gwichin', NULL, 'gwi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Haida', NULL, 'hai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Haitian; Haitian Creole', 'ht', 'hat')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Hausa', 'ha', 'hau')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Hawaiian', NULL, 'haw')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Hebrew', 'he', 'heb', 13)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Herero', 'hz', 'her')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Hiligaynon', NULL, 'hil')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Himachali', NULL, 'him')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Hindi', 'hi', 'hin', 57)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Hittite', NULL, 'hit')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Hmong', NULL, 'hmn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Hiri Motu', 'ho', 'hmo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Croatian', 'hr', 'hrv', 26)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Upper Sorbian', NULL, 'hsb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Hungarian', 'hu', 'hun', 14)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Hupa', NULL, 'hup')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Armenian', 'hy', 'hye', 43)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Iban', NULL, 'iba')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Igbo', 'ig', 'ibo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ido', 'io', 'ido')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sichuan Yi', 'ii', 'iii')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ijo', NULL, 'ijo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Inuktitut', 'iu', 'iku')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Interlingue', 'ie', 'ile')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Iloko', NULL, 'ilo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Interlingua (International Auxiliary Language Association)', 'ia', 'ina')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Indic (Other)', NULL, 'inc')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Indonesian', 'id', 'ind', 33)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Indo-European (Other)', NULL, 'ine')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ingush', NULL, 'inh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Inupiaq', 'ik', 'ipk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Iranian (Other)', NULL, 'ira')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Iroquoian languages', NULL, 'iro')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Icelandic', 'is', 'isl', 15)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Italian', 'it', 'ita', 16)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Javanese', 'jv', 'jav')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lojban', NULL, 'jbo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Japanese', 'ja', 'jpn', 17)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Judeo-Persian', NULL, 'jpr')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Judeo-Arabic', NULL, 'jrb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kara-Kalpak', NULL, 'kaa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kabyle', NULL, 'kab')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kachin', NULL, 'kac')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kalaallisut; Greenlandic', 'kl', 'kal')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kamba', NULL, 'kam')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Kannada', 'kn', 'kan', 75)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Karen', NULL, 'kar')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Kashmiri', 'ks', 'kas', 96)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Georgian', 'ka', 'kat', 55)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kanuri', 'kr', 'kau')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kawi', NULL, 'kaw')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Kazakh', 'kk', 'kaz', 63)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kabardian', NULL, 'kbd')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Khasi', NULL, 'kha')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Khoisan (Other)', NULL, 'khi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Khmer; [Cambodian]', 'km', 'khm')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Khotanese', NULL, 'kho')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kikuyu; Gikuyu', 'ki', 'kik')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kinyarwanda', 'rw', 'kin')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kirghiz', 'ky', 'kir')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kimbundu', NULL, 'kmb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Konkani', NULL, 'kok', 87)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Komi', 'kv', 'kom')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kongo', 'kg', 'kon')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Korean', 'ko', 'kor', 18)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kosraean', NULL, 'kos')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kpelle', NULL, 'kpe')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Karachay-Balkar', NULL, 'krc')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kru', NULL, 'kro')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kurukh', NULL, 'kru')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kuanyama; Kwanyama', 'kj', 'kua')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kumyk', NULL, 'kum')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kurdish', 'ku', 'kur')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kutenai', NULL, 'kut')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ladino', NULL, 'lad')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lahnda', NULL, 'lah')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lamba', NULL, 'lam')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lao', 'lo', 'lao')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Latin', 'la', 'lat')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Latvian', 'lv', 'lav', 38)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lezghian', NULL, 'lez')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Limburgan; Limburger; Limburgish', 'li', 'lim')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lingala', 'ln', 'lin')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Lithuanian', 'lt', 'lit', 39)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mongo', NULL, 'lol')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lozi', NULL, 'loz')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Luxembourgish; Letzeburgesch', 'lb', 'ltz')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Luba-Lulua', NULL, 'lua')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Luba-Katanga', 'lu', 'lub')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ganda', 'lg', 'lug')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Luiseno', NULL, 'lui')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lunda', NULL, 'lun')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Luo (Kenya and Tanzania)', NULL, 'luo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('lushai', NULL, 'lus')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Madurese', NULL, 'mad')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Magahi', NULL, 'mag')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Marshallese', 'mh', 'mah')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Maithili', NULL, 'mai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Makasar', NULL, 'mak')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Malayalam', 'ml', 'mal', 76)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mandingo', NULL, 'man')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Austronesian (Other)', NULL, 'map')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Marathi', 'mr', 'mar', 78)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Masai', NULL, 'mas')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Moksha', NULL, 'mdf')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mandar', NULL, 'mdr')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mende', NULL, 'men')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Irish, Middle (900-1200)', NULL, 'mga')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Micmac', NULL, 'mic')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Minangkabau', NULL, 'min')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Miscellaneous languages', NULL, 'mis')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Macedonian', 'mk', 'mkd', 47)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mon-Khmer (Other)', NULL, 'mkh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Malagasy', 'mg', 'mlg')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Maltese', 'mt', 'mlt')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Manchu', NULL, 'mnc')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Manipuri', NULL, 'mni', 88)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Manobo languages', NULL, 'mno')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mohawk', NULL, 'moh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Moldavian', 'mo', 'mol')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Mongolian', 'mn', 'mon', 80)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mossi', NULL, 'mos')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Maori', 'mi', 'mri')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Malay', 'ms', 'msa', 62)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Multiple languages', NULL, 'mul')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Munda languages', NULL, 'mun')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Creek', NULL, 'mus')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Marwari', NULL, 'mwr')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Burmese', 'my', 'mya')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Mayan languages', NULL, 'myn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Erzya', NULL, 'myv')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nahuatl', NULL, 'nah')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('North American Indian', NULL, 'nai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Neapolitan', NULL, 'nap')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nauru', 'na', 'nau')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Navajo; Navaho', 'nv', 'nav')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ndebele, South; South Ndebele', 'nr', 'nbl')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ndebele, North; North Ndebele', 'nd', 'nde')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ndonga', 'ng', 'ndo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Low German; Low Saxon; German, Low; Saxon, Low', NULL, 'nds')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Nepali', 'ne', 'nep', 97)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Newari', NULL, 'new')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nias', NULL, 'nia')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Niger-Kordofanian (Other)', NULL, 'nic')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Niuean', NULL, 'niu')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Dutch; Flemish', 'nl', 'nld', 19)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Norwegian Nynorsk; Nynorsk, Norwegian', 'nn', 'nno', 20)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Norwegian Bokml; Bokml, Norwegian', 'nb', 'nob')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nogai', NULL, 'nog')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Norse, Old', NULL, 'non')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Norwegian', 'no', 'nor')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sotho, Northern', NULL, 'nso')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nubian languages', NULL, 'nub')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Classical Newari; Old Newari', NULL, 'nwc')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Chichewa; Chewa; Nyanja', 'ny', 'nya')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nyamwezi', NULL, 'nym')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nyankole', NULL, 'nyn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nyoro', NULL, 'nyo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nzima', NULL, 'nzi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Occitan (post 1500); Provenal', 'oc', 'oci')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ojibwa', 'oj', 'oji')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Oriya', 'or', 'ori', 72)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Oromo', 'om', 'orm')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Osage', NULL, 'osa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ossetian; Ossetic', 'os', 'oss')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Turkish, Ottoman (1500-1928)', NULL, 'ota')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Otomian languages', NULL, 'oto')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Papuan (Other)', NULL, 'paa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Pangasinan', NULL, 'pag')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Pahlavi', NULL, 'pal')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Pampanga', NULL, 'pam')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Panjabi; Punjabi', 'pa', 'pan', 70)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Papiamento', NULL, 'pap')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Palauan', NULL, 'pau')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Persian, Old (ca.600-400 B.C.)', NULL, 'peo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Philippine (Other)', NULL, 'phi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Phoenician', NULL, 'phn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Pali', 'pi', 'pli')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Polish', 'pl', 'pol', 21)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Pohnpeian', NULL, 'pon')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Portuguese', 'pt', 'por', 22)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Prakrit languages', NULL, 'pra')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Provenal, Old (to 1500)', NULL, 'pro')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Pushto', 'ps', 'pus')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Quechua', 'qu', 'que')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Rajasthani', NULL, 'raj')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Rapanui', NULL, 'rap')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Rarotongan', NULL, 'rar')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Romance (Other)', NULL, 'roa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Raeto-Romance', 'rm', 'roh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Romany', NULL, 'rom')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Romanian', 'ro', 'ron', 24)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Rundi', 'rn', 'run')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Russian', 'ru', 'rus', 25)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sandawe', NULL, 'sad')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sango', 'sg', 'sag')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Yakut', NULL, 'sah')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('South American Indian (Other)', NULL, 'sai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Salishan languages', NULL, 'sal')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Samaritan Aramaic', NULL, 'sam')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Sanskrit', 'sa', 'san', 79)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sasak', NULL, 'sas')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Santali', NULL, 'sat')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Scots', NULL, 'sco')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Selkup', NULL, 'sel')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Semitic (Other)', NULL, 'sem')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Irish, Old (to 900)', NULL, 'sga')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sign Languages', NULL, 'sgn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Shan', NULL, 'shn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sidamo', NULL, 'sid')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sinhalese', 'si', 'sin')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Siouan languages', NULL, 'sio')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sino-Tibetan (Other)', NULL, 'sit')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Slavic (Other)', NULL, 'sla')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Slovak', 'sk', 'slk', 27)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Slovenian', 'sl', 'slv', 36)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Southern Sami', NULL, 'sma')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Northern Sami', 'se', 'sme')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sami languages (Other)', NULL, 'smi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Lule Sami', NULL, 'smj')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Inari Sami', NULL, 'smn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Samoan', 'sm', 'smo')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Skolt Sami', NULL, 'sms')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Shona', 'sn', 'sna')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Sindhi', 'sd', 'snd', 89)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Soninke', NULL, 'snk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sogdian', NULL, 'sog')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Somali', 'so', 'som')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Songhai', NULL, 'son')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sotho, Southern', 'st', 'sot')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Spanish; Castilian', 'es', 'spa', 10)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Albanian', 'sq', 'sqi', 28)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sardinian', 'sc', 'srd')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Serbian', 'sr', 'srp', 26)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Serer', NULL, 'srr')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Nilo-Saharan (Other)', NULL, 'ssa')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Swati', 'ss', 'ssw')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sukuma', NULL, 'suk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sundanese', 'su', 'sun')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Susu', NULL, 'sus')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sumerian', NULL, 'sux')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Swahili', 'sw', 'swa', 65)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Swedish', 'sv', 'swe', 29)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Syriac', NULL, 'syr', 90)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tahitian', 'ty', 'tah')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tai (Other)', NULL, 'tai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Tamil', 'ta', 'tam', 73)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Tatar', 'tt', 'tat', 68)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Telugu', 'te', 'tel', 74)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Timne', NULL, 'tem')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tereno', NULL, 'ter')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tetum', NULL, 'tet')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tajik', 'tg', 'tgk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tagalog', 'tl', 'tgl')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Thai', 'th', 'tha', 30)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tigre', NULL, 'tig')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tigrinya', 'ti', 'tir')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tiv', NULL, 'tiv')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tokelau', NULL, 'tkl')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tlingit', NULL, 'tli')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tamashek', NULL, 'tmh')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tonga (Nyasa)', NULL, 'tog')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tonga (Tonga Islands)', 'to', 'ton')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tok Pisin', NULL, 'tpi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tsimshian', NULL, 'tsi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tswana', 'tn', 'tsn')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tsonga', 'ts', 'tso')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Turkmen', 'tk', 'tuk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tumbuka', NULL, 'tum')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tupi languages', NULL, 'tup')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Turkish', 'tr', 'tur', 31)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Altaic (Other)', NULL, 'tut')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tuvalu', NULL, 'tvl')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Twi', 'tw', 'twi')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Tuvinian', NULL, 'tyv')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Udmurt', NULL, 'udm')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Ugaritic', NULL, 'uga')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Uighur', 'ug', 'uig')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Ukrainian', 'uk', 'ukr', 34)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Umbundu', NULL, 'umb')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Undetermined', NULL, 'und')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Urdu', 'ur', 'urd', 32)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Uzbek', 'uz', 'uzb', 67)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Vai', NULL, 'vai')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Venda', 've', 'ven')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Vietnamese', 'vi', 'vie', 42)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Volap\xC3\xBCk', 'vo', 'vol')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Votic', NULL, 'vot')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Wakashan languages', NULL, 'wak')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Walamo', NULL, 'wal')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Waray', NULL, 'war')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Washo', NULL, 'was')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Sorbian languages', NULL, 'wen')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Walloon', 'wa', 'wln')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Wolof', 'wo', 'wol')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Kalmyk', NULL, 'xal')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Xhosa', 'xh', 'xho')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Yao', NULL, 'yao')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Yapese', NULL, 'yap')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Yiddish', 'yi', 'yid')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Yoruba', 'yo', 'yor')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Yupik languages', NULL, 'ypk')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Zapotec', NULL, 'zap')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Zenaga', NULL, 'zen')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Zhuang; Chuang', 'za', 'zha')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Chinese (Simplified)', 'zh', 'zho', 4)
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Zande', NULL, 'znd')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Zulu', 'zu', 'zul')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3)
		VALUES ('Zuni', NULL, 'zun')
	INSERT INTO Language (EnglishName, LangCode2, LangCode3, MSLanguageID)
		VALUES ('Neutral', NULL, 'neu', 0)
END
GO

IF NOT EXISTS(SELECT * FROM Country)
BEGIN
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AD', 'Andorra')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AE', 'United Arab Emirates')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AF', 'Afghanistan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AG', 'Antigua and Barbuda')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AI', 'Anguilla')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AL', 'Albania')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AM', 'Armenia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AN', 'Netherlands Antilles')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AO', 'Angola')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AQ', 'Antarctica')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AR', 'Argentina')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AS', 'American Samoa')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AT', 'Austria')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AU', 'Australia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AW', 'Aruba')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AX', 'Land Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('AZ', 'Azerbaijan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BA', 'Bosnia and Herzegovina')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BB', 'Barbados')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BD', 'Bangladesh')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BE', 'Belgium')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BF', 'Burkina Faso')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BG', 'Bulgaria')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BH', 'Bahrain')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BI', 'Burundi')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BJ', 'Benin')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BM', 'Bermuda')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BN', 'Brunei Darussalam')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BO', 'Bolivia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BR', 'Brazil')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BS', 'Bahamas')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BT', 'Bhutan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BV', 'Bouvet Island')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BW', 'Botswana')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BY', 'Belarus')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('BZ', 'Belize')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CA', 'Canada')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CC', 'Cocos (Keeling) Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CD', 'Congo, The Democratic Republic of The')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CF', 'Central African Republic')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CG', 'Congo')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CH', 'Switzerland')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CK', 'Cook Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CL', 'Chile')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CM', 'Cameroon')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CN', 'China')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CO', 'Colombia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CR', 'Costa Rica')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CS', 'Serbia and Montenegro')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CU', 'Cuba')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CV', 'Cape Verde')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CX', 'Christmas Island')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CY', 'Cyprus')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('CZ', 'Czech Republic')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('DE', 'Germany')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('DJ', 'Djibouti')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('DK', 'Denmark')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('DM', 'Dominica')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('DO', 'Dominican Republic')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('DZ', 'Algeria')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('EC', 'Ecuador')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('EE', 'Estonia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('EG', 'Egypt')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('EH', 'Western Sahara')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ER', 'Eritrea')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ES', 'Spain')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ET', 'Ethiopia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('FI', 'Finland')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('FJ', 'Fiji')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('FK', 'Falkland Islands (Malvinas)')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('FM', 'Micronesia, Federated States of')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('FO', 'Faroe Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('FR', 'France')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GA', 'Gabon')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GB', 'United Kingdom')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GD', 'Grenada')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GE', 'Georgia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GF', 'French Guiana')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GH', 'Ghana')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GI', 'Gibraltar')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GL', 'Greenland')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GM', 'Gambia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GN', 'Guinea')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GP', 'Guadeloupe')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GQ', 'Equatorial Guinea')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GR', 'Greece')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GS', 'South Georgia and The South Sandwich Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GT', 'Guatemala')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GU', 'Guam')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GW', 'Guinea-bissau')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('GY', 'Guyana')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('HK', 'Hong Kong')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('HM', 'Heard Island and Mcdonald Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('HN', 'Honduras')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('HR', 'Croatia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('HT', 'Haiti')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('HU', 'Hungary')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ID', 'Indonesia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IE', 'Ireland')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IL', 'Israel')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IN', 'India')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IO', 'British Indian Ocean Territory')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IQ', 'Iraq')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IR', 'Iran, Islamic Republic of')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IS', 'Iceland')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('IT', 'Italy')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('JM', 'Jamaica')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('JO', 'Jordan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('JP', 'Japan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KE', 'Kenya')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KG', 'Kyrgyzstan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KH', 'Cambodia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KI', 'Kiribati')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KM', 'Comoros')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KN', 'Saint Kitts and Nevis')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KP', 'Korea, Democratic People"s Republic of')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KR', 'Korea, Republic of')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KW', 'Kuwait')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KY', 'Cayman Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('KZ', 'Kazakhstan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LA', 'Lao People"s Democratic Republic')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LB', 'Lebanon')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LC', 'Saint Lucia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LI', 'Liechtenstein')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LK', 'Sri Lanka')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LR', 'Liberia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LS', 'Lesotho')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LT', 'Lithuania')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LU', 'Luxembourg')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LV', 'Latvia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('LY', 'Libyan Arab Jamahiriya')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MA', 'Morocco')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MC', 'Monaco')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MD', 'Moldova, Republic of')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MG', 'Madagascar')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MH', 'Marshall Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MK', 'Macedonia, The Former Yugoslav Republic of')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ML', 'Mali')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MM', 'Myanmar')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MN', 'Mongolia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MO', 'Macao')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MP', 'Northern Mariana Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MQ', 'Martinique')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MR', 'Mauritania')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MS', 'Montserrat')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MT', 'Malta')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MU', 'Mauritius')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MV', 'Maldives')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MW', 'Malawi')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MX', 'Mexico')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MY', 'Malaysia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('MZ', 'Mozambique')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NA', 'Namibia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NC', 'New Caledonia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NE', 'Niger')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NF', 'Norfolk Island')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NG', 'Nigeria')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NI', 'Nicaragua')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NL', 'Netherlands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NO', 'Norway')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NP', 'Nepal')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NR', 'Nauru')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NU', 'Niue')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('NZ', 'New Zealand')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('OM', 'Oman')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PA', 'Panama')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PE', 'Peru')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PF', 'French Polynesia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PG', 'Papua New Guinea')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PH', 'Philippines')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PK', 'Pakistan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PL', 'Poland')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PM', 'Saint Pierre and Miquelon')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PN', 'Pitcairn')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PR', 'Puerto Rico')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PS', 'Palestinian Territory, Occupied')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PT', 'Portugal')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PW', 'Palau')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('PY', 'Paraguay')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('QA', 'Qatar')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('RE', 'R\303\251union')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('RO', 'Romania')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('RU', 'Russian Federation')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('RW', 'Rwanda')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SA', 'Saudi Arabia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SB', 'Solomon Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SC', 'Seychelles')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SD', 'Sudan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SE', 'Sweden')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SG', 'Singapore')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SH', 'Saint Helena')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SI', 'Slovenia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SJ', 'Svalbard and Jan Mayen')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SK', 'Slovakia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SL', 'Sierra Leone')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SM', 'San Marino')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SN', 'Senegal')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SO', 'Somalia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SR', 'Suriname')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ST', 'Sao Tome and Principe')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SV', 'El Salvador')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SY', 'Syrian Arab Republic')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('SZ', 'Swaziland')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TC', 'Turks and Caicos Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TD', 'Chad')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TF', 'French Southern Territories')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TG', 'Togo')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TH', 'Thailand')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TJ', 'Tajikistan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TK', 'Tokelau')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TL', 'Timor-leste')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TM', 'Turkmenistan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TN', 'Tunisia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TO', 'Tonga')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TR', 'Turkey')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TT', 'Trinidad and Tobago')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TV', 'Tuvalu')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TW', 'Taiwan, Province of China')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('TZ', 'Tanzania, United Republic of')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('UA', 'Ukraine')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('UG', 'Uganda')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('UM', 'United States Minor Outlying Islands')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('US', 'United States')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('UY', 'Uruguay')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('UZ', 'Uzbekistan')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('VA', 'Holy See (Vatican City State)')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('VC', 'Saint Vincent and The Grenadines')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('VE', 'Venezuela')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('VG', 'Virgin Islands, British')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('VI', 'Virgin Islands, U.S.')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('VN', 'Viet Nam')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('VU', 'Vanuatu')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('WF', 'Wallis and Futuna')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('WS', 'Samoa')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('YE', 'Yemen')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('YT', 'Mayotte')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ZA', 'South Africa')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ZM', 'Zambia')
	INSERT INTO Country (CountryCode, Name)
		VALUES ('ZW', 'Zimbabwe')
END
GO

IF NOT EXISTS(SELECT * FROM Locale)
BEGIN
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('eng', 'eng', 'English')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('fra', 'fra', 'French')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('deu', 'deu', 'German')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('ara', 'ara', 'Arabic')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('zho', 'zho', 'Chinese')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('nld', 'nld', 'Dutch')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('ita', 'ita', 'Italian')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('por', 'por', 'Portugese')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('srp', 'srp', 'Serbian')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('spa', 'spa', 'Spanish')
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName)
		VALUES ('uzb', 'uzb', 'Uzbek')
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('dz', 'ara', 'ara-dz', 'Arabic (Algeria)', 5121)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('bh', 'ara', 'ara-bh', 'Arabic (Bahrain)', 15361)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('eg', 'ara', 'ara-eg', 'Arabic (Egypt)', 3073)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('jo', 'ara', 'ara-jo', 'Arabic (Jordan)', 11265)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('kw', 'ara', 'ara-kw', 'Arabic (Kuwait)', 13313)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('lb', 'ara', 'ara-lb', 'Arabic (Lebanon)', 12289)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ly', 'ara', 'ara-ly', 'Arabic (Libya)', 4097)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ma', 'ara', 'ara-ma', 'Arabic (Morocco)', 6145)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('om', 'ara', 'ara-om', 'Arabic (Oman)', 8193)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('sy', 'ara', 'ara-sy', 'Arabic (Syria)', 10241)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('tn', 'ara', 'ara-tn', 'Arabic (Tunisia)', 7169)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ye', 'ara', 'ara-ye', 'Arabic (Yemen)', 9217)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('at', 'deu', 'deu-at', 'German (Austria)', 3079)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('li', 'deu', 'deu-li', 'German (Liechtenstein)', 5127)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('lu', 'deu', 'deu-lu', 'German (Luxembourg)', 4103)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ch', 'deu', 'deu-ch', 'German (Switzerland)', 2055)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('au', 'eng', 'eng-au', 'English (Australia)', 3081)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('bz', 'eng', 'eng-bz', 'English (Belize)', 10249)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ca', 'eng', 'eng-ca', 'English (Canada)', 4105)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ie', 'eng', 'eng-ie', 'English (Ireland)', 6153)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('jm', 'eng', 'eng-jm', 'English (Jamaica)', 8201)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('nz', 'eng', 'eng-nz', 'English (New Zealand)', 5129)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ph', 'eng', 'eng-ph', 'English (Philippines)', 13321)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('za', 'eng', 'eng-za', 'English (South Africa)', 7177)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('tt', 'eng', 'eng-tt', 'English (Trinidad)', 11273)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('gb', 'eng', 'eng-gb', 'English (United Kingdom)', 2057)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('zw', 'eng', 'eng-zw', 'English (Zimbabwe)', 12297)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('be', 'fra', 'fra-be', 'French (Belgium)', 2060)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cm', 'fra', 'fra-cm', 'French (Cameroon)', 11276)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ca', 'fra', 'fra-ca', 'French (Canada)', 3084)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cd', 'fra', 'fra-cd', 'French (Congo, DRC)', 9228)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('lu', 'fra', 'fra-lu', 'French (Luxembourg)', 5132)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ml', 'fra', 'fra-ml', 'French (Mali)', 13324)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('mc', 'fra', 'fra-mc', 'French (Monaco)', 6156)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('sn', 'fra', 'fra-sn', 'French (Senegal)', 10252)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ch', 'fra', 'fra-ch', 'French (Switzerland)', 4108)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ch', 'ita', 'ita-ch', 'Italian (Switzerland)', 2064)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('be', 'nld', 'nld-be', 'Dutch (Belgium)', 2067)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ar', 'spa', 'spa-ar', 'Spanish (Argentina)', 11274)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('bo', 'spa', 'spa-bo', 'Spanish (Bolivia)', 16394)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cl', 'spa', 'spa-cl', 'Spanish (Chile)', 13322)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('co', 'spa', 'spa-co', 'Spanish (Colombia)', 9226)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cr', 'spa', 'spa-cr', 'Spanish (Costa Rica)', 5130)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('do', 'spa', 'spa-do', 'Spanish (Dominican Republic)', 7178)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ec', 'spa', 'spa-ec', 'Spanish (Ecuador)', 12298)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('sv', 'spa', 'spa-sv', 'Spanish (El Salvador)', 17418)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('gt', 'spa', 'spa-gt', 'Spanish (Guatemala)', 4106)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('hn', 'spa', 'spa-hn', 'Spanish (Honduras)', 18442)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('mx', 'spa', 'spa-mx', 'Spanish (Mexico)', 2058)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ni', 'spa', 'spa-ni', 'Spanish (Nicaragua)', 19466)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('pa', 'spa', 'spa-pa', 'Spanish (Panama)', 6154)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('py', 'spa', 'spa-py', 'Spanish (Paraguay)', 15370)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('pe', 'spa', 'spa-pe', 'Spanish (Peru)', 10250)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('pr', 'spa', 'spa-pr', 'Spanish (Puerto Rico)', 20490)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('uy', 'spa', 'spa-uy', 'Spanish (Uruguay)', 14346)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('ve', 'spa', 'spa-ve', 'Spanish (Venezuela)', 8202)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cs', 'srp', 'srp-cs', 'Serbian (Cyrillic)', 3098)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('tr', 'srp', 'srp-tr', 'Serbian (Latin)', 2074)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cs', 'uzb', 'uzb-cs', 'Uzbek (Cyrillic)', 2115)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('tr', 'uzb', 'uzb-tr', 'Uzbek (Latin)', 1091)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('sa', 'ara', 'ara-sa', 'Arabic (Saudi Arabia)', 1025)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('hk', 'zho', 'zho-hk', 'Chinese (Hong Kong S.A.R.)', 3076)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cn', 'zho', 'zho-cn', 'Chinese (PRC)', 2052)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('tw', 'zho', 'zho-tw', 'Chinese (Taiwan)', 1028)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('cz', 'ces', 'ces', 'Czech', 1029)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('dk', 'dan', 'dan', 'Danish', 1030)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('nl', 'nld', 'nld-nl', 'Dutch (Netherlands)', 1043)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('us', 'eng', 'eng-us', 'English (United States)', 1033)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('fi', 'fin', 'fin', 'Finnish', 1035)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('fr', 'fra', 'fra-fr', 'French (France)', 1036)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('de', 'deu', 'deu-de', 'German (Germany)', 1031)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('gr', 'ell', 'ell-gr', 'Greek', 1032)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('il', 'heb', 'heb', 'Hebrew', 1037)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('hu', 'hun', 'hun', 'Hungarian', 1038)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('it', 'ita', 'ita-it', 'Italian (Italy)', 1040)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('jp', 'jpn', 'jpn', 'Japanese', 1041)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('kr', 'kor', 'kor', 'Korean', 1042)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('no', 'nob', 'nob-no', 'Norwegian (Bokmal)', 1044)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('no', 'nno', 'nno-no', 'Norwegian (Nynorsk)', 2068)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('pl', 'pol', 'pol', 'Polish', 1045)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('pt', 'por', 'por-pt', 'Portuguese (Portugal)', 2070)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('br', 'por', 'por-br', 'Portuguese (Brazil)', 1046)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('pt', 'rus', 'rus', 'Russian', 1049)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('si', 'slv', 'slv', 'Slovenian', 1060)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('sk', 'slk', 'slk', 'Slovak', 1051)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('es', 'spa', 'spa-es', 'Spanish (Spain)', 3082)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('se', 'swe', 'swe', 'Swedish', 1053)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('th', 'tha', 'tha', 'Thai', 1054)
	INSERT INTO Locale (CountryCode, LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('tr', 'tur', 'tur', 'Turkish', 1055)
	INSERT INTO Locale (LangCode3, LocaleCode, LocaleName, MSLocaleID)
		VALUES ('neu', 'neu', 'Neutral', 0)
END
GO

IF NOT EXISTS(SELECT * FROM Organization)
BEGIN
	SET IDENTITY_INSERT Organization ON
	-- This organization represents the universal domain,
	-- which contains Directory items having no domain relevance
	-- (mainly the SYSTEM user).
	INSERT INTO Organization (DomainID, OrganizationID, RDN)
		VALUES (1, 1, '')
	SET IDENTITY_INSERT Organization OFF
END
GO

IF NOT EXISTS(SELECT * FROM Domain)
BEGIN
	-- This is the universal domain, which contains Directory items
	-- having no domain relevance (mainly the SYSTEM user).
	INSERT INTO Domain (DN, DNReverse, OrganizationID)
		VALUES (NULL, NULL, 1)
END
GO

IF NOT EXISTS(SELECT * FROM [User])
BEGIN
	SET IDENTITY_INSERT [User] ON
	-- The universal user SYSTEM identifies the computer itself,
	-- rather than an identified user.
	INSERT INTO [User] (UserCN, UserID, UserOUID)
		VALUES ('SYSTEM', 1, 1)
	SET IDENTITY_INSERT [User] OFF
END
GO

IF NOT EXISTS(SELECT * FROM [Group])
BEGIN
	SET IDENTITY_INSERT [Group] ON
	INSERT INTO [Group] (GUID, GroupCN, GroupID, GroupOUID)
		VALUES (0x00000000000000000000000000000000, 'Authenticated Users', 1, 1)
	SET IDENTITY_INSERT [Group] OFF
END
GO

IF NOT EXISTS(SELECT * FROM PackageState)
BEGIN
	SET IDENTITY_INSERT PackageState ON
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Packaging', 1)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Packed', 2)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Received', 3)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Integration: testing', 4)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Integration: tested', 5)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Integration: successful', 6)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Integration: unsuccessful', 7)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Release Acceptance: testing', 8)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Release Acceptance: tested', 9)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Release Acceptance: successful', 10)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Test Cycle Release Acceptance: unsuccessful', 11)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (1, 'Certified', 12)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (1, 'Released', 13)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'On Hold', 14)
	INSERT INTO PackageState (CanAddToPolicy, Name, PackageStateID)
		VALUES (0, 'Archived', 15)
	SET IDENTITY_INSERT PackageState OFF
END
GO

IF NOT EXISTS(SELECT * FROM PackagePathType)
BEGIN
	SET IDENTITY_INSERT PackagePathType ON
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('Unknown', 1)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('Package', 2)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('Schedule', 3)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('Schedule,6.0', 4)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('ClientConfiguration', 5)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('ClientSettings,6.0', 6)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('Folder', 7)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('CustomPackage', 8)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('OSImage', 9)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('RolloutConfig', 10)
	INSERT INTO PackagePathType (Description, PackagePathTypeID)
		VALUES ('NameMap', 11)
	SET IDENTITY_INSERT PackagePathType OFF
END
GO

IF NOT EXISTS(SELECT * FROM Architecture)
BEGIN
	SET IDENTITY_INSERT Architecture ON
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (1, 'i386')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (2, 'i586')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (3, 'i686')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (4, 'athlon')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (5, 'noarch')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (6, 'pa_risc1.0')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (7, 'pa_risc2.0')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (8, 'ia32')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (9, 'sparc')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (10, 'ia64')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (11, 'sparcv9')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (12, 'powerpc')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (13, 'ppc')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (14, 'ppc64')
	INSERT INTO Architecture (ArchitectureID, ArchitectureName)
		VALUES (15, 'x86_64')
	SET IDENTITY_INSERT Architecture OFF
END
GO

IF NOT EXISTS(SELECT * FROM MediaType)
BEGIN
	SET IDENTITY_INSERT MediaType ON
	-- ManageSoft administration server has MediaType 1.
	INSERT INTO MediaType (Description, MediaTypeID)
		VALUES ('Warehouse', 1)
	INSERT INTO MediaType (Description, MediaTypeID)
		VALUES ('Filesystem', 2)
	INSERT INTO MediaType (Description, MediaTypeID)
		VALUES ('CD', 3)
	SET IDENTITY_INSERT MediaType OFF
END
GO

IF NOT EXISTS(SELECT * FROM Media)
BEGIN
	SET IDENTITY_INSERT Media ON
	-- The local ManageSoft administration server is Media 1.
	INSERT INTO Media (MediaID, MediaTypeID, Title)
		VALUES (1, 1, '')
	SET IDENTITY_INSERT Media OFF
END
GO

IF NOT EXISTS(SELECT * FROM HardwareClass)
BEGIN
	SET IDENTITY_INSERT HardwareClass ON
	-- CIM ClassIDs start from 1.
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ManagedSystemElement', 1)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Dependency', 2)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Setting', 3)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Component', 4)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ElementSetting', 5)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_StatisticalInformation', 6)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Statistics', 7)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_RelatedStatistics', 8)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Product', 9)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Location', 10)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_PhysicalElementLocation', 11)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_PhysicalCapacity', 12)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ElementCapacity', 13)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ReplacementSet', 14)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ParticipatesInSet', 15)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_AdjacentSlots', 16)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ActsAsSpare', 17)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_CompatibleProduct', 18)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Configuration', 19)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_DependencyContext', 20)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_DirectorySpecificationFile', 21)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ElementConfiguration', 22)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_FromDirectoryAction', 23)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_FromDirectorySpecification', 24)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_FRU', 25)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_FRUIncludesProduct', 26)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_FRUPhysicalElements', 27)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ProductFRU', 28)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ProductParentChild', 29)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ProductPhysicalElements', 30)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ProductProductDependency', 31)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ProductSupport', 32)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_SettingContext', 33)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_StorageDefect', 34)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_StorageError', 35)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_SupportAccess', 36)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ToDirectoryAction', 37)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ToDirectorySpecification', 38)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Export', 39)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Check', 40)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_Action', 41)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_SoftwareElementActions', 42)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ProductSoftwareFeatures', 43)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_InstalledSoftwareElement', 44)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_SoftwareElementChecks', 45)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ActionSequence', 46)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogicalElement', 47, 1)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Process', 48, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_ManagedElement', 49)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SystemResource', 58, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_IRQ', 59, 58)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MemoryMappedIO', 60, 58)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DMA', 61, 58)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_System', 62, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ComputerSystem', 63, 62)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_UnitaryComputerSystem', 64, 63)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Service', 65, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ClusteringService', 66, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ServiceAccessPoint', 67, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ClusteringSAP', 68, 67)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogicalDevice', 69, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MediaAccessDevice', 70, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DisketteDrive', 71, 70)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CoolingDevice', 72, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Refrigeration', 73, 72)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_HeatPipe', 74, 72)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Fan', 75, 72)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Sensor', 76, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_NumericSensor', 77, 76)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_TemperatureSensor', 78, 77)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CurrentSensor', 79, 77)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VoltageSensor', 80, 77)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CDROMDrive', 81, 70)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DiskDrive', 82, 70)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_TapeDrive', 83, 70)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Controller', 84, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VideoController', 85, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PCVideoController', 86, 85)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PCMCIAController', 87, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_USBController', 88, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_NetworkAdapter', 89, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_UserDevice', 90, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Display', 91, 90)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DesktopMonitor', 92, 91)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_FlatPanel', 93, 91)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Keyboard', 94, 90)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PointingDevice', 95, 90)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_StorageExtent', 96, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalExtent', 97, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AggregatePExtent', 98, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogicalDisk', 99, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ServiceServiceDependency', 100, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceConnection', 101, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedSensor', 102, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ControlledBy', 103, 101)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BasedOn', 104, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MediaPresent', 105, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DiskPartition', 106, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogicalDiskBasedOnPartition', 107, 104)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PotsModem', 108, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PowerSupply', 109, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Battery', 110, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Printer', 111, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Processor', 112, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_UninterruptiblePowerSupply', 113, 109)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ParallelController', 114, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SCSIController', 115, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SerialController', 116, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogicalFile', 117, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DataFile', 118, 117)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ProcessExecutable', 119, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Directory', 120, 117)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SoftwareElement', 121, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BIOSElement', 122, 121)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Job', 123, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_OperatingSystem', 124, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Thread', 126, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VideoControllerResolution', 127, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DirectoryContainsFile', 128, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SystemComponent', 129, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SystemDevice', 130, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_InstalledOS', 131, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VideoSetting', 132, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MonitorResolution', 133, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MonitorSetting', 134, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedBattery', 135, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AllocatedResource', 136, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceErrorCounts', 137, 6)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ErrorCountersForDevice', 138, 7)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalElement', 139, 1)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Realizes', 140, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Memory', 141, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_NonVolatileStorage', 142, 141)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VolatileStorage', 143, 141)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedMemory', 144, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedProcessorMemory', 145, 144)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CacheMemory', 146, 141)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AlarmDevice', 147, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MemoryCapacity', 148, 12)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalPackage', 149, 139)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Container', 150, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalFrame', 151, 149)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Rack', 152, 151)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Chassis', 153, 151)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ChassisInRack', 154, 150)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PackageInChassis', 155, 150)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Docked', 156, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Card', 157, 149)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CardOnCard', 158, 150)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalComponent', 159, 139)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PackagedComponent', 160, 150)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Chip', 161, 159)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalMemory', 162, 161)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MemoryOnCard', 163, 160)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalMedia', 164, 159)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MemoryWithMedia', 165, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RealizesPExtent', 166, 140)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RealizesDiskPartition', 167, 140)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RealizesAggregatePExtent', 168, 140)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalLink', 169, 139)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ElementsLinked', 170, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PhysicalConnector', 171, 139)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ConnectedTo', 172, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Slot', 173, 171)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SlotInSlot', 174, 172)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PackageInSlot', 175, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CardInSlot', 176, 175)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LinkHasConnector', 177, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ConnectorOnPackage', 178, 150)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ComputerSystemPackage', 179, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PackageCooling', 180, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PackageTempSensor', 181, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PackageAlarm', 182, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CollectionOfSensors', 183, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_FileStorage', 184, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_OperatingSystemSoftwareFeature', 185, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_OSProcess', 186, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ProcessThread', 187, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RedundancyComponent', 188, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AggregateRedundancyComponent', 189, 188)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PExtentRedundancyComponent', 190, 188)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ComputerSystemResource', 191, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ComputerSystemDMA', 192, 191)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ComputerSystemIRQ', 193, 191)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ComputerSystemMappedIO', 194, 191)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_HostedFileSystem', 195, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedAlarm', 196, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedCooling', 197, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedSupplyCurrentSensor', 198, 102)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AssociatedSupplyVoltageSensor', 199, 102)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogicalDiskBasedOnVolumeSet', 200, 104)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PSExtentBasedOnPExtent', 201, 104)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BIOSLoadedInNV', 202, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BootOSFromFS', 203, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceAccessedByFile', 204, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SCSIInterface', 205, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SerialInterface', 206, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceServiceImplementation', 207, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceSoftware', 208, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_HostedAccessPoint', 209, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_HostedBootSAP', 210, 209)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_HostedJobDestination', 211, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_HostedService', 212, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_HostedBootService', 213, 212)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_JobDestinationJobs', 214, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ResidesOnExtent', 215, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RunningOS', 216, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_FileSystem', 217, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LocalFileSystem', 218, 217)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_JobDestination', 219, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ManagementController', 220, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PCIController', 221, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MagnetoOpticalDrive', 222, 70)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_WORMDrive', 223, 70)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Scanner', 224, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BinarySensor', 225, 76)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DiscreteSensor', 226, 76)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MultiStateSensor', 227, 76)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Tachometer', 228, 77)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AggregatePSExtent', 229, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ProtectedSpaceExtent', 230, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VolumeSet', 231, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceFile', 232, 117)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RedundancyGroup', 233, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ExtraCapacityGroup', 234, 233)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SpareGroup', 235, 233)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_StorageRedundancyGroup', 236, 233)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VideoBIOSElement', 237, 121)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ApplicationSystem', 238, 62)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Mount', 239, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RemoteFileSystem', 240, 217)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_NFS', 241, 240)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BootService', 242, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SoftwareFeature', 243, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DirectorySpecification', 244, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_FileSpecification', 245, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DirectoryAction', 246, 41)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CreateDirectoryAction', 247, 246)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RemoveDirectoryAction', 248, 246)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_FileAction', 249, 41)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CopyFileAction', 250, 249)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RemoveFileAction', 251, 249)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SoftwareFeatureSoftwareElements', 252, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ServiceAccessBySAP', 253, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ExecuteProgram', 254, 41)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ModifySettingAction', 255, 41)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_RebootAction', 256, 41)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DiskSpaceCheck', 257, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MemoryCheck', 258, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_OSVersionCheck', 259, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SettingCheck', 260, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SoftwareElementVersionCheck', 261, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SwapSpaceCheck', 262, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VersionCompatibilityCheck', 263, 40)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BIOSFeatureBIOSElements', 264, 252)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VideoBIOSFeatureVideoBIOSElements', 265, 252)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ApplicationSystemSoftwareFeature', 266, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceSAPImplementation', 267, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SoftwareFeatureSAPImplementation', 268, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SoftwareFeatureServiceImplementation', 269, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SAPSAPDependency', 270, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BootServiceAccessBySAP', 271, 253)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ClusterServiceAccessBySAP', 272, 253)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ServiceSAPDependency', 273, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BootSAP', 274, 67)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BIOSFeature', 275, 243)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_VideoBIOSFeature', 276, 243)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_USBDevice', 277, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_USBHub', 278, 277)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_USBControllerHasHub', 279, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_UnixProcess', 280, 48)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_UnixDirectory', 281, 120)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_UnixLocalFileSystem', 282, 218)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_StorageVolume', 283, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DiskPartitionBasedOnVolume', 284, 104)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_InfraredController', 285, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BindsTo', 286, 270)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_BindsToLANEndpoint', 287, 286)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_NextHopRouting', 288, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_IPRoute', 289, 288)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_IndicationHandler', 290, 49)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SystemStatisticalInformation', 291, 6)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceStatistics', 292, 7)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PrintJob', 293, 123)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PrintService', 294, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PrintQueue', 295, 219)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PrintQueueForPrintService', 296, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_OwningPrintQueue', 297, 214)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PrinterServicingQueue', 298, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_PrintSAP', 299, 67)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_EthernetAdapter', 300, 89)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_MessageLog', 301, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogRecord', 302, 49)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('CIM_RecordInLog', 303)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogInDataFile', 304, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_AdminDomain', 305, 62)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ProtocolEndpoint', 306, 67)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_IPProtocolEndpoint', 307, 306)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Collection', 308, 49)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_CollectionOfMSEs', 309, 308)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_LogicalNetwork', 310, 309)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_IPSubnet', 311, 310)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_SystemStatistics', 312, 7)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DeviceStatisticalInformation', 313, 6)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_TimeZone', 314, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_Account', 315, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DiskGroup', 316, 309)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ExtentRedundancyComponent', 317, 188)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_DriveInDiskGroup', 318, 309)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('CIM_ExtentInDiskGroup', 319, 309)
	-- Windows ClassIDs start from 1000.
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('__ExtendedStatus', 1000)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('__ExtrinsicEvent', 1001)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_MethodParameterClass', 1002)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_WMISetting', 1011, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_WMIElementSetting', 1012, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ComputerSystem', 1013, 64)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ProgramGroupOrItem', 1014, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalProgramGroup', 1015, 1014)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalProgramGroupItem', 1016, 1014)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Processor', 1017, 112)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ShortcutFile', 1018, 118)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_CodecFile', 1019, 118)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_BaseService', 1020, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemDriver', 1021, 1020)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Directory', 1022, 120)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PageFile', 1023, 118)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PageFileUsage', 1024, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PageFileSetting', 1025, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PageFileElementSetting', 1026, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Registry', 1027, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ScheduledJob', 1028, 123)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Service', 1029, 1020)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Share', 1100, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_AssociatedProcessorMemory', 1101, 145)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_BaseBoard', 1102, 157)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_BIOS', 1103, 122)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_BootConfiguration', 1104, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Desktop', 1105, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemDevices', 1106, 130)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ComputerSystemProcessor', 1107, 1106)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ComputerSystemProduct', 1108, 9)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DependentService', 1109, 100)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DMAChannel', 1110, 61)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Environment', 1111, 58)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_IRQResource', 1112, 59)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LoadOrderGroup', 1113, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LoadOrderGroupServiceMembers', 1114, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LoadOrderGroupServiceDependencies', 1115, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalDiskRootDirectory', 1116, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_OperatingSystem', 1117, 124)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_OperatingSystemQFE', 1118, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_OSRecoveryConfiguration', 1119, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemMemoryResource', 1120, 60)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DeviceMemoryAddress', 1121, 1120)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PortResource', 1122, 1120)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ProcessStartup', 1123, 1002)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Process', 1124, 48)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_StartupCommand', 1125, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SubDirectory', 1126, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemBIOS', 1127, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemOperatingSystem', 1128, 131)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemSystemDriver', 1129, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemUsers', 1130, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemProcesses', 1131, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemPartitions', 1132, 1106)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemServices', 1133, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemNetworkConnections', 1134, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemResources', 1135, 191)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemSetting', 1136, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemDesktop', 1137, 1136)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemBootConfiguration', 1138, 1136)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemEnclosure', 1139, 153)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemLoadOrderGroups', 1140, 129)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemTimeZone', 1141, 1136)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemProgramGroups', 1142, 1136)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemLogicalMemoryConfiguration', 1143, 1136)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemSlot', 1144, 173)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Thread', 1145, 126)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_TimeZone', 1146, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_UserDesktop', 1147, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_QuickFixEngineering', 1148, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalProgramGroupDirectory', 1149, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalProgramGroupItemDataFile', 1150, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ProgramGroupContents', 1151, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_FloppyDrive', 1152, 71)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_TapeDrive', 1153, 83)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DiskDrive', 1154, 82)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_CDROMDrive', 1155, 81)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_FloppyController', 1156, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalDisk', 1157, 99)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DiskPartition', 1158, 106)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DiskDriveToDiskPartition', 1159, 105)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalDiskToPartition', 1160, 107)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_AutochkSetting', 1161, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_OperatingSystemAutochkSetting', 1162, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_MappedLogicalDisk', 1163, 99)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogonSessionMappedDisk', 1164, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DesktopMonitor', 1165, 92)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PointingDevice', 1166, 95)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Keyboard', 1167, 94)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_VideoController', 1168, 86)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DriverVXD', 1169, 121)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DisplayConfiguration', 1170, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DisplayControllerConfiguration', 1171, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_VideoConfiguration', 1172, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_VideoSettings', 1173, 132)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SoundDevice', 1174, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Battery', 1175, 110)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_AssociatedBattery', 1176, 135)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PortableBattery', 1177, 110)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PowerManagementEvent', 1178, 1001)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PCMCIAController', 1179, 87)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_1394Controller', 1180, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_MotherboardDevice', 1181, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_1394ControllerDevice', 1182, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_IDEController', 1183, 84)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_IDEControllerDevice', 1184, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DeviceSettings', 1185, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_AllocatedResource', 1186, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PNPAllocatedResource', 1187, 136)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Bus', 1188, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DeviceBus', 1189, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_CIMLogicalDeviceCIMDataFile', 1190, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_OnBoardDevice', 1191, 159)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_VoltageProbe', 1192, 80)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_CurrentProbe', 1193, 79)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_TemperatureProbe', 1194, 78)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Fan', 1195, 75)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Refrigeration', 1196, 73)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_HeatPipe', 1197, 74)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PnPEntity', 1198, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemDriverPNPEntity', 1199, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DeviceChangeEvent', 1200, 1001)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_VolumeChangeEvent', 1201, 1200)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemConfigurationChangeEvent', 1202, 1200)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Printer', 1203, 111)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrinterDriver', 1204, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DriverForDevice', 1205, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_TCPIPPrinterPort', 1206, 67)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrinterController', 1207, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_UninterruptiblePowerSupply', 1208, 113)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrinterDriverDll', 1209, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrinterShare', 1210, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrintJob', 1211, 123)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrinterConfiguration', 1212, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ProgramGroup', 1213, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrinterSetting', 1214, 1185)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_POTSModem', 1215, 108)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_POTSModemToSerialPort', 1216, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_NetworkAdapter', 1217, 89)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_NetworkConnection', 1218, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_NetworkClient', 1219, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_NetworkProtocol', 1220, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_NetworkAdapterConfiguration', 1221, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_NetworkAdapterSetting', 1222, 1185)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_USBController', 1223, 88)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PrivilegesStatus', 1224, 1000)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_InfraredDevice', 1225, 285)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SerialPort', 1226, 116)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SCSIController', 1227, 115)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_USBControllerDevice', 1228, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SCSIControllerDevice', 1229, 103)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ParallelPort', 1230, 114)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SerialPortConfiguration', 1231, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SerialPortSetting', 1232, 1185)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PortConnector', 1233, 171)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogicalMemoryConfiguration', 1234, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PhysicalMemory', 1235, 162)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_CacheMemory', 1236, 146)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PhysicalMemoryArray', 1237, 149)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SMBIOSMemory', 1238, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_MemoryArray', 1239, 1238)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_MemoryDevice', 1240, 1238)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_MemoryDeviceLocation', 1241, 140)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_MemoryArrayLocation', 1242, 140)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_PhysicalMemoryLocation', 1243, 160)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_MemoryDeviceArray', 1244, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_COMApplication', 1245, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_COMClass', 1246, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_COMSetting', 1247, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_COMApplicationClasses', 1248, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DCOMApplication', 1249, 1245)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_DCOMApplicationSetting', 1250, 1247)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ClassicCOMClass', 1251, 1246)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ClassicCOMClassSetting', 1252, 1247)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ClassicCOMApplicationClasses', 1253, 1248)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_COMApplicationSettings', 1254, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ClassicCOMClassSettings', 1255, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_ComponentCategory', 1256, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Account', 1257, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SystemAccount', 1258, 1257)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Group', 1259, 1257)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_UserAccount', 1260, 1257)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_NetworkLoginProfile', 1261, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_GroupUser', 1262, 4)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_Session', 1263, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LogonSession', 1264, 1263)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SubSession', 1265, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_LoggedOnUser', 1266, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SessionResource', 1267, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Win32_SessionProcess', 1268, 1267)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_ShareToDirectory', 1269)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_PnPDevice', 1270)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_ProtocolBinding', 1271)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_ImplementedCategory', 1272)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_ComClassEmulator', 1273)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_ComClassAutoEmulator', 1274)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Win32_ClientApplicationSetting', 1275)
	-- Unix/Linux ClassIDs start from 2000.
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_BIOSProduct', 2000, 9)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_BIOSFeature', 2001, 275)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_BIOSElement', 2002, 122)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_BIOSProductBIOSFeatures', 2003, 43)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_BIOSFeatureBIOSElements', 2004, 264)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_UnitaryComputerSystem', 2005, 64)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_OperatingSystem', 2006, 124)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_Process', 2007, 48)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_OSProcess', 2008, 186)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_RunningOS', 2009, 216)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_Directory', 2010, 281)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_Ext2FileSystem', 2011, 282)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_Ext3FileSystem', 2012, 282)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_ReiserFileSystem', 2013, 282)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_Mount', 2014, 239)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_NFS', 2015, 241)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_NFSExport', 2016, 39)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_HostedFileSystem', 2017, 195)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_BootOSFromFS', 2018, 203)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_ResidesOnExtent', 2019, 215)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_NativeLogicalDisk', 2020, 99)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_DiskPartition', 2021, 106)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_StorageVolume', 2022, 283)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_DiskPartitionBasedOnVolume', 2023, 284)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_LogicalDiskBasedOnPartition', 2024, 107)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_Processor', 2025, 112)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Linux_VideoController', 2026, 86)
	-- HP-UX uses classes of the Pegasus convention; we start from 3000.
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('PG_Authorization', 3000)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_ComputerSystem', 3001, 64)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('PG_ConfigSetting', 3003)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_DNSService', 3004, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_IndicationHandlerSNMPMapper', 3005, 290)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_BindsIPToLANEndpoint', 3006, 287)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_IPRoute', 3007, 289)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_NISServerService', 3008, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_NTPService', 3009, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_OperatingSystem', 3010, 124)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_ProviderModule', 3011, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_Provider', 3012, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_CapabilitiesRegistration', 3013, 49)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_ProviderCapabilities', 3014, 3013)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_ConsumerCapabilities', 3015, 3013)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_ProviderCapabilitiesElements', 3016, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_ProviderModuleElements', 3017, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('PG_ShutdownService', 3018)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_UnixProcess', 3019, 48)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_UnixProcessStatisticalInformation', 3020, 6)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_UnixProcessStatistics', 3021, 7)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('PG_UnixThread', 3022, 126)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('PG_User', 3023)
	-- On some platforms we mine CIM compliant data ourselves.	 MGS ClassID's start from 4000.
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_Processor', 4000, 112)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_LogicalDisk', 4001, 99)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_CDROMDrive', 4002, 81)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_DiskDrive', 4003, 82)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_NetworkAdapter', 4004, 89)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_NetworkAdapterConfiguration', 4005, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_OperatingSystem', 4006, 124)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_ComputerSystem', 4007, 64)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_PhysicalMemory', 4008, 162)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_MacBootROM', 4009, 122)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('MGS_SystemEnclosure', 4010, 153)
	-- Solaris ClassIDs start from 5000.
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_Acl', 5000)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_NamespaceAcl', 5001, 5000)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_UserAcl', 5002, 5000)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SoftwareElement', 5003, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Package', 5004, 5003)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Patch', 5005, 5003)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SoftwareElementDependency', 5006, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_PatchToPatchDependency', 5007, 5006)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_PatchPackageDependency', 5008, 5006)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SoftwareFeature', 5009, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ProductSoftwareFeatures', 5010, 43)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_RegistryElementDependency', 5011, 5006)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_RegistrySoftwareElement', 5012, 5003)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SoftwareFeatureSoftwareElements', 5013, 252)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SoftwareFeatureDependency', 5014)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_InstalledSoftwareElement', 5015, 44)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_ProductSoftwareFeatureDependency', 5016)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_ProductSoftwareElementDependency', 5017)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SoftwareFeatureSoftwareElementDependency', 5018)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SoftwareElementProductDependency', 5019)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SoftwareElementSoftwareFeatureDependency', 5020)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_ProductSoftwareElements', 5021)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SoftwareFeatureParentChild', 5022)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SoftwareFeatureProductDependency', 5023)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_ProviderPath', 5024)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_CIMOM', 5025)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_LogRecord', 5026)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ComputerSystem', 5027, 64)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LogService', 5028, 65)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SystemSetting', 5029, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Product', 5030, 9)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ProductProductDependency', 5031, 31)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ProductParentChild', 5032, 29)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SystemDownStatisticalInformation', 5033, 291)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SystemUpStatisticalInformation', 5034, 291)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Processor', 5035, 112)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_CpuUtilizationPerformanceMonitor', 5036, 292)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_CpuVminfoPerformanceMonitor', 5037, 292)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_CpuSysinfoPerformanceMonitor', 5038, 292)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SerialPort', 5039, 116)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SerialPortConfiguration', 5040, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SerialPortSetting', 5041, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Environment', 5042, 58)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Printer', 5043, 111)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_PrintJob', 5044, 293)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_PrintService', 5045, 294)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_PrintQueue', 5046, 295)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_QueueForPrintService', 5047, 296)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_OwningPrintQueue', 5048, 297)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_PrinterServicingQueue', 5049, 292)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_PrintSAP', 5050, 299)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_EthernetAdapter', 5051, 292)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Keyboard', 5052, 94)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SoundDevice', 5053, 69)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_MessageLog', 5054, 301)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_MessageLogRecord', 5055, 302)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LogEntry', 5056, 5055)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SyslogRecord', 5057, 5055)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_RecordInLog', 5058, 303)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LogInDataFile', 5059, 304)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LogServiceProperties', 5060, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LogServiceSetting', 5061, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_MessageLogSetting', 5062, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_AdminDomain', 5063, 305)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SystemAdminDomain', 5064, 5063)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_NisAdminDomain', 5065, 5064)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_NisplusAdminDomain', 5066, 5064)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LdapAdminDomain', 5067, 5064)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_DnsAdminDomain', 5068, 5064)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_IPProtocolEndPoint', 5069, 301)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_IPSubnet', 5070, 311)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ActiveUser', 5071, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ActiveProject', 5072, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ProcessStatisticalInformation', 5073, 291)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_UserProcessAggregateStatisticalInformation', 5074)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_ProjectProcessAggregateStatisticalInformation', 5075)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ProcessStatistics', 5076, 312)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_UserProcessStatistics', 5077, 312)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ProjectProcessStatistics', 5078, 312)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ActiveUserProcessAggregateStatistics', 5079, 312)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ActiveProjectProcessAggregateStatistics', 5080, 312)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SNMPSystem', 5081)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SNMPSystemConf', 5082)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_SNMPGroupConf', 5083)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Process', 5084, 48)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_OperatingSystem', 5085, 124)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_InstalledOS', 5086, 131)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_RunningOS', 5087, 216)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_OSProcess', 5088, 186)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_DataFile', 5089, 118)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ScheduledJob', 5090, 123)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ScheduleJob_Cron', 5091, 5090)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_JobScheduler', 5092, 219)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_JobScheduler_Cron', 5093, 5092)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_OwningJobScheduler', 5094, 214)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_HostedJobScheduler', 5095, 211)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SystemDevice', 5096, 130)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_CpuUtilizationInformation', 5097, 313)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_CpuVminfo', 5098, 313)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_CpuSysinfo', 5099, 313)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_DiskIOInformation', 5100, 313)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_DisklessClient', 5101)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_OsService', 5102)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Eeprom', 5103, 122)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_EepromSetting', 5104, 3)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_EepromElementSetting', 5105, 5)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_TimeZone', 5106, 314)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_SystemTimeZone', 5107, 5029)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_AuthorizationAttribute', 5108)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_ExecutionProfile', 5109)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_UserGroup', 5110)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_ProfileAttribute', 5111)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_EmailAlias', 5112)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_UserHomeDirectory', 5113, 120)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_UserAccount', 5114, 315)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_UserTemplate', 5115)
	INSERT INTO HardwareClass (Class, HardwareClassID)
		VALUES ('Solaris_MailBox', 5116)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_ShellSAP', 5117, 67)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMStateDatabase', 5118, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMSoftPartition', 5119, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMExtent', 5120, 96)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMStripe', 5121, 236)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMConcat', 5122, 236)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMMirror', 5123, 236)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMRaid5', 5124, 236)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMTrans', 5125, 47)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMHotSparePool', 5126, 235)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMDiskSet', 5127, 316)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMStorageVolume', 5128, 283)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMConcatComponent', 5129, 317)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_DiskDrive', 5130, 82)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMDriveInDiskSet', 5131, 318)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMExtentBasedOn', 5132, 104)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMSoftPartComponent', 5133, 104)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMExtentInDiskSet', 5134, 319)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMHostInDiskSet', 5135, 309)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMHotSpareInUse', 5136, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMHotSpares', 5137, 17)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMMirrorSubmirrors', 5138, 188)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMRaid5Component', 5139, 317)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMStatistics', 5140, 7)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMStripeComponent', 5141, 317)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMTransLog', 5142, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMTransMaster', 5143, 2)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMUsesHotSparePool', 5144, 188)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_VMVolumeBasedOn', 5145, 104)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_DiskPartition', 5146, 106)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_MediaPresent', 5147, 105)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_DiskIOPerformanceMonitor', 5148, 292)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LocalFileSystem', 5149, 218)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_UFS', 5150, 5149)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_HSFS', 5151, 5149)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_NFSExport', 5152, 39)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Directory', 5153, 120)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_Mount', 5154, 239)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_NFSMount', 5155, 5154)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_UFSMount', 5156, 5154)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_HSFSMount', 5157, 5154)
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('Solaris_LocalFSResidesOnExtent', 5158, 215)
	-- IOKit ClassIDs start from 6000.
	INSERT INTO HardwareClass (Class, HardwareClassID, SuperClassID)
		VALUES ('IOKit_VideoController', 6000, 86)
	SET IDENTITY_INSERT HardwareClass OFF
END
GO

IF NOT EXISTS(SELECT * FROM DistributionServerType)
BEGIN
	INSERT INTO DistributionServerType (DistributionServerTypeID, DistributionServerTypeName)
		VALUES (10, 'DistributionGroup')
	INSERT INTO DistributionServerType (DistributionServerTypeID, DistributionServerTypeName)
		VALUES (11, 'DistributionLocation')
	INSERT INTO DistributionServerType (DistributionServerTypeID, DistributionServerTypeName)
		VALUES (12, 'DistributionServer')
END
GO

IF NOT EXISTS(SELECT * FROM LicenseModel)
BEGIN
	SET IDENTITY_INSERT LicenseModel ON
	INSERT INTO LicenseModel (ModelID, Name)
		VALUES (1, '')
	SET IDENTITY_INSERT LicenseModel OFF
END
GO

IF NOT EXISTS(SELECT * FROM SoftwarePublisher)
BEGIN
	SET IDENTITY_INSERT SoftwarePublisher ON
	INSERT INTO SoftwarePublisher (Name, PublisherID)
		VALUES ('', 1)
	SET IDENTITY_INSERT SoftwarePublisher OFF
END
GO

IF NOT EXISTS(SELECT * FROM SoftwareReseller)
BEGIN
	SET IDENTITY_INSERT SoftwareReseller ON
	INSERT INTO SoftwareReseller (Name, ResellerID)
		VALUES ('', 1)
	SET IDENTITY_INSERT SoftwareReseller OFF
END
GO

IF NOT EXISTS(SELECT * FROM DeviceRole)
BEGIN
	SET IDENTITY_INSERT DeviceRole ON
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Computer', 0)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Workstation', 1)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Server', 2)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Printer', 3)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Switch', 4)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Router', 5)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Hub', 6)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('NetworkDevice', 7)
	INSERT INTO DeviceRole (Description, DeviceRoleID)
		VALUES ('Vendor', 8)
	SET IDENTITY_INSERT DeviceRole OFF
END
GO

IF NOT EXISTS(SELECT * FROM ASN1Object)
BEGIN
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('IBM Network Printer 12', '1.3.6.1.4.1.2.3.5', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('IBM Network Printer 24', '1.3.6.1.4.1.2.3.6', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('IBM InfoPrint (v1) 21', '1.3.6.1.4.1.2.3.53', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('IBM NPS 530/532Print serverV5.36', '1.3.6.1.4.1.2.6.108.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.1', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.2', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.3', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.4', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.5', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.6', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.7', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.8', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.9', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.10', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.11', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.12', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.13', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.14', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.15', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.16', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.17', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.18', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.19', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.20', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.21', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.22', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.23', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.24', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.25', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.26', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.27', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.28', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.29', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.30', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.31', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.32', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.33', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.34', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.35', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.36', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.37', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.40', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.41', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.42', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.43', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.44', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.45', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.46', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.48', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.49', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.50', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.51', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.52', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.53', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.55', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.56', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.57', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.58', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.59', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.60', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.61', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.62', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.63', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.65', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.66', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.67', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.68', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.69', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.70', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.71', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.72', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.73', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.74', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.75', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.88', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.89', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.92', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.93', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.98', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.99', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.102', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.103', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.104', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.105', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.106', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.108', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.109', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.110', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.113', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.114', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.115', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.116', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.117', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.118', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.119', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.120', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.122', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.123', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.124', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.125', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.126', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.127', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.128', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.129', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.130', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.131', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.136', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.140', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.142', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.143', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.144', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.145', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Router', '1.3.6.1.4.1.9.1.146', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.2', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.1', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.3', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.4', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.5', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.6', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.7', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.8', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.9.5.9', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Sun Microsystems SunOS', '1.3.6.1.4.1.42.2.1.1', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP HP9000/HP-UX:1', '1.3.6.1.4.1.11.2.3.2.2', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP HP9000/HP-UX:2', '1.3.6.1.4.1.11.2.3.2.3', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP HP9000/HP-UX:3', '1.3.6.1.4.1.11.2.3.2.5', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP Hub', '1.3.6.1.4.1.11.2.3.7.5.21', 6)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP J2418A Hub', '1.3.6.1.4.1.11.2.3.7.1.3', 6)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP Switch 16', '1.3.6.1.4.1.11.2.3.7.1.8', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP Switch 200', '1.3.6.1.4.1.11.2.3.7.1.10', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP 28688B Ethertwist Hub Plusb', '1.3.6.1.4.1.11.2.3.7.5.1', 6)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP 28699A Ethertwist Hub Plus 48', '1.3.6.1.4.1.11.2.3.7.5.4', 6)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP Router', '1.3.6.1.4.1.11.2.3.7.2', 5)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP Jet-Direct Print Server', '1.3.6.1.4.1.11.2.3.9.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP Jet-Direct Network Plotter', '1.3.6.1.4.1.11.2.3.9.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP JeTDirect', '1.3.6.1.4.1.11.2.3.9.5', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP Jet-Direct Print Server', '1.3.6.1.4.1.11.2.3.9.6', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Digital(TM) Printer Ethernet(TM)', '1.3.6.1.4.1.24', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Compaq Laser Printer LN16', '1.3.6.1.4.1.36.2.15.17.14.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Compaq Laser Printer LN32', '1.3.6.1.4.1.36.2.15.17.15.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('3COM Switch', '1.3.6.1.4.1.43.1.16', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('NCD HMXpro24 hmx_x V5.0.202', '1.3.6.1.4.1.82.1.12', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('NCD EXPLORApro xpl V5.0.202', '1.3.6.1.4.1.82.1.13', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Allen-Bradley PLC5', '1.3.6.1.4.1.95.1.3', 7)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Allen-Bradley 1756-ENET', '1.3.6.1.4.1.95.1.5', 7)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Allen-Bradley 1747-L551 SLC-5/05', '1.3.6.1.4.1.95.1.8', 7)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Allen-Bradley 1747-L552 SLC-5/05', '1.3.6.1.4.1.95.1.9', 7)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Allen-Bradley 1756-ENET', '1.3.6.1.4.1.95.1.12', 7)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Emulex NetQue Version 4.0', '1.3.6.1.4.1.108.1.1.7', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.119.1.14.2', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Tektronix, Inc., Phaser 740', '1.3.6.1.4.1.128.2.1.4.1.3.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Tektronix, Inc., Phaser 750P', '1.3.6.1.4.1.128.2.1.4.1.5.3', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Tektronix, Inc., Phaser 360', '1.3.6.1.4.1.128.2.1.4.2.3.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Tektronix, Inc., Phaser 840N', '1.3.6.1.4.1.128.2.1.4.2.7.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Tektronix, Inc., Phaser 840DP', '1.3.6.1.4.1.128.2.1.4.2.7.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Tektronix, Inc., Phaser 850N', '1.3.6.1.4.1.128.2.1.4.2.8.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('D-Link DP-101 Print Server', '1.3.6.1.4.1.171.10.10.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('ADC Kentrox', '1.3.6.1.4.1.181.1.25', 7)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch eps2015', '1.3.6.1.4.1.197.2.2', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch CPW2115r', '1.3.6.1.4.1.197.2.3', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch CPW16', '1.3.6.1.4.1.197.2.4', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch c3000r', '1.3.6.1.4.1.197.2.5', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch cpw500', '1.3.6.1.4.1.197.2.10', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('GDC ATM Switch', '1.3.6.1.4.1.251.3.3.0', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('GDC ATM Switch', '1.3.6.1.4.1.251.3.3.6', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('GDC ATM Switch', '1.3.6.1.4.1.251.3.3.8', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('GDC ATM Switch', '1.3.6.1.4.1.251.3.6.0', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('GDC ATM Switch', '1.3.6.1.4.1.251.3.6.6', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 230ST', '1.3.6.1.4.1.253.8.62.1.1.3.1.3', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 220/230', '1.3.6.1.4.1.253.8.62.1.1.6.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 255ST', '1.3.6.1.4.1.253.8.62.1.1.7.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 255ST', '1.3.6.1.4.1.253.8.62.1.1.7.1.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 220ST', '1.3.6.1.4.1.253.8.62.1.1.8.1.3', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 420ST', '1.3.6.1.4.1.253.8.62.1.1.19.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 432ST,v2.0', '1.3.6.1.4.1.253.8.62.1.1.20.1.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 440ST', '1.3.6.1.4.1.253.8.62.1.1.21.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre 440ST', '1.3.6.1.4.1.253.8.62.1.1.21.1.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox DocuPrint N24/N32/N40 Network Laser Printer', '1.3.6.1.4.1.253.8.62.1.3.2.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Xerox Document Centre ColorSeries 50, v135', '1.3.6.1.4.1.253.8.62.1.4.2.1.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Microsoft Windows NT', '1.3.6.1.4.1.311.1.1.3.1', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Microsoft Windows NT Workstation', '1.3.6.1.4.1.311.1.1.3.1.1', 1)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Microsoft Windows NT Server', '1.3.6.1.4.1.311.1.1.3.1.2', 2)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Microsoft Windows NT Domain Controller', '1.3.6.1.4.1.311.1.1.3.1.3', 2)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Microsoft Windows 98/Me', '1.3.6.1.4.1.311.1.1.3.2', 1)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Microsoft Windows CE', '1.3.6.1.4.1.311.1.1.3.3', 1)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('NetportExpress 10/100 3-port Print Server', '1.3.6.1.4.1.343.2.4.12', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('AXIS 560 Network Print Server V6.00', '1.3.6.1.4.1.368.1.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('AXIS 560 Network Print Server V5.30', '1.3.6.1.4.1.368.1.1.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('AXIS 560 Network Print Server V6.00', '1.3.6.1.4.1.368.1.1.1.1.0', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('AXIS StorPoint CDCD-ROM Server HP CD-ROM Server compatible', '1.3.6.1.4.1.368.1.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('AXIS 640/642 Network Print Server', '1.3.6.1.4.1.368.9.1.9.1.0', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Cisco Switch', '1.3.6.1.4.1.494.8', 4)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Lexmark Optra SC 1275 / Optra S1255 / MarkNet Pro 3', '1.3.6.1.4.1.641.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('ExtendNet DX', '1.3.6.1.4.1.683.6', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Winterm 3000 Series', '1.3.6.1.4.1.714.1.2.3', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('3 port Print Server', '1.3.6.1.4.1.722.2.6.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('32 PPM laser printer, version 1.0', '1.3.6.1.4.1.1205.1.2.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('CANON Network Multi-PDL Printer Board-D1', '1.3.6.1.4.1.1602.4.7', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('X EPSON Ethernet Ver. 4.20 6.04,9C21CE-92', '1.3.6.1.4.1.1750.1.1', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('IBM Infoprint Color 8', '1.3.6.1.4.1.2001.1.2.683.6', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP-UX 9', '1.3.6.1.4.1.2021.250.1', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('SunOs 4', '1.3.6.1.4.1.2021.250.2', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Sun Solaris', '1.3.6.1.4.1.2021.250.3', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('DEC OSF', '1.3.6.1.4.1.2021.250.4', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('HP-UX 10', '1.3.6.1.4.1.2021.250.6', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Linux', '1.3.6.1.4.1.2021.250.10', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('Open BSD', '1.3.6.1.4.1.2021.250.12', 0)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('EFI Fiery Color Printer Server', '1.3.6.1.4.1.2136.2.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('EFI Fiery X12', '1.3.6.1.4.1.2136.2.11.2', 3)
	INSERT INTO ASN1Object (Description, OID, ObjectRole)
		VALUES ('EPSON Type B 10Base-T/100Base-TX Print Server', '1.3.6.1.4.1.1248.1.1.2.2.3.8.80.82.73.70.78.87.50.83', 3)
END
GO

IF NOT EXISTS(SELECT * FROM AdministrationAccount)
BEGIN
	SET IDENTITY_INSERT AdministrationAccount ON
	INSERT INTO AdministrationAccount (AccountID, AccountName)
		VALUES (1, 'MGSAccountNone')
	SET IDENTITY_INSERT AdministrationAccount OFF
END
GO

IF NOT EXISTS(SELECT * FROM TaskType)
BEGIN
	SET IDENTITY_INSERT TaskType ON
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (1, 'Unknown')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (2, 'CommandExecutionClient')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (3, 'CommandExecutionServer')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (4, 'Inventory')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (5, 'Adoption')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (6, 'PolicyUpdate')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (7, 'Discovery')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (8, 'Cancel')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (9, 'PackageDistribution')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (10, 'SecurityUpdate')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (11, 'WakeOnLAN')
	INSERT INTO TaskType (TaskTypeID, TaskTypeName)
		VALUES (12, 'Distribute')
	SET IDENTITY_INSERT TaskType OFF
END
GO

IF NOT EXISTS(SELECT * FROM ActionState)
BEGIN
	SET IDENTITY_INSERT ActionState ON
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (1, 'Created')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (2, 'DistributionInProgress')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (3, 'DistributionFailed')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (4, 'Distributed')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (5, 'SchedulePending')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (6, 'ScheduledFailed')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (7, 'Scheduled')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (8, 'Applied')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (9, 'ApplyFailed')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (10, 'CancelPending')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (11, 'CancelFailed')
	INSERT INTO ActionState (ActionStateID, ActionStateName)
		VALUES (12, 'Cancelled')
	SET IDENTITY_INSERT ActionState OFF
END
GO

IF NOT EXISTS(SELECT * FROM EventClass)
BEGIN
	SET IDENTITY_INSERT EventClass ON
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (1, 'Insert')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (2, 'Remove')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (3, 'Override')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (4, 'PolicyApplied')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (5, 'PolicyFailed')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (6, 'ConsoleConnect')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (7, 'ConsoleDisconnect')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (8, 'RemoteConnect')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (9, 'RemoteDisconnect')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (10, 'LogOn')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (11, 'LogOff')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (12, 'SessionLocked')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (13, 'SessionUnlocked')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (14, 'CurrentConsoleSession')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (15, 'CurrentNonConsoleSession')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (16, 'ServiceStart')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (17, 'ServiceStop')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (18, 'ElevationRequest')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (19, 'DeelevationRequest')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (20, 'SettingOverride')
	INSERT INTO EventClass (EventClassID, Name)
		VALUES (21, 'SettingRevert')
	SET IDENTITY_INSERT EventClass OFF
END
GO

IF NOT EXISTS(SELECT * FROM PartitionType)
BEGIN
	SET IDENTITY_INSERT PartitionType ON
	INSERT INTO PartitionType (PartitionTypeID, PartitionTypeName)
		VALUES (1, 'Organization')
	INSERT INTO PartitionType (PartitionTypeID, PartitionTypeName)
		VALUES (2, 'PackagePath')
	INSERT INTO PartitionType (PartitionTypeID, PartitionTypeName)
		VALUES (3, 'DistributionTarget')
	SET IDENTITY_INSERT PartitionType OFF
END
GO

IF NOT EXISTS(SELECT * FROM Resource)
BEGIN
	SET IDENTITY_INSERT Resource ON
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (1, 'ConfigureAS')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (2, 'Activities')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (3, 'Allocations')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (3, 4, 'Distribution')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (5, 'Domains')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 6, 'Hardware')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (3, 7, 'Hierarchy')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (3, 8, 'Bandwidth')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (2, 9, 'Library')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (2, 10, 'Packages')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (2, 11, 'Approvals')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 12, 'Licenses')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (13, 'ConfigureMDs')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (14, 'NetworkDevices')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (15, 'Policies')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 16, 'Purchases')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 17, 'RemoteControl')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 18, 'RemoteExecution')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (19, 'AssignSchedules')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (20, 'Schedules')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (21, 'Security')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 22, 'Installations')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 23, 'SoftwareUsage')
	INSERT INTO Resource (PartitionTypeID, ResourceID, ResourceName)
		VALUES (1, 24, 'WakeOnLAN')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (25, 'Customizations')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (26, 'SpmAnalysis')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (27, 'SpmAllocations')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (28, 'CMLicenses')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (29, 'CMApplications')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (30, 'CMAssets')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (31, 'CMInventory')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (32, 'CMPurchaseOrders')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (33, 'CMContracts')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (34, 'CMVendors')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (35, 'CMReporting')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (36, 'EntLocations')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (37, 'EntCorpStruct')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (38, 'EntCostCenters')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (39, 'EntCategories')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (40, 'EntRoles')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (41, 'CMEnterprise')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (42, 'CMImport')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (43, 'CMCompliance')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (44, 'CMLicenseUpgrade')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (45, 'EntUsers')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (46, 'EntComputers')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (47, 'CMCurrency')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (48, 'EcmConfiguration')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (49, 'EcmEnableUsage')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (100, 'SSAdmin')
	INSERT INTO Resource (ResourceID, ResourceName)
		VALUES (101, 'SSManager')
	SET IDENTITY_INSERT Resource OFF
END
GO

IF NOT EXISTS(SELECT * FROM ActionClass)
BEGIN
	SET IDENTITY_INSERT ActionClass ON
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (1, 'read')
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (2, 'modify')
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (3, 'create')
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (4, 'delete')
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (5, 'list')
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (6, 'execute')
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (7, 'transfer')
	INSERT INTO ActionClass (ActionClassID, ActionClassName)
		VALUES (8, 'configure')
	SET IDENTITY_INSERT ActionClass OFF
END
GO

-- Every NetworkLocation belongs to a domain.
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_NetworkLocation_Domain'
)
BEGIN
	PRINT 'Create FK_NetworkLocation_Domain constraint'

	ALTER TABLE NetworkLocation
		ADD CONSTRAINT FK_NetworkLocation_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_NetworkLocation_Domain constraint'
GO
-- Every Organization belongs to a Domain.
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Organization_Domain'
)
BEGIN
	PRINT 'Create FK_Organization_Domain constraint'

	ALTER TABLE Organization
		ADD CONSTRAINT FK_Organization_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Organization_Domain constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersionInState_PackageVersion'
)
BEGIN
	PRINT 'Create FK_PackageVersionInState_PackageVersion constraint'

	ALTER TABLE PackageVersionInState
		ADD CONSTRAINT FK_PackageVersionInState_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageVersionInState_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareUsagePerWeek_SoftwareFileUsage'
)
BEGIN
	PRINT 'Create FK_SoftwareUsagePerWeek_SoftwareFileUsage constraint'

	ALTER TABLE SoftwareUsagePerWeek
		ADD CONSTRAINT FK_SoftwareUsagePerWeek_SoftwareFileUsage
		FOREIGN KEY(SoftwareFileUsageID)
		REFERENCES SoftwareFileUsage(SoftwareFileUsageID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareUsagePerWeek_SoftwareFileUsage constraint'
GO
/*
 * $Id: schema795.sql 100106 2011-06-16 00:30:20Z rlowery $
 *
 * SQL Functions, mostly for massaging various hardware property values
 *
 * The Hardware data on Windows boxes is mostly from WMI.
 * WMI is the Windows implementation of the DMTF's Common Information Model,
 * so the functions names start with 'CIM'. See www.dmtf.org for more info.
 */

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'LikeNullable' AND xtype = 'FN'
)
	DROP FUNCTION LikeNullable
GO
-- A null value matches a pattern which allows an empty string.
-- Otherwise a null value only matches a null pattern.
CREATE FUNCTION LikeNullable(
	@Value		nvarchar(1024),
	@Pattern	nvarchar(1024)
)
RETURNS bit
AS BEGIN
	RETURN	CASE
		WHEN	ISNULL(@Value, '') LIKE @Pattern
		  OR	(@Value IS NULL AND @Pattern IS NULL)
		THEN 1
		ELSE 0
		END
END
GO

/*
 * The semantics of range matching is a bit odd, but is customized
 * to suit the reporting, including drill-downs:
 * Min	Max	Meaning
 * NULL	NULL	Show NULLs only
 * NULL !NULL	Show ALL rows
 * 0	M	At most M
 * N	M	In range N..M inclusive
 * N	NULL	At least N
 */
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'RangeMatchInt' AND xtype = 'FN'
)
	DROP FUNCTION RangeMatchInt
GO
CREATE FUNCTION RangeMatchInt(
	@Value	int,
	@Min	int,
	@Max	int
)
RETURNS bit
AS BEGIN
	RETURN	CASE
		WHEN	((@Min IS NULL AND (@Value IS NULL OR @Max IS NOT NULL))
			 OR @Min <= @Value)
		 AND	(@Min IS NULL
			 OR @Max IS NULL
			 OR @Max >= @Value)
		THEN 1
		ELSE 0
		END
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'RangeMatchBigint' AND xtype = 'FN'
)
	DROP FUNCTION RangeMatchBigint
GO
CREATE FUNCTION RangeMatchBigint(
	@Value	bigint,
	@Min	bigint,
	@Max	bigint
)
RETURNS bit
AS BEGIN
	RETURN	CASE
		WHEN	((@Min IS NULL AND (@Value IS NULL OR @Max IS NOT NULL))
			 OR (@Min = 0 AND @Max IS NULL)
			 OR @Min <= @Value)
		 AND	(@Min IS NULL
			 OR @Max IS NULL
			 OR @Max >= @Value)
		THEN 1
		ELSE 0
		END
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'RangeMatchDate' AND xtype = 'FN'
)
	DROP FUNCTION RangeMatchDate
GO
CREATE FUNCTION RangeMatchDate(
	@Value	datetime,
	@Min	datetime,
	@Max	datetime
)
RETURNS bit
AS BEGIN
	RETURN	CASE
		WHEN	((@Min IS NULL AND (@Value IS NULL OR @Max IS NOT NULL))
			 OR (@Min = 0 AND @Max IS NULL)
			 OR @Min <= @Value)
		 AND	(@Min IS NULL
			 OR @Max IS NULL
			 OR @Max >= @Value)
		THEN 1
		ELSE 0
		END
END
GO

-- This function simplifies the row horizontalization for the Hardware views:
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMProp' AND xtype = 'FN'
)
	DROP FUNCTION CIMProp
GO
CREATE FUNCTION CIMProp(@PropID int, @PropertyID int, @Value nvarchar(1000))
RETURNS nvarchar(1000)
AS
BEGIN
	RETURN	CASE WHEN @PropertyID = @PropID THEN @Value ELSE '' END
END
GO

-- Translate the CIM "Availability" property from a number to a string (unused)
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMAvailabilityName' AND xtype = 'FN'
)
	DROP FUNCTION CIMAvailabilityName
GO
CREATE FUNCTION CIMAvailabilityName(@Availability nvarchar(20))
RETURNS varchar(40)
AS
BEGIN
	RETURN CASE
		WHEN @Availability IS NULL THEN NULL
		WHEN @Availability = '' THEN NULL
		WHEN @Availability = '1' THEN 'Other'
		WHEN @Availability = '2' THEN 'Unknown'
		WHEN @Availability = '3' THEN 'Running'
		WHEN @Availability = '4' THEN 'Warning'
		WHEN @Availability = '5' THEN 'In Test'
		WHEN @Availability = '6' THEN 'Not Applicable'
		WHEN @Availability = '7' THEN 'Power Off'
		WHEN @Availability = '8' THEN 'Off Line'
		WHEN @Availability = '9' THEN 'Off Duty'
		WHEN @Availability = '10' THEN 'Degraded'
		WHEN @Availability = '11' THEN 'Not Installed'
		WHEN @Availability = '12' THEN 'Install Error'
		WHEN @Availability = '13' THEN 'Power Save - Unknown'
		WHEN @Availability = '14' THEN 'Power Save - Low Power Mode'
		WHEN @Availability = '15' THEN 'Power Save - Standby'
		WHEN @Availability = '16' THEN 'Power Cycle'
		WHEN @Availability = '17' THEN 'Power Save - Warning'
		ELSE @Availability
		END
END
GO

-- Translate the CIM "StatusInfo" property from a number to a string (unused)
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMStatusInfo' AND xtype = 'FN'
)
	DROP FUNCTION CIMStatusInfo
GO
CREATE FUNCTION CIMStatusInfo(@StatusInfo nvarchar(80))
RETURNS int
AS
BEGIN
	RETURN CASE	-- Handle null, empty and numeric first:
		WHEN @StatusInfo IS NULL THEN NULL
		WHEN @StatusInfo = '' THEN NULL
		WHEN @StatusInfo NOT LIKE '%[^0-9 ]%' THEN CONVERT(int, @StatusInfo)
		WHEN @StatusInfo = 'Other' THEN 1
		WHEN @StatusInfo = 'Unknown' THEN 2
		WHEN @StatusInfo = 'Enabled' THEN 3
		WHEN @StatusInfo = 'Disabled' THEN 4
		WHEN @StatusInfo = 'Not Applicable' THEN 5
		ELSE  NULL
		END
END
GO

-- Translate a CIM int16, uint16, int32 or uint32 to Integer
-- This could overflow with uint32 values; use with care or use bigint!
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMInteger' AND xtype = 'FN'
)
	DROP FUNCTION CIMInteger
GO
CREATE FUNCTION CIMInteger(@String nvarchar(80))
RETURNS int
AS
BEGIN
	RETURN CASE
		WHEN @String IS NULL THEN NULL
		WHEN @String = '' THEN NULL
		WHEN @String NOT LIKE '%[^ 0-9]%' THEN CONVERT(int, @String)
		ELSE  NULL
		END
END
GO

-- Translate a CIM int64 or uint64 to bigint
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMBigInteger' AND xtype = 'FN'
)
	DROP FUNCTION CIMBigInteger
GO
CREATE FUNCTION CIMBigInteger(@String nvarchar(80))
RETURNS bigint
AS
BEGIN
	RETURN CASE
		WHEN @String IS NULL THEN NULL
		WHEN @String = '' THEN NULL
		WHEN @String NOT LIKE '%[^-+ 0-9]%' THEN CONVERT(bigint, @String)
		ELSE  NULL
		END
END
GO

-- Translate a CIM real to real
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMReal' AND xtype = 'FN'
)
	DROP FUNCTION CIMReal
GO
CREATE FUNCTION CIMReal(@String nvarchar(80))
RETURNS real
AS
BEGIN
	RETURN CASE
		WHEN @String IS NULL THEN NULL
		WHEN @String = '' THEN NULL
		WHEN @String NOT LIKE '%[^-+. 0-9]%' THEN CONVERT(real, @String)
		ELSE  NULL
		END
END
GO

-- Translate a CIMString to string. Since the horizontalization loses NULLs,
-- we treat an empty string as NULL - check that this is what you want!
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMString' AND xtype = 'FN'
)
	DROP FUNCTION CIMString
GO
CREATE FUNCTION CIMString(@String nvarchar(1000))
RETURNS nvarchar(1000)
AS
BEGIN
	RETURN CASE
		WHEN @String IS NULL OR @String = '' THEN NULL
		ELSE @String
		END
END
GO

-- Massage a CIM Date string into an SQL datetime (in UTC) as well as we can.
-- The input string looks like this example: '20040105091426.984000-360'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMDate' AND xtype = 'FN'
)
	DROP FUNCTION CIMDate
GO
CREATE FUNCTION CIMDate(@Date nvarchar(30))
RETURNS datetime
AS
BEGIN
	IF @Date IS NULL
	OR LEN(@Date) < 14+1+6+2
	OR SUBSTRING(@Date, 15, 1) <> '.'
		RETURN NULL

	--	yyyy-mm-dd hh:mi:ss
	--	12345678901234567890
	DECLARE @ODBCDate varchar(19)
	SET @ODBCDATE	= SUBSTRING(@Date, 1, 4)
			+ '-'
			+ SUBSTRING(@Date, 5, 2)
			+ '-'
			+ SUBSTRING(@Date, 7, 2)
			+ ' '
			+ CASE WHEN SUBSTRING(@Date, 9, 1) = '*'
			  THEN '00:00:00'
			  ELSE	SUBSTRING(@Date, 9, 2)
			  	+ ':'
				+ SUBSTRING(@Date, 11, 2)
				+ ':'
				+ SUBSTRING(@Date, 13, 2)
			  END
	DECLARE @result	datetime

	-- Don't throw an exception! Check the format carefully.
	IF @ODBCDATE LIKE '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
	BEGIN
		SET @result = CONVERT(datetime, @ODBCDATE, 120)

		DECLARE @ZoneMinutes	int
		IF LEN(@Date) > 21 AND STUFF(@Date, 1, 21, '') NOT LIKE '%[^0-9]%'
		BEGIN
			SET @ZoneMinutes = CAST(STUFF(@Date, 1, 21, '') AS int)
			SET @result = DATEADD(minute, 0-@ZoneMinutes, @result)
		END
	END

	return @result
END
GO

-- Match members of a comma-separated array of strings, e.g. BIOSCharacteristics
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMNumberListContains' AND xtype = 'FN'
)
	DROP FUNCTION CIMNumberListContains
GO
CREATE FUNCTION CIMNumberListContains(@String nvarchar(1000), @Pattern nvarchar(20))
RETURNS BIT
AS
BEGIN
	RETURN	CASE
		WHEN @String IS NULL OR @String = '' THEN NULL
		WHEN ','+@String+',' LIKE '%,'+@Pattern+',%' THEN 1
		ELSE 0
		END
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMProcessor' AND xtype = 'FN'
)
	DROP FUNCTION CIMProcessor
GO
-- Convert a CIM Processor number to a name:
CREATE FUNCTION CIMProcessor(@ProcessorFamily nvarchar(120))
RETURNS nvarchar(120)
AS
BEGIN
	RETURN
	CASE
	WHEN @ProcessorFamily IS NULL THEN NULL
	WHEN @ProcessorFamily NOT LIKE '%[^0-9]'
	THEN CASE CAST(@ProcessorFamily AS int)	-- All numeric
		WHEN 1		THEN 'Other'
		WHEN 2		THEN NULL
		WHEN 3		THEN '8086'
		WHEN 4		THEN '80286'
		WHEN 5		THEN '80386'
		WHEN 6		THEN '80486'
		WHEN 7		THEN '8087'
		WHEN 8		THEN '80287'
		WHEN 9		THEN '80387'
		WHEN 10		THEN '80487'
		WHEN 11		THEN 'Pentium brand'
		WHEN 12		THEN 'Pentium Pro'
		WHEN 13		THEN 'Pentium II'
		WHEN 14		THEN 'Pentium processor with MMX technology'
		WHEN 15		THEN 'Celeron'
		WHEN 16		THEN 'Pentium II Xeon'
		WHEN 17		THEN 'Pentium III'
		WHEN 18		THEN 'M1 Family'
		WHEN 19		THEN 'M2 Family'
		WHEN 24		THEN 'K5 Family'
		WHEN 25		THEN 'K6 Family'
		WHEN 26		THEN 'K6-2'
		WHEN 27		THEN 'K6-3'
		WHEN 28		THEN 'AMD Athlon Processor Family'
		WHEN 29		THEN 'AMD Duron Processor'
		WHEN 30		THEN 'AMD2900 Family'
		WHEN 31		THEN 'K6-2+'
		WHEN 32		THEN 'Power PC Family'
		WHEN 33		THEN 'Power PC 601'
		WHEN 34		THEN 'Power PC 603'
		WHEN 35		THEN 'Power PC 603+'
		WHEN 36		THEN 'Power PC 604'
		WHEN 37		THEN 'Power PC 620'
		WHEN 38		THEN 'Power PC X704'
		WHEN 39		THEN 'Power PC 750'
		WHEN 48		THEN 'Alpha Family'
		WHEN 49		THEN 'Alpha 21064'
		WHEN 50		THEN 'Alpha 21066'
		WHEN 51		THEN 'Alpha 21164'
		WHEN 52		THEN 'Alpha 21164PC'
		WHEN 53		THEN 'Alpha 21164a'
		WHEN 54		THEN 'Alpha 21264'
		WHEN 55		THEN 'Alpha 21364'
		WHEN 64		THEN 'MIPS Family'
		WHEN 65		THEN 'MIPS R4000'
		WHEN 66		THEN 'MIPS R4200'
		WHEN 67		THEN 'MIPS R4400'
		WHEN 68		THEN 'MIPS R4600'
		WHEN 69		THEN 'MIPS R10000'
		WHEN 80		THEN 'SPARC Family'
		WHEN 81		THEN 'SuperSPARC'
		WHEN 82		THEN 'microSPARC II'
		WHEN 83		THEN 'microSPARC IIep'
		WHEN 84		THEN 'UltraSPARC'
		WHEN 85		THEN 'UltraSPARC II'
		WHEN 86		THEN 'UltraSPARC IIi'
		WHEN 87		THEN 'UltraSPARC III'
		WHEN 88		THEN 'UltraSPARC IIIi'
		WHEN 96		THEN '68040'
		WHEN 97		THEN '68xxx Family'
		WHEN 98		THEN '68000'
		WHEN 99		THEN '68010'
		WHEN 100	THEN '68020'
		WHEN 101	THEN '68030'
		WHEN 112	THEN 'Hobbit Family'
		WHEN 120	THEN 'Crusoe TM5000 Family'
		WHEN 121	THEN 'Crusoe TM3000 Family'
		WHEN 128	THEN 'Weitek'
		WHEN 130	THEN 'Itanium Processor'
		WHEN 144	THEN 'PA-RISC Family'
		WHEN 145	THEN 'PA-RISC 8500'
		WHEN 146	THEN 'PA-RISC 8000'
		WHEN 147	THEN 'PA-RISC 7300LC'
		WHEN 148	THEN 'PA-RISC 7200'
		WHEN 149	THEN 'PA-RISC 7100LC'
		WHEN 150	THEN 'PA-RISC 7100'
		WHEN 160	THEN 'V30 Family'
		WHEN 176	THEN 'Pentium III Xeon'
		WHEN 177	THEN 'Pentium III Processor with Intel SpeedStep Technology'
		WHEN 178	THEN 'Pentium 4'
		WHEN 179	THEN 'Intel Xeon'
		WHEN 180	THEN 'AS400 Family'
		WHEN 181	THEN 'Intel Xeon processor MP'
		WHEN 182	THEN 'AMD AthlonXP Family'
		WHEN 183	THEN 'AMD AthlonMP Family'
		WHEN 184	THEN 'Intel Itanium 2'
		WHEN 185	THEN 'AMD Opteron Family'
		WHEN 190	THEN 'K7'
		WHEN 200	THEN 'IBM390 Family'
		WHEN 201	THEN 'G4'
		WHEN 202	THEN 'G5'
		WHEN 250	THEN 'i860'
		WHEN 251	THEN 'i960'
		WHEN 260	THEN 'SH-3'
		WHEN 261	THEN 'SH-4'
		WHEN 280	THEN 'ARM'
		WHEN 281	THEN 'StrongARM'
		WHEN 300	THEN '6x86'
		WHEN 301	THEN 'MediaGX'
		WHEN 302	THEN 'MII'
		WHEN 320	THEN 'WinChip'
		WHEN 350	THEN 'DSP'
		WHEN 500	THEN 'Video Processor'
				ELSE @ProcessorFamily
		END
	ELSE @ProcessorFamily	-- Not all numeric, no change
	END
END
GO

-- Round a CPU clock speed to nearest value
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMCPUSpeed' AND xtype = 'FN'
)
	DROP FUNCTION CIMCPUSpeed
GO
CREATE FUNCTION CIMCPUSpeed(@speed_str nvarchar(100))
RETURNS int
AS
BEGIN
	DECLARE @speed int
	SET @speed = dbo.CIMInteger(@speed_str)
	IF @speed IS NULL
		RETURN NULL

	IF @speed > 970	-- Nearest 100
		return (@speed+50)/100*100

	DECLARE @round50 int
	SET @round50 = (@speed+25)/50*50
		
	DECLARE @round33 int
	SET @round33 = @speed/100*100 + (@speed-@speed/100*100+16)/33*33
	IF (@round33+1)%100 <= 1 OR	-- 99 or 0
	 ABS(@round50-@speed) < ABS(@round33-@speed)
		RETURN @round50
	RETURN @round33
END
GO

-- Massage a CPU manufacturer
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMCPUManufacturer' AND xtype = 'FN'
)
	DROP FUNCTION CIMCPUManufacturer
GO
CREATE FUNCTION CIMCPUManufacturer(@manuf_str nvarchar(200))
RETURNS nvarchar(200)
AS
BEGIN
	DECLARE @m nvarchar(200)
	set @m = replace(@manuf_str, 'Authentic', '')	-- Usually AMD
	set @m = replace(@m, 'Genuine', '')		-- Usually Intel
	set @m = replace(@m, 'TMx86', 'Transmeta')	-- Crusoe processor
	RETURN @m
END
GO

-- Massage various CIM_Processor facts into a vaguely useful ProcessorType
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMProcessorType' AND xtype = 'FN'
)
	DROP FUNCTION CIMProcessorType
GO
CREATE FUNCTION CIMProcessorType(
	@Name		nvarchar(256),
	@Description	nvarchar(256),
	@Manufacturer	nvarchar(256),
	@Version	nvarchar(256)	-- May contain 'Model N Stepping M'
)
RETURNS nvarchar(256)
AS BEGIN
	RETURN	CASE WHEN @Manufacturer IS NOT NULL
			AND @Name NOT LIKE '%'+@Manufacturer+'%'
			THEN @Manufacturer+' '
			ELSE ''
		END +
		CASE WHEN @Name IS NULL THEN @Description
		     WHEN @Description LIKE 'x86%'
			OR @Description IS NULL THEN @Name
		     ELSE @Description
		END
		-- REVISIT: Someday it'd be nice to map Family/Model/Stepping into a name.
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'IPAddressFromString' AND xtype = 'FN'
)
	DROP FUNCTION IPAddressFromString
GO
CREATE FUNCTION IPAddressFromString(
	@IPAddress	nvarchar(128)
)
RETURNS bigint
AS BEGIN
        DECLARE @Dot1 int
        DECLARE @Dot2 int
        DECLARE @Dot3 int
	DECLARE @End  int
	SET @Dot1 = CHARINDEX('.', @IPAddress)
	SET @Dot2 = CHARINDEX('.', @IPAddress, @Dot1+1)
	SET @Dot3 = CHARINDEX('.', @IPAddress, @Dot2+1)
	SET @End = CHARINDEX(',', @IPAddress, @Dot3+1)
	IF @End = 0
		SET @End = LEN(@IPAddress)+1

	-- Check everything's kosher before converting:
	IF @Dot1 IS NULL
	OR @Dot2 IS NULL
	OR @Dot3 IS NULL
	OR @End IS NULL
	OR @Dot1+1 >= @Dot2
	OR @Dot2+1 >= @Dot3
	OR @Dot3+1 >= @End
	OR SUBSTRING(@IPAddress, 1, @Dot1-1) LIKE '%[^0-9]%'
	OR SUBSTRING(@IPAddress, @Dot1+1, @Dot2-@Dot1-1) LIKE '%[^0-9]%'
	OR SUBSTRING(@IPAddress, @Dot2+1, @Dot3-@Dot2-1) LIKE '%[^0-9]%'
	OR SUBSTRING(@IPAddress, @Dot3+1, @End-@Dot3-1) LIKE '%[^0-9]%'
		RETURN NULL

	RETURN	CAST(SUBSTRING(@IPAddress, 1, @Dot1-1) AS BIGINT)*256*256*256
	+	CAST(SUBSTRING(@IPAddress, @Dot1+1, @Dot2-@Dot1-1) AS INT)*256*256
	+	CAST(SUBSTRING(@IPAddress, @Dot2+1, @Dot3-@Dot2-1) AS INT)*256
	+	CAST(SUBSTRING(@IPAddress, @Dot3+1, @End-@Dot3-1) AS INT)
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'IPNetworkFilter' AND xtype = 'FN'
)
	DROP FUNCTION IPNetworkFilter
GO
CREATE FUNCTION IPNetworkFilter(
	@IPAddress	nvarchar(128),
	@IPNetworkSize	int
)
RETURNS nvarchar(128)
AS BEGIN
    
    /**
     * Note: Function expects a full IP address in form: xxx.xxx.xxx.xxx
     * This function will return the following:
     * Network size >= 24: xxx.xxx.xxx.%
     * Network size >= 16 and < 24: xxx.xxx.%
     * Network size >= 8 and < 16: xxx.%
     * Network size >= 0 and < 8: %
     */
    
    DECLARE @Dot1 int
    DECLARE @Dot2 int
    DECLARE @Dot3 int
	DECLARE @End  int
	SET @Dot1 = CHARINDEX('.', @IPAddress)
	SET @Dot2 = CHARINDEX('.', @IPAddress, @Dot1+1)
	SET @Dot3 = CHARINDEX('.', @IPAddress, @Dot2+1)
	SET @End = CHARINDEX(',', @IPAddress, @Dot3+1)
	IF @End = 0
		SET @End = LEN(@IPAddress)+1

	-- Check everything's kosher before converting:	
	IF @Dot1 IS NULL
	OR @Dot2 IS NULL
	OR @Dot3 IS NULL
	OR @End IS NULL
	OR @Dot1+1 >= @Dot2
	OR @Dot2+1 >= @Dot3
	OR @Dot3+1 >= @End
	OR SUBSTRING(@IPAddress, 1, @Dot1-1) LIKE '%[^0-9]%'
	OR SUBSTRING(@IPAddress, @Dot1+1, @Dot2-@Dot1-1) LIKE '%[^0-9]%'
	OR SUBSTRING(@IPAddress, @Dot2+1, @Dot3-@Dot2-1) LIKE '%[^0-9]%'
	OR SUBSTRING(@IPAddress, @Dot3+1, @End-@Dot3-1) LIKE '%[^0-9]%'
		RETURN NULL

	IF (@IPNetworkSize >= 24)
	RETURN SUBSTRING(@IPAddress, 1, @Dot1)
	+	SUBSTRING(@IPAddress, @Dot1+1, @Dot2-@Dot1)
	+	SUBSTRING(@IPAddress, @Dot2+1, @Dot3-@Dot2)
	+	'%'
	
	IF (@IPNetworkSize >= 16 AND @IPNetworkSize < 24)
	RETURN SUBSTRING(@IPAddress, 1, @Dot1)
	+	SUBSTRING(@IPAddress, @Dot1+1, @Dot2-@Dot1)
	+	'%'
	
	IF (@IPNetworkSize >= 8 AND @IPNetworkSize < 16)
	RETURN SUBSTRING(@IPAddress, 1, @Dot1)
	+	'%'	

	IF (@IPNetworkSize < 8)
	RETURN '%'	

	RETURN @IPAddress
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'IPAddressToString' AND xtype = 'FN'
)
	DROP FUNCTION IPAddressToString
GO
CREATE FUNCTION IPAddressToString(
	@IPAddress	bigint
)
RETURNS nvarchar(16)
AS BEGIN
	RETURN REPLACE(
			STR(@IPAddress/(256*256*256), 3)+'.'+
			STR(@IPAddress/(256*256)%256, 3)+'.'+
			STR(@IPAddress/256%256, 3)+'.'+
			STR(@IPAddress%256, 3),
			' ',
			''
		)
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'IPNetworkSize' AND xtype = 'FN'
)
	DROP FUNCTION IPNetworkSize
GO
CREATE FUNCTION IPNetworkSize(
	@IPMask	bigint
)
RETURNS int
AS BEGIN
	DECLARE @bits int
	SET @bits = 32
	IF (@IPMask IS NULL)
		RETURN NULL
	WHILE (@IPMask > 0 AND @IPMask & 1 = 0)
	BEGIN
		SET @bits = @bits-1
		SET @IPMask = @IPMask/2
	END
	RETURN @bits
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMDomainRole' AND xtype = 'FN'
)
	DROP FUNCTION CIMDomainRole
GO
CREATE FUNCTION CIMDomainRole(@DomainRole nvarchar(200))
RETURNS varchar(40)
AS
BEGIN
	RETURN CASE
		WHEN @DomainRole IS NULL THEN NULL
		WHEN @DomainRole = '' THEN NULL
		WHEN @DomainRole = '0' THEN 'Standalone Workstation'
		WHEN @DomainRole = '1' THEN 'Member Workstation'
		WHEN @DomainRole = '2' THEN 'Standalone Server'
		WHEN @DomainRole = '3' THEN 'Member Server'
		WHEN @DomainRole = '4' THEN 'Backup Domain Controller'
		WHEN @DomainRole = '5' THEN 'Primary Domain Controller'
		ELSE @DomainRole
		END
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMMemoryType' AND xtype = 'FN'
)
	DROP FUNCTION CIMMemoryType
GO
CREATE FUNCTION CIMMemoryType(@MemoryType nvarchar(200))
RETURNS varchar(40)
AS
BEGIN
	RETURN CASE
		WHEN @MemoryType IS NULL THEN NULL
		WHEN @MemoryType = '' THEN NULL
		WHEN @MemoryType = '0' THEN NULL
		WHEN @MemoryType = '1' THEN 'Other'
		WHEN @MemoryType = '2' THEN 'DRAM'
		WHEN @MemoryType = '3' THEN 'Synchronous DRAM'
		WHEN @MemoryType = '4' THEN 'Cache DRAM'
		WHEN @MemoryType = '5' THEN 'EDO'
		WHEN @MemoryType = '6' THEN 'EDRAM'
		WHEN @MemoryType = '7' THEN 'VRAM'
		WHEN @MemoryType = '8' THEN 'SRAM'
		WHEN @MemoryType = '9' THEN 'RAM'
		WHEN @MemoryType = '10' THEN 'ROM'
		WHEN @MemoryType = '11' THEN 'Flash'
		WHEN @MemoryType = '12' THEN 'EEPROM'
		WHEN @MemoryType = '13' THEN 'FEPROM'
		WHEN @MemoryType = '14' THEN 'EPROM'
		WHEN @MemoryType = '15' THEN 'CDRAM'
		WHEN @MemoryType = '16' THEN '3DRAM'
		WHEN @MemoryType = '17' THEN 'SDRAM'
		WHEN @MemoryType = '18' THEN 'SGRAM'
		WHEN @MemoryType = '19' THEN 'RDRAM'
		WHEN @MemoryType = '20' THEN 'DDR'
		ELSE @MemoryType
		END
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CIMChassisType' AND xtype = 'FN'
)
	DROP FUNCTION CIMChassisType
GO
CREATE FUNCTION CIMChassisType(@ChassisType nvarchar(200))
RETURNS varchar(40)
AS
BEGIN
	RETURN CASE
		WHEN @ChassisType IS NULL THEN NULL
		WHEN @ChassisType = '' THEN NULL
		WHEN @ChassisType = '0' THEN NULL
		WHEN @ChassisType = '1' THEN 'Other'
		-- MSDN says that type =2 is supposed to be 'Unknown'
		-- Within ManageSoft 'Unknown' depicts the NULL value.
		-- Thus we are masking chassis type 2 to be 'Reported unknown' 
		WHEN @ChassisType = '2' THEN 'Reported Unknown'
		WHEN @ChassisType = '3' THEN 'Desktop'
		WHEN @ChassisType = '4' THEN 'Low Profile Desktop'
		WHEN @ChassisType = '5' THEN 'Pizza Box'
		WHEN @ChassisType = '6' THEN 'Mini Tower'
		WHEN @ChassisType = '7' THEN 'Tower'
		WHEN @ChassisType = '8' THEN 'Portable'
		WHEN @ChassisType = '9' THEN 'Laptop'
		WHEN @ChassisType = '10' THEN 'Notebook'
		WHEN @ChassisType = '11' THEN 'Hand Held'
		WHEN @ChassisType = '12' THEN 'Docking Station'
		WHEN @ChassisType = '13' THEN 'All in One'
		WHEN @ChassisType = '14' THEN 'Sub Notebook'
		WHEN @ChassisType = '15' THEN 'Space-Saving'
		WHEN @ChassisType = '16' THEN 'Lunch Box'
		WHEN @ChassisType = '17' THEN 'Main System Chassis'
		WHEN @ChassisType = '18' THEN 'Expansion Chassis'
		WHEN @ChassisType = '19' THEN 'SubChassis'
		WHEN @ChassisType = '20' THEN 'Bus Expansion Chassis'
		WHEN @ChassisType = '21' THEN 'Peripheral Chassis'
		WHEN @ChassisType = '22' THEN 'Storage Chassis'
		WHEN @ChassisType = '23' THEN 'Rack Mount Chassis'
		WHEN @ChassisType = '24' THEN 'Sealed-Case PC'
		ELSE @ChassisType
		END
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'NetworkDeviceType' AND xtype = 'FN'
)
	DROP FUNCTION NetworkDeviceType
GO
CREATE FUNCTION NetworkDeviceType(@deviceTypeID int)
RETURNS nvarchar(20)
AS
BEGIN
	RETURN CASE @deviceTypeID
		WHEN 0 THEN 'Computer'
		WHEN 1 THEN 'Workstation'
		WHEN 2 THEN 'Server'
		WHEN 3 THEN 'Printer'
		WHEN 4 THEN 'Switch' 
		WHEN 5 THEN 'Router' 
		WHEN 6 THEN 'Hub' 
		ELSE  ''
	END
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'GetStartOfWeekForDate' AND xtype = 'FN'
)
	DROP FUNCTION GetStartOfWeekForDate
GO
CREATE FUNCTION GetStartOfWeekForDate(@date DATETIME)
RETURNS DATETIME
AS
BEGIN
	--Local variables
	DECLARE @l_date AS DATETIME	
	DECLARE @l_numWeeks AS INTEGER
	
	--Set the time to start at 12:00 AM
	SET @l_date = '1/1/1990 12:00:00 AM'

	--Move the till date to the start of the week 
	SET @l_numWeeks = DATEDIFF(Week,@l_date,@date)
	SET @l_date = DATEADD(Week,@l_numWeeks,@l_date)

	RETURN @l_date
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'OUName' AND xtype = 'FN'
)
	DROP FUNCTION OUName
GO
CREATE FUNCTION OUName(@OrganizationID int)
RETURNS nvarchar(501)
AS
BEGIN
	DECLARE @OUName nvarchar(501)
	SELECT @OUName =
		CASE
		WHEN o.RDN IS NULL THEN '<NULL>,'+d.DN
		WHEN o.RDN = '' THEN d.DN
		ELSE o.RDN+','+d.DN
		END
	FROM Organization AS o,
		Domain AS d
	WHERE	@OrganizationID = o.OrganizationID
	  AND	o.DomainID = d.OrganizationID
	RETURN @OUName
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'OUDomainDN' AND xtype = 'FN'
)
	DROP FUNCTION OUDomainDN
GO
CREATE FUNCTION OUDomainDN(@OrganizationID int)
RETURNS nvarchar(100)
AS
BEGIN
	DECLARE @OUDomainDN nvarchar(100)
	SELECT @OUDomainDN = d.DN
	FROM Organization AS o,
		Domain AS d
	WHERE	@OrganizationID = o.OrganizationID
	  AND	o.DomainID = d.OrganizationID
	RETURN @OUDomainDN
END
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'Target' AND xtype = 'IF'
)
	DROP FUNCTION Target
GO
CREATE FUNCTION Target(@MaxType int)
	returns table
AS
	RETURN	(
		SELECT	1 AS TargetTypeID,
			c.ComputerID as TargetID,
			c.ComputerOUID AS TargetOUID
		FROM	Computer AS c
		WHERE @MaxType >= 1
		
		UNION ALL
		
		SELECT	2 AS TargetTypeID,
			u.UserID as TargetID,
			u.UserOUID AS TargetOUID
		FROM	[User] AS u
		WHERE @MaxType >= 2
		
		UNION ALL
		
		SELECT	3 as TargetTypeID,
			g.GroupID as TargetID,
			g.GroupOUID AS TargetOUID
		FROM	[Group] as g
		WHERE @MaxType >= 3
	)
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ColumnCollate' AND xtype = 'FN'
)
	DROP FUNCTION ColumnCollate
GO
CREATE FUNCTION ColumnCollate(@Table sysname, @Column sysname)
RETURNS sysname
AS
BEGIN
	DECLARE @Collation sysname

	SET @Collation = (SELECT	top 1 collation
			FROM	dbo.syscolumns
			WHERE	[id] = object_id(@Table)
			  AND	[name] = @Column)

	IF ISNULL(@Collation, '') = ''
		SET @Collation = 'database_default'

	RETURN 'COLLATE ' + @Collation
END
GO

-- Function to obtain the file path
IF EXISTS(SELECT * FROM sysobjects WHERE xtype = 'FN'
		AND name = 'GetFilePath')
	DROP FUNCTION GetFilePath
GO
CREATE FUNCTION GetFilePath(@Path nvarchar(800))
RETURNS nvarchar(800)
AS
BEGIN
	DECLARE @FilePath	nvarchar(800)
	DECLARE @Index		int
	DECLARE @TempIndex1	int
	DECLARE @TempIndex2	int

	SET @Index = 0
	WHILE (@Index >= 0)
	BEGIN
		SET @TempIndex1 = ISNULL(CHARINDEX('\', @Path, @Index + 1), 0)
		IF @TempIndex1 > 0
			SET @Index = @TempIndex1

		SET @TempIndex2 = ISNULL(CHARINDEX('/', @Path, @Index + 1), 0)
		IF @TempIndex2 > 0
			SET @Index = @TempIndex2

		IF @TempIndex1 = 0 AND @TempIndex2 = 0
		BEGIN
			IF @Index = 0
			BEGIN
				SET @FilePath = ''
			END
			ELSE
			BEGIN
				SET @FilePath = SUBSTRING(@Path, 1, @Index - 1)
			END
			SET @Index = -1
		END
	END

	RETURN @FilePath
END
GO

-- Function to obtain the base file name
IF EXISTS(SELECT * FROM sysobjects WHERE xtype = 'FN'
		AND name = 'GetFileName')
	DROP FUNCTION GetFileName
GO
CREATE FUNCTION GetFileName(@Path nvarchar(800))
RETURNS nvarchar(800)
AS
BEGIN
	DECLARE @FileName	nvarchar(800)
	DECLARE @Index		int
	DECLARE @TempIndex1	int
	DECLARE @TempIndex2	int

	SET @Index = 0
	WHILE (@Index >= 0)
	BEGIN
		SET @TempIndex1 = ISNULL(CHARINDEX('\', @Path, @Index + 1), 0)
		IF @TempIndex1 > 0
			SET @Index = @TempIndex1

		SET @TempIndex2 = ISNULL(CHARINDEX('/', @Path, @Index + 1), 0)
		IF @TempIndex2 > 0
			SET @Index = @TempIndex2

		IF @TempIndex1 = 0 AND @TempIndex2 = 0
		BEGIN
			IF @Index = 0
			BEGIN
				SET @FileName = @Path
			END
			ELSE
			BEGIN
				SET @FileName = SUBSTRING(@Path, @Index + 1, LEN(@Path) - @Index)
			END
			SET @Index = -1
		END
	END

	RETURN @FileName
END
GO
/*
 * $Id: schema795.sql 100106 2011-06-16 00:30:20Z rlowery $
 *
 * Views over Computers and (mostly) their reported hardware inventory.
 *
 * The views whose names start with Win32_ contain records only for Windows
 * operating systems. Others match any operating system, by matching the CIM
 * hardware class and immediate subclasses. Many of these views contain columns
 * which will only have values on Windows operating systems.
 *
 * Columns are the order defined in \Windows\system32\wbem\cimwin32.mof, with
 * superclasses before subclasses (and including Win32_ subclasses).
 *
 * All views contain ComputerID and should be joined on this column.
 *
 *	ComputerBIOS			Each Computer with BIOS info
 *	CDROMDrive			Each CDROM drive in each computer
 *	ComputerSystem			Each Computer + details
 *	DiskDrive			Each DiskDrive in each computer
 *	FloppyDrive			Each Floppy Drive in each computer
 *	LogicalDisk			Each Logical disk (partition etc)
 *	NetworkAdapter			Each Network Adapter
 *	ComputerOperatingSystem		Each Computer with OperatingSystem
 *	ComputerProcessor		Each CPU in each computer
 *	PhysicalMemory			Each memory card in each computer
 *	ComputerSystemEnclosure		Each computer with case/enclosure data
 *	VideoController			Each VideoController in each computer
 *	Win32_NetworkAdapterConfiguration Windows; configuration of network card
 *	Win32_SoundDevice		Windows; Sound card
 *	Win32_PnPEntity			Windows; assorted devices and drivers
 */

PRINT 'Creating view CDROMDrive'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'CDROMDrive' AND xtype = 'V'
)
	DROP VIEW CDROMDrive
GO
CREATE VIEW CDROMDrive AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvDescription.Value)
		AS Description,
	dbo.CIMString(hvName.Value)
		AS Name,
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_LogicalDevice,
	-- CIM_MediaAccessDevice,
	dbo.CIMString(hvCapabilities.Value)
		AS Capabilities,
		-- uint32 NumberOfMediaSupported,
	-- Win32_CDROMDrive,
	dbo.CIMString(hvDrive.Value)
		AS Drive,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMNumberListContains(','+hvCapabilities.Value+',', '4')
		AS Writable

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_CDROMDrive'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpDescription
	ON hpDescription.Property = 'Description'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDescription
	ON ho.HardwareObjectID = hvDescription.HardwareObjectID
	AND hpDescription.HardwarePropertyID = hvDescription.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpName
	ON hpName.Property = 'Name'
	LEFT OUTER LOOP JOIN HardwareValue AS hvName
	ON ho.HardwareObjectID = hvName.HardwareObjectID
	AND hpName.HardwarePropertyID = hvName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_LogicalDevice
	-- CIM_MediaAccessDevice
	LEFT OUTER JOIN HardwareProperty AS hpCapabilities
	ON hpCapabilities.Property = 'Capabilities'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCapabilities
	ON ho.HardwareObjectID = hvCapabilities.HardwareObjectID
	AND hpCapabilities.HardwarePropertyID = hvCapabilities.HardwarePropertyID
	
	-- Win32_CDROMDrive
	LEFT OUTER JOIN HardwareProperty AS hpDrive
	ON hpDrive.Property = 'Drive'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDrive
	ON ho.HardwareObjectID = hvDrive.HardwareObjectID
	AND hpDrive.HardwarePropertyID = hvDrive.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	 
GO

PRINT 'Creating view ComputerBIOS'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ComputerBIOS' AND xtype = 'V'
)
	DROP VIEW ComputerBIOS
GO
CREATE VIEW ComputerBIOS AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_LogicalElement properties,
	-- CIM_SoftwareElement properties,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMString(hvSerialNumber.Value)
		AS SerialNumber,
	-- CIM_BIOS Properties:,
		-- Version,
	-- Win32_BIOS Properties:,
	dbo.CIMDate(hvReleaseDate.Value)
		AS ReleaseDate,
	-- Win32 BiosCharacteristics:,
	dbo.CIMString(hvBiosCharacteristics.Value)
		AS BiosCharacteristics,
	dbo.CIMNumberListContains(','+hvBiosCharacteristics.Value+',', '8')
		AS PCMCIA,
	dbo.CIMNumberListContains(','+hvBiosCharacteristics.Value+',', '15')
		AS BootCD,
	dbo.CIMNumberListContains(','+hvBiosCharacteristics.Value+',', '32')
		AS ACPI,
	dbo.CIMNumberListContains(','+hvBiosCharacteristics.Value+',', '34')
		AS AGP

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_BIOSElement'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_LogicalElement properties
	-- CIM_SoftwareElement properties
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpSerialNumber
	ON hpSerialNumber.Property = 'SerialNumber'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSerialNumber
	ON ho.HardwareObjectID = hvSerialNumber.HardwareObjectID
	AND hpSerialNumber.HardwarePropertyID = hvSerialNumber.HardwarePropertyID
	
	-- CIM_BIOS Properties:
	-- Win32_BIOS Properties:
	LEFT OUTER JOIN HardwareProperty AS hpReleaseDate
	ON hpReleaseDate.Property = 'ReleaseDate'
	LEFT OUTER LOOP JOIN HardwareValue AS hvReleaseDate
	ON ho.HardwareObjectID = hvReleaseDate.HardwareObjectID
	AND hpReleaseDate.HardwarePropertyID = hvReleaseDate.HardwarePropertyID
	
	-- Win32 BiosCharacteristics:
	LEFT OUTER JOIN HardwareProperty AS hpBiosCharacteristics
	ON hpBiosCharacteristics.Property = 'BiosCharacteristics'
	LEFT OUTER LOOP JOIN HardwareValue AS hvBiosCharacteristics
	ON ho.HardwareObjectID = hvBiosCharacteristics.HardwareObjectID
	AND hpBiosCharacteristics.HardwarePropertyID = hvBiosCharacteristics.HardwarePropertyID
	 
GO

PRINT 'Creating view ComputerSystem'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ComputerSystem' AND xtype = 'V'
)
	DROP VIEW ComputerSystem
GO
CREATE VIEW ComputerSystem AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_LogicalElement,
	-- CIM_System : CIM_LogicalElement,
	-- CIM_ComputerSystem : CIM_System,
	-- CIM_UnitaryComputerSystem : CIM_ComputerSystem,
	-- Win32_ComputerSystem,
	dbo.CIMString(hvDomain.Value)
		AS Domain,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMString(hvModel.Value)
		AS Model,
	dbo.CIMDomainRole(hvDomainRole.Value)
		AS DomainRole,
	dbo.CIMInteger(hvAdminPasswordStatus.Value)
		AS AdminPasswordStatus,
	dbo.CIMInteger(hvNumberOfProcessors.Value)
		AS NumberOfProcessors,
	dbo.CIMBigInteger(hvTotalPhysicalMemory.Value)
		AS TotalPhysicalMemory

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_UnitaryComputerSystem'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_LogicalElement
	-- CIM_System : CIM_LogicalElement
	-- CIM_ComputerSystem : CIM_System
	-- CIM_UnitaryComputerSystem : CIM_ComputerSystem
	-- Win32_ComputerSystem
	LEFT OUTER JOIN HardwareProperty AS hpDomain
	ON hpDomain.Property = 'Domain'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDomain
	ON ho.HardwareObjectID = hvDomain.HardwareObjectID
	AND hpDomain.HardwarePropertyID = hvDomain.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpModel
	ON hpModel.Property = 'Model'
	LEFT OUTER LOOP JOIN HardwareValue AS hvModel
	ON ho.HardwareObjectID = hvModel.HardwareObjectID
	AND hpModel.HardwarePropertyID = hvModel.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDomainRole
	ON hpDomainRole.Property = 'DomainRole'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDomainRole
	ON ho.HardwareObjectID = hvDomainRole.HardwareObjectID
	AND hpDomainRole.HardwarePropertyID = hvDomainRole.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpAdminPasswordStatus
	ON hpAdminPasswordStatus.Property = 'AdminPasswordStatus'
	LEFT OUTER LOOP JOIN HardwareValue AS hvAdminPasswordStatus
	ON ho.HardwareObjectID = hvAdminPasswordStatus.HardwareObjectID
	AND hpAdminPasswordStatus.HardwarePropertyID = hvAdminPasswordStatus.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpNumberOfProcessors
	ON hpNumberOfProcessors.Property = 'NumberOfProcessors'
	LEFT OUTER LOOP JOIN HardwareValue AS hvNumberOfProcessors
	ON ho.HardwareObjectID = hvNumberOfProcessors.HardwareObjectID
	AND hpNumberOfProcessors.HardwarePropertyID = hvNumberOfProcessors.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpTotalPhysicalMemory
	ON hpTotalPhysicalMemory.Property = 'TotalPhysicalMemory'
	LEFT OUTER LOOP JOIN HardwareValue AS hvTotalPhysicalMemory
	ON ho.HardwareObjectID = hvTotalPhysicalMemory.HardwareObjectID
	AND hpTotalPhysicalMemory.HardwarePropertyID = hvTotalPhysicalMemory.HardwarePropertyID
	 
GO

PRINT 'Creating view DiskDrive'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'DiskDrive' AND xtype = 'V'
)
	DROP VIEW DiskDrive
GO
CREATE VIEW DiskDrive AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvDescription.Value)
		AS Description,
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_LogicalElement : CIM_ManagedSystemElement,
	-- CIM_LogicalDevice : CIM_LogicalElement,
	-- CIM_MediaAccessDevice : CIM_LogicalDevice,
		-- uint64 MaxMediaSize,
	-- Win32_DiskDrive : CIM_DiskDrive,
	dbo.CIMString(hvInterfaceType.Value)
		AS InterfaceType,
	dbo.CIMInteger(hvPartitions.Value)
		AS Partitions,
	dbo.CIMInteger(hvIndex.Value)
		AS [Index],
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMString(hvModel.Value)
		AS Model,
	dbo.CIMBigInteger(hvSize.Value)
		AS Size

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_DiskDrive'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpDescription
	ON hpDescription.Property = 'Description'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDescription
	ON ho.HardwareObjectID = hvDescription.HardwareObjectID
	AND hpDescription.HardwarePropertyID = hvDescription.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_LogicalElement : CIM_ManagedSystemElement
	-- CIM_LogicalDevice : CIM_LogicalElement
	-- CIM_MediaAccessDevice : CIM_LogicalDevice
	-- Win32_DiskDrive : CIM_DiskDrive
	LEFT OUTER JOIN HardwareProperty AS hpInterfaceType
	ON hpInterfaceType.Property = 'InterfaceType'
	LEFT OUTER LOOP JOIN HardwareValue AS hvInterfaceType
	ON ho.HardwareObjectID = hvInterfaceType.HardwareObjectID
	AND hpInterfaceType.HardwarePropertyID = hvInterfaceType.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpPartitions
	ON hpPartitions.Property = 'Partitions'
	LEFT OUTER LOOP JOIN HardwareValue AS hvPartitions
	ON ho.HardwareObjectID = hvPartitions.HardwareObjectID
	AND hpPartitions.HardwarePropertyID = hvPartitions.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpIndex
	ON hpIndex.Property = 'Index'
	LEFT OUTER LOOP JOIN HardwareValue AS hvIndex
	ON ho.HardwareObjectID = hvIndex.HardwareObjectID
	AND hpIndex.HardwarePropertyID = hvIndex.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpModel
	ON hpModel.Property = 'Model'
	LEFT OUTER LOOP JOIN HardwareValue AS hvModel
	ON ho.HardwareObjectID = hvModel.HardwareObjectID
	AND hpModel.HardwarePropertyID = hvModel.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpSize
	ON hpSize.Property = 'Size'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSize
	ON ho.HardwareObjectID = hvSize.HardwareObjectID
	AND hpSize.HardwarePropertyID = hvSize.HardwarePropertyID
	 
GO

PRINT 'Creating view DisketteDrive'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'DisketteDrive' AND xtype = 'V'
)
	DROP VIEW DisketteDrive
GO
CREATE VIEW DisketteDrive AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvDescription.Value)
		AS Description,
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_LogicalDevice : CIM_LogicalElement,
	-- CIM_MediaAccessDevice : CIM_LogicalDevice,
	-- Win32_FloppyDrive : CIM_DisketteDrive,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_DisketteDrive'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpDescription
	ON hpDescription.Property = 'Description'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDescription
	ON ho.HardwareObjectID = hvDescription.HardwareObjectID
	AND hpDescription.HardwarePropertyID = hvDescription.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_LogicalDevice : CIM_LogicalElement
	-- CIM_MediaAccessDevice : CIM_LogicalDevice
	-- Win32_FloppyDrive : CIM_DisketteDrive
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	 
GO

PRINT 'Creating view LogicalDisk'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'LogicalDisk' AND xtype = 'V'
)
	DROP VIEW LogicalDisk
GO
CREATE VIEW LogicalDisk AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvDescription.Value)
		AS Description,
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_LogicalDevice : CIM_LogicalElement,
	-- CIM_LogicalDisk,
	dbo.CIMBigInteger(hvFreeSpace.Value)
		AS FreeSpace,
	dbo.CIMBigInteger(hvSize.Value)
		AS Size,
	-- Win32_LogicalDisk : CIM_LogicalDisk,
	dbo.CIMInteger(hvDriveType.Value)
		AS DriveType,
	dbo.CIMString(hvFileSystem.Value)
		AS FileSystem,
	dbo.CIMString(hvProviderName.Value)
		AS ProviderName,
	dbo.CIMString(hvVolumeName.Value)
		AS VolumeName,
	dbo.CIMString(hvVolumeSerialNumber.Value)
		AS VolumeSerialNumber,
	dbo.CIMInteger(hvMediaType.Value)
		AS MediaType

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_LogicalDisk'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpDescription
	ON hpDescription.Property = 'Description'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDescription
	ON ho.HardwareObjectID = hvDescription.HardwareObjectID
	AND hpDescription.HardwarePropertyID = hvDescription.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_LogicalDevice : CIM_LogicalElement
	-- CIM_LogicalDisk
	LEFT OUTER JOIN HardwareProperty AS hpFreeSpace
	ON hpFreeSpace.Property = 'FreeSpace'
	LEFT OUTER LOOP JOIN HardwareValue AS hvFreeSpace
	ON ho.HardwareObjectID = hvFreeSpace.HardwareObjectID
	AND hpFreeSpace.HardwarePropertyID = hvFreeSpace.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpSize
	ON hpSize.Property = 'Size'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSize
	ON ho.HardwareObjectID = hvSize.HardwareObjectID
	AND hpSize.HardwarePropertyID = hvSize.HardwarePropertyID
	
	-- Win32_LogicalDisk : CIM_LogicalDisk
	LEFT OUTER JOIN HardwareProperty AS hpDriveType
	ON hpDriveType.Property = 'DriveType'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDriveType
	ON ho.HardwareObjectID = hvDriveType.HardwareObjectID
	AND hpDriveType.HardwarePropertyID = hvDriveType.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpFileSystem
	ON hpFileSystem.Property = 'FileSystem'
	LEFT OUTER LOOP JOIN HardwareValue AS hvFileSystem
	ON ho.HardwareObjectID = hvFileSystem.HardwareObjectID
	AND hpFileSystem.HardwarePropertyID = hvFileSystem.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpProviderName
	ON hpProviderName.Property = 'ProviderName'
	LEFT OUTER LOOP JOIN HardwareValue AS hvProviderName
	ON ho.HardwareObjectID = hvProviderName.HardwareObjectID
	AND hpProviderName.HardwarePropertyID = hvProviderName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpVolumeName
	ON hpVolumeName.Property = 'VolumeName'
	LEFT OUTER LOOP JOIN HardwareValue AS hvVolumeName
	ON ho.HardwareObjectID = hvVolumeName.HardwareObjectID
	AND hpVolumeName.HardwarePropertyID = hvVolumeName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpVolumeSerialNumber
	ON hpVolumeSerialNumber.Property = 'VolumeSerialNumber'
	LEFT OUTER LOOP JOIN HardwareValue AS hvVolumeSerialNumber
	ON ho.HardwareObjectID = hvVolumeSerialNumber.HardwareObjectID
	AND hpVolumeSerialNumber.HardwarePropertyID = hvVolumeSerialNumber.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpMediaType
	ON hpMediaType.Property = 'MediaType'
	LEFT OUTER LOOP JOIN HardwareValue AS hvMediaType
	ON ho.HardwareObjectID = hvMediaType.HardwareObjectID
	AND hpMediaType.HardwarePropertyID = hvMediaType.HardwarePropertyID
	 
GO

PRINT 'Creating view NetworkAdapter'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'NetworkAdapter' AND xtype = 'V'
)
	DROP VIEW NetworkAdapter
GO
CREATE VIEW NetworkAdapter AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
		-- Status,
	-- CIM_LogicalElement,
	-- CIM_LogicalDevice,
	-- CIM_NetworkAdapter,
	dbo.CIMBigInteger(hvSpeed.Value)
		AS Speed,
		-- MaxSpeed,
	-- Win32_NetworkAdapter,
	dbo.CIMString(hvMACAddress.Value)
		AS MACAddress,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMString(hvTimeOfLastReset.Value)
		AS TimeOfLastReset

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_NetworkAdapter'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	-- CIM_LogicalElement
	-- CIM_LogicalDevice
	-- CIM_NetworkAdapter
	LEFT OUTER JOIN HardwareProperty AS hpSpeed
	ON hpSpeed.Property = 'Speed'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSpeed
	ON ho.HardwareObjectID = hvSpeed.HardwareObjectID
	AND hpSpeed.HardwarePropertyID = hvSpeed.HardwarePropertyID
	
	-- Win32_NetworkAdapter
	LEFT OUTER JOIN HardwareProperty AS hpMACAddress
	ON hpMACAddress.Property = 'MACAddress'
	LEFT OUTER LOOP JOIN HardwareValue AS hvMACAddress
	ON ho.HardwareObjectID = hvMACAddress.HardwareObjectID
	AND hpMACAddress.HardwarePropertyID = hvMACAddress.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpTimeOfLastReset
	ON hpTimeOfLastReset.Property = 'TimeOfLastReset'
	LEFT OUTER LOOP JOIN HardwareValue AS hvTimeOfLastReset
	ON ho.HardwareObjectID = hvTimeOfLastReset.HardwareObjectID
	AND hpTimeOfLastReset.HardwarePropertyID = hvTimeOfLastReset.HardwarePropertyID
	 
GO

PRINT 'Creating view Win32_NetworkAdapterConfiguration'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'Win32_NetworkAdapterConfiguration' AND xtype = 'V'
)
	DROP VIEW Win32_NetworkAdapterConfiguration
GO
CREATE VIEW Win32_NetworkAdapterConfiguration AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	dbo.CIMString(hvCaption.Value)
		AS Caption,
	dbo.CIMString(hvDescription.Value)
		AS Description,
	dbo.CIMInteger(hvIndex.Value)
		AS [Index],
	dbo.CIMString(hvMACAddress.Value)
		AS MACAddress,
	dbo.CIMString(hvIPEnabled.Value)
		AS IPEnabled,
	dbo.CIMString(hvDHCPEnabled.Value)
		AS DHCPEnabled,
	dbo.CIMString(hvIPAddress.Value)
		AS IPAddress,
	dbo.CIMString(hvDHCPServer.Value)
		AS DHCPServer,
	dbo.CIMString(hvDNSHostName.Value)
		AS DNSHostName,
	dbo.CIMString(hvDNSDomain.Value)
		AS DNSDomain,
	dbo.CIMString(hvDNSServerSearchOrder.Value)
		AS DNSServerSearchOrder,
	dbo.CIMString(hvDefaultIPGateway.Value)
		AS DefaultIPGateway,
	dbo.CIMString(hvIPSubnet.Value)
		AS IPSubnet

FROM	(SELECT	HardwareClassID,
		Class AS HardwareClass
	 FROM	HardwareClass
	 WHERE	Class = 'Win32_NetworkAdapterConfiguration'
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	LEFT OUTER JOIN HardwareProperty AS hpCaption
	ON hpCaption.Property = 'Caption'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCaption
	ON ho.HardwareObjectID = hvCaption.HardwareObjectID
	AND hpCaption.HardwarePropertyID = hvCaption.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDescription
	ON hpDescription.Property = 'Description'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDescription
	ON ho.HardwareObjectID = hvDescription.HardwareObjectID
	AND hpDescription.HardwarePropertyID = hvDescription.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpIndex
	ON hpIndex.Property = 'Index'
	LEFT OUTER LOOP JOIN HardwareValue AS hvIndex
	ON ho.HardwareObjectID = hvIndex.HardwareObjectID
	AND hpIndex.HardwarePropertyID = hvIndex.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpMACAddress
	ON hpMACAddress.Property = 'MACAddress'
	LEFT OUTER LOOP JOIN HardwareValue AS hvMACAddress
	ON ho.HardwareObjectID = hvMACAddress.HardwareObjectID
	AND hpMACAddress.HardwarePropertyID = hvMACAddress.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpIPEnabled
	ON hpIPEnabled.Property = 'IPEnabled'
	LEFT OUTER LOOP JOIN HardwareValue AS hvIPEnabled
	ON ho.HardwareObjectID = hvIPEnabled.HardwareObjectID
	AND hpIPEnabled.HardwarePropertyID = hvIPEnabled.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDHCPEnabled
	ON hpDHCPEnabled.Property = 'DHCPEnabled'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDHCPEnabled
	ON ho.HardwareObjectID = hvDHCPEnabled.HardwareObjectID
	AND hpDHCPEnabled.HardwarePropertyID = hvDHCPEnabled.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpIPAddress
	ON hpIPAddress.Property = 'IPAddress'
	LEFT OUTER LOOP JOIN HardwareValue AS hvIPAddress
	ON ho.HardwareObjectID = hvIPAddress.HardwareObjectID
	AND hpIPAddress.HardwarePropertyID = hvIPAddress.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDHCPServer
	ON hpDHCPServer.Property = 'DHCPServer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDHCPServer
	ON ho.HardwareObjectID = hvDHCPServer.HardwareObjectID
	AND hpDHCPServer.HardwarePropertyID = hvDHCPServer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDNSHostName
	ON hpDNSHostName.Property = 'DNSHostName'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDNSHostName
	ON ho.HardwareObjectID = hvDNSHostName.HardwareObjectID
	AND hpDNSHostName.HardwarePropertyID = hvDNSHostName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDNSDomain
	ON hpDNSDomain.Property = 'DNSDomain'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDNSDomain
	ON ho.HardwareObjectID = hvDNSDomain.HardwareObjectID
	AND hpDNSDomain.HardwarePropertyID = hvDNSDomain.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDNSServerSearchOrder
	ON hpDNSServerSearchOrder.Property = 'DNSServerSearchOrder'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDNSServerSearchOrder
	ON ho.HardwareObjectID = hvDNSServerSearchOrder.HardwareObjectID
	AND hpDNSServerSearchOrder.HardwarePropertyID = hvDNSServerSearchOrder.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDefaultIPGateway
	ON hpDefaultIPGateway.Property = 'DefaultIPGateway'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDefaultIPGateway
	ON ho.HardwareObjectID = hvDefaultIPGateway.HardwareObjectID
	AND hpDefaultIPGateway.HardwarePropertyID = hvDefaultIPGateway.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpIPSubnet
	ON hpIPSubnet.Property = 'IPSubnet'
	LEFT OUTER LOOP JOIN HardwareValue AS hvIPSubnet
	ON ho.HardwareObjectID = hvIPSubnet.HardwareObjectID
	AND hpIPSubnet.HardwarePropertyID = hvIPSubnet.HardwarePropertyID
	 
GO

PRINT 'Creating view ComputerOperatingSystem'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ComputerOperatingSystem' AND xtype = 'V'
)
	DROP VIEW ComputerOperatingSystem
GO
CREATE VIEW ComputerOperatingSystem AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvCaption.Value)
		AS Caption,
	dbo.CIMDate(hvInstallDate.Value)
		AS InstallDate,
	dbo.CIMString(hvName.Value)
		AS [Name],
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_OperatingSystem : CIM_LogicalElement,
	dbo.CIMString(hvCSName.Value)
		AS CSName,
	dbo.CIMBigInteger(hvFreePhysicalMemory.Value)
		AS FreePhysicalMemory,
	dbo.CIMBigInteger(hvFreeVirtualMemory.Value)
		AS FreeVirtualMemory,
	dbo.CIMString(hvVersion.Value)
		AS Version,
	dbo.CIMDate(hvLastBootUpTime.Value)
		AS LastBootUpTime,
	-- Win32_OperatingSystem : CIM_OperatingSystem,
	dbo.CIMString(hvCountryCode.Value)
		AS CountryCode,
	dbo.CIMString(hvCSDVersion.Value)
		AS CSDVersion,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMInteger(hvOSLanguage.Value)
		AS OSLanguage,
	dbo.CIMString(hvSerialNumber.Value)
		AS SerialNumber,
	dbo.CIMInteger(hvServicePackMajorVersion.Value)
		AS ServicePackMajorVersion,
	dbo.CIMInteger(hvServicePackMinorVersion.Value)
		AS ServicePackMinorVersion,
	dbo.CIMString(hvSystemDevice.Value)
		AS SystemDevice,
	dbo.CIMString(hvSystemDirectory.Value)
		AS SystemDirectory,
	dbo.CIMString(hvWindowsDirectory.Value)
		AS WindowsDirectory

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_OperatingSystem'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpCaption
	ON hpCaption.Property = 'Caption'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCaption
	ON ho.HardwareObjectID = hvCaption.HardwareObjectID
	AND hpCaption.HardwarePropertyID = hvCaption.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpInstallDate
	ON hpInstallDate.Property = 'InstallDate'
	LEFT OUTER LOOP JOIN HardwareValue AS hvInstallDate
	ON ho.HardwareObjectID = hvInstallDate.HardwareObjectID
	AND hpInstallDate.HardwarePropertyID = hvInstallDate.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpName
	ON hpName.Property = 'Name'
	LEFT OUTER LOOP JOIN HardwareValue AS hvName
	ON ho.HardwareObjectID = hvName.HardwareObjectID
	AND hpName.HardwarePropertyID = hvName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_OperatingSystem : CIM_LogicalElement
	LEFT OUTER JOIN HardwareProperty AS hpCSName
	ON hpCSName.Property = 'CSName'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCSName
	ON ho.HardwareObjectID = hvCSName.HardwareObjectID
	AND hpCSName.HardwarePropertyID = hvCSName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpFreePhysicalMemory
	ON hpFreePhysicalMemory.Property = 'FreePhysicalMemory'
	LEFT OUTER LOOP JOIN HardwareValue AS hvFreePhysicalMemory
	ON ho.HardwareObjectID = hvFreePhysicalMemory.HardwareObjectID
	AND hpFreePhysicalMemory.HardwarePropertyID = hvFreePhysicalMemory.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpFreeVirtualMemory
	ON hpFreeVirtualMemory.Property = 'FreeVirtualMemory'
	LEFT OUTER LOOP JOIN HardwareValue AS hvFreeVirtualMemory
	ON ho.HardwareObjectID = hvFreeVirtualMemory.HardwareObjectID
	AND hpFreeVirtualMemory.HardwarePropertyID = hvFreeVirtualMemory.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpVersion
	ON hpVersion.Property = 'Version'
	LEFT OUTER LOOP JOIN HardwareValue AS hvVersion
	ON ho.HardwareObjectID = hvVersion.HardwareObjectID
	AND hpVersion.HardwarePropertyID = hvVersion.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpLastBootUpTime
	ON hpLastBootUpTime.Property = 'LastBootUpTime'
	LEFT OUTER LOOP JOIN HardwareValue AS hvLastBootUpTime
	ON ho.HardwareObjectID = hvLastBootUpTime.HardwareObjectID
	AND hpLastBootUpTime.HardwarePropertyID = hvLastBootUpTime.HardwarePropertyID
	
	-- Win32_OperatingSystem : CIM_OperatingSystem
	LEFT OUTER JOIN HardwareProperty AS hpCountryCode
	ON hpCountryCode.Property = 'CountryCode'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCountryCode
	ON ho.HardwareObjectID = hvCountryCode.HardwareObjectID
	AND hpCountryCode.HardwarePropertyID = hvCountryCode.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpCSDVersion
	ON hpCSDVersion.Property = 'CSDVersion'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCSDVersion
	ON ho.HardwareObjectID = hvCSDVersion.HardwareObjectID
	AND hpCSDVersion.HardwarePropertyID = hvCSDVersion.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpOSLanguage
	ON hpOSLanguage.Property = 'OSLanguage'
	LEFT OUTER LOOP JOIN HardwareValue AS hvOSLanguage
	ON ho.HardwareObjectID = hvOSLanguage.HardwareObjectID
	AND hpOSLanguage.HardwarePropertyID = hvOSLanguage.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpSerialNumber
	ON hpSerialNumber.Property = 'SerialNumber'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSerialNumber
	ON ho.HardwareObjectID = hvSerialNumber.HardwareObjectID
	AND hpSerialNumber.HardwarePropertyID = hvSerialNumber.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpServicePackMajorVersion
	ON hpServicePackMajorVersion.Property = 'ServicePackMajorVersion'
	LEFT OUTER LOOP JOIN HardwareValue AS hvServicePackMajorVersion
	ON ho.HardwareObjectID = hvServicePackMajorVersion.HardwareObjectID
	AND hpServicePackMajorVersion.HardwarePropertyID = hvServicePackMajorVersion.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpServicePackMinorVersion
	ON hpServicePackMinorVersion.Property = 'ServicePackMinorVersion'
	LEFT OUTER LOOP JOIN HardwareValue AS hvServicePackMinorVersion
	ON ho.HardwareObjectID = hvServicePackMinorVersion.HardwareObjectID
	AND hpServicePackMinorVersion.HardwarePropertyID = hvServicePackMinorVersion.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpSystemDevice
	ON hpSystemDevice.Property = 'SystemDevice'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSystemDevice
	ON ho.HardwareObjectID = hvSystemDevice.HardwareObjectID
	AND hpSystemDevice.HardwarePropertyID = hvSystemDevice.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpSystemDirectory
	ON hpSystemDirectory.Property = 'SystemDirectory'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSystemDirectory
	ON ho.HardwareObjectID = hvSystemDirectory.HardwareObjectID
	AND hpSystemDirectory.HardwarePropertyID = hvSystemDirectory.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpWindowsDirectory
	ON hpWindowsDirectory.Property = 'WindowsDirectory'
	LEFT OUTER LOOP JOIN HardwareValue AS hvWindowsDirectory
	ON ho.HardwareObjectID = hvWindowsDirectory.HardwareObjectID
	AND hpWindowsDirectory.HardwarePropertyID = hvWindowsDirectory.HardwarePropertyID
	 
GO

PRINT 'Creating view ComputerProcessor'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ComputerProcessor' AND xtype = 'V'
)
	DROP VIEW ComputerProcessor
GO
CREATE VIEW ComputerProcessor AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvDescription.Value)
		AS Description,
	LTRIM(dbo.CIMString(hvName.Value))
		AS [Name],
	dbo.CIMString(hvStatus.Value)
		AS Status,
	-- CIM_LogicalDevice : CIM_LogicalElement,
	-- CIM_Processor : CIM_LogicalDevice,
	dbo.CIMCPUSpeed(hvCurrentClockSpeed.Value)
		AS CurrentClockSpeed,
	dbo.CIMInteger(hvFamily.Value)
		AS Family,
	dbo.CIMCPUSpeed(hvMaxClockSpeed.Value)
		AS MaxClockSpeed,
	-- Win32_Processor : CIM_Processor,
	dbo.CIMString(hvVersion.Value)
		AS Version,
	dbo.CIMCPUManufacturer(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMInteger(hvL2CacheSize.Value)
		AS L2CacheSize,
	dbo.CIMInteger(hvCurrentVoltage.Value)
		AS CurrentVoltage,
	dbo.CIMString(hvProcessorId.Value)
		AS ProcessorId

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_Processor'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpDescription
	ON hpDescription.Property = 'Description'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDescription
	ON ho.HardwareObjectID = hvDescription.HardwareObjectID
	AND hpDescription.HardwarePropertyID = hvDescription.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpName
	ON hpName.Property = 'Name'
	LEFT OUTER LOOP JOIN HardwareValue AS hvName
	ON ho.HardwareObjectID = hvName.HardwareObjectID
	AND hpName.HardwarePropertyID = hvName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpStatus
	ON hpStatus.Property = 'Status'
	LEFT OUTER LOOP JOIN HardwareValue AS hvStatus
	ON ho.HardwareObjectID = hvStatus.HardwareObjectID
	AND hpStatus.HardwarePropertyID = hvStatus.HardwarePropertyID
	
	-- CIM_LogicalDevice : CIM_LogicalElement
	-- CIM_Processor : CIM_LogicalDevice
	LEFT OUTER JOIN HardwareProperty AS hpCurrentClockSpeed
	ON hpCurrentClockSpeed.Property = 'CurrentClockSpeed'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCurrentClockSpeed
	ON ho.HardwareObjectID = hvCurrentClockSpeed.HardwareObjectID
	AND hpCurrentClockSpeed.HardwarePropertyID = hvCurrentClockSpeed.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpFamily
	ON hpFamily.Property = 'Family'
	LEFT OUTER LOOP JOIN HardwareValue AS hvFamily
	ON ho.HardwareObjectID = hvFamily.HardwareObjectID
	AND hpFamily.HardwarePropertyID = hvFamily.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpMaxClockSpeed
	ON hpMaxClockSpeed.Property = 'MaxClockSpeed'
	LEFT OUTER LOOP JOIN HardwareValue AS hvMaxClockSpeed
	ON ho.HardwareObjectID = hvMaxClockSpeed.HardwareObjectID
	AND hpMaxClockSpeed.HardwarePropertyID = hvMaxClockSpeed.HardwarePropertyID
	
	-- Win32_Processor : CIM_Processor
	LEFT OUTER JOIN HardwareProperty AS hpVersion
	ON hpVersion.Property = 'Version'
	LEFT OUTER LOOP JOIN HardwareValue AS hvVersion
	ON ho.HardwareObjectID = hvVersion.HardwareObjectID
	AND hpVersion.HardwarePropertyID = hvVersion.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpL2CacheSize
	ON hpL2CacheSize.Property = 'L2CacheSize'
	LEFT OUTER LOOP JOIN HardwareValue AS hvL2CacheSize
	ON ho.HardwareObjectID = hvL2CacheSize.HardwareObjectID
	AND hpL2CacheSize.HardwarePropertyID = hvL2CacheSize.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpCurrentVoltage
	ON hpCurrentVoltage.Property = 'CurrentVoltage'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCurrentVoltage
	ON ho.HardwareObjectID = hvCurrentVoltage.HardwareObjectID
	AND hpCurrentVoltage.HardwarePropertyID = hvCurrentVoltage.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpProcessorId
	ON hpProcessorId.Property = 'ProcessorId'
	LEFT OUTER LOOP JOIN HardwareValue AS hvProcessorId
	ON ho.HardwareObjectID = hvProcessorId.HardwareObjectID
	AND hpProcessorId.HardwarePropertyID = hvProcessorId.HardwarePropertyID
	 
GO

PRINT 'Creating view PhysicalMemory'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'PhysicalMemory' AND xtype = 'V'
)
	DROP VIEW PhysicalMemory
GO
CREATE VIEW PhysicalMemory AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
		-- string Status,
	-- CIM_PhysicalElement : CIM_ManagedSystemElement,
	-- CIM_PhysicalComponent : CIM_PhysicalElement,
	-- CIM_Chip : CIM_PhysicalComponent,
	-- CIM_PhysicalMemory : CIM_Chip,
	dbo.CIMMemoryType(hvMemoryType.Value)
		AS MemoryType,
	dbo.CIMInteger(hvSpeed.Value)
		AS Speed,
	dbo.CIMBigInteger(hvCapacity.Value)
		AS Capacity,
	dbo.CIMInteger(hvPositionInRow.Value)
		AS PositionInRow

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_PhysicalMemory'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	-- CIM_PhysicalElement : CIM_ManagedSystemElement
	-- CIM_PhysicalComponent : CIM_PhysicalElement
	-- CIM_Chip : CIM_PhysicalComponent
	-- CIM_PhysicalMemory : CIM_Chip
	LEFT OUTER JOIN HardwareProperty AS hpMemoryType
	ON hpMemoryType.Property = 'MemoryType'
	LEFT OUTER LOOP JOIN HardwareValue AS hvMemoryType
	ON ho.HardwareObjectID = hvMemoryType.HardwareObjectID
	AND hpMemoryType.HardwarePropertyID = hvMemoryType.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpSpeed
	ON hpSpeed.Property = 'Speed'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSpeed
	ON ho.HardwareObjectID = hvSpeed.HardwareObjectID
	AND hpSpeed.HardwarePropertyID = hvSpeed.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpCapacity
	ON hpCapacity.Property = 'Capacity'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCapacity
	ON ho.HardwareObjectID = hvCapacity.HardwareObjectID
	AND hpCapacity.HardwarePropertyID = hvCapacity.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpPositionInRow
	ON hpPositionInRow.Property = 'PositionInRow'
	LEFT OUTER LOOP JOIN HardwareValue AS hvPositionInRow
	ON ho.HardwareObjectID = hvPositionInRow.HardwareObjectID
	AND hpPositionInRow.HardwarePropertyID = hvPositionInRow.HardwarePropertyID
	 
GO

PRINT 'Creating view ComputerSystemEnclosure'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ComputerSystemEnclosure' AND xtype = 'V'
)
	DROP VIEW ComputerSystemEnclosure
GO
CREATE VIEW ComputerSystemEnclosure AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	-- CIM_PhysicalElement : CIM_ManagedSystemElement,
	dbo.CIMString(hvSerialNumber.Value)
		AS SerialNumber,
	-- CIM_PhysicalPackage : CIM_PhysicalElement,
	-- CIM_PhysicalFrame : CIM_PhysicalPackage,
	-- CIM_Chassis : CIM_PhysicalFrame,
	dbo.CIMChassisType(hvChassisTypes.Value)
		AS ChassisTypes,
	-- Win32_SystemEnclosure : CIM_Chassis,
	dbo.CIMString(hvSMBiosAssetTag.Value)
		AS SMBiosAssetTag

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_Chassis'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	-- CIM_PhysicalElement : CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpSerialNumber
	ON hpSerialNumber.Property = 'SerialNumber'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSerialNumber
	ON ho.HardwareObjectID = hvSerialNumber.HardwareObjectID
	AND hpSerialNumber.HardwarePropertyID = hvSerialNumber.HardwarePropertyID
	
	-- CIM_PhysicalPackage : CIM_PhysicalElement
	-- CIM_PhysicalFrame : CIM_PhysicalPackage
	-- CIM_Chassis : CIM_PhysicalFrame
	LEFT OUTER JOIN HardwareProperty AS hpChassisTypes
	ON hpChassisTypes.Property = 'ChassisTypes'
	LEFT OUTER LOOP JOIN HardwareValue AS hvChassisTypes
	ON ho.HardwareObjectID = hvChassisTypes.HardwareObjectID
	AND hpChassisTypes.HardwarePropertyID = hvChassisTypes.HardwarePropertyID
	
	-- Win32_SystemEnclosure : CIM_Chassis
	LEFT OUTER JOIN HardwareProperty AS hpSMBiosAssetTag
	ON hpSMBiosAssetTag.Property = 'SMBiosAssetTag'
	LEFT OUTER LOOP JOIN HardwareValue AS hvSMBiosAssetTag
	ON ho.HardwareObjectID = hvSMBiosAssetTag.HardwareObjectID
	AND hpSMBiosAssetTag.HardwarePropertyID = hvSMBiosAssetTag.HardwarePropertyID
	 
GO

PRINT 'Creating view VideoController'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'VideoController' AND xtype = 'V'
)
	DROP VIEW VideoController
GO
CREATE VIEW VideoController AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvName.Value)
		AS [Name],
	-- CIM_LogicalElement : CIM_ManagedSystemElement,
	-- CIM_LogicalDevice : CIM_LogicalElement,
	-- CIM_Controller : CIM_LogicalDevice,
	-- CIM_VideoController : CIM_Controller,
	dbo.CIMString(hvVideoProcessor.Value)
		AS VideoProcessor,
	-- CIM_PCVideoController : CIM_VideoController,
	-- Win32_VideoController : CIM_PCVideoController,
	dbo.CIMInteger(hvAdapterRAM.Value)
		AS AdapterRAM,
	dbo.CIMString(hvInstalledDisplayDrivers.Value)
		AS InstalledDisplayDrivers,
	dbo.CIMDate(hvDriverDate.Value)
		AS DriverDate,
	dbo.CIMString(hvDriverVersion.Value)
		AS DriverVersion

FROM	(SELECT	sub.HardwareClassID,
		sub.Class AS HardwareClass
	 FROM	HardwareClass AS cim,
	 	HardwareClass AS sub
	 WHERE	cim.Class = 'CIM_PCVideoController'
	   AND	cim.HardwareClassID IN (sub.SuperClassID, sub.HardwareClassID)
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpName
	ON hpName.Property = 'Name'
	LEFT OUTER LOOP JOIN HardwareValue AS hvName
	ON ho.HardwareObjectID = hvName.HardwareObjectID
	AND hpName.HardwarePropertyID = hvName.HardwarePropertyID
	
	-- CIM_LogicalElement : CIM_ManagedSystemElement
	-- CIM_LogicalDevice : CIM_LogicalElement
	-- CIM_Controller : CIM_LogicalDevice
	-- CIM_VideoController : CIM_Controller
	LEFT OUTER JOIN HardwareProperty AS hpVideoProcessor
	ON hpVideoProcessor.Property = 'VideoProcessor'
	LEFT OUTER LOOP JOIN HardwareValue AS hvVideoProcessor
	ON ho.HardwareObjectID = hvVideoProcessor.HardwareObjectID
	AND hpVideoProcessor.HardwarePropertyID = hvVideoProcessor.HardwarePropertyID
	
	-- CIM_PCVideoController : CIM_VideoController
	-- Win32_VideoController : CIM_PCVideoController
	LEFT OUTER JOIN HardwareProperty AS hpAdapterRAM
	ON hpAdapterRAM.Property = 'AdapterRAM'
	LEFT OUTER LOOP JOIN HardwareValue AS hvAdapterRAM
	ON ho.HardwareObjectID = hvAdapterRAM.HardwareObjectID
	AND hpAdapterRAM.HardwarePropertyID = hvAdapterRAM.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpInstalledDisplayDrivers
	ON hpInstalledDisplayDrivers.Property = 'InstalledDisplayDrivers'
	LEFT OUTER LOOP JOIN HardwareValue AS hvInstalledDisplayDrivers
	ON ho.HardwareObjectID = hvInstalledDisplayDrivers.HardwareObjectID
	AND hpInstalledDisplayDrivers.HardwarePropertyID = hvInstalledDisplayDrivers.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDriverDate
	ON hpDriverDate.Property = 'DriverDate'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDriverDate
	ON ho.HardwareObjectID = hvDriverDate.HardwareObjectID
	AND hpDriverDate.HardwarePropertyID = hvDriverDate.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDriverVersion
	ON hpDriverVersion.Property = 'DriverVersion'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDriverVersion
	ON ho.HardwareObjectID = hvDriverVersion.HardwareObjectID
	AND hpDriverVersion.HardwarePropertyID = hvDriverVersion.HardwarePropertyID
	 
GO

PRINT 'Creating view Win32_SoundDevice'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'Win32_SoundDevice' AND xtype = 'V'
)
	DROP VIEW Win32_SoundDevice
GO
CREATE VIEW Win32_SoundDevice AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvName.Value)
		AS [Name],
	-- CIM_LogicalElement : CIM_ManagedSystemElement,
	-- CIM_LogicalDevice : CIM_LogicalElement,
	-- Win32_SoundDevice : CIM_LogicalDevice,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer

FROM	(SELECT	HardwareClassID,
		Class AS HardwareClass
	 FROM	HardwareClass
	 WHERE	Class = 'Win32_SoundDevice'
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpName
	ON hpName.Property = 'Name'
	LEFT OUTER LOOP JOIN HardwareValue AS hvName
	ON ho.HardwareObjectID = hvName.HardwareObjectID
	AND hpName.HardwarePropertyID = hvName.HardwarePropertyID
	
	-- CIM_LogicalElement : CIM_ManagedSystemElement
	-- CIM_LogicalDevice : CIM_LogicalElement
	-- Win32_SoundDevice : CIM_LogicalDevice
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	 
GO

PRINT 'Creating view Win32_PnPEntity'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'Win32_PnPEntity' AND xtype = 'V'
)
	DROP VIEW Win32_PnPEntity
GO
CREATE VIEW Win32_PnPEntity AS
SELECT	ho.ComputerID,
	ho.ComputerOUID,
	ho.HardwareName,
	hc.HardwareClass,
	-- CIM_ManagedSystemElement,
	dbo.CIMString(hvCaption.Value)
		AS Caption,
	dbo.CIMString(hvDescription.Value)
		AS Description,
	dbo.CIMString(hvName.Value)
		AS [Name],
	dbo.CIMString(hvClassGuid.Value)
		AS ClassGuid,
	dbo.CIMString(hvDeviceID.Value)
		AS DeviceID,
	dbo.CIMString(hvManufacturer.Value)
		AS Manufacturer,
	dbo.CIMString(hvService.Value)
		AS Service

FROM	(SELECT	HardwareClassID,
		Class AS HardwareClass
	 FROM	HardwareClass
	 WHERE	Class = 'Win32_PnPEntity'
	) AS hc

	INNER JOIN HardwareObject AS ho
	ON hc.HardwareClassID = ho.HardwareClassID

	-- CIM_ManagedSystemElement
	LEFT OUTER JOIN HardwareProperty AS hpCaption
	ON hpCaption.Property = 'Caption'
	LEFT OUTER LOOP JOIN HardwareValue AS hvCaption
	ON ho.HardwareObjectID = hvCaption.HardwareObjectID
	AND hpCaption.HardwarePropertyID = hvCaption.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDescription
	ON hpDescription.Property = 'Description'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDescription
	ON ho.HardwareObjectID = hvDescription.HardwareObjectID
	AND hpDescription.HardwarePropertyID = hvDescription.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpName
	ON hpName.Property = 'Name'
	LEFT OUTER LOOP JOIN HardwareValue AS hvName
	ON ho.HardwareObjectID = hvName.HardwareObjectID
	AND hpName.HardwarePropertyID = hvName.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpClassGuid
	ON hpClassGuid.Property = 'ClassGuid'
	LEFT OUTER LOOP JOIN HardwareValue AS hvClassGuid
	ON ho.HardwareObjectID = hvClassGuid.HardwareObjectID
	AND hpClassGuid.HardwarePropertyID = hvClassGuid.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpDeviceID
	ON hpDeviceID.Property = 'DeviceID'
	LEFT OUTER LOOP JOIN HardwareValue AS hvDeviceID
	ON ho.HardwareObjectID = hvDeviceID.HardwareObjectID
	AND hpDeviceID.HardwarePropertyID = hvDeviceID.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpManufacturer
	ON hpManufacturer.Property = 'Manufacturer'
	LEFT OUTER LOOP JOIN HardwareValue AS hvManufacturer
	ON ho.HardwareObjectID = hvManufacturer.HardwareObjectID
	AND hpManufacturer.HardwarePropertyID = hvManufacturer.HardwarePropertyID
	
	LEFT OUTER JOIN HardwareProperty AS hpService
	ON hpService.Property = 'Service'
	LEFT OUTER LOOP JOIN HardwareValue AS hvService
	ON ho.HardwareObjectID = hvService.HardwareObjectID
	AND hpService.HardwarePropertyID = hvService.HardwarePropertyID
	 
GO

IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'NetworkDeviceTypeBaseCounts' AND xtype = 'V'
)
	DROP VIEW NetworkDeviceTypeBaseCounts
GO
CREATE VIEW NetworkDeviceTypeBaseCounts AS
SELECT [DeviceRole] = 0, [DeviceType] = dbo.NetworkDeviceType(0), [DeviceCount] = 0 -- Computer
UNION
SELECT [DeviceRole] = 1, [DeviceType] = dbo.NetworkDeviceType(1), [DeviceCount] = 0 -- Workstation
UNION
SELECT [DeviceRole] = 2, [DeviceType] = dbo.NetworkDeviceType(2), [DeviceCount] = 0 -- Server
UNION
SELECT [DeviceRole] = 3, [DeviceType] = dbo.NetworkDeviceType(3), [DeviceCount] = 0 -- Printer
UNION
SELECT [DeviceRole] = 4, [DeviceType] = dbo.NetworkDeviceType(4), [DeviceCount] = 0 -- Switch
UNION
SELECT [DeviceRole] = 5, [DeviceType] = dbo.NetworkDeviceType(5), [DeviceCount] = 0 -- Router
UNION
SELECT [DeviceRole] = 6, [DeviceType] = dbo.NetworkDeviceType(6), [DeviceCount] = 0 -- Hub
GO
/*
 * $Id: schema795.sql 100106 2011-06-16 00:30:20Z rlowery $
 *
 * After creating the database tables,
 * populate initial values and create views, etc.
 */

IF NOT EXISTS (SELECT *
	FROM DatabaseConfiguration
	WHERE Property = 'SchemaVersion'
)
	/*
	 * Set database version.
	 *
	 * The SchemaVersion property should be updated when the database
	 * schema is changed; it should be the same as version number of
	 * the product.
	 *
	 * If the product version changes but the database schema
	 * doesn't change, the version column should not be updated.
	 */
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('SchemaVersion', '7.9.5');
GO

PRINT 'Creating TargetedPackage view:'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'TargetedPackage' AND xtype = 'V'
)
	DROP VIEW TargetedPackage
GO
CREATE VIEW TargetedPackage
AS	
	SELECT	pa.TargetTypeID,
		pa.TargetOUID,
		pa.TargetID,
		pa.PackagePathID,
		pa.PolicyGUID,
		pa.AllocationDetailsID,
		ip.PostponePeriodStart,
		pa.Precedence, pa.PolicyVersion,
		cast(0 as bit) as FromRSOP,
		cast(null as int) as RSOPGroupID
	FROM	PackageApplies AS pa
		LEFT OUTER JOIN
		InstallationPostponement AS ip
		ON	pa.TargetTypeID = ip.TargetTypeID
		  AND	pa.TargetID = ip.TargetID
		  AND	pa.PackagePathID = ip.PackagePathID
		  AND	pa.PolicyGUID = ip.PolicyGUID
		  AND	pa.AllocationDetailsID = ip.AllocationDetailsID
	WHERE pa.TargetTypeID <= 2	-- Users and Computers only
	
	UNION ALL
	
	SELECT	t.TargetTypeID,
		t.TargetOUID,
		t.TargetID,
		pa.PackagePathID,
		pa.PolicyGUID,
		pa.AllocationDetailsID,
		(
			SELECT top 1 ip.PostponePeriodStart
			FROM dbo.InstallationPostponement AS ip
			WHERE t.TargetTypeID = ip.TargetTypeID
			  AND	t.TargetID = ip.TargetID
			  AND	pa.PackagePathID = ip.PackagePathID
			  AND	pa.PolicyGUID = ip.PolicyGUID
			  AND	pa.AllocationDetailsID = ip.AllocationDetailsID
		) AS PostponePeriodStart,
		pa.Precedence,
		pa.PolicyVersion,
		cast(1 as bit) FromRSOP,
		pa.TargetID as RSOPGroupID
	FROM	PackageApplies AS pa,
		Member AS tm,
		dbo.Target(2) AS t			-- Users and Computers
	WHERE	pa.TargetTypeID = 3			-- RSoP Package Applies
	  AND	pa.TargetID = tm.GroupID			-- Having a member
	  AND	tm.TargetTypeID = t.TargetTypeID	-- which is of this type
	  AND	tm.TargetID = t.TargetID		-- and this ID
GO

-- For some reason SQL Server doesn't allow CREATE VIEW inside an IF statement
PRINT 'Creating ComputerWithDN view:'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ComputerWithDN' AND xtype = 'V'
)
	DROP VIEW ComputerWithDN
GO
CREATE VIEW ComputerWithDN AS
	SELECT ComputerID,
		ComputerCN,
		ComputerOUID,
		s.OperatingSystemName,
		c.GUID,
		('CN='+c.ComputerCN+','+ISNULL(o.RDN,'OU=unknown')+','+d.DN)
			AS DN,
		(ISNULL(o.RDN,'OU=unknown')+','+d.DN) AS OU
	FROM	Computer AS c,
		Organization AS o,
		Domain AS d,
		OperatingSystem as s
	WHERE c.ComputerOUID = o.OrganizationID
	  AND o.OrganizationID = d.OrganizationID
	  AND c.OperatingSystemID = s.OperatingSystemID
GO

/*
 * Provides directory details for each computer
 */
PRINT 'Creating ComputerDirectory view:'
IF EXISTS (
	SELECT * FROM sysobjects
	WHERE name = 'ComputerDirectory' AND xtype = 'V'
)
	DROP VIEW ComputerDirectory
GO
CREATE VIEW ComputerDirectory AS
	SELECT	ComputerID,			-- db ID of computer
		ComputerCN,			-- Common name of computer
		c.GUID,				-- AD objectGUID of computer
		c.OperatingSystemID,		-- db ID of Operating System
		s.OperatingSystemName AS OperatingSystem, -- name of Operating System
		ComputerOUID,			-- db ID of OrganizationalUnit
		CASE
			WHEN d.DomainType = 'MGS' THEN
				-- There are no OUs for MGS domain type
				''
			ELSE
				o.RDN
		END AS OURDN,			-- Relative DN of OU
		CASE
			WHEN d.DomainType = 'MGS' THEN
				-- There are no OUs for MGS domain type
				''
			ELSE
				o.RDNReverse+','
		END AS OURDNReverse,		-- (components in reverse order)
		CASE
			WHEN d.DomainType = 'MGS' THEN
				-- There are no OUs for MGS domain type
				d.DN
			ELSE
				(ISNULL(o.RDN,'unknown')+','+d.DN)
		END AS OUDN,			-- Full DN of OU
		d.OrganizationID AS DomainID,	-- db ID of Domain
		d.DN AS DomainDN,		-- Distinguished Name of Domain
		d.DNReverse AS DomainDNReverse, -- (components in reverse order)
		CASE
			WHEN d.DomainType = 'MGS' THEN
				-- There are no OUs for MGS domain type
				('CN='+c.ComputerCN+','+d.DN)
			ELSE
				('CN='+c.ComputerCN+','+ISNULL(o.RDN,'OU=unknown')+','+d.DN)
		END AS ComputerDN		-- Computer Distinguished Name
	FROM	Computer AS c,
		Organization AS o,
		Domain AS d,
		OperatingSystem AS s
	WHERE	c.ComputerOUID = o.OrganizationID
	  AND	o.DomainID = d.OrganizationID
	  AND	ISNULL(c.OperatingSystemID, 0) = s.OperatingSystemID
GO

-- DEFAULT_NAMING_CONTEXT should be substituted, e.g. 'DC=company,DC=com'
PRINT 'Saving Domain Controller name into DatabaseConfiguration table:'
IF EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'DomainController'
)
	UPDATE DatabaseConfiguration SET Value = 'DEFAULT_NAMING_CONTEXT'
		WHERE Property = 'DomainController'
ELSE
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('DomainController', 'DEFAULT_NAMING_CONTEXT')
GO

-- For some reason SQL Server doesn't allow CREATE VIEW inside an IF statement
PRINT 'Creating SDOU view:'
IF EXISTS (
	SELECT * FROM sysobjects WHERE xtype = 'V'
	AND name = 'SDOU'
)
	DROP VIEW SDOU
GO

CREATE VIEW SDOU AS
	SELECT	o.OrganizationID as OUID,
		CASE
		    WHEN RDN IS NULL THEN	-- Unknown OU in this domain
			'<NULL>,'+d.DN		-- or the universal domain
		    WHEN RDN = '' THEN
		    	d.DN			-- DomainDN for the domain
		    ELSE
		    	o.RDN+','+d.DN		-- Ordinary OU or container
		END AS OU,
		GUID
	FROM Organization AS o, Domain AS d
	WHERE o.DomainID = d.OrganizationID
GO

PRINT 'Creating ReverseDN stored procedure'
IF EXISTS(SELECT * FROM sysobjects WHERE xtype = 'P'
        AND [name] = 'ReverseDN'
)
        DROP PROCEDURE ReverseDN
GO
CREATE PROCEDURE ReverseDN 
	@dn nvarchar(400),
	@dn_reverse nvarchar(400) OUTPUT
AS

	DECLARE @not_end_of_dn bit
	DECLARE @end int
	DECLARE @start int
	DECLARE @dnreverse nvarchar(400)

	SET @dnreverse = ''
	SET @not_end_of_dn = 1
	SET @start = 1 

	WHILE (@not_end_of_dn = 1)
	BEGIN
		-- search for comma
		SET @end = CHARINDEX(',', @dn, @start)
		IF @end = 0
		BEGIN
			-- reach the end of string
			SET @not_end_of_dn = 0
			IF @start = 1
				SET @dnreverse = SUBSTRING(
						@dn, @start, LEN(@dn)-@start+1
					) + @dnreverse
			ELSE
				SET @dnreverse = SUBSTRING(
						@dn, @start, LEN(@dn)-@start+1
					) + ',' + @dnreverse

			BREAK
		END

		-- if there is a \ before comma, keep searching for comma
		-- that indicate the actual separator
		WHILE (SUBSTRING(@dn, @end - 1, 1) = '\')
		BEGIN
			SET @end = CHARINDEX(',', @dn, @end + 1)
			IF @end = 0
			BEGIN
				-- reach the end of string
				SET @not_end_of_dn = 0

				-- set @end to length+1, as it is used as
				-- the index of the separator which logically
				-- appears one character after the end
				SET @end = LEN(@dn) + 1
				BREAK
			END
		END

		-- retrieve the string between the last search and the comma
		IF @start = 1 
			SET @dnreverse = SUBSTRING(@dn, @start, @end-@start)
					+ @dnreverse
		ELSE
			SET @dnreverse = SUBSTRING(@dn, @start, @end-@start)
					+ ',' + @dnreverse

		SET @start = @end + 1
	END

	SET @dn_reverse = @dnreverse
GO

PRINT 'Creating Organization table triggers'
PRINT '	ReverseOrganizationDN'
IF EXISTS (SELECT name FROM sysobjects WHERE type = 'TR'
      AND name = 'ReverseOrganizationDN'
)
	DROP TRIGGER ReverseOrganizationDN
GO
CREATE TRIGGER ReverseOrganizationDN 
	ON Organization FOR INSERT, UPDATE
AS 
	DECLARE @rdn nvarchar(400)
	DECLARE @rdnreverse nvarchar(400)

	IF @@ROWCOUNT = 1
	BEGIN
		IF UPDATE (RDN) 
		BEGIN
			SELECT @rdn = RDN FROM inserted
			IF @rdn IS NOT NULL
			BEGIN
				EXEC ReverseDN @rdn, @rdnreverse OUTPUT

				UPDATE Organization 
				SET RDNReverse = @rdnreverse
				WHERE RDN = @rdn
			END
			ELSE	-- If NULL, replace placeholder text with NULL
			BEGIN
				UPDATE Organization
				SET RDNReverse = NULL
				WHERE RDN IS NULL
			END
		END
	END
	ELSE
	BEGIN
		DECLARE rdn_cursor CURSOR FOR 
		SELECT RDN
		FROM inserted
		WHERE RDN IS NOT NULL

		OPEN rdn_cursor

		FETCH NEXT FROM rdn_cursor 
		INTO @rdn

		WHILE @@FETCH_STATUS = 0
		BEGIN
			EXEC ReverseDN @rdn, @rdnreverse OUTPUT

			UPDATE Organization 
			SET RDNReverse = @rdnreverse
			WHERE RDN = @rdn

			FETCH NEXT FROM rdn_cursor 
			INTO @rdn
		END

		CLOSE rdn_cursor
		DEALLOCATE rdn_cursor
	END
GO

PRINT 'Creating Domain table triggers:'
PRINT '	ReverseDomainDN'
IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'ReverseDomainDN' AND type = 'TR'
)
	DROP TRIGGER ReverseDomainDN
GO
CREATE TRIGGER ReverseDomainDN 
	ON Domain FOR INSERT, UPDATE
AS 
	DECLARE @dn nvarchar(100)
	DECLARE @dnreverse nvarchar(100)

	IF @@ROWCOUNT = 1
	BEGIN
		IF UPDATE (DN) 
		BEGIN
			SELECT @dn = DN FROM inserted

			IF @dn IS NOT NULL
			BEGIN
				EXEC ReverseDN @dn, @dnreverse OUTPUT

				UPDATE Domain 
				SET DNReverse = @dnreverse
				WHERE DN = @dn
			END
		END
	END
	ELSE
	BEGIN
		DECLARE dn_cursor CURSOR FOR 
		SELECT DN
		FROM inserted
		WHERE DN IS NOT NULL

		OPEN dn_cursor

		FETCH NEXT FROM dn_cursor 
		INTO @dn

		WHILE @@FETCH_STATUS = 0
		BEGIN
			EXEC ReverseDN @dn, @dnreverse OUTPUT

			UPDATE Domain 
			SET DNReverse = @dnreverse
			WHERE DN = @dn

			FETCH NEXT FROM dn_cursor 
			INTO @dn
		END

		CLOSE dn_cursor
		DEALLOCATE dn_cursor
	END	

GO

PRINT 'Creating DomainConfiguration table triggers:'
PRINT '	DomainConfigurationSyncWithDomain'
IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'DomainConfigurationSyncWithDomain' AND type = 'TR'
)
	DROP TRIGGER DomainConfigurationSyncWithDomain
GO
CREATE TRIGGER DomainConfigurationSyncWithDomain ON [dbo].[DomainConfiguration] 
FOR INSERT, UPDATE 
AS

if exists(select * from inserted where Property = 'ADReconcile')
	begin
		update dbo.Domain
		set ADReconcile = (case when i.Value = '1' then 1 else 0 end)
		from inserted as i
		where i.Property = 'ADReconcile'
			and i.DomainID = OrganizationID
			and ADReconcile <> (case when i.Value = '1' then 1 else 0 end)
	end

if exists(select * from inserted where Property = 'MergePolicies')
	begin
		update dbo.Domain
		set MergePolicies = (case when i.Value = '1' then 1 else 0 end)
		from inserted as i
		where i.Property = 'MergePolicies'
			and i.DomainID = OrganizationID
			and MergePolicies <> (case when i.Value = '1' then 1 else 0 end)
	end

if exists(select * from inserted where Property = 'ADLoadLatency')
	begin
		update dbo.Domain
		set ADLoadLatency = case when ISNUMERIC(i.Value) = 1 then i.Value else null end
		from inserted as i
		where i.Property = 'ADLoadLatency'
			and i.DomainID = OrganizationID
			and ADLoadLatency <> case when ISNUMERIC(i.Value) = 1 then i.Value else null end
	end

if exists(select * from inserted where Property = 'LastADReconcile')
	begin
		update dbo.Domain
		set LastADReconcile = i.DateValue
		from inserted as i
		where i.Property = 'LastADReconcile'
			and i.DomainID = OrganizationID
			and (LastADReconcile <> cast(i.DateValue as smalldatetime) or LastADReconcile is null)
	end

if exists(select * from inserted where Property = 'LastPolicyMerge')
	begin
		update dbo.Domain
		set LastPolicyMerge = i.DateValue
		from inserted as i
		where i.Property = 'LastPolicyMerge'
			and i.DomainID = OrganizationID
			and (LastPolicyMerge <> cast(i.DateValue as smalldatetime) or LastPolicyMerge is null)
	end
GO

-- This constraint has be created after Domain entry with DomainID = 1
-- is created for upgrade purposes. DomainID is a new column and during
-- upgrade, we initialise it to 1 since Organization.DomainID is a
-- foreign key of Domain.OrganizationID, an entry with DomainID = 1 has to exist.
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Organization_Domain'
)
BEGIN
	PRINT 'Create FK_Organization_Domain constraint' 

	ALTER TABLE Organization WITH NOCHECK
		ADD CONSTRAINT FK_Organization_Domain
			FOREIGN KEY (DomainID) 
				REFERENCES Domain(OrganizationID)
END
ELSE
	PRINT 'Using existing FK_Organization_Domain constraint'
GO

PRINT '	DomainCreate'
IF EXISTS(SELECT * FROM sysobjects WHERE xtype = 'P'
        AND [name] = 'DomainCreate'
)
        DROP PROCEDURE DomainCreate
GO
CREATE PROCEDURE DomainCreate 
	@dn nvarchar(100),
	@domain_type varchar(4) = NULL
AS

	DECLARE @OrganizationID int

	-- Disable the foreign key constraint so we can create the new entry:
	ALTER TABLE Organization NOCHECK CONSTRAINT FK_Organization_Domain

	-- Make an Organization entry for the Domain (allocate the ID):
	INSERT INTO Organization (RDN) VALUES ('')

	-- Find the ID and set the DomainID to the same:
	SELECT @OrganizationID = OrganizationID 
		FROM Organization 
		WHERE RDN = '' AND DomainID = 0
	UPDATE Organization
		SET DomainID = @OrganizationID 
		WHERE OrganizationID = @OrganizationID

	-- Insert the matching DomainID
	INSERT INTO Domain (OrganizationID, DN, DomainType)
		VALUES (@OrganizationID, @dn, @domain_type)

	-- Create the DomainConfiguration entries
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'ADReconcile', '0', NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'MergePolicies', '0', NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'LastADReconcile', NULL, NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'LastPolicyMerge', NULL, NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'ADLoadLatency', '60', NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'LoadRootDomain', '1', NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'MergeComputers', '0', NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'MergeUsers', '0', NULL, GETDATE()
	)
	INSERT INTO DomainConfiguration VALUES (
		@OrganizationID, 'ReconcileSites', '0', NULL, GETDATE()
	)

	-- Re-enable the constraint
	ALTER TABLE Organization CHECK CONSTRAINT FK_Organization_Domain

	-- Insert the "unknown" OU for the domain, if not first domain
	IF (@dn IS NOT NULL)
		INSERT INTO Organization (DomainID) VALUES (@OrganizationID)

	RETURN @OrganizationID
GO

PRINT '	DomainUpdate'
IF EXISTS(SELECT * FROM sysobjects WHERE xtype = 'P'
        AND [name] = 'DomainUpdate'
)
        DROP PROCEDURE DomainUpdate
GO
CREATE PROCEDURE DomainUpdate
	@OrganizationID		int,
	@DN			nvarchar(100) = NULL,
	@DomainType		varchar(4) = NULL,
	@FlatName		nvarchar(32) = NULL,
	@PreferredDomainController nvarchar(32) = NULL,
	@PreferredDomainControllerOnly bit = 0,
	@ADReconcile		bit = 0,
	@ADLoadLatency		int = 60,
	@MergePolicies		bit = 0,
	@LastADReconcile	smalldatetime = NULL,
	@LastADReconcileStatus	bit = 0,
	@LastADLoad		smalldatetime = NULL,
	@LastPolicyMerge	smalldatetime = NULL,
	@LastPolicyMergeStatus	bit = 0
AS
	UPDATE	Domain
	SET	DN = @DN,
		DomainType = @DomainType,
		FlatName = @FlatName,
		PreferredDomainController = @PreferredDomainController,
		PreferredDomainControllerOnly = @PreferredDomainControllerOnly,
		ADReconcile = @ADReconcile,
		ADLoadLatency = @ADLoadLatency,
		MergePolicies = @MergePolicies,
		LastADReconcile = @LastADReconcile,
		LastADReconcileStatus = @LastADReconcileStatus,
		LastADLoad = @LastADLoad,
		LastPolicyMerge = @LastPolicyMerge,
		LastPolicyMergeStatus = @LastPolicyMergeStatus
	WHERE	OrganizationID = @OrganizationID
GO

IF 'DEFAULT_NAMING_CONTEXT' <> ('DEFAULT_'+'NAMING_CONTEXT')
AND NOT EXISTS(SELECT * FROM Domain WHERE DN = 'DEFAULT_NAMING_CONTEXT')
BEGIN
	PRINT 'Creating the default AD domain'
	DECLARE @default_domain_id int
	EXEC @default_domain_id = DomainCreate 'DEFAULT_NAMING_CONTEXT', 'AD'
	UPDATE Domain
		SET	ADReconcile = 1,
			MergePolicies = 1,
			FlatName = 'DEFAULT_FLAT_NAME'
		WHERE DN = 'DEFAULT_NAMING_CONTEXT'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_domain_id
		  AND	Property = 'ADReconcile'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_domain_id
		  AND	Property = 'MergePolicies'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_domain_id
		  AND	Property = 'MergeComputers'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_domain_id
		  AND	Property = 'MergeUsers'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_domain_id
		  AND	Property = 'ReconcileSites'
END
GO

IF 'DEFAULT_NAMING_CONTEXT' <> ('DEFAULT_'+'NAMING_CONTEXT')
AND EXISTS(SELECT * FROM Domain WHERE DN = 'DEFAULT_NAMING_CONTEXT' AND (FlatName IS NULL OR FlatName = ''))
BEGIN
	PRINT 'Updating the default domain flat name column'
	UPDATE Domain
		SET FlatName = 'DEFAULT_FLAT_NAME'
		WHERE DN = 'DEFAULT_NAMING_CONTEXT'
END
GO

IF 'DEFAULT_MGS_DOMAIN' <> ('DEFAULT_'+'MGS_DOMAIN')
AND NOT EXISTS(SELECT * FROM Domain WHERE DN = 'DEFAULT_MGS_DOMAIN')
BEGIN
	PRINT 'Creating the default MGS domain'
	DECLARE @default_mgs_domain_id int
	EXEC @default_mgs_domain_id = DomainCreate	'DEFAULT_MGS_DOMAIN',
						'MGS'
	UPDATE Domain
		SET	ADReconcile = 1,
			MergePolicies = 1,
			FlatName = 'DEFAULT_MGS_FLAT_NAME'
		WHERE DN = 'DEFAULT_MGS_DOMAIN'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_mgs_domain_id
		  AND	Property = 'ADReconcile'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_mgs_domain_id
		  AND	Property = 'MergePolicies'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_mgs_domain_id
		  AND	Property = 'MergeComputers'
	UPDATE DomainConfiguration
		SET	Value = '1'
		WHERE	DomainID = @default_mgs_domain_id
		  AND	Property = 'MergeUsers'
END
GO

IF NOT EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'UsageDataObsoletePeriod'
)
BEGIN
	PRINT 'Setting default period to obsolete old software usage data'
	-- Default to 26 weeks, 6 months
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('UsageDataObsoletePeriod', '26')
END
ELSE
	PRINT 'Found existing UsageDataObsoletePeriod property in the DatabaseConfiguration table. Using that instead.'
GO

IF NOT EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'UsageDataObsoleteScope'
)
BEGIN
	PRINT 'Setting default scope of deletion when obsoleting old software usage data'
	-- Default to all OUs and DNs
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('UsageDataObsoleteScope', '%')
END
ELSE
	PRINT 'Found existing UsageDataObsoleteScope property in the DatabaseConfiguration table. Using that instead.'
GO

IF NOT EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'DNListingDelimiter'
)
BEGIN
	PRINT 'Setting default delimiter for DN listing in UsageDataObsoleteScope'
	-- Default to all OUs and DNs
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('DNListingDelimiter', ';')
END
ELSE
	PRINT 'Found existing DNListingDelimiter property in the DatabaseConfiguration table. Using that instead.'
GO

IF NOT EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'DNExactMatch'
)
BEGIN
	PRINT 'Setting default DN match option for processing of UsageDataObsoleteScope'
	-- Default to all OUs and DNs
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('DNExactMatch', '0')
END
ELSE
	PRINT 'Found existing DNExactMatch property in the DatabaseConfiguration table. Using that instead.'
GO

IF NOT EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'HoursBeforeDSOffline'
)
BEGIN
	PRINT 'Setting default DN match option for processing of UsageDataObsoleteScope'
	-- Default to all OUs and DNs
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('HoursBeforeDSOffline', '24')
END
ELSE
	PRINT 'Found existing HoursBeforeDSOffline property in the DatabaseConfiguration table. Using that instead.'
GO

IF NOT EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'SecurityEventObsoletePeriod'
)
BEGIN
	PRINT 'Setting default period for obsolete security event data'
	-- Default to 4 weeks, 1 month
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('SecurityEventObsoletePeriod', '4')
END
ELSE
	PRINT 'Found existing SecurityEventObsoletePeriod property in the DatabaseConfiguration table. Using that instead.'
GO

IF NOT EXISTS(
	SELECT * from DatabaseConfiguration
	WHERE Property = 'SecurityEventObsoleteScope'
)
BEGIN
	PRINT 'Setting default scope of deletion for obsolete security event data'
	-- Default to all OUs and DNs
	INSERT INTO DatabaseConfiguration (Property, Value)
		VALUES ('SecurityEventObsoleteScope', '%')
END
ELSE
	PRINT 'Found existing SecurityEventObsoleteScope property in the DatabaseConfiguration table. Using that instead.'
GO

IF EXISTS(
	SELECT * FROM dbo.sysobjects
	WHERE name = 'WakeOnLANHostInfo' AND xtype = 'V'
)
	DROP VIEW WakeOnLANHostInfo
GO

CREATE VIEW WakeOnLANHostInfo AS
	SELECT DISTINCT	c.ComputerID as ComputerID,
		c.ComputerCN AS ComputerCN,
		ISNULL(o.RDN, '<unknown>') AS RDN,
		ISNULL(d.DN, '') AS DomainDN,
		ip.Value AS IP,
		subnet.Value AS NetMask,
		mac.Value AS MAC
	FROM	Computer as c,				-- Computer
		Domain as d,				-- Domain of computer
		Organization as o,			-- OU of computer
		HardwareObject as mau,			-- Media Attachment Unit
		HardwareProperty as ip_prop,		-- IP addr Property name
		HardwareValue as ip,
		HardwareProperty as mac_prop,		-- MAC Property name
		HardwareValue as mac,
		HardwareProperty as subnet_prop,	-- Subnet Property name
		HardwareValue as subnet,
		HardwareProperty as gateway_prop,	-- Gateway Property name
		HardwareValue as gateway
	WHERE	o.OrganizationID = c.ComputerOUID	-- Computer in OU
	  AND	d.OrganizationID = o.DomainID		-- OU in Domain
  	  AND 	c.ComputerID = mau.ComputerID		-- MAU in Computer
	  AND	ip_prop.Property = 'IPAddress' 		-- IPAddress property
	    AND	mau.HardwareObjectID = ip.HardwareObjectID -- of the MAU
	    AND	ip.HardwarePropertyID = ip_prop.HardwarePropertyID
	  AND	mac_prop.Property = 'MACAddress' 
	    AND	mau.HardwareObjectID = mac.HardwareObjectID
	    AND	mac.HardwarePropertyID = mac_prop.HardwarePropertyID
	  AND	subnet_prop.Property = 'IPSubnet' 
	    AND	mau.HardwareObjectID = subnet.HardwareObjectID
	    AND	subnet.HardwarePropertyID = subnet_prop.HardwarePropertyID
	  AND	gateway_prop.Property = 'DefaultIPGateway' 
	    AND	mau.HardwareObjectID = gateway.HardwareObjectID
	    AND	gateway.HardwarePropertyID = gateway_prop.HardwarePropertyID
GO

PRINT 'Creating Compatibility views:'
PRINT 'Creating view V6PackageVersion:'
IF EXISTS (
	SELECT * FROM sysobjects WHERE xtype = 'V'
	AND name = 'V6PackageVersion'
)
	DROP VIEW V6PackageVersion
GO

CREATE VIEW V6PackageVersion
AS	SELECT	p.PackageFullName,
		f.PackageName,
		v.Version,
		v.Title,
		v.MD5,
		v.Size,
		f.PackageName + ' ' + v.Version AS PackageNameAndVersion
	FROM	PackageVersion AS v,
		PackagePath AS p,
		PackageFamily AS f
	WHERE	v.PackagePathID = p.PackagePathID
	  AND	v.PackageFamilyID = f.PackageFamilyID
	  AND NOT EXISTS(
		-- Only where there isn't another Warehouse version of this pkg
		SELECT *
		FROM	PackageVersion AS alt_pv,
			MediaContainsPackageVersion AS mcp
		WHERE	v.PackagePathID = alt_pv.PackagePathID	-- Same path
		  AND	v.PackageVersionID != alt_pv.PackageVersionID --Not same
		  AND	alt_pv.PackageVersionID = mcp.PackageVersionID
		  AND	mcp.MediaID = 1				-- In Warehouse
		)
GO

PRINT 'Creating view V6DistributedPackage:'
IF EXISTS (
	SELECT * FROM sysobjects WHERE xtype = 'V'
	AND name = 'V6DistributedPackage'
)
	DROP VIEW V6DistributedPackage
GO

CREATE VIEW V6DistributedPackage
AS	SELECT	p.ServerUID,
		rp.PackageFullName,
		p.ConfirmedState,
		p.ConfirmedDate AS ConfirmedUpdate,
		(SELECT c.Version FROM PackageVersion AS c
		 WHERE p.ConfirmedVersionID = c.PackageVersionID
		) AS ConfirmedVersion,
		p.ConfirmedReason,
		p.RequestState,
		p.RequestDate AS RequestUpdate,
		r.Version AS RequestVersion
	FROM	DistributedPackage AS p,
		PackageVersion AS r,
		PackagePath AS rp
	WHERE	p.RequestedVersionID = r.PackageVersionID
	  AND	r.PackagePathID = rp.PackagePathID
GO

PRINT 'Creating view V6SoftwareOccurrence:'
IF EXISTS (
	SELECT * FROM sysobjects WHERE xtype = 'V'
	AND name = 'V6SoftwareOccurrence'
)
	DROP VIEW V6SoftwareOccurrence
GO

CREATE VIEW V6SoftwareOccurrence
AS	SELECT	SoftwareID,
		ComputerID,
		ComputerOUID,
		UserID,
		UserOUID,
		Evidence,
		p.PackageFullName,
		NULL AS PolicyGUID
	FROM	SoftwareOccurrence AS o
		LEFT OUTER JOIN
		PackagePath AS p
	ON	o.PackagePathID = p.PackagePathID
GO

-------------------------------------------------
PRINT 'Creating view LicenseFull:'
IF EXISTS(
	SELECT * FROM sysobjects
	WHERE name = 'LicenseFull' AND xtype = 'V'
)
	DROP VIEW LicenseFull
GO

CREATE VIEW LicenseFull
AS
SELECT     
	sp.*,
	lm.Name AS LMName, 
	spub.Name AS PublisherName, 
	tt.TargetTypeName AS TrackBy
FROM         
	dbo.SoftwareProduct sp 
	LEFT OUTER JOIN dbo.LicenseModel lm 
	ON sp.ModelID = lm.ModelID 
	LEFT OUTER JOIN dbo.SoftwarePublisher spub 
	ON sp.PublisherID = spub.PublisherID 
	LEFT OUTER JOIN dbo.TargetType tt 
	ON sp.TrackedByID = tt.TargetTypeID
GO

-------------------------------------------------
PRINT 'Creating view OrganizationFull:'
IF EXISTS(
	SELECT * FROM sysobjects
	WHERE name = 'OrganizationFull' AND xtype = 'V'
)
	DROP VIEW OrganizationFull
GO

CREATE VIEW OrganizationFull AS
SELECT
	o.*,						-- Everything from the current Organization Table
	CASE
	    WHEN RDN IS NULL THEN	-- Unknown OU in this domain
			'<NULL>'			-- or the universal domain
	    ELSE
	    	o.RDN
	END AS RDNFull,
	CASE
	    WHEN RDNReverse IS NULL THEN	-- Unknown OU in this domain
			'<NULL>'					-- or the universal domain
	    ELSE
	    	o.RDNReverse		
	END AS RDNReverseFull,
	CASE
	    WHEN RDN IS NULL THEN	-- Unknown OU in this domain
			'<NULL>,'+d.DN		-- or the universal domain
	    WHEN RDN = '' THEN
	    	d.DN				-- DomainDN for the domain
	    ELSE
	    	o.RDN+','+d.DN		-- Ordinary OU or container
	END AS DN,
	CASE
	    WHEN RDNReverse IS NULL THEN	-- Unknown OU in this domain
			d.DNReverse+',<NULL>'		-- or the universal domain
	    WHEN RDNReverse = '' THEN
	    	d.DNReverse					-- DomainDN for the domain
	    ELSE
	    	d.DNReverse+','+o.RDNReverse -- Ordinary OU or container
	END AS DNReverse
FROM 
	Organization AS o, 
	Domain AS d
WHERE o.DomainID = d.OrganizationID

GO

-------------------------------------------------
PRINT 'Creating view PackageVersionFull:'

IF EXISTS(
	SELECT * FROM sysobjects
	WHERE name = 'PackageVersionFull' AND xtype = 'V'
)
	DROP VIEW PackageVersionFull
GO

CREATE VIEW PackageVersionFull AS	
	SELECT	
		v.*, -- Everything from the PackageVersion Table
		p.PackageFullName,
		f.PackageName,
		f.PackageName + ' ' + v.Version AS PackageNameAndVersion,
		mcp.MediaID
	FROM	
		PackageVersion AS v,
		PackagePath AS p,
		PackageFamily AS f,
		MediaContainsPackageVersion AS mcp
	WHERE	v.PackagePathID = p.PackagePathID
		AND	v.PackageFamilyID = f.PackageFamilyID
		AND	v.PackageVersionID = mcp.PackageVersionID
GO

