IF OBJECT_ID(N'dbo.HJGL_WeldingDailyTempDetail', N'U') IS NULL
BEGIN
    CREATE TABLE dbo.HJGL_WeldingDailyTempDetail
    (
        TempDetailId NVARCHAR(50) NOT NULL,
        ProjectId NVARCHAR(50) NULL,
        UnitId NVARCHAR(50) NULL,
        UnitWorkId NVARCHAR(50) NULL,
        WeldJointId NVARCHAR(50) NOT NULL,
        WeldingDate DATETIME NOT NULL,
        CoverWelderId NVARCHAR(50) NULL,
        BackingWelderId NVARCHAR(50) NULL,
        JointAttribute NVARCHAR(50) NULL,
        WeldingLocationId NVARCHAR(50) NULL,
        WeldingMode NVARCHAR(50) NULL,
        AttachUrl NVARCHAR(500) NULL,
        SubmitPersonId NVARCHAR(50) NULL,
        SubmitDate DATETIME NOT NULL CONSTRAINT DF_HJGL_WeldingDailyTempDetail_SubmitDate DEFAULT(GETDATE()),
        AuditState INT NOT NULL CONSTRAINT DF_HJGL_WeldingDailyTempDetail_AuditState DEFAULT(0),
        AuditMan NVARCHAR(50) NULL,
        AuditDate DATETIME NULL,
        AuditRemark NVARCHAR(500) NULL,
        CONSTRAINT PK_HJGL_WeldingDailyTempDetail PRIMARY KEY CLUSTERED (TempDetailId),
        CONSTRAINT CK_HJGL_WeldingDailyTempDetail_AuditState CHECK (AuditState IN (0, 1))
    );

    CREATE UNIQUE NONCLUSTERED INDEX UX_HJGL_WeldingDailyTempDetail_Pending
    ON dbo.HJGL_WeldingDailyTempDetail (WeldJointId, WeldingDate)
    WHERE AuditState = 0;

    CREATE NONCLUSTERED INDEX IX_HJGL_WeldingDailyTempDetail_Query
    ON dbo.HJGL_WeldingDailyTempDetail (ProjectId, AuditState, WeldingDate, UnitWorkId);

    CREATE NONCLUSTERED INDEX IX_HJGL_WeldingDailyTempDetail_WeldJointId
    ON dbo.HJGL_WeldingDailyTempDetail (WeldJointId);
END
GO