IF NOT EXISTS (SELECT 1 FROM SYS.TABLES WHERE NAME='LD0P0MOUSubGoal')
BEGIN
CREATE TABLE LD0P0MOUSubGoal 
(
SUBGOALID DECIMAL(9) NOT NULL,
GOALID DECIMAL(9) NOT NULL,
SUBGOAL DECIMAL(5) NOT NULL,
TITLE VARCHAR(80) DEFAULT ' ' NOT NULL,
STARTDATE VARCHAR(8) DEFAULT ' ' NOT NULL,
DETAILS VARCHAR(400) DEFAULT ' ' NOT NULL,
UPDSIGN VARCHAR(10) NOT NULL,
UPDDAT VARCHAR(8) NOT NULL,
	CONSTRAINT LD0P0MOUSubGoal_PKID PRIMARY KEY CLUSTERED
	(
	SUBGOALID
	)
)
END
GO

IF EXISTS (SELECT 1 FROM SYS.DEFAULT_CONSTRAINTS WHERE NAME='DF_Seq_LD0P0MOUSubGoal' AND parent_object_id = OBJECT_ID('LD0P0MOUSubGoal'))
BEGIN
    ALTER TABLE LD0P0MOUSubGoal DROP CONSTRAINT DF_Seq_LD0P0MOUSubGoal
END
GO

IF NOT EXISTS (SELECT 1 FROM SYS.SEQUENCES WHERE NAME = 'LD0P0MOUSubGoal_Seq')
BEGIN
CREATE SEQUENCE LD0P0MOUSubGoal_Seq 
		AS DECIMAL(9)
		START WITH 1
		MINVALUE 0
		MAXVALUE 999999999
		INCREMENT BY 1
		CACHE 50
END
GO
ALTER TABLE LD0P0MOUSubGoal ADD  CONSTRAINT DF_Seq_LD0P0MOUSubGoal  DEFAULT (NEXT VALUE FOR LD0P0MOUSubGoal_Seq) FOR SUBGOALID
GO

IF NOT EXISTS (SELECT 1 FROM SYS.INDEXES WHERE NAME='LD0I0MOUSubGoal')
BEGIN
CREATE INDEX LD0I0MOUSubGoal ON LD0P0MOUSubGoal
(
SUBGOALID
)
END
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0MOUSubGoal')
BEGIN
DROP VIEW LD0V0MOUSubGoal
END
GO

CREATE OR ALTER VIEW LD0V0MOUSubGoal AS SELECT * FROM LD0P0MOUSubGoal
GO
GRANT ALL ON LD0V0MOUSubGoal TO LD0ROANV
GO
