简体   繁体   中英

How do I insert into a temporary table with one extra column

I want to write a procedure that returns 3 columns, the last column doesn't exist in the original table thought, it's a time difference I want to get between LastLogin and the current time but I don't know how to write it. So far this is what I have but I'm not even sure if this is how it goes.

ALTER PROCEDURE sp_W_getSesionSupervisor
@Plaza varchar(255),
@UserLevel int

DECLARE @tempSupervisor TABLE (
    UserName varchar(255),
    LastLogin DATETIMEOFFSET,
    tTranscurrido DATETIMEOFFSET
);
BEGIN
    INSERT INTO @tempSupervisor (UserName, LastLogin)
    SELECT UserName, LastLogin FROM UsersAdmin
    WHERE Plaza = @Plaza AND UserLevel = @UserLevel

    UPDATE @tempSupervisor
    SET tTranscurrido = DIFFERENCE(LastLogin, DateTimeOffset)

    SELECT * FROM @tempSupervisor
END

The same result can be returned by a single SELECT statement, like the below one. Is there a requirement to have the procedure?

SELECT UserName, LastLogin, DIFFERENCE(LastLogin, DateTimeOffset) FROM UsersAdmin WHERE Plaza = @Plaza AND UserLevel = @UserLevel

@Plaza and @UserLevel would be the parameters for the query. You can skip the parameters if these are not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM