﻿

--去除多个QHSE项目绑定同一个集团主数据项目的异常数据，只保留一条QHSE项目绑定

WITH CTE AS (
    SELECT 
        RN = ROW_NUMBER() OVER(PARTITION BY MasterSysId ORDER BY ISNULL(ProjectState,'1'),StartDate asc )
		,*
    FROM Base_Project
	WHERE MasterSysId <>'' AND MasterSysId IS NOT NULL
)

/***

SELECT ProjectId as 项目id,MasterSysId as sss,ProjectState,StartDate ,RN as 序号,* 
FROM CTE
--WHERE RN > 1
ORDER BY MasterSysId,RN

***/

UPDATE Base_Project 
SET MasterSysId=NULL
WHERE ProjectId IN (select ProjectId FROM CTE WHERE RN > 1 );


GO





--集团在建项目使用详情补充字段：审批人信息
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Project_MasterDataUsage' AND COLUMN_NAME = 'HandleMan')
BEGIN
	ALTER TABLE Project_MasterDataUsage ADD HandleMan nvarchar(50);
	ALTER TABLE Project_MasterDataUsage ADD HandleManName nvarchar(50);
	ALTER TABLE Project_MasterDataUsage ADD HandleState char(1);
	ALTER TABLE Project_MasterDataUsage ADD HandleDate datetime;
	EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'审批人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Project_MasterDataUsage', @level2type=N'COLUMN',@level2name=N'HandleMan'
	EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'审批人姓名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Project_MasterDataUsage', @level2type=N'COLUMN',@level2name=N'HandleManName'
	EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'审批状态（0：通过，1：不通过）' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Project_MasterDataUsage', @level2type=N'COLUMN',@level2name=N'HandleState'
	EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'审批时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Project_MasterDataUsage', @level2type=N'COLUMN',@level2name=N'HandleDate'
END
GO


--集团在建项目使用详情补充字段：原因类型
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Project_MasterDataUsage' AND COLUMN_NAME = 'ReasonType')
BEGIN
	ALTER TABLE Project_MasterDataUsage ADD ReasonType nvarchar(50);
	EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'原因类型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Project_MasterDataUsage', @level2type=N'COLUMN',@level2name=N'ReasonType'
END
GO
