如何使这段代码看起来更专业呢?

时间:2016-06-25 10:21:30

标签: sql sql-server

DECLARE @Coid INT
DECLARE @DTRID INT
DECLARE @EMPID INT
DECLARE @DATE datetime

SELECT TOP 1 @EMPID = tblEmployees.Id, @Coid = tblEmployees.CompanyId, @DATE = tblDailyTimeRecord.TimeIn, @DTRID = tblDailyTimeRecord.Id   FROM tblEmployees INNER JOIN tblDailyTimeRecord ON tblEmployees.Id = tblDailyTimeRecord.EmployeeId WHERE tblDailyTimeRecord.Date = CONVERT(date, GETDATE()) AND tblDailyTimeRecord.TimeOut IS NULL ORDER BY tblDailyTimeRecord.ID DESC
IF @Coid IS NULL
BEGIN
DECLARE @IdentityValue TABLE ( ContactID int,EmpId int,DATE datetime)
INSERT INTO [tblDailyTimeRecord]([EmployeeId],[Date],[TimeOut],[IsModified],[CompanyId])
OUTPUT INSERTED.Id,INSERTED.EmployeeId,INSERTED.TimeOut INTO @IdentityValue
Select Id,CONVERT(date, GETDATE()),GETDATE(),0,CompanyId From tblEmployees Where AccessCode = 'GI0056'
INSERT INTO tblTimeLog([EmployeeId],[Time],[Type],[TimeLogSourceId],[CreationDate])
Select EmpId,DATE,2,11,GETDATE() From @IdentityValue
END
ELSE
BEGIN
    DECLARE @PAIR int
    DECLARE @TIMEOUT datetime
    SET @TIMEOUT = GETDATE()
    IF @DATE IS NULL
        SET @PAIR = 0
    ELSE
        SET @PAIR = 1

    UPDATE [tblDailyTimeRecord] SET TimeOut = @TIMEOUT, PairNo = @PAIR WHERE ID = @DTRID
    INSERT INTO tblTimeLog([EmployeeId],[Time],[Type],[TimeLogSourceId],[CreationDate])
    VALUES (@EMPID,@TIMEOUT,2,11,@TIMEOUT)
END

1 个答案:

答案 0 :(得分:0)

只是一般的格式规则:
1.所有声明都在开头 2.保持一致:首都所有保留字 3.不要对列名使用保留字 4.评论你的代码 5.使用统一意图。
6.用分号结束语句 7.如果可以,请使用CASE代替IF SET 8.对表格和视图使用短别名 9.在SELECT语句中命名变量,然后将清楚地说明了什么 我忘记了什么吗?

<强>逻辑/设计即可。
1.您可以使用临时表变量或临时表来代替多个变量。它会使代码更短,更易读 2.首先SELECT返回一行,但在INSERT语句中,您可能会插入完全不相关的内容 3.在两个表中使用相同的数据可能是设计不良的标志 4.同一行中同一事件的日期和日期时间非常可疑。

可能如下面的代码所示。希望有所帮助。

DECLARE @Coid INT;
DECLARE @DTRID INT;
DECLARE @EMPID INT;
DECLARE @DATE DATETIME;
DECLARE @PAIR INT;
DECLARE @TIMEOUT DATETIME;

DECLARE @IdentityValue TABLE ( 
    ContactID int,
    EmpId int, 
    ContactDate DATETIME
);

-- Select ONLY one row
SELECT TOP 1 
    @EMPID = e.Id, 
    @Coid = e.CompanyId, 
    @DATE = tr.TimeIn, 
    @DTRID = tr.Id,
    @TIMEOUT = GETDATE(),
    @PAIR = CASE WHEN tr.TimeIn IS NULL THEN 0 ELSE 1 END
FROM tblEmployees as e
INNER JOIN tblDailyTimeRecord as tr
    ON e.Id = tr.EmployeeId 
WHERE tr.Date = CONVERT(DATE, GETDATE()) 
    AND tr.TimeOut IS NULL 
ORDER BY tr.ID DESC;

IF @Coid IS NULL
BEGIN
    -- Insert more than one row or might insert nothing
    INSERT INTO [tblDailyTimeRecord] (
        [EmployeeId],
        [Date],
        [TimeOut],
        [IsModified],
        [CompanyId]
    )
    OUTPUT INSERTED.Id, 
            INSERTED.EmployeeId, 
            INSERTED.TimeOut 
        INTO @IdentityValue
    SELECT Id as [EmployeeId], 
        CONVERT(DATE, GETDATE()) as [Date],
        @TIMEOUT as [TimeOut],
        0 as [IsModified],
        CompanyId 
    FROM tblEmployees 
    WHERE AccessCode = 'GI0056';

    INSERT INTO tblTimeLog(
        [EmployeeId],
        [Time],
        [Type],
        [TimeLogSourceId],
        [CreationDate])
    SELECT EmpId as [EmployeeId],
        ContactDate as [Time],
        2 as [Type],
        11 as [TimeLogSourceId],
        @TIMEOUT as [CreationDate]
    FROM @IdentityValue;
END
ELSE
BEGIN
    UPDATE [tblDailyTimeRecord] 
    SET TimeOut = @TIMEOUT, 
        PairNo = @PAIR 
    WHERE ID = @DTRID

    INSERT INTO tblTimeLog(
        [EmployeeId],
        [Time],
        [Type],
        [TimeLogSourceId],
        [CreationDate]
    ) VALUES (
        @EMPID, 
        @TIMEOUT,
        2,
        11,
        @TIMEOUT
    );
END