ALTER VIEW [dbo].[View_Hazard_HazardRegister] AS /**********安全巡检视图*************/ SELECT Registration.HazardRegisterId, Registration.HazardCode, Registration.RegisterDate, Registration.RegisterDef, Registration.Rectification, Registration.Place, Registration.ResponsibleUnit, Registration.Observer, Registration.RectifiedDate, Registration.ProjectId, Registration.states, Registration.IsEffective, Registration.ResponsibleMan, Registration.CheckManId, Registration.CheckTime, Registration.RectificationPeriod, (CASE WHEN Registration.ImageUrl IS NULL THEN at.AttachUrl ELSE Registration.ImageUrl END) AS ImageUrl, (CASE WHEN Registration.RectificationImageUrl IS NULL THEN at_r.AttachUrl ELSE Registration.RectificationImageUrl END) AS RectificationImageUrl, Registration.RectificationTime, Registration.ConfirmMan, Registration.ConfirmDate, Registration.HandleIdea, Registration.CutPayment, Registration.ProblemTypes, Registration.CheckSpecialId, Registration.CheckItemDetailId, Registration.SupCheckItemSetId, Registration.CheckItemSetId, Registration.SafeSupervisionId, Registration.SafeSupervisionIsOK, Project.ProjectName, Registration.CheckCycle, ISNULL(WorkArea.UnitWorkName,'') AS WorkAreaName, Unit.UnitName AS ResponsibilityUnitName, User1.UserName AS ResponsibilityManName, ISNULL(User1.Telephone,'') AS ResponsibilityManTel, User2.UserName AS CheckManName, ISNULL(User2.Telephone,'') AS CheckManTel, User2.UnitId AS SendUnitId, User3.UserName AS ConfirmManName, ISNULL(User3.Telephone,'') AS ConfirmManTel, User4.UserName AS ResponsibilityManName2, ISNULL(User4.Telephone,'') AS ResponsibilityMan2Tel, (CASE WHEN Registration.states='1' and (Registration.SafeSupervisionIsOK is null OR Registration.SafeSupervisionIsOK=0) THEN '待整改' WHEN Registration.states='1' and Registration.SafeSupervisionIsOK=1 THEN '合格' WHEN Registration.states='2' THEN '已整改' WHEN Registration.states='3' THEN '已闭环' ELSE '已作废' END ) AS StatesStr, RegisterTypes.RegisterTypesId, RegisterTypes.RegisterTypesName, Registration.DIC_ID, Registration.CCManIds, CCManNames = STUFF(( SELECT ',' + UserName FROM Sys_User where PATINDEX('%,' + RTRIM(Sys_User.UserId) + ',%',',' + Registration.CCManIds + ',')>0 ORDER BY PATINDEX('%,' + RTRIM(Registration.CCManIds) + ',%',',' + Registration.CCManIds + ',') FOR XML PATH('')), 1, 1,''), Registration.Requirements, Registration.HazardValue, Registration.RegisterTypes2Id , RegisterTypes2.RegisterTypesName as RegisterTypes2Name, Registration.RegisterTypes3Id , RegisterTypes3.RegisterTypesName as RegisterTypes3Name, Registration.RegisterTypes4Id , RegisterTypes4.RegisterTypesName as RegisterTypes4Name FROM dbo.HSSE_Hazard_HazardRegister AS Registration LEFT JOIN dbo.Base_Project AS Project ON Project.ProjectId = Registration.ProjectId LEFT JOIN dbo.WBS_UnitWork AS WorkArea ON WorkArea.UnitWorkId = Registration.Place LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes ON RegisterTypes.RegisterTypesId = Registration.RegisterTypesId LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes2 ON RegisterTypes2.RegisterTypesId = Registration.RegisterTypes2Id LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes3 ON RegisterTypes3.RegisterTypesId = Registration.RegisterTypes3Id LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes4 ON RegisterTypes4.RegisterTypesId = Registration.RegisterTypes4Id LEFT JOIN dbo.Base_Unit AS Unit ON Unit.UnitId = Registration.ResponsibleUnit LEFT JOIN dbo.Sys_User AS User1 ON User1.UserId = Registration.ResponsibleMan LEFT JOIN dbo.Sys_User AS User2 ON User2.UserId = Registration.CheckManId LEFT JOIN dbo.Sys_User AS User3 ON User3.UserId = Registration.ConfirmMan LEFT JOIN dbo.Sys_User AS User4 ON User4.UserId = Registration.ResponsibleMan2 LEFT JOIN dbo.AttachFile AS at ON at.ToKeyId = Registration.HazardRegisterId LEFT JOIN dbo.AttachFile AS at_r ON at_r.ToKeyId = Registration.HazardRegisterId+'-R' GO